diff --git a/.env.local b/.env.local index 822136a..820f66f 100644 --- a/.env.local +++ b/.env.local @@ -13,4 +13,4 @@ HOST="http://team.dtutimes.com" ENABLE_EMAIL="false" # set to true if you want to enable email service TOTP_SECRET=--TOTP 2FA SECRET-- NOTIF_SECRET=--PASSWORD-- # this is for notifications -NOTIF_PRIVATE_KEY=--PASSWORD-- # this is for notifications +NOTIF_PRIVATE_KEY=--PASSWORD-- # this is for notifications \ No newline at end of file diff --git a/src/api/controllers/userController.ts b/src/api/controllers/userController.ts index b518ec8..49feb1a 100644 --- a/src/api/controllers/userController.ts +++ b/src/api/controllers/userController.ts @@ -11,7 +11,8 @@ import { IUser, PopulatedUser, User } from "../models/userModel"; import * as UserService from "../services/userService"; import { Blog } from "../models/blogModel"; import { assertHydratedUser, assertProtectedUser } from "../helpers/assertions"; - +import RoleUpdateMail from "../services/emails/roleUpdate"; +import { getUsersPermissionBased } from "../helpers/emailHelper"; /** * @description Retrieves all team members excluding those with the role MainWebsiteRole.DoNotDisplay. * @route GET /get-team @@ -61,6 +62,7 @@ export const getTeam = asyncErrorHandler(async (req, res) => { * - Sends a success response with the list of users. */ + export const getAllUsers = asyncErrorHandler(async (req, res) => { //add logic here @@ -315,6 +317,34 @@ export const permsUpdateController = asyncErrorHandler( await user.save(); + if (req.body.team_role !== undefined && process.env.ENABLE_EMAIL === "true") { + try { + //find the person who updated the role + const updaterId = res.locals.user_id; + const senderUser = await User.findById(updaterId); + + if (!senderUser) { + console.error("Sender user not found"); + return; + } + //get their name + const updaterName = senderUser.name; + const mail = new RoleUpdateMail(user, req.body.team_role, updaterName); + //send the mail to the user + await mail.sendTo(user.email); + console.log(`Role update email sent to ${user.email}`); + + const superUsers = await getUsersPermissionBased([]); + //send the mail to all users with superuser role + for (const superUser of superUsers) { + await mail.sendTo(superUser.email); + console.log(`Role update notification sent to superuser ${superUser.email}`); + } + } catch (error) { + console.error(`Failed to send role update email to ${user.email}:`, error); + } + } + const user_resp = user_to_response(user); return res.status(StatusCode.OK).json({ status: "success", @@ -323,6 +353,10 @@ export const permsUpdateController = asyncErrorHandler( user: user_resp, }, }); + + + + }, ); diff --git a/src/api/services/emails/roleUpdate.ts b/src/api/services/emails/roleUpdate.ts new file mode 100644 index 0000000..8595b98 --- /dev/null +++ b/src/api/services/emails/roleUpdate.ts @@ -0,0 +1,56 @@ +import { PopulatedUser } from "../../models/userModel"; +import Email, { APP_URL, COPYRIGHT_YEAR, DEVELOPER_FOOTER } from "../emailService"; +import MainWebsiteRole from "../../helpers/mainWebsiteRole"; + +class RoleUpdateMail extends Email { + constructor(user: PopulatedUser, newRole: MainWebsiteRole, updatedBy: string) { + super(); + const { name } = user; + + this.subject = "Your DTU Times Role Has Been Updated"; + this.html = ` + + + + + + + + +
+
+
+ DTU Times Logo +
+
+ +
+

+ Role Update +

+ +
+

Hey ${name},

+

Your role on DTU Times has been updated to:

+
+ ${MainWebsiteRole[newRole]} +
+

Updated by: ${updatedBy}

+
+ + View My Profile +
+ + +
+ + + `; + } +} + +export default RoleUpdateMail; \ No newline at end of file