pdf.ts 1.7 KB

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