-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add email module and remove notify strategy
- Loading branch information
Showing
16 changed files
with
172 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const SMTP_CONFIG_OPTIONS = 'SMTP_CONFIG_OPTIONS'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export class EmailRequest { | ||
from: string; | ||
to: string; | ||
subject: string; | ||
body: string; | ||
body_html?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { DynamicModule, Global, Module } from '@nestjs/common'; | ||
import { SMTP_CONFIG_OPTIONS } from './constants'; | ||
import { EmailService } from './email.service'; | ||
import { createTransport } from 'nodemailer'; | ||
import { SmtpOptions } from './interfaces/smtp-options.interface'; | ||
|
||
@Global() | ||
@Module({}) | ||
export class EmailModule { | ||
public static forRoot(smtpOptions: SmtpOptions): DynamicModule { | ||
const transporter = createTransport({ | ||
name: smtpOptions.host, | ||
host: smtpOptions.host, | ||
port: smtpOptions.port, | ||
secure: smtpOptions.secure, | ||
auth: { | ||
user: smtpOptions.email, | ||
pass: smtpOptions.password, | ||
}, | ||
tls: { | ||
ciphers: 'SSLv3', | ||
}, | ||
}); | ||
|
||
return { | ||
module: EmailModule, | ||
providers: [ | ||
{ | ||
provide: SMTP_CONFIG_OPTIONS, | ||
useValue: transporter, | ||
}, | ||
EmailService, | ||
], | ||
exports: [EmailService], | ||
global: true, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { mock } from 'jest-mock-extended'; | ||
import { SentMessageInfo, Transporter } from 'nodemailer'; | ||
import { SMTP_CONFIG_OPTIONS } from './constants'; | ||
import { EmailService } from './email.service'; | ||
|
||
describe('EmailService', () => { | ||
let service: EmailService; | ||
const transporterMock = mock<Transporter<SentMessageInfo>>(); | ||
|
||
beforeEach(async () => { | ||
const smtpOptionsProvider = { | ||
provide: SMTP_CONFIG_OPTIONS, | ||
useValue: transporterMock, | ||
}; | ||
|
||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [smtpOptionsProvider, EmailService], | ||
}).compile(); | ||
|
||
service = module.get<EmailService>(EmailService); | ||
}); | ||
|
||
it('should send email', async () => { | ||
let sendMail = false; | ||
transporterMock.verify.mockReturnValue(Promise.resolve(true)); | ||
transporterMock.sendMail.mockImplementation(async () => (sendMail = true)); | ||
await service.sendMail({ | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Email sent', | ||
body: 'body', | ||
body_html: '<p>body</p>', | ||
}); | ||
|
||
expect(sendMail).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { SentMessageInfo, Transporter } from 'nodemailer'; | ||
import { EmailRequest } from './dto/email-request.dto'; | ||
import { SMTP_CONFIG_OPTIONS } from './constants'; | ||
import { ConnectionFailed } from './errors/connection-failed.error'; | ||
|
||
interface IEmailService { | ||
sendMail(emailRequest: EmailRequest): Promise<void>; | ||
} | ||
|
||
@Injectable() | ||
export class EmailService implements IEmailService { | ||
constructor( | ||
@Inject(SMTP_CONFIG_OPTIONS) | ||
private transporter: Transporter<SentMessageInfo>, | ||
) {} | ||
|
||
async sendMail({ | ||
from, | ||
to, | ||
subject, | ||
body, | ||
body_html = '', | ||
}: EmailRequest): Promise<void> { | ||
const verifier = await this.transporter.verify(); | ||
|
||
if (!verifier) | ||
throw new ConnectionFailed( | ||
'Connection failed. Verify your credentials or connection.', | ||
); | ||
|
||
const mail = { | ||
from: from, | ||
to: to, | ||
subject: subject, | ||
text: body, | ||
html: body_html, | ||
}; | ||
|
||
try { | ||
await this.transporter.sendMail(mail); | ||
} catch (err) { | ||
throw err; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class ConnectionFailed extends Error { | ||
constructor(message?: string) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './smtp-options.interface'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export interface SmtpOptions { | ||
/** | ||
* host | ||
*/ | ||
host: string; | ||
|
||
/** | ||
* port | ||
*/ | ||
port: number; | ||
|
||
/** | ||
*/ | ||
email: string; | ||
|
||
/** | ||
* password | ||
*/ | ||
password: string; | ||
|
||
/** | ||
* secure | ||
*/ | ||
secure: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters