src/module/template/template.service.ts
Properties |
|
Methods |
constructor(settings: SettingsService)
|
||||||
Defined in src/module/template/template.service.ts:25
|
||||||
Parameters :
|
Private assertFileExists | ||||||
assertFileExists(filePath: string)
|
||||||
Defined in src/module/template/template.service.ts:83
|
||||||
Parameters :
Returns :
void
|
checkAllTemplatesPresent |
checkAllTemplatesPresent()
|
Defined in src/module/template/template.service.ts:49
|
Returns :
void
|
getAttendanceTemplate |
getAttendanceTemplate()
|
Defined in src/module/template/template.service.ts:29
|
Returns :
Template<AttendanceAttributes>
|
getCredentialsTemplate |
getCredentialsTemplate()
|
Defined in src/module/template/template.service.ts:33
|
Returns :
Template<CredentialsAttributes>
|
getMailTemplate |
getMailTemplate()
|
Defined in src/module/template/template.service.ts:37
|
Returns :
Template<MailAttribtutes>
|
Private getPathToTemplateFile | ||||||
getPathToTemplateFile(file: string)
|
||||||
Defined in src/module/template/template.service.ts:79
|
||||||
Parameters :
Returns :
string
|
getScheinexamTemplate |
getScheinexamTemplate()
|
Defined in src/module/template/template.service.ts:41
|
Returns :
Template<ScheinexamAttributes>
|
getScheinstatusTemplate |
getScheinstatusTemplate()
|
Defined in src/module/template/template.service.ts:45
|
Returns :
Template<ScheinstatusAttributes>
|
Private getTemplate | ||||||
getTemplate(file: string)
|
||||||
Defined in src/module/template/template.service.ts:72
|
||||||
Parameters :
Returns :
pug.compileTemplate
|
Private Readonly logger |
Default value : new Logger(TemplateService.name)
|
Defined in src/module/template/template.service.ts:18
|
Private Readonly TEMPLATE_FILES |
Type : object
|
Default value : {
attendance: 'attendance.pug',
credentials: 'credentials.pug',
mail: 'mail.pug',
scheinexam: 'scheinexam.pug',
scheinstatus: 'scheinstatus.pug',
}
|
Defined in src/module/template/template.service.ts:19
|
import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common';
import fs from 'fs';
import path from 'path';
import pug from 'pug';
import { StartUpException } from '../../exceptions/StartUpException';
import { SettingsService } from '../settings/settings.service';
import {
AttendanceAttributes,
CredentialsAttributes,
MailAttribtutes,
ScheinexamAttributes,
ScheinstatusAttributes,
Template,
} from './template.types';
@Injectable()
export class TemplateService {
private readonly logger = new Logger(TemplateService.name);
private readonly TEMPLATE_FILES = {
attendance: 'attendance.pug',
credentials: 'credentials.pug',
mail: 'mail.pug',
scheinexam: 'scheinexam.pug',
scheinstatus: 'scheinstatus.pug',
};
constructor(private readonly settings: SettingsService) {}
getAttendanceTemplate(): Template<AttendanceAttributes> {
return this.getTemplate(this.TEMPLATE_FILES.attendance);
}
getCredentialsTemplate(): Template<CredentialsAttributes> {
return this.getTemplate(this.TEMPLATE_FILES.credentials);
}
getMailTemplate(): Template<MailAttribtutes> {
return this.getTemplate(this.TEMPLATE_FILES.mail);
}
getScheinexamTemplate(): Template<ScheinexamAttributes> {
return this.getTemplate(this.TEMPLATE_FILES.scheinexam);
}
getScheinstatusTemplate(): Template<ScheinstatusAttributes> {
return this.getTemplate(this.TEMPLATE_FILES.scheinstatus);
}
checkAllTemplatesPresent(): void {
const notExisting: string[] = [];
for (const [, value] of Object.entries(this.TEMPLATE_FILES)) {
const filePath = this.getPathToTemplateFile(value);
try {
fs.accessSync(filePath);
this.logger.log(`Template file '${value}' found.`);
} catch {
notExisting.push(filePath);
}
}
if (notExisting.length > 0) {
throw new StartUpException(
`The following template files could not be found: \n\t- ${notExisting.join(
'\n\t- '
)}`
);
}
}
private getTemplate(file: string): pug.compileTemplate {
const filePath = this.getPathToTemplateFile(file);
this.assertFileExists(filePath);
return pug.compileFile(filePath);
}
private getPathToTemplateFile(file: string): string {
return path.join(this.settings.getConfigPath(), 'templates', file);
}
private assertFileExists(filePath: string) {
try {
fs.accessSync(filePath);
} catch (err) {
this.logger.error(`Could not find the template file at '${filePath}'`);
throw new InternalServerErrorException(
`Could not find the template file at '${filePath}'`
);
}
}
}