Skip to content

Commit

Permalink
Merge pull request #943 from OneCommunityGlobal/Imran_Split_Blue_Squa…
Browse files Browse the repository at this point in the history
…res_Permissions

Imran split blue squares permissions
  • Loading branch information
one-community authored Sep 5, 2024
2 parents b72800d + 0f177f9 commit 32a4774
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 17 deletions.
140 changes: 129 additions & 11 deletions src/controllers/userProfileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ const userProfileController = function (UserProfile, Project) {
}
});

// Since we leverage cache for all team code retrival (refer func getAllTeamCode()),
// Since we leverage cache for all team code retrival (refer func getAllTeamCode()),
// we need to remove the cache when team code is updated in case of new team code generation
if (req.body.teamCode) {
// remove teamCode cache when new team assigned
Expand Down Expand Up @@ -684,12 +684,7 @@ const userProfileController = function (UserProfile, Project) {
userData.startDate = record.startDate.toISOString();
}
}
if (
req.body.infringements !== undefined &&
(await hasPermission(req.body.requestor, 'infringementAuthorizer'))
) {
record.infringements = req.body.infringements;
}

let updatedDiff = null;
if (PROTECTED_EMAIL_ACCOUNT.includes(record.email)) {
updatedDiff = record.modifiedPaths();
Expand Down Expand Up @@ -1549,6 +1544,124 @@ const userProfileController = function (UserProfile, Project) {
}
};

const addInfringements = async function (req, res) {
if (!(await hasPermission(req.body.requestor, 'addInfringements'))) {
res.status(403).send('You are not authorized to add blue square');
return;
}
const userid = req.params.userId;

cache.removeCache(`user-${userid}`);

if (req.body.blueSquare === undefined) {
res.status(400).send('Invalid Data');
return;
}

UserProfile.findById(userid, async (err, record) => {
if (err || !record) {
res.status(404).send('No valid records found');
return;
}
// find userData in cache
const isUserInCache = cache.hasCache('allusers');
let allUserData;
let userData;
let userIdx;
if (isUserInCache) {
allUserData = JSON.parse(cache.getCache('allusers'));
userIdx = allUserData.findIndex((users) => users._id === userid);
userData = allUserData[userIdx];
}

const originalinfringements = record?.infringements ?? [];
record.infringements = originalinfringements.concat(req.body.blueSquare);

record
.save()
.then((results) => {
userHelper.notifyInfringements(originalinfringements, results.infringements);
res.status(200).json({
_id: record._id,
});

// update alluser cache if we have cache
if (isUserInCache) {
allUserData.splice(userIdx, 1, userData);
cache.setCache('allusers', JSON.stringify(allUserData));
}
})
.catch((error) => res.status(400).send(error));
});
};

const editInfringements = async function (req, res) {
if (!(await hasPermission(req.body.requestor, 'editInfringements'))) {
res.status(403).send('You are not authorized to edit blue square');
return;
}
const { userId, blueSquareId } = req.params;
const { dateStamp, summary } = req.body;

UserProfile.findById(userId, async (err, record) => {
if (err || !record) {
res.status(404).send('No valid records found');
return;
}

const originalinfringements = record?.infringements ?? [];

record.infringements = originalinfringements.map((blueSquare) => {
if (blueSquare._id.equals(blueSquareId)) {
blueSquare.date = dateStamp ?? blueSquare.date;
blueSquare.description = summary ?? blueSquare.description;
}
return blueSquare;
});

record
.save()
.then((results) => {
userHelper.notifyInfringements(originalinfringements, results.infringements);
res.status(200).json({
_id: record._id,
});
})
.catch((error) => res.status(400).send(error));
});
};

const deleteInfringements = async function (req, res) {
if (!(await hasPermission(req.body.requestor, 'deleteInfringements'))) {
res.status(403).send('You are not authorized to delete blue square');
return;
}
const { userId, blueSquareId } = req.params;

UserProfile.findById(userId, async (err, record) => {
if (err || !record) {
res.status(404).send('No valid records found');
return;
}

const originalinfringements = record?.infringements ?? [];

record.infringements = originalinfringements.filter(
(infringement) => !infringement._id.equals(blueSquareId),
);

record
.save()
.then((results) => {
userHelper.notifyInfringements(originalinfringements, results.infringements);
res.status(200).json({
_id: record._id,
});
})
.catch((error) => res.status(400).send(error));
});
};

const getProjectsByPerson = async function (req, res) {
try {
const { name } = req.params;
Expand Down Expand Up @@ -1607,23 +1720,25 @@ const userProfileController = function (UserProfile, Project) {
return teamCodes;
}
const distinctTeamCodes = await UserProfile.distinct('teamCode', {
teamCode: { $ne: null }
teamCode: { $ne: null },
});
cache.setCache('teamCodes', JSON.stringify(distinctTeamCodes));
return distinctTeamCodes;
} catch (error) {
throw new Error('Encountered an error to get all team codes, please try again!');
}
}
};

const getAllTeamCode = async function (req, res) {
try {
const distinctTeamCodes = await getAllTeamCodeHelper();
return res.status(200).send({ message: 'Found', distinctTeamCodes });
} catch (error) {
return res.status(500).send({ message: 'Encountered an error to get all team codes, please try again!' });
return res
.status(500)
.send({ message: 'Encountered an error to get all team codes, please try again!' });
}
}
};

return {
postUserProfile,
Expand All @@ -1647,6 +1762,9 @@ const userProfileController = function (UserProfile, Project) {
getUserByFullName,
changeUserRehireableStatus,
authorizeUser,
addInfringements,
editInfringements,
deleteInfringements,
getProjectsByPerson,
getAllTeamCode,
getAllTeamCodeHelper,
Expand Down
7 changes: 7 additions & 0 deletions src/routes/userProfileRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ const routes = function (userProfile, project) {
.route('/userProfile/authorizeUser/weeeklySummaries')
.post(controller.authorizeUser);

userProfileRouter.route('/userProfile/:userId/addInfringement').post(controller.addInfringements);

userProfileRouter
.route('/userProfile/:userId/infringements/:blueSquareId')
.put(controller.editInfringements)
.delete(controller.deleteInfringements);

userProfileRouter.route('/userProfile/projects/:name').get(controller.getProjectsByPerson);

userProfileRouter.route('/userProfile/teamCode/list').get(controller.getAllTeamCode);
Expand Down
16 changes: 12 additions & 4 deletions src/test/createTestPermissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ const permissionsRoles = [
'changeUserStatus',
'updatePassword',
'deleteUserProfile',
'infringementAuthorizer',
'addInfringements',
'editInfringements',
'deleteInfringements',
// WBS
'postWbs',
'deleteWbs',
Expand Down Expand Up @@ -111,7 +113,9 @@ const permissionsRoles = [
'getUserProfiles',
'getProjectMembers',
'putUserProfile',
'infringementAuthorizer',
'addInfringements',
'editInfringements',
'deleteInfringements',
'getReporteesLimitRoles',
'updateTask',
'putTeam',
Expand Down Expand Up @@ -139,7 +143,9 @@ const permissionsRoles = [
'getUserProfiles',
'getProjectMembers',
'putUserProfile',
'infringementAuthorizer',
'addInfringements',
'editInfringements',
'deleteInfringements',
'getReporteesLimitRoles',
'getAllInvInProjectWBS',
'postInvInProjectWBS',
Expand Down Expand Up @@ -204,7 +210,9 @@ const permissionsRoles = [
'putUserProfileImportantInfo',
'updateSummaryRequirements',
'deleteUserProfile',
'infringementAuthorizer',
'addInfringements',
'editInfringements',
'deleteInfringements',
'postWbs',
'deleteWbs',
'getAllInvInProjectWBS',
Expand Down
20 changes: 18 additions & 2 deletions src/utilities/createInitialPermissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ const permissionsRoles = [
'changeUserRehireableStatus',
'updatePassword',
'deleteUserProfile',
'infringementAuthorizer',
'addInfringements',
'editInfringements',
'deleteInfringements',
'manageAdminLinks',
'manageTimeOffRequests',
'changeUserRehireableStatus',
Expand Down Expand Up @@ -123,6 +125,12 @@ const permissionsRoles = [
{
roleName: 'Manager',
permissions: [
'getUserProfiles',
'getProjectMembers',
'putUserProfile',
'addInfringements',
'editInfringements',
'deleteInfringements',
'getReporteesLimitRoles',
'postTask',
'updateTask',
Expand Down Expand Up @@ -152,6 +160,12 @@ const permissionsRoles = [
'updateTask',
'suggestTask',
'putReviewStatus',
'getUserProfiles',
'getProjectMembers',
'putUserProfile',
'addInfringements',
'editInfringements',
'deleteInfringements',
'getReporteesLimitRoles',
'getAllInvInProjectWBS',
'postInvInProjectWBS',
Expand Down Expand Up @@ -222,7 +236,9 @@ const permissionsRoles = [
'putUserProfileImportantInfo',
'updateSummaryRequirements',
'deleteUserProfile',
'infringementAuthorizer',
'addInfringements',
'editInfringements',
'deleteInfringements',
'postWbs',
'deleteWbs',
'getAllInvInProjectWBS',
Expand Down

0 comments on commit 32a4774

Please sign in to comment.