Skip to content

Commit

Permalink
Merge pull request #685 from OneCommunityGlobal/development
Browse files Browse the repository at this point in the history
Backend Release to Main [1.29]
  • Loading branch information
one-community authored Jan 4, 2024
2 parents 5659686 + ce31c28 commit 06616fd
Show file tree
Hide file tree
Showing 13 changed files with 1,398 additions and 1,186 deletions.
60 changes: 29 additions & 31 deletions src/controllers/taskController.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const mongoose = require('mongoose');
const wbs = require('../models/wbs');
const WBS = require('../models/wbs');
const UserProfile = require('../models/userProfile');
const timeEntryHelper = require('../helpers/timeEntryHelper')();
const taskHelper = require('../helpers/taskHelper')();
const { hasPermission } = require('../utilities/permissions');
const emailSender = require('../utilities/emailSender');
const userProfile = require('../models/userProfile');

const taskController = function (Task) {
const getTasks = (req, res) => {
Expand Down Expand Up @@ -33,7 +33,7 @@ const taskController = function (Task) {
const getWBSId = (req, res) => {
const { wbsId } = req.params;

wbs.findById(wbsId)
WBS.findById(wbsId)
.then(results => res.status(200).send(results))
.catch(error => res.status(404).send(error));
};
Expand Down Expand Up @@ -446,7 +446,7 @@ const taskController = function (Task) {
});

const saveTask = _task.save();
const saveWbs = wbs.findById(wbsId).then((currentwbs) => {
const saveWbs = WBS.findById(wbsId).then((currentwbs) => {
currentwbs.modifiedDatetime = Date.now();
return currentwbs.save();
});
Expand Down Expand Up @@ -803,31 +803,28 @@ const taskController = function (Task) {
res.status(200).send('done');
};

const getTasksByUserList = async (req, res) => {
const { members } = req.query;
const membersArr = members.split(',');
const getTasksByUserId = async (req, res) => {
const { userId } = req.params;
try {
Task.find(
{ 'resources.userID': { $in: membersArr } },
'-resources.profilePic',
).then((results) => {
wbs
.find({
_id: { $in: results.map(item => item.wbsId) },
})
.then((projectIds) => {
const resultsWithProjectsIds = results.map((item) => {
item.set(
'projectId',
projectIds?.find(
projectId => projectId._id.toString() === item.wbsId.toString(),
)?.projectId,
{ strict: false },
);
return item;
});
res.status(200).send(resultsWithProjectsIds);
Task.find({
'resources.userID': mongoose.Types.ObjectId(userId),
}, '-resources.profilePic')
.then((results) => {
WBS.find({
_id: { $in: results.map(item => item.wbsId) },
}).then((WBSs) => {
const resultsWithProjectsIds = results.map((item) => {
item.set(
'projectId',
WBSs?.find(
wbs => wbs._id.toString() === item.wbsId.toString(),
)?.projectId,
{ strict: false },
);
return item;
});
res.status(200).send(resultsWithProjectsIds);
});
});
} catch (error) {
res.status(400).send(error);
Expand All @@ -837,14 +834,15 @@ const taskController = function (Task) {
const getTasksForTeamsByUser = async (req, res) => {
try {
const userId = mongoose.Types.ObjectId(req.params.userId);
const teamsData = await taskHelper.getTasksForTeams(userId).exec();
const teamsData = await taskHelper.getTasksForTeams(userId);
if (teamsData.length > 0) {
res.status(200).send(teamsData);
} else {
const singleUserData = await taskHelper.getTasksForSingleUser(userId).exec();
res.status(200).send(singleUserData);
}
} catch (error) {
console.log(error);
res.status(400).send(error);
}
};
Expand Down Expand Up @@ -872,8 +870,8 @@ const taskController = function (Task) {

const getRecipients = async function (myUserId) {
const recipients = [];
const user = await userProfile.findById(myUserId);
const membership = await userProfile.find({ role: { $in: ['Administrator', 'Manager', 'Mentor'] } });
const user = await UserProfile.findById(myUserId);
const membership = await UserProfile.find({ role: { $in: ['Administrator', 'Manager', 'Mentor'] } });
membership.forEach((member) => {
if (member.teams.some(team => user.teams.includes(team))) {
recipients.push(member.email);
Expand Down Expand Up @@ -917,7 +915,7 @@ const taskController = function (Task) {
updateAllParents,
deleteTaskByWBS,
moveTask,
getTasksByUserList,
getTasksByUserId,
getTasksForTeamsByUser,
updateTaskStatus,
sendReviewReq,
Expand Down
82 changes: 28 additions & 54 deletions src/controllers/teamController.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,66 +115,40 @@ const teamcontroller = function (Team) {
return;
}

if (
!req.params.teamId
|| !mongoose.Types.ObjectId.isValid(req.params.teamId)
|| !req.body.users
|| req.body.users.length === 0
) {
res.status(400).send({ error: 'Invalid request' });
const { teamId } = req.params;

if (!teamId || !mongoose.Types.ObjectId.isValid(teamId)) {
res.status(400).send({ error: 'Invalid teamId' });
return;
}

// verify team exists
const targetTeam = await Team.findById(teamId);

Team.findById(req.params.teamId)
.then((team) => {
if (!team || team.length === 0) {
res.status(400).send({ error: 'Invalid team' });
return;
}
const { users } = req.body;
const assignlist = [];
const unassignlist = [];

users.forEach((element) => {
const { userId, operation } = element;
// if user's profile is stored in cache, clear it so when you visit their profile page it will be up to date
if (cache.hasCache(`user-${userId}`)) cache.removeCache(`user-${userId}`);

if (operation === 'Assign') {
assignlist.push(userId);
} else {
unassignlist.push(userId);
}
});
if (!targetTeam || targetTeam.length === 0) {
res.status(400).send({ error: 'Invalid team' });
return;
}

const addTeamToUserProfile = userProfile
.updateMany({ _id: { $in: assignlist } }, { $addToSet: { teams: team._id } })
.exec();
const removeTeamFromUserProfile = userProfile
.updateMany({ _id: { $in: unassignlist } }, { $pull: { teams: team._id } })
.exec();
const addUserToTeam = Team.updateOne(
{ _id: team._id },
{ $addToSet: { members: { $each: assignlist.map(userId => ({ userId })) } } },
).exec();
const removeUserFromTeam = Team.updateOne(
{ _id: team._id },
{ $pull: { members: { userId: { $in: unassignlist } } } },
).exec();

Promise.all([addTeamToUserProfile, removeTeamFromUserProfile, addUserToTeam, removeUserFromTeam])
.then(() => {
res.status(200).send({ result: 'Done' });
})
.catch((error) => {
res.status(500).send({ error });
});
})
.catch((error) => {
res.status(500).send({ error });
});
try {
const { userId, operation } = req.body;

// if user's profile is stored in cache, clear it so when you visit their profile page it will be up to date
if (cache.hasCache(`user-${userId}`)) cache.removeCache(`user-${userId}`);


if (operation === 'Assign') {
await Team.findOneAndUpdate({ _id: teamId }, { $addToSet: { members: { userId } } }, { new: true });
const newMember = await userProfile.findOneAndUpdate({ _id: userId }, { $addToSet: { teams: teamId } }, { new: true });
res.status(200).send({ newMember });
} else {
await Team.findOneAndUpdate({ _id: teamId }, { $pull: { members: { userId } } });
await userProfile.findOneAndUpdate({ _id: userId }, { $pull: { teams: teamId } }, { new: true });
res.status(200).send({ result: 'Delete Success' });
}
} catch (error) {
res.status(500).send({ error });
}
};

const getTeamMembership = function (req, res) {
Expand Down
Loading

0 comments on commit 06616fd

Please sign in to comment.