-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLetterController.ts
More file actions
69 lines (60 loc) · 2.47 KB
/
LetterController.ts
File metadata and controls
69 lines (60 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { Request, Response, NextFunction } from 'express';
import { ResponseError, Trainee } from '../../models';
import { LetterData, LetterGeneratorType, LetterType } from '../../services';
import { TraineesRepository } from '../../repositories';
import sentry from '@sentry/node';
export interface LetterControllerType {
generateLetter(req: Request, res: Response, next: NextFunction): Promise<void>;
}
export class LetterController implements LetterControllerType {
constructor(
private readonly letterGenerator: LetterGeneratorType,
private readonly traineeRepository: TraineesRepository
) {}
async generateLetter(req: Request, res: Response, next: NextFunction): Promise<void> {
const { type } = req.body as { type: LetterType };
const traineeID = String(req.params.id);
const trainee = await this.traineeRepository.getTrainee(traineeID);
if (!trainee) {
const error = new ResponseError(`Trainee with ID ${traineeID} not found`);
res.status(404).json(error);
return;
}
if (!type || !Object.values(LetterType).includes(type)) {
const error = new ResponseError(`Invalid letter type provided. Available types: [${Object.values(LetterType)}]`);
res.status(400).json(error);
return;
}
try {
const data = this.getLetterData(trainee, type);
const pdfStream = await this.letterGenerator.generateLetter(type, data);
res.status(200).header({
'Content-Type': 'application/pdf',
'Content-Disposition': `attachment; filename="${this.getLetterFileName(trainee, type)}"`,
});
pdfStream.pipe(res);
} catch (error) {
sentry.captureException(error);
next(error);
}
}
private getLetterData(trainee: Trainee, type: LetterType): LetterData {
const dateString = new Date().toLocaleDateString('en-gb', { year: 'numeric', month: 'long', day: 'numeric' });
if (type === LetterType.GITHUB_TRAINEE) {
return {
NAME: trainee.personalInfo.firstName + ' ' + trainee.personalInfo.lastName,
GITHUB: trainee.contactInfo.githubHandle ?? 'N/A',
DATE: dateString,
};
} else {
throw new Error(`Unsupported letter type: ${type}`);
}
}
private getLetterFileName(trainee: Trainee, type: LetterType): string {
const typeMap = {
[LetterType.GITHUB_TRAINEE]: 'GitHub',
};
const fullName = `${trainee.personalInfo.firstName} ${trainee.personalInfo.lastName}`;
return `${fullName} - ${typeMap[type] ?? type}.pdf`;
}
}