|
| 1 | +import { Mutation, Resolver } from '@nestjs/graphql'; |
| 2 | +import { ID, IdArg, InputException, LoggedInSession, Session } from '~/common'; |
| 3 | +import { IEventBus, Loader, LoaderOf } from '~/core'; |
| 4 | +import { FileNodeLoader, FileService, resolveDefinedFile } from '../../file'; |
| 5 | +import { PeriodicReportLoader } from '../../periodic-report'; |
| 6 | +import { PeriodicReportUploadedEvent } from '../../periodic-report/events'; |
| 7 | +import { PnpProgressExtractionResult } from '../../pnp/extraction-result'; |
| 8 | + |
| 9 | +@Resolver() |
| 10 | +export class ReextractPnpResolver { |
| 11 | + constructor( |
| 12 | + private readonly files: FileService, |
| 13 | + private readonly eventBus: IEventBus, |
| 14 | + ) {} |
| 15 | + |
| 16 | + @Mutation(() => PnpProgressExtractionResult) |
| 17 | + async reextractPnpProgress( |
| 18 | + @IdArg({ |
| 19 | + name: 'reportId', |
| 20 | + description: 'An ID of a ProgressReport that has a reportFile uploaded', |
| 21 | + }) |
| 22 | + reportId: ID, |
| 23 | + @Loader(PeriodicReportLoader) reportLoader: LoaderOf<PeriodicReportLoader>, |
| 24 | + @Loader(FileNodeLoader) fileLoader: LoaderOf<FileNodeLoader>, |
| 25 | + @LoggedInSession() session: Session, |
| 26 | + ): Promise<PnpProgressExtractionResult> { |
| 27 | + const report = await reportLoader.load(reportId); |
| 28 | + if (report.type !== 'Progress') { |
| 29 | + throw new InputException( |
| 30 | + "Only ProgressReports can have PnP's re-extracted", |
| 31 | + ); |
| 32 | + } |
| 33 | + const file = await resolveDefinedFile(fileLoader, report.reportFile); |
| 34 | + if (!file.value) { |
| 35 | + throw new InputException('This report does not have a PnP uploaded'); |
| 36 | + } |
| 37 | + |
| 38 | + const fv = await this.files.getFileVersion( |
| 39 | + file.value.latestVersionId, |
| 40 | + session, |
| 41 | + ); |
| 42 | + const pnp = this.files.asDownloadable(fv); |
| 43 | + |
| 44 | + const event = new PeriodicReportUploadedEvent(report, pnp, session); |
| 45 | + await this.eventBus.publish(event); |
| 46 | + |
| 47 | + return event.pnpResult; |
| 48 | + } |
| 49 | +} |
0 commit comments