diff --git a/src/controller/admin/templateUsageController.ts b/src/controller/admin/templateUsageController.ts index 0d146d7..e121215 100644 --- a/src/controller/admin/templateUsageController.ts +++ b/src/controller/admin/templateUsageController.ts @@ -6,6 +6,8 @@ import TemplateUsageCommandHandler from "../../command/templateUsageCommandHandl import PermissionHelper from "../../helpers/permissionHelper"; import ForbiddenRequestException from "../../exceptions/forbiddenRequestException"; import { PermissionModule } from "../../type/permissionTypes"; +import { PdfExport } from "../../helpers/pdfExport"; +import { DemoDataHelper } from "../../helpers/demoDataHelper"; /** * @description get all templateUsages @@ -28,6 +30,27 @@ export async function getAllTemplateUsages(req: Request, res: Response): Promise res.json(TemplateUsageFactory.mapToBase(templateUsages)); } +/** + * @description print demo of templateUsage + * @param req {Request} Express req object + * @param res {Response} Express res object + * @returns {Promise<*>} + */ +export async function printTemplateUsageDemo(req: Request, res: Response): Promise { + const scope = req.params.scope as PermissionModule; + + let demoData = DemoDataHelper.getData(scope); + let pdf = await PdfExport.renderFile({ template: scope, saveToDisk: false, data: demoData }); + + let pdfbuffer = Buffer.from(pdf); + + res.setHeader("Content-Type", "application/pdf"); + res.setHeader("Content-Length", pdfbuffer.byteLength); + res.setHeader("Content-Disposition", "inline; filename=preview.pdf"); + + res.send(pdfbuffer); +} + /** * @description update templateUsage * @param req {Request} Express req object diff --git a/src/demodata/protocol.data.ts b/src/demodata/protocol.data.ts new file mode 100644 index 0000000..1feeace --- /dev/null +++ b/src/demodata/protocol.data.ts @@ -0,0 +1,56 @@ +import { member } from "../entity/member"; +import { protocolAgenda } from "../entity/protocolAgenda"; +import { protocolDecision } from "../entity/protocolDecision"; +import { protocolVoting } from "../entity/protocolVoting"; + +export const protocolDemoData: { + title: string; + summary: string; + iteration: number; + date: string; + start: string; + end: string; + agenda: Array>; + decisions: Array>; + presence: Array>; + votings: Array>; +} = { + title: "Beispiel Protokoll Daten", + summary: "Zusammenfassung der Demodaten.", + iteration: 1, + date: new Date().toLocaleDateString("de-DE", { + weekday: "long", + day: "2-digit", + month: "2-digit", + year: "numeric", + }), + start: "19:00:00", + end: "21:00:00", + agenda: [ + { + topic: "Protokoll-TOP", + context: "Inhalt des Punktes", + }, + ], + decisions: [ + { + topic: "Entscheidung yz", + context: "Inhalt der Entscheidung", + }, + ], + presence: [ + { + firstname: "Julian", + lastname: "Krauser", + }, + ], + votings: [ + { + topic: "Abstimmung xy", + context: "Inhalt der Abstimmung", + favour: 1, + abstain: 2, + against: 3, + }, + ], +}; diff --git a/src/helpers/demoDataHelper.ts b/src/helpers/demoDataHelper.ts new file mode 100644 index 0000000..158f17b --- /dev/null +++ b/src/helpers/demoDataHelper.ts @@ -0,0 +1,13 @@ +import { protocolDemoData } from "../demodata/protocol.data"; +import { PermissionModule } from "../type/permissionTypes"; + +export abstract class DemoDataHelper { + static getData(scope: PermissionModule) { + switch (scope) { + case "protocol": + return protocolDemoData; + default: + return {}; + } + } +} diff --git a/src/helpers/pdfExport.ts b/src/helpers/pdfExport.ts index ff15fad..827f3db 100644 --- a/src/helpers/pdfExport.ts +++ b/src/helpers/pdfExport.ts @@ -6,13 +6,17 @@ export abstract class PdfExport { static async renderFile({ template, title = "pdf-export Mitgliederverwaltung", - filename, - data, + filename = null, + data = {}, + saveToDisk = true, + margins = { top: "15mm", bottom: "15mm" }, }: { template: PermissionModule; - title: string; - filename: string; - data: any; + title?: string; + filename?: string; + data?: any; + saveToDisk?: boolean; + margins?: { top: string; bottom: string }; }) { const { header, footer, body } = await TemplateHelper.renderFileForModule({ module: template, @@ -27,13 +31,13 @@ export abstract class PdfExport { const page = await browser.newPage(); await page.setContent(body, { waitUntil: "domcontentloaded" }); - await page.pdf({ - path: process.cwd() + `/export/${filename}.pdf`, + let pdf = await page.pdf({ + ...(saveToDisk ? { path: process.cwd() + `/export/${filename}.pdf` } : {}), format: "A4", printBackground: false, margin: { - top: "15mm", - bottom: "15mm", + top: margins.top, + bottom: margins.bottom, left: "10mm", right: "10mm", }, @@ -43,5 +47,7 @@ export abstract class PdfExport { }); await browser.close(); + + return pdf; } } diff --git a/src/helpers/templateHelper.ts b/src/helpers/templateHelper.ts index a8c4dc0..04a0874 100644 --- a/src/helpers/templateHelper.ts +++ b/src/helpers/templateHelper.ts @@ -39,15 +39,11 @@ export abstract class TemplateHelper { headerData?: any; bodyData?: any; footerData?: any; - }): Promise<{ header: string; body: string; footer: string }> { + }): Promise<{ header: string; body: string; footer: string; margins?: { top: string; bottom: string } }> { const moduleTemplates = await TemplateUsageService.getByScope(module); let header = `

