Skip to content

Commit

Permalink
Merge pull request #676 from OneCommunityGlobal/xiaow_hotfix_of_activ…
Browse files Browse the repository at this point in the history
…e_icon_of_newMember

XiaoW_Hotfix of active icon when adding new member to a team
  • Loading branch information
one-community authored Jan 3, 2024
2 parents 0818af5 + bda1712 commit b36808c
Showing 1 changed file with 28 additions and 54 deletions.
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

0 comments on commit b36808c

Please sign in to comment.