This commit is contained in:
phaichayon
2026-06-19 12:30:26 +07:00
parent 721653f692
commit a85d24235c
14 changed files with 1250 additions and 102 deletions

View File

@@ -22,6 +22,7 @@ import {
getQuotationDocumentPreviewData,
prepareApprovedQuotationSnapshot
} from '@/features/crm/quotations/document/server/service';
import type { ApprovedQuotationSnapshot } from '@/features/crm/quotations/document/types';
import { getQuotationDetail } from '@/features/crm/quotations/server/service';
type GeneratePdfFromTemplateInput = {
@@ -323,14 +324,23 @@ export async function generateApprovedQuotationPdf(
userId: string
): Promise<GeneratedQuotationPdf> {
const preview = await getQuotationDocumentPreviewData(quotationId, organizationId);
const quotation = await getQuotationDetail(quotationId, organizationId);
const approvedSnapshot = quotation.approvedSnapshot as ApprovedQuotationSnapshot | null;
if (preview.documentData.quotation.statusCode !== 'approved') {
throw new AuthError('Only approved quotations can generate approved PDF', 400);
}
const snapshotTemplateInput =
approvedSnapshot &&
approvedSnapshot.quotationId === quotationId &&
approvedSnapshot.templateInput &&
typeof approvedSnapshot.templateInput === 'object'
? approvedSnapshot.templateInput
: null;
const buffer = await generatePdfFromTemplate({
template: preview.template.version.schemaJson as Template,
inputs: [preview.templateInput]
inputs: [snapshotTemplateInput ?? preview.templateInput]
});
const fileName = `${preview.documentData.quotation.code}-approved.pdf`;
const artifact = await persistApprovedQuotationArtifact({