Skip to content

Commit

Permalink
feat: send email when create a new admin user
Browse files Browse the repository at this point in the history
  • Loading branch information
codaisa committed Jan 26, 2024
1 parent 835baa6 commit db32a0a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/modules/user/service/index.ts
Original file line number Diff line number Diff line change
@@ -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<User | null> => {
Expand Down Expand Up @@ -56,6 +58,9 @@ export class UserService {

static signUpAdmin = async (newUser: Partial<User>): Promise<User> => {
const userRepository = dataSource.getRepository(User);

await this.sendAdminWelcome(newUser);

return await userRepository.save(
userRepository.create({
...newUser,
Expand All @@ -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);
}
};
}
27 changes: 27 additions & 0 deletions src/modules/user/service/template-email.ts
Original file line number Diff line number Diff line change
@@ -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
`;

0 comments on commit db32a0a

Please sign in to comment.