From db32a0a253bba9fde2f5c4e09ee7b10298299dd1 Mon Sep 17 00:00:00 2001 From: Poowerllz Date: Fri, 26 Jan 2024 00:06:05 -0300 Subject: [PATCH] feat: send email when create a new admin user --- src/modules/user/service/index.ts | 35 ++++++++++++++++++++++ src/modules/user/service/template-email.ts | 27 +++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/modules/user/service/template-email.ts diff --git a/src/modules/user/service/index.ts b/src/modules/user/service/index.ts index 21cb7f3..3a24f37 100644 --- a/src/modules/user/service/index.ts +++ b/src/modules/user/service/index.ts @@ -1,6 +1,8 @@ import { In } from "typeorm"; import dataSource from "../../../database/config/ormconfig"; import { User } from "../entity"; +import sgMail from "@sendgrid/mail"; +import { WELCOME_ADMIN_NP, WELCOME_ADMIN_SL } from "./template-email"; export class UserService { static findUserByID = async (id: string): Promise => { @@ -56,6 +58,9 @@ export class UserService { static signUpAdmin = async (newUser: Partial): Promise => { const userRepository = dataSource.getRepository(User); + + await this.sendAdminWelcome(newUser); + return await userRepository.save( userRepository.create({ ...newUser, @@ -69,4 +74,34 @@ export class UserService { await userRepository.delete({ id }); return; }; + + private static sendAdminWelcome = async (user: User) => { + try { + sgMail.setApiKey(process.env.SENDGRID_API_KEY || ""); + + const msg = { + to: user.email, + from: "noreply@quanti.ca", + subject: `${process.env.APP_NAME} - ${ + process.env.COUNTRY === "np" ? "एक-पटकको पासकोड" : "one-time passcode" + }`, + text: (process.env.COUNTRY === "np" + ? WELCOME_ADMIN_NP + : WELCOME_ADMIN_SL + ) + .replace("#{userName}", user?.name || "") + .replace("#{appName}", process.env.APP_NAME || "Coach Digital") + .replace("#{appName}", process.env.APP_NAME || "Coach Digital") + .replace("#{email}", user?.email || "") + .replace( + "#{url}", + `https://coachdigital.org/${process.env.COUNTRY}/admin/` + ), + }; + + await sgMail.send(msg); + } catch (err) { + console.log(err); + } + }; } diff --git a/src/modules/user/service/template-email.ts b/src/modules/user/service/template-email.ts new file mode 100644 index 0000000..f040a74 --- /dev/null +++ b/src/modules/user/service/template-email.ts @@ -0,0 +1,27 @@ +export const WELCOME_ADMIN_NP = ` +प्रिय #{userName}, + +हाम्रो खुशी छ कि तपाईंलाई सूचित गर्न सक्छौं कि तपाईंलाई #{appName} मा समाहित हुनको लागि आमन्त्रित गरिएको छ! कृपया तलको लिंकमा पहुँच गर्नुहोस् र ईमेलबाट लग इन गर्नुहोस्: #{email}। + +तपाईंले आफ्नो खाता यस लिंकमा पहुँच गर्न सक्नुहुनेछ: #{url} + +यदि तपाईंले यो नामांकन नगरे भने, तपाईंले यस ईमेललाई सुरक्षित रूपमा नदेख्न सक्नुहुनेछ वा हामीलाई जानकारी दिनुहोस्। + +धन्यवाद, +#{appName} टोली +`; + +export const WELCOME_ADMIN_SL = ` +traduza para nepali: + +Dear #{userName}, + +We are pleased to inform you that you have been invited to join #{appName}! Please access the link below and log in with the email: #{email}. + +You can access your account in this link: #{url} + +If you didn't request this, you can safely ignore this email or let us know. + +Thank you, +#{appName} team +`;