| 1 | import { findUserById } from '@app/services/user.service';
|
| 2 | import { sendEmail } from '@app/lib/mail';
|
| 3 | import { logger } from '@app/lib/logger';
|
| 4 |
|
| 5 | const APP_SCHEME = 'orbit';
|
| 6 | const RESET_PATH = 'auth/reset-password';
|
| 7 |
|
| 8 | const resetLink = (token: string) => `${APP_SCHEME}://${RESET_PATH}?token=${token}`;
|
| 9 |
|
| 10 | const escapeHtml = (value: string) =>
|
| 11 | value.replace(
|
| 12 | /[&<>"']/g,
|
| 13 | (char) =>
|
| 14 | ({
|
| 15 | '&': '&',
|
| 16 | '<': '<',
|
| 17 | '>': '>',
|
| 18 | '"': '"',
|
| 19 | "'": ''',
|
| 20 | })[char]!,
|
| 21 | );
|
| 22 |
|
| 23 | const text = (firstName: string, token: string) => `
|
| 24 | Oi, ${firstName}.
|
| 25 |
|
| 26 | Ouvimos por aí que você precisa trocar a senha.
|
| 27 |
|
| 28 | Abra este link no celular para escolher uma nova senha:
|
| 29 | ${resetLink(token)}
|
| 30 |
|
| 31 | O link expira em uma hora.
|
| 32 |
|
| 33 | Qualquer coisa prende o grito!
|
| 34 | `;
|
| 35 |
|
| 36 | const html = (firstName: string, token: string) => `
|
| 37 | <div style="font-family: sans-serif; font-size: 16px; line-height: 1.6; color: #111;">
|
| 38 | <p>Oi, ${escapeHtml(firstName)}.</p>
|
| 39 | <p>Ouvimos por aí que você precisa trocar a senha.</p>
|
| 40 | <p>
|
| 41 | <a href="${resetLink(token)}"
|
| 42 | style="display: inline-block; padding: 12px 20px; border-radius: 8px; background-color: #13ec5b; color: #111; font-weight: bold; text-decoration: none;">
|
| 43 | Escolher uma nova senha
|
| 44 | </a>
|
| 45 | </p>
|
| 46 | <p style="font-size: 13px; color: #666;">
|
| 47 | Se o botão não abrir o app, copie e cole este endereço no celular:<br />
|
| 48 | ${resetLink(token)}
|
| 49 | </p>
|
| 50 | <p>O link expira em uma hora.</p>
|
| 51 | <p>Qualquer coisa prende o grito!</p>
|
| 52 | </div>
|
| 53 | `;
|
| 54 |
|
| 55 | export async function sendResetPasswordEmail(userId: number, token: string): Promise<void> {
|
| 56 | const user = await findUserById(userId)!;
|
| 57 |
|
| 58 | const { data, error } = await sendEmail({
|
| 59 | to: user!.email,
|
| 60 | subject: "Esqueceu a senha do nexo?",
|
| 61 | text: text(user!.firstName, token),
|
| 62 | html: html(user!.firstName, token),
|
| 63 | });
|
| 64 |
|
| 65 | if (error) {
|
| 66 | logger.error({ err: error }, "Error sending email");
|
| 67 | }
|
| 68 |
|
| 69 | logger.info({ emailId: data?.id }, "Email sent successfully");
|
| 70 | }
|