#6-messages #24

Merged
jkeffects merged 16 commits from #6-messages into main 2024-12-31 13:25:12 +00:00
8 changed files with 124 additions and 17 deletions
Showing only changes of commit 020ae4a939 - Show all commits

View file

@ -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<any> {
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

View file

@ -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<Partial<protocolAgenda>>;
decisions: Array<Partial<protocolDecision>>;
presence: Array<Partial<member>>;
votings: Array<Partial<protocolVoting>>;
} = {
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,
},
],
};

View file

@ -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 {};
}
}
}

View file

@ -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;
}
}

View file

@ -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 = `<h1 style="font-size:10px; text-align:center; width:100%;">${title}</h1>`;
let footer = `
<div style="font-size:10px; text-align:center; width:100%; color:#888;">
Seite <span class="pageNumber"></span> von <span class="totalPages"></span>
</div>
`;
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);

View file

@ -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"),

View file

@ -0,0 +1,3 @@
<div style="font-size: 10px; text-align: center; width: 100%; color: #888">
Seite <span class="pageNumber"></span> von <span class="totalPages"></span>
</div>