diff --git a/src/command/templateUsageCommand.ts b/src/command/templateUsageCommand.ts index b9218a9..35a3311 100644 --- a/src/command/templateUsageCommand.ts +++ b/src/command/templateUsageCommand.ts @@ -3,4 +3,6 @@ export interface UpdateTemplateUsageCommand { headerId: number | null; bodyId: number | null; footerId: number | null; + headerHeight: number | null; + footerHeight: number | null; } diff --git a/src/command/templateUsageCommandHandler.ts b/src/command/templateUsageCommandHandler.ts index 54626ba..c9eecce 100644 --- a/src/command/templateUsageCommandHandler.ts +++ b/src/command/templateUsageCommandHandler.ts @@ -17,6 +17,8 @@ export default abstract class TemplateUsageCommandHandler { headerId: updateTemplateUsage.headerId, bodyId: updateTemplateUsage.bodyId, footerId: updateTemplateUsage.footerId, + headerHeight: updateTemplateUsage.headerHeight, + footerHeight: updateTemplateUsage.footerHeight, }) .where("scope = :scope", { scope: updateTemplateUsage.scope }) .execute() diff --git a/src/controller/admin/templateUsageController.ts b/src/controller/admin/templateUsageController.ts index ac1e74a..0a75af3 100644 --- a/src/controller/admin/templateUsageController.ts +++ b/src/controller/admin/templateUsageController.ts @@ -78,12 +78,16 @@ export async function updateTemplateUsage(req: Request, res: Response): Promise< const headerId = req.body.headerId ?? null; const bodyId = req.body.bodyId ?? null; const footerId = req.body.footerId ?? null; + const headerHeight = req.body.headerHeight ?? null; + const footerHeight = req.body.footerHeight ?? null; let updateTemplateUsage: UpdateTemplateUsageCommand = { scope: scope, headerId: headerId, bodyId: bodyId, footerId: footerId, + headerHeight: headerHeight, + footerHeight: footerHeight, }; await TemplateUsageCommandHandler.update(updateTemplateUsage); diff --git a/src/data-source.ts b/src/data-source.ts index 7e8321f..f4ea044 100644 --- a/src/data-source.ts +++ b/src/data-source.ts @@ -61,6 +61,7 @@ import { newsletterRecipients } from "./entity/newsletterRecipients"; import { Newsletter1735118780511 } from "./migrations/1735118780511-newsletter"; import { newsletterConfig } from "./entity/newsletterConfig"; import { NewsletterConfig1735207446910 } from "./migrations/1735207446910-newsletterConfig"; +import { TemplateMargins1735733514043 } from "./migrations/1735733514043-templateMargins"; const dataSource = new DataSource({ type: DB_TYPE as any, @@ -132,6 +133,7 @@ const dataSource = new DataSource({ TemplateUsage1734949173739, Newsletter1735118780511, NewsletterConfig1735207446910, + TemplateMargins1735733514043, ], migrationsRun: true, migrationsTransactionMode: "each", diff --git a/src/entity/templateUsage.ts b/src/entity/templateUsage.ts index 8aefd11..00f41bf 100644 --- a/src/entity/templateUsage.ts +++ b/src/entity/templateUsage.ts @@ -7,15 +7,21 @@ export class templateUsage { @PrimaryColumn({ type: "varchar", length: 255 }) scope: PermissionModule; - @Column({ type: "number", nullable: true }) + @Column({ type: "int", nullable: true }) headerId: number | null; - @Column({ type: "number", nullable: true }) + @Column({ type: "int", nullable: true }) bodyId: number | null; - @Column({ type: "number", nullable: true }) + @Column({ type: "int", nullable: true }) footerId: number | null; + @Column({ type: "int", nullable: true }) + headerHeight: number | null; + + @Column({ type: "int", nullable: true }) + footerHeight: number | null; + @ManyToOne(() => template, { nullable: true, onDelete: "RESTRICT", diff --git a/src/factory/admin/templateUsage.ts b/src/factory/admin/templateUsage.ts index e2a5acb..3472d52 100644 --- a/src/factory/admin/templateUsage.ts +++ b/src/factory/admin/templateUsage.ts @@ -13,6 +13,8 @@ export default abstract class TemplateUsageFactory { header: record.header ? { id: record.header.id, template: record.header.template } : null, body: record.body ? { id: record.body.id, template: record.body.template } : null, footer: record.footer ? { id: record.footer.id, template: record.footer.template } : null, + headerHeight: record.headerHeight, + footerHeight: record.footerHeight, }; } diff --git a/src/helpers/pdfExport.ts b/src/helpers/pdfExport.ts index 26a32c2..ee1af97 100644 --- a/src/helpers/pdfExport.ts +++ b/src/helpers/pdfExport.ts @@ -11,7 +11,6 @@ export abstract class PdfExport { filename = null, data = {}, saveToDisk = true, - margins = { top: "15mm", bottom: "15mm" }, folder = "", }: { template: PermissionModule; @@ -19,12 +18,11 @@ export abstract class PdfExport { filename?: string; data?: any; saveToDisk?: boolean; - margins?: { top: string; bottom: string }; folder?: string; }) { if (folder != "") FileSystemHelper.createFolder(folder); - const { header, footer, body } = await TemplateHelper.renderFileForModule({ + const { header, footer, body, headerMargin, footerMargin } = await TemplateHelper.renderFileForModule({ module: template, headerData: data, bodyData: data, @@ -46,8 +44,8 @@ export abstract class PdfExport { format: "A4", printBackground: false, margin: { - top: margins.top, - bottom: margins.bottom, + top: (headerMargin ?? 15) + "mm", + bottom: (footerMargin ?? 15) + "mm", left: "10mm", right: "10mm", }, diff --git a/src/helpers/templateHelper.ts b/src/helpers/templateHelper.ts index cc1567b..eb681b6 100644 --- a/src/helpers/templateHelper.ts +++ b/src/helpers/templateHelper.ts @@ -1,7 +1,7 @@ import TemplateService from "../service/templateService"; import { PermissionModule } from "../type/permissionTypes"; import TemplateUsageService from "../service/templateUsageService"; -import Handlebars from "handlebars"; +import Handlebars, { template } from "handlebars"; import { FileSystemHelper } from "./fileSystemHelper"; export abstract class TemplateHelper { @@ -39,27 +39,27 @@ export abstract class TemplateHelper { headerData?: any; bodyData?: any; footerData?: any; - }): Promise<{ header: string; body: string; footer: string; margins?: { top: string; bottom: string } }> { - const moduleTemplates = await TemplateUsageService.getByScope(module); + }): Promise<{ header: string; body: string; footer: string; headerMargin?: number; footerMargin?: number }> { + const moduleTemplate = await TemplateUsageService.getByScope(module); let header = `

${title}

`; let footer = ""; let body = ""; - if (moduleTemplates.headerId) { - header = await this.getTemplateFromStore(moduleTemplates.headerId); + if (moduleTemplate.headerId) { + header = await this.getTemplateFromStore(moduleTemplate.headerId); header = this.applyDataToTemplate(header, { title, ...headerData }); } - if (moduleTemplates.footerId) { - footer = await this.getTemplateFromStore(moduleTemplates.footerId); + if (moduleTemplate.footerId) { + footer = await this.getTemplateFromStore(moduleTemplate.footerId); } else { footer = this.getTemplateFromFile(module + ".footer"); } footer = this.applyDataToTemplate(footer, footerData); - if (moduleTemplates.bodyId) { - body = await this.getTemplateFromStore(moduleTemplates.bodyId); + if (moduleTemplate.bodyId) { + body = await this.getTemplateFromStore(moduleTemplate.bodyId); } else { body = this.getTemplateFromFile(module + ".body"); } @@ -69,6 +69,8 @@ export abstract class TemplateHelper { header, footer, body, + headerMargin: moduleTemplate.headerHeight, + footerMargin: moduleTemplate.footerHeight, }; } } diff --git a/src/migrations/1735733514043-templateMargins.ts b/src/migrations/1735733514043-templateMargins.ts new file mode 100644 index 0000000..784d4fd --- /dev/null +++ b/src/migrations/1735733514043-templateMargins.ts @@ -0,0 +1,35 @@ +import { MigrationInterface, QueryRunner, TableColumn } from "typeorm"; +import { DB_TYPE } from "../env.defaults"; + +export class TemplateMargins1735733514043 implements MigrationInterface { + name = "TemplateMargins1735733514043"; + + public async up(queryRunner: QueryRunner): Promise { + const variableType_int = DB_TYPE == "mysql" ? "int" : "integer"; + + await queryRunner.addColumn( + "template_usage", + new TableColumn({ + name: "headerHeight", + type: variableType_int, + default: null, + isNullable: true, + }) + ); + + await queryRunner.addColumn( + "template_usage", + new TableColumn({ + name: "footerHeight", + type: variableType_int, + default: null, + isNullable: true, + }) + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.dropColumn("template_usage", "footerHeight"); + await queryRunner.dropColumn("template_usage", "headerHeight"); + } +} diff --git a/src/viewmodel/admin/templateUsage.models.ts b/src/viewmodel/admin/templateUsage.models.ts index 3f9aa00..8783eac 100644 --- a/src/viewmodel/admin/templateUsage.models.ts +++ b/src/viewmodel/admin/templateUsage.models.ts @@ -5,4 +5,6 @@ export interface TemplateUsageViewModel { header: { id: number; template: string } | null; body: { id: number; template: string } | null; footer: { id: number; template: string } | null; + headerHeight: number | null; + footerHeight: number | null; }