diff --git a/src/controller/admin/unit/inspectionController.ts b/src/controller/admin/unit/inspectionController.ts index 9337899..1b35af0 100644 --- a/src/controller/admin/unit/inspectionController.ts +++ b/src/controller/admin/unit/inspectionController.ts @@ -16,6 +16,8 @@ import { InspectionPointEnum } from "../../../enums/inspectionEnum"; import multer from "multer"; import { FileSystemHelper } from "../../../helpers/fileSystemHelper"; import { PdfExport } from "../../../helpers/pdfExport"; +import { PDFDocument } from "pdf-lib"; +import sharp from "sharp"; /** * @description get all inspections sorted by id not having newer inspection @@ -240,13 +242,37 @@ export async function finishInspection(req: Request, res: Response): Promise (a.sort ?? 0) - (b.sort ?? 0) + )) { + let value = formattedInspection.checks.find((c) => c.inspectionPointId == ip.id).value; + let image = ""; + if (ip.type == InspectionPointEnum.file && ip.others == "img") { + const imagePath = FileSystemHelper.formatPath("inspection", inspection.id, value); + let pngImageBytes = await sharp(imagePath).png().toBuffer(); + image = `data:image/png;base64,${pngImageBytes.toString("base64")}`; + } else if (ip.type == InspectionPointEnum.oknok) { + value = value ? "OK" : "Nicht OK"; + } + inspectionPoints.push({ + title: ip.title, + description: ip.description, + type: ip.type, + min: ip.min, + max: ip.max, + others: ip.others, + value: value, + image: image, + }); + } + + let pdf = await PdfExport.renderFile({ template: "inspection", title, - filename: "printout", - folder: `inspection/${inspection.id}`, + saveToDisk: false, data: { - inspector: `${req.userId}`, + inspector: `${req.lastname}, ${req.firstname}`, context: formattedInspection.context || "---", createdAt: formattedInspection.created, finishedAt: formattedInspection.finished ?? new Date(), @@ -255,23 +281,55 @@ export async function finishInspection(req: Request, res: Response): Promise (a.sort ?? 0) - (b.sort ?? 0)) - .map((ip) => ({ - title: ip.title, - description: ip.description, - type: ip.type, - min: ip.min, - max: ip.max, - value: - ip.type == InspectionPointEnum.file - ? "siehe Anhang" - : formattedInspection.checks.find((c) => c.inspectionPointId == ip.id).value, - })), + checks: inspectionPoints, }, }); - // TODO: Anhang hinzufügen + const finalDocument = await PDFDocument.create(); + const printout = await PDFDocument.load(pdf); + const copiedPages = await finalDocument.copyPages(printout, printout.getPageIndices()); + copiedPages.forEach((page) => finalDocument.addPage(page)); + + let resultsForAppend = inspectionPoints.filter((ip) => ip.type == InspectionPointEnum.file && ip.others == "pdf"); + + if (resultsForAppend.length !== 0) { + const appendixPage = finalDocument.addPage(); + const { width, height } = appendixPage.getSize(); + appendixPage.drawText("Anhang:", { + x: 50, + y: height - 50, + size: 24, + }); + } + + for (const appendix of resultsForAppend) { + const appendixPdfBytes = FileSystemHelper.readFileAsBase64("inspection", inspection.id, appendix.value); + const appendixPdf = await PDFDocument.load(appendixPdfBytes); + const appendixPages = await finalDocument.copyPages(appendixPdf, appendixPdf.getPageIndices()); + appendixPages.forEach((page) => finalDocument.addPage(page)); + + /** print image + const imagePath = FileSystemHelper.formatPath("inspection", inspection.id, checkValue); + let pngImageBytes = await sharp(imagePath).png().toBuffer(); + let image = await finalDocument.embedPng(pngImageBytes); + let dims = image.scale(1); + if (image) { + const page = finalDocument.addPage(); + const { width, height } = page.getSize(); + const x = (width - dims.width) / 2; + const y = (height - dims.height) / 2; + page.drawImage(image, { + x, + y, + width: dims.width, + height: dims.height, + }); + } + */ + } + + const mergedPdfBytes = await finalDocument.save(); + FileSystemHelper.writeFile(`inspection/${inspection.id}`, `printout.pdf`, mergedPdfBytes); let finish: FinishInspectionCommand = { id: inspectionId, diff --git a/src/handlebars.config.ts b/src/handlebars.config.ts index 19b32ad..6494994 100644 --- a/src/handlebars.config.ts +++ b/src/handlebars.config.ts @@ -59,3 +59,7 @@ Handlebars.registerHelper("longdatetimeWithWeekday", function (aString) { Handlebars.registerHelper("json", function (context) { return JSON.stringify(context); }); + +Handlebars.registerHelper("eq", function (p, q, options) { + return p == q ? options.fn(this) : options.inverse(this); +}); diff --git a/src/helpers/fileSystemHelper.ts b/src/helpers/fileSystemHelper.ts index 9bc409a..c97bf4f 100644 --- a/src/helpers/fileSystemHelper.ts +++ b/src/helpers/fileSystemHelper.ts @@ -15,7 +15,7 @@ export abstract class FileSystemHelper { return readFileSync(this.formatPath(...filePath), "utf8"); } - static readFileasBase64(...filePath: string[]) { + static readFileAsBase64(...filePath: string[]) { this.createFolder(...filePath); return readFileSync(this.formatPath(...filePath), "base64"); } diff --git a/src/helpers/pdfExport.ts b/src/helpers/pdfExport.ts index 429f551..b42564e 100644 --- a/src/helpers/pdfExport.ts +++ b/src/helpers/pdfExport.ts @@ -127,7 +127,7 @@ export abstract class PdfExport { const mergedPdf = await PDFDocument.create(); for (const pdfPath of pdfFilePaths) { - const pdfBytes = FileSystemHelper.readFileasBase64(inputFolder, pdfPath); + const pdfBytes = FileSystemHelper.readFileAsBase64(inputFolder, pdfPath); const pdf = await PDFDocument.load(pdfBytes); const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices()); copiedPages.forEach((page) => mergedPdf.addPage(page)); diff --git a/src/index.ts b/src/index.ts index 13de2ee..1e28a8c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,8 @@ declare global { export interface Request { userId: string; username: string; + firstname: string; + lastname: string; isOwner: boolean; permissions: PermissionObject; isPWA: boolean; diff --git a/src/middleware/authenticate.ts b/src/middleware/authenticate.ts index abeb832..c961b19 100644 --- a/src/middleware/authenticate.ts +++ b/src/middleware/authenticate.ts @@ -35,6 +35,8 @@ export default async function authenticate(req: Request, res: Response, next: Ne req.userId = decoded.userId; req.username = decoded.username; + req.firstname = decoded.firstname; + req.lastname = decoded.lastname; req.isOwner = decoded.isOwner; req.permissions = decoded.permissions; req.isWebApiRequest = decoded?.sub == "webapi_access_token"; diff --git a/src/templates/inspection.body.template.html b/src/templates/inspection.body.template.html index 0d243ab..cc2a6e5 100644 --- a/src/templates/inspection.body.template.html +++ b/src/templates/inspection.body.template.html @@ -17,16 +17,23 @@

Kontext: {{context}}


-

Prüfpunkte:

+

Prüfpunkte:

{{#each checks}} - + - {{/each}}
+ {{this.title}}
- {{this.description}} + {{#if this.description}} > {{this.description}} +
+ {{/if}} + (Typ: {{this.type}}) +
+ {{#eq this.type "file"}} {{#eq this.others "img"}} + + {{else}} siehe Anhang {{/eq}} {{else}} {{this.value}} {{/eq}} {{this.value}}