Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.local
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 35 additions & 1 deletion src/api/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -315,6 +317,34 @@ export const permsUpdateController = asyncErrorHandler(

await user.save();

if (req.body.team_role !== undefined && process.env.ENABLE_EMAIL === "true") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be good to add a comment here explaining why we send an email when team role is not defined.

also will the email be sent even if some permissions are updated ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now every superuser will receive the same email for change in permission

added a few comments

the email will be sent even if only PERMISSIONS are updated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went through the new commit.

Contrary to your though process, looking superuser with this way will actually fetch all user since no permission criteria is given.

You should rather use the getUsersFromRoleID with role id of superuser (available as environment variable).

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",
Expand All @@ -323,6 +353,10 @@ export const permsUpdateController = asyncErrorHandler(
user: user_resp,
},
});




},
);

Expand Down
56 changes: 56 additions & 0 deletions src/api/services/emails/roleUpdate.ts
Original file line number Diff line number Diff line change
@@ -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 = `
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
</head>
<body style="font-family: 'Montserrat', sans-serif; background-color: #f8f9fa; color: #333;">
<div style="width: calc(80%); margin: 2rem auto; text-align: center; background-color: #fff; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); overflow: hidden;">
<div style="padding: 1.5rem; background: #222222; color: white;">
<div style="text-align: center;">
<img src="http://dtutimes.dtu.ac.in/images/logo-dark.png" alt="DTU Times Logo" style="width: 12rem;">
</div>
</div>

<div style="padding: 2rem;">
<h1 style="margin-top: 1rem; font-size: 2.2rem; font-weight: 300; text-align: center; color: #333;">
Role Update
</h1>

<div style="margin: 1.5rem auto; font-size: 1rem; line-height: 1.6;">
<p>Hey ${name},</p>
<p>Your role on DTU Times has been updated to:</p>
<div style="display: inline-block; padding: 0.6rem 1.2rem; background: #1ABC9C; color: white; border-radius: 8px; margin: 1rem 0; font-size: 1.1rem; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);">
<strong>${MainWebsiteRole[newRole]}</strong>
</div>
<p>Updated by: ${updatedBy}</p>
</div>

<a href="${APP_URL}/member/profile" style="display: inline-block; padding: 0.8rem 1.5rem; background: #1ABC9C; color: white; border-radius: 5px; text-decoration: none; transition: background-color 0.3s ease;">View My Profile</a>
</div>

<footer style="background-color: #222222; color: #ddd; text-align: center; padding: 1rem; font-size: 0.7rem;">
&copy; DTU Times ${COPYRIGHT_YEAR}. All Rights Reserved.
<br />
Developed by ${DEVELOPER_FOOTER}
</footer>
</div>
</body>
</html>
`;
}
}

export default RoleUpdateMail;