${title}

`; - let footer = ` -
- Seite von -
- `; + let footer = ""; let body = ""; if (moduleTemplates.headerId) { @@ -58,12 +54,14 @@ export abstract class TemplateHelper { if (moduleTemplates.footerId) { footer = await this.getTemplateFromStore(moduleTemplates.footerId); footer = this.applyDataToTemplate(footer, footerData); + } else { + footer = this.getTemplateFromFile(module + ".footer"); } if (moduleTemplates.bodyId) { body = await this.getTemplateFromStore(moduleTemplates.bodyId); } else { - body = this.getTemplateFromFile(module); + body = this.getTemplateFromFile(module + ".body"); } body = this.applyDataToTemplate(body, bodyData); diff --git a/src/routes/admin/templateUsage.ts b/src/routes/admin/templateUsage.ts index ca4da5b..d25b321 100644 --- a/src/routes/admin/templateUsage.ts +++ b/src/routes/admin/templateUsage.ts @@ -1,6 +1,10 @@ import express, { Request, Response } from "express"; import PermissionHelper from "../../helpers/permissionHelper"; -import { getAllTemplateUsages, updateTemplateUsage } from "../../controller/admin/templateUsageController"; +import { + getAllTemplateUsages, + printTemplateUsageDemo, + updateTemplateUsage, +} from "../../controller/admin/templateUsageController"; import { PermissionModule } from "../../type/permissionTypes"; import ForbiddenRequestException from "../../exceptions/forbiddenRequestException"; @@ -10,6 +14,10 @@ router.get("/", async (req: Request, res: Response) => { await getAllTemplateUsages(req, res); }); +router.get("/:scope", async (req: Request, res: Response) => { + await printTemplateUsageDemo(req, res); +}); + router.patch( "/:scope", PermissionHelper.passCheckMiddleware("update", "settings", "template_usage"), diff --git a/src/templates/protocol.template.html b/src/templates/protocol.body.template.html similarity index 100% rename from src/templates/protocol.template.html rename to src/templates/protocol.body.template.html diff --git a/src/templates/protocol.footer.template.html b/src/templates/protocol.footer.template.html new file mode 100644 index 0000000..7a3190b --- /dev/null +++ b/src/templates/protocol.footer.template.html @@ -0,0 +1,3 @@ +
+ Seite von +