import { Logger } from "@nestjs/common"; import { S3Service } from "../../../foundations/s3/services/s3.service"; import { DocxTemplateService, DocumentTemplate } from "./docx-template.service"; import { DocxToPdfService } from "../../pdf/services/docx-to-pdf.service"; export type DocumentFormat = "docx" | "pdf"; export interface DocumentTarget { /** The output format to produce. */ format: DocumentFormat; /** S3 key where the produced file should be stored. Caller supplies this; the library never decides key names. */ s3Key: string; } export interface DocumentGenerationResult { format: DocumentFormat; s3Key: string; success: boolean; /** Populated only when `success` is false. */ error?: Error; } /** * Template-method base class for backend document generation. * * Subclasses supply all domain knowledge (how to load the entity, which * template to use, how to build the field context, where to persist URLs). * The algorithm — render → post-process → watermark → upload DOCX → convert * → upload PDF → persist — is fixed here and cannot be overridden. * * ### Error semantics * * | Failure point | Behaviour | * |---|---| * | `loadEntityWithRelations` / `loadTemplate` / DOCX render | Whole call throws. No persistence. | * | DOCX upload | Whole call throws. No persistence. | * | PDF conversion or PDF upload | DOCX result stays successful. PDF result added with `success: false`. `persistDocumentUrls` is still called. | * * ### Edge cases * * - Caller passes only `[{ format: "pdf", … }]`: DOCX is rendered as an * in-memory intermediate but never uploaded. * - Caller passes only `[{ format: "docx", … }]`: PDF conversion is skipped. * - Caller passes both: both run; PDF failure leaves DOCX untouched. */ export declare abstract class AbstractDocumentGeneratorService { protected readonly docxTemplateService: DocxTemplateService; protected readonly docxToPdfService: DocxToPdfService; protected readonly s3Service: S3Service; protected readonly logger: Logger; /** Human-readable entity type name used in log messages (e.g. `"invoice"`). */ protected abstract readonly entityType: string; constructor(docxTemplateService: DocxTemplateService, docxToPdfService: DocxToPdfService, s3Service: S3Service, logger: Logger); /** Load the entity and all relationships needed to build the field context. */ protected abstract loadEntityWithRelations(id: string): Promise; /** Resolve the document template to render (DOCX or BlockNote). */ protected abstract loadTemplate(entity: T): Promise; /** * Build the flat field-context object whose keys match template placeholders. * Must be a pure function of `entity` and `template` — no async work here. * * `template` is provided so subclasses can branch on `template.kind` to choose * between (a) plain markdown values for the BlockNote path (inlined directly * into the rendered document) and (b) sentinel strings for the DOCX-file path * that are subsequently replaced by `postProcessDocx` with raw WordprocessingML. */ protected abstract buildFieldContext(entity: T, template: DocumentTemplate): Record; /** * Persist the S3 keys that were successfully produced. * Called exactly once per `generate()` invocation, even when PDF conversion * failed (best-effort semantic for PDF). * Must return the updated entity. */ protected abstract persistDocumentUrls(id: string, results: DocumentGenerationResult[]): Promise; /** * Post-process the rendered DOCX buffer before upload/conversion. * Typical use: inject a line-item table via `injectXml`. */ protected postProcessDocx?(buffer: Buffer, entity: T): Promise; /** * Return `true` if a draft watermark should be applied to this entity. * Defaults to no watermark when not overridden. */ protected shouldApplyWatermark?(entity: T): boolean; /** * Generate document artifacts for the given entity and upload them to S3. * * @param id - Entity ID. * @param targets - One or more `{ format, s3Key }` descriptors. At least one required. * @returns The entity after `persistDocumentUrls` has been called. */ generate(id: string, targets: DocumentTarget[]): Promise; } //# sourceMappingURL=abstract-document-generator.service.d.ts.map