Skip to content
Merged
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
104 changes: 51 additions & 53 deletions package-lock.json

Large diffs are not rendered by default.

65 changes: 49 additions & 16 deletions src/controllers/userProfileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ async function ValidatePassword(req, res) {
});
return;
}

const canUpdate = await hasPermission(req.body.requestor, 'updatePassword');

// Verify request is authorized by self or adminsitrator
if (
userId !== requestor.requestorId &&
!(await hasPermission(req.body.requestor, 'updatePassword'))
!canUpdate
) {
res.status(403).send({
error: "You are unauthorized to update this user's password",
Expand All @@ -60,8 +63,7 @@ async function ValidatePassword(req, res) {

// Verify request is authorized by self or adminsitrator
if (
userId === requestor.requestorId ||
!(await hasPermission(req.body.requestor, 'updatePassword'))
userId === requestor.requestorId && !canUpdate
) {
res.status(403).send({
error: "You are unauthorized to update this user's password",
Expand Down Expand Up @@ -523,14 +525,14 @@ const userProfileController = function (UserProfile, Project) {
}
// validate userprofile pic

if (req.body.profilePic) {
const results = userHelper.validateProfilePic(req.body.profilePic);
// if (req.body.profilePic) {
// const results = userHelper.validateProfilePic(req.body.profilePic);

if (!results.result) {
res.status(400).json(results.errors);
return;
}
}
// if (!results.result) {
// res.status(400).json(results.errors);
// return;
// }
// }

const canEditTeamCode =
req.body.requestor.role === 'Owner' ||
Expand Down Expand Up @@ -1412,7 +1414,7 @@ const userProfileController = function (UserProfile, Project) {

const resetPassword = async function (req, res) {
try {
ValidatePassword(req);
await ValidatePassword(req);

const requestor = await UserProfile.findById(req.body.requestor.requestorId)
.select('firstName lastName email role')
Expand All @@ -1433,11 +1435,6 @@ const userProfileController = function (UserProfile, Project) {
return;
}

if (!(await hasPermission(requestor, 'putUserProfileImportantInfo'))) {
res.status(403).send('You are not authorized to reset this users password');
return;
}

if (user.role === 'Owner' && !(await hasPermission(requestor, 'addDeleteEditOwners'))) {
res.status(403).send('You are not authorized to reset this user password');
return;
Expand Down Expand Up @@ -1957,6 +1954,41 @@ const userProfileController = function (UserProfile, Project) {
}
};

const replaceTeamCodeForUsers = async (req, res) => {
const { oldTeamCodes, newTeamCode } = req.body;

// Validate input
if (!Array.isArray(oldTeamCodes) || oldTeamCodes.length === 0 || !newTeamCode) {
console.error('Validation Failed:', { oldTeamCodes, newTeamCode });
return res.status(400).send({ error: 'Invalid input. Provide oldTeamCodes as an array and a valid newTeamCode.' });
}

try {
// Sanitize input
const sanitizedOldTeamCodes = oldTeamCodes.map(code => String(code).trim());

// Find and update users
const usersToUpdate = await UserProfile.find({ teamCode: { $in: sanitizedOldTeamCodes } });

if (usersToUpdate.length === 0) {
return res.status(404).send({ error: 'No users found with the specified team codes.' });
}

const updateResult = await UserProfile.updateMany(
{ teamCode: { $in: sanitizedOldTeamCodes } },
{ $set: { teamCode: newTeamCode } }
);

return res.status(200).send({
message: 'Team codes updated successfully.',
updatedCount: updateResult.nModified,
});
} catch (error) {
console.error('Error updating team codes:', error);
return res.status(500).send({ error: 'An error occurred while updating team codes.' });
}
};

return {
postUserProfile,
getUserProfiles,
Expand Down Expand Up @@ -1992,6 +2024,7 @@ const userProfileController = function (UserProfile, Project) {
getUserByAutocomplete,
getUserProfileBasicInfo,
updateUserInformation,
replaceTeamCodeForUsers,
};
};

Expand Down
2 changes: 2 additions & 0 deletions src/cronjobs/userProfileJobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const userProfileJobs = () => {
const SUNDAY = 0;
if (moment().tz('America/Los_Angeles').day() === SUNDAY) {
await userhelper.completeHoursAndMissedSummary();
await userhelper.weeklyBlueSquareReminderFunction();
await userhelper.inCompleteHoursEmailFunction();
}
},
null,
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/helperModels/myTeam.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ const myTeamSchema = new Schema({
}],

});

// Add index on the 'myteam._id' field to optimize lookups on team members
myTeamSchema.index({ 'myteam._id': 1 });
myTeamSchema.index({ 'myteam.role': 1 });
module.exports = mongoose.model('myTeam', myTeamSchema, 'myTeam');
Loading