-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
Copy pathemail.ts
57 lines (52 loc) · 1.82 KB
/
email.ts
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
import { ADMIN } from '../../types/permissions';
import {
type EmailService,
TemplateFormat,
} from '../../services/email-service';
import type { IUnleashConfig } from '../../types/option';
import type { IUnleashServices } from '../../types/services';
import type { Request, Response } from 'express';
import Controller from '../controller';
import type { Logger } from '../../logger';
import sanitize from 'sanitize-filename';
export default class EmailController extends Controller {
private emailService: EmailService;
private logger: Logger;
constructor(
config: IUnleashConfig,
{ emailService }: Pick<IUnleashServices, 'emailService'>,
) {
super(config);
this.emailService = emailService;
this.logger = config.getLogger('routes/admin-api/email');
this.get('/preview/html/:template', this.getHtmlPreview, ADMIN);
this.get('/preview/text/:template', this.getTextPreview, ADMIN);
}
async getHtmlPreview(req: Request, res: Response): Promise<void> {
const { template } = req.params;
const ctx = req.query;
const data = await this.emailService.compileTemplate(
sanitize(template),
TemplateFormat.HTML,
ctx,
);
res.setHeader('Content-Type', 'text/html');
res.status(200);
res.send(data);
res.end();
}
async getTextPreview(req: Request, res: Response): Promise<void> {
const { template } = req.params;
const ctx = req.query;
const data = await this.emailService.compileTemplate(
sanitize(template),
TemplateFormat.PLAIN,
ctx,
);
res.setHeader('Content-Type', 'text/plain');
res.status(200);
res.send(data);
res.end();
}
}
module.exports = EmailController;