Now accepting arrays for sponsoring contract generation

ref #5
This commit is contained in:
Nicolai Ort 2021-02-05 21:56:33 +01:00
parent 4cd437b6af
commit f833ae222e
3 changed files with 14 additions and 9 deletions

View File

@ -43,11 +43,11 @@ export class PdfCreator {
* @param runner The runner you want to generate the contracts for. * @param runner The runner you want to generate the contracts for.
* @param locale The locale used for the contracts (default:en) * @param locale The locale used for the contracts (default:en)
*/ */
public async generateSponsoringContract(runner: Runner, locale: string = "en"): Promise<any> { public async generateSponsoringContract(runners: Runner[], locale: string = "en"): Promise<any> {
i18next.changeLanguage(locale); i18next.changeLanguage(locale);
const template_source = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8'); const template_source = fs.readFileSync(`${this.templateDir}/sponsoring_contract.html`, 'utf8');
const template = Handlebars.compile(template_source); const template = Handlebars.compile(template_source);
const result = template({ runner: runner }) const result = template({ runners })
return await this.renderPdf(result, { format: "A5", landscape: true }); return await this.renderPdf(result, { format: "A5", landscape: true });
} }

View File

@ -12,9 +12,12 @@ export class PdfController {
@Post('/contracts') @Post('/contracts')
@OpenAPI({ description: "Generate Sponsoring contract pdfs from runner objects." }) @OpenAPI({ description: "Generate Sponsoring contract pdfs from runner objects." })
async generateContracts(@Body({ validate: true }) runner: Runner, @Res() res: any, @QueryParam("locale") locale: string) { async generateContracts(@Body({ validate: true }) runners: Runner | Runner[], @Res() res: any, @QueryParam("locale") locale: string) {
const contracts = await this.pdf.generateSponsoringContract(runner, locale); if (!Array.isArray(runners)) {
runners = [runners]
}
const contracts = await this.pdf.generateSponsoringContract(runners, locale);
res.setHeader('content-type', 'application/pdf'); res.setHeader('content-type', 'application/pdf');
return await contracts; return contracts;
} }
} }

View File

@ -5,11 +5,13 @@
<link rel="stylesheet" href="https://unpkg.com/mvp.css"> <link rel="stylesheet" href="https://unpkg.com/mvp.css">
</head> </head>
<body> <body>
{{#each runners}}
<div class="page"> <div class="page">
<p>ID: {{runner.id}}</p> <p>ID: {{this.id}}</p>
<p>{{__ "firstname"}}: {{runner.firstname}}</p> <p>{{__ "firstname"}}: {{this.firstname}}</p>
<p>{{__ "lastname"}}: {{runner.lastname}}</p> <p>{{__ "lastname"}}: {{this.lastname}}</p>
<p>{{__ "group"}}: {{runner.group.name}}</p> <p>{{__ "group"}}: {{this.group.name}}</p>
</div> </div>
{{/each}}
</body> </body>
</html> </html>