From 9abc6e45a4461fdeb436e67cdc77d848f221013b Mon Sep 17 00:00:00 2001
From: Sebastian <sebastian.mihalache@thinslices.com>
Date: Tue, 27 Mar 2018 15:24:44 +0300
Subject: [PATCH] feat(component-invite): remove team from user on revoke

---
 packages/component-invite/src/helpers/Team.js            | 4 +---
 packages/component-invite/src/routes/deleteInvitation.js | 5 +++--
 2 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/packages/component-invite/src/helpers/Team.js b/packages/component-invite/src/helpers/Team.js
index 9a9319e25..9c8d5175a 100644
--- a/packages/component-invite/src/helpers/Team.js
+++ b/packages/component-invite/src/helpers/Team.js
@@ -118,12 +118,10 @@ const getMatchingTeams = (teams, TeamModel, collectionId, role) =>
     .filter(Boolean)
 
 const removeTeamMember = async (teamId, userId, TeamModel) => {
-  let team = await TeamModel.find(teamId)
+  const team = await TeamModel.find(teamId)
   const members = team.members.filter(member => member !== userId)
   team.members = members
 
-  team = await team.updateProperties(team)
-
   await team.save()
 }
 
diff --git a/packages/component-invite/src/routes/deleteInvitation.js b/packages/component-invite/src/routes/deleteInvitation.js
index 906226282..59dd9905e 100644
--- a/packages/component-invite/src/routes/deleteInvitation.js
+++ b/packages/component-invite/src/routes/deleteInvitation.js
@@ -28,7 +28,7 @@ module.exports = models => async (req, res) => {
   const { collectionId, userId } = req.params
   try {
     const collection = await models.Collection.find(collectionId)
-    let user = await models.User.find(userId)
+    const user = await models.User.find(userId)
     const team = await teamHelper.getTeamByGroupAndCollection(
       collectionId,
       role,
@@ -42,8 +42,9 @@ module.exports = models => async (req, res) => {
       return
     }
     await inviteHelper.revokeInvitation(user, collectionId, role)
-    user = await models.User.find(userId)
     await teamHelper.removeTeamMember(team.id, userId, models.Team)
+    user.teams = user.teams.filter(userTeam => team.id !== userTeam.id)
+    await user.save()
     await collectionHelper.removeAssignedPeople(collection, user.email)
     try {
       await mailService.setupRevokeInvitationEmail(
-- 
GitLab