pdf.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { BodyParams, Logger } from '@tsed/common';
  2. import { Controller, Inject } from '@tsed/di';
  3. import { InternalServerError } from '@tsed/exceptions';
  4. import {
  5. Post, Returns, Enum, Description,
  6. } from '@tsed/schema';
  7. import PdfConvertService, { JobStatusSharedWithGrowi, JobStatus } from '../service/pdf-convert';
  8. @Controller('/pdf')
  9. class PdfCtrl {
  10. @Inject()
  11. logger: Logger;
  12. constructor(private readonly pdfConvertService: PdfConvertService) {}
  13. @Post('/sync-job')
  14. @(Returns(202).ContentType('application/json').Schema({
  15. type: 'object',
  16. properties: {
  17. status: { type: 'string', enum: Object.values(JobStatus) },
  18. },
  19. required: ['status'],
  20. }))
  21. @Returns(500)
  22. @Description(`
  23. Sync job pdf convert status with GROWI.
  24. Register or update job inside pdf-converter with given jobId, expirationDate, and status.
  25. Return resulting status of job to GROWI.
  26. `)
  27. async syncJobStatus(
  28. @BodyParams('jobId') jobId: string,
  29. @BodyParams('expirationDate') expirationDateStr: string,
  30. @BodyParams('status') @Enum(Object.values(JobStatusSharedWithGrowi)) growiJobStatus: JobStatusSharedWithGrowi,
  31. ): Promise<{ status: JobStatus }> {
  32. const expirationDate = new Date(expirationDateStr);
  33. try {
  34. await this.pdfConvertService.registerOrUpdateJob(jobId, expirationDate, growiJobStatus);
  35. this.pdfConvertService.cleanUpJobList();
  36. return { status: this.pdfConvertService.getJobStatus(jobId) };
  37. }
  38. catch (err) {
  39. this.logger.error('Failed to register or update job', err);
  40. throw new InternalServerError(err);
  41. }
  42. }
  43. }
  44. export default PdfCtrl;