Skip to content

Commit

Permalink
feat: add logic to send forgot password link
Browse files Browse the repository at this point in the history
  • Loading branch information
hdev14 committed Feb 14, 2024
1 parent 4f72095 commit dbd626c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/b3_stock_alerts/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@ export default class AuthService {

return { data: true };
}

async forgotPassword(email: string): Promise<Result<void>> {
return {};
}
}
15 changes: 14 additions & 1 deletion src/b3_stock_alerts/EmailGateway.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import nodemailer, { Transporter } from 'nodemailer';
import AlertNotification, { AlertNotificationTypes, NotificationData } from './AlertNotification';
import ConfirmationCode, { SendCodeParams } from './ConfirmationCode';
import ForgotPassword, { ForgotPasswordParams } from './ForgotPassword';

export default class EmailGateway implements AlertNotification, ConfirmationCode {
export default class EmailGateway implements AlertNotification, ConfirmationCode, ForgotPassword {
private readonly transporter: Transporter;

constructor() {
Expand Down Expand Up @@ -50,4 +51,16 @@ export default class EmailGateway implements AlertNotification, ConfirmationCode

await this.transporter.sendMail(message);
}

async sendForgotPasswordLink(params: ForgotPasswordParams): Promise<void> {
const message = {
from: process.env.APPLICATION_EMAIL,
to: params.email,
subject: 'Esqueceu a senha?',
text: `Acesse o link ${process.env.SERVER_URL}/pages/forgot-password?user_id=${params.user_id} para redefinir sua senha.`,
html: `<p>Acesse o <a href="${process.env.SERVER_URL}/pages/forgot-password?user_id=${params.user_id}">link</a> para redefinir sua senha.</p>`,
};

await this.transporter.sendMail(message);
}
}
21 changes: 20 additions & 1 deletion src/b3_stock_alerts/EmailGateway.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe("EmailGateway's unit tests", () => {
});
});

describe('EmailGateway.notify', () => {
describe('EmailGateway.sendCode', () => {
it('sends an email for code confirmation', async () => {
expect.assertions(1);

Expand All @@ -107,4 +107,23 @@ describe("EmailGateway's unit tests", () => {
});
});
});

describe('EmailGateway.sendForgotPasswordLink', () => {
it('sends an email with forgot password link', async () => {
expect.assertions(1);

const email = faker.internet.email();
const user_id = faker.string.uuid();

await email_gateway.sendForgotPasswordLink({ email, user_id });

expect(transport_mock.sendMail).toHaveBeenCalledWith({
from: 'test@server.com',
to: email,
subject: 'Esqueceu a senha?',
text: `Acesse o link http://localhost:5000/pages/forgot-password?user_id=${user_id} para redefinir sua senha.`,
html: `<p>Acesse o <a href="http://localhost:5000/pages/forgot-password?user_id=${user_id}">link</a> para redefinir sua senha.</p>`,
});
});
});
});
10 changes: 10 additions & 0 deletions src/b3_stock_alerts/ForgotPassword.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type ForgotPasswordParams = {
email: string;
user_id: string;
};

interface ForgotPassword {
sendForgotPasswordLink(params: ForgotPasswordParams): Promise<void>;
}

export default ForgotPassword;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
"skipLibCheck": true
},
"exclude": [
"node_modules",
"node_modules"
]
}

0 comments on commit dbd626c

Please sign in to comment.