|
1 |
| -import fs from 'fs'; |
2 |
| -import path from 'path'; |
3 | 1 | import nodemailer from 'nodemailer';
|
4 |
| -import handlebars from 'handlebars'; |
5 |
| -import { fileURLToPath } from 'url'; |
6 | 2 | import { injectable } from 'tsyringe';
|
| 3 | +import type { Email } from '../interfaces/email.interface'; |
| 4 | +import { config } from '../common/config'; |
7 | 5 |
|
8 |
| -/* -------------------------------------------------------------------------- */ |
9 |
| -/* Service */ |
10 |
| -/* -------------------------------------------------------------------------- */ |
11 |
| -/* -------------------------------------------------------------------------- */ |
12 |
| -/* ---------------------------------- About --------------------------------- */ |
13 |
| -/* |
14 |
| -Services are responsible for handling business logic and data manipulation. |
15 |
| -They genreally call on repositories or other services to complete a use-case. |
16 |
| -*/ |
17 |
| -/* ---------------------------------- Notes --------------------------------- */ |
18 |
| -/* |
19 |
| -Services should be kept as clean and simple as possible. |
20 | 6 |
|
21 |
| -Create private functions to handle complex logic and keep the public methods as |
22 |
| -simple as possible. This makes the service easier to read, test and understand. |
23 |
| -*/ |
24 |
| -/* -------------------------------------------------------------------------- */ |
25 |
| - |
26 |
| -type SendMail = { |
27 |
| - to: string | string[]; |
28 |
| - subject: string; |
29 |
| - html: string; |
30 |
| -}; |
31 |
| - |
32 |
| -type SendTemplate<T> = { |
| 7 | +type SendProps = { |
33 | 8 | to: string | string[];
|
34 |
| - props: T; |
35 |
| -}; |
| 9 | + email: Email; |
| 10 | +} |
36 | 11 |
|
37 | 12 | @injectable()
|
38 | 13 | export class MailerService {
|
39 |
| - private nodemailer = nodemailer.createTransport({ |
40 |
| - host: 'smtp.ethereal.email', |
41 |
| - port: 587, |
42 |
| - secure: false, // Use `true` for port 465, `false` for all other ports |
43 |
| - auth: { |
44 |
| - |
45 |
| - pass: 'dshNQZYhATsdJ3ENke' |
46 |
| - } |
47 |
| - }); |
48 |
| - |
49 |
| - sendEmailVerificationToken(data: SendTemplate<{ token: string }>) { |
50 |
| - const template = handlebars.compile(this.getTemplate('email-verification-token')); |
51 |
| - return this.send({ |
52 |
| - to: data.to, |
53 |
| - subject: 'Email Verification', |
54 |
| - html: template({ token: data.props.token }) |
55 |
| - }); |
56 |
| - } |
57 |
| - |
58 |
| - sendEmailChangeNotification(data: SendTemplate<null>) { |
59 |
| - const template = handlebars.compile(this.getTemplate('email-change-notice')); |
60 |
| - return this.send({ |
61 |
| - to: data.to, |
62 |
| - subject: 'Email Change Notice', |
63 |
| - html: template(null) |
64 |
| - }); |
65 |
| - } |
66 |
| - |
67 |
| - sendLoginRequest(data: SendTemplate<{ token: string }>) { |
68 |
| - const template = handlebars.compile(this.getTemplate('email-verification-token')); |
69 |
| - return this.send({ |
70 |
| - to: data.to, |
71 |
| - subject: 'Login Request', |
72 |
| - html: template({ token: data.props.token }) |
73 |
| - }); |
74 |
| - } |
75 |
| - |
76 |
| - sendWelcome(data: SendTemplate<null>) { |
77 |
| - const template = handlebars.compile(this.getTemplate('welcome')); |
78 |
| - return this.send({ |
79 |
| - to: data.to, |
80 |
| - subject: 'Welcome!', |
81 |
| - html: template(null) |
82 |
| - }); |
83 |
| - } |
84 |
| - |
85 |
| - private async send({ to, subject, html }: SendMail) { |
86 |
| - const message = await this.nodemailer.sendMail({ |
87 |
| - from: '"Example" <[email protected]>', // sender address |
| 14 | + private async sendDev({ to, email }: SendProps) { |
| 15 | + const message = await nodemailer.createTransport({ |
| 16 | + host: 'smtp.ethereal.email', |
| 17 | + port: 587, |
| 18 | + secure: false, // Use `true` for port 465, `false` for all other ports |
| 19 | + auth: { |
| 20 | + |
| 21 | + pass: 'dshNQZYhATsdJ3ENke' |
| 22 | + } |
| 23 | + }).sendMail({ |
| 24 | + from: '"Example" <[email protected]>', |
88 | 25 | bcc: to,
|
89 |
| - subject, // Subject line |
90 |
| - text: html, |
91 |
| - html |
| 26 | + subject: email.subject(), |
| 27 | + text: email.html(), |
| 28 | + html: email.html() |
92 | 29 | });
|
93 | 30 | console.log(nodemailer.getTestMessageUrl(message));
|
94 | 31 | }
|
95 | 32 |
|
96 |
| - private getTemplate(template: string) { |
97 |
| - const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file |
98 |
| - const __dirname = path.dirname(__filename); // get the name of the directory |
99 |
| - return fs.readFileSync( |
100 |
| - path.join(__dirname, `../infrastructure/email-templates/${template}.hbs`), |
101 |
| - 'utf-8' |
102 |
| - ); |
| 33 | + private async sendProd({ to, email }: SendProps) { |
| 34 | + // CONFIGURE MAILER |
| 35 | + } |
| 36 | + |
| 37 | + async send({ to, email }: SendProps) { |
| 38 | + if (config.isProduction) { |
| 39 | + await this.sendProd({ to, email }); |
| 40 | + } else { |
| 41 | + await this.sendDev({ to, email }); |
| 42 | + } |
103 | 43 | }
|
104 | 44 | }
|
0 commit comments