Skip to content

Commit

Permalink
Ensure consistency while adding/removing billing_team_members
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaymankar committed May 4, 2020
1 parent f4d4f74 commit d37b290
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 39 deletions.
25 changes: 0 additions & 25 deletions services/galley/src/Galley/API/Teams.hs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ createBindingTeam zusr tid (BindingNewTeam body) = do
let owner = newTeamMember zusr fullPermissions Nothing
team <- Data.createTeam (Just tid) zusr (body ^. newTeamName) (body ^. newTeamIcon) (body ^. newTeamIconKey) Binding
finishCreateTeam team owner [] Nothing
Data.addBillingTeamMember tid (owner ^. userId)
pure tid

updateTeamStatusH :: TeamId ::: JsonRequest TeamStatusUpdate ::: JSON -> Galley Response
Expand Down Expand Up @@ -454,12 +453,6 @@ uncheckedAddTeamMember tid nmem = do
-- FUTUREWORK: We cannot enable legalhold on large teams right now
ensureNotTooLargeForLegalHold tid mems
(TeamSize sizeBeforeAdd) <- addTeamMemberInternal tid Nothing Nothing nmem mems
let new = nmem ^. ntmNewTeamMember

-- This has to only be done for binding teams, it is assumed here as this function is
-- supposed to be called only for binding teams
when (hasPermission new SetBilling) $
Data.addBillingTeamMember tid (new ^. userId)

Journal.teamUpdate tid (sizeBeforeAdd + 1) $ Just $ newTeamMemberList ((nmem ^. ntmNewTeamMember) : mems ^. teamMembers) (mems ^. teamMemberListType)

Expand Down Expand Up @@ -500,28 +493,10 @@ updateTeamMember zusr zcon tid targetMember = do
-- update target in Cassandra
Data.updateTeamMember tid targetId targetPermissions

-- Manage billing member data
when (team ^. teamBinding == Binding) $ do
-- add new billing member, if applicable
let isNewBillingMember =
not (isBillingMember previousMember)
&& isBillingMember targetMember
when isNewBillingMember $
Data.addBillingTeamMember tid targetId

-- remove old billing member, if applicable
let isNotBillingMemberAnymore =
isBillingMember previousMember
&& not (isBillingMember targetMember)
when isNotBillingMemberAnymore $
Data.deleteBillingTeamMember tid targetId

updatedMembers <- Data.teamMembersForFanout tid
updateJournal team updatedMembers
updatePeers targetId targetPermissions updatedMembers
where
isBillingMember :: TeamMember -> Bool
isBillingMember = flip hasPermission SetBilling
--
canDowngradeOwner = canDeleteMember
--
Expand Down
27 changes: 13 additions & 14 deletions services/galley/src/Galley/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ module Galley.Data
updateTeamMember,
createTeam,
removeTeamMember,
addBillingTeamMember,
listBillingTeamMembers,
deleteBillingTeamMember,
team,
Galley.Data.teamName,
teamConversation,
Expand Down Expand Up @@ -122,7 +120,7 @@ import Data.List1 (List1, list1, singleton)
import qualified Data.Map.Strict as Map
import Data.Misc (Milliseconds)
import Data.Range
import qualified Data.Set
import qualified Data.Set as Set
import Data.Time.Clock
import qualified Data.UUID.Tagged as U
import Data.UUID.V4 (nextRandom)
Expand Down Expand Up @@ -372,9 +370,18 @@ addTeamMember t m =
m ^? invitation . _Just . _2
)
addPrepQuery Cql.insertUserTeam (m ^. userId, t)
when (m `hasPermission` SetBilling) $
addPrepQuery Cql.insertBillingTeamMember (t, m ^. userId)

updateTeamMember :: MonadClient m => TeamId -> UserId -> Permissions -> m ()
updateTeamMember t u p = retry x5 $ write Cql.updatePermissions (params Quorum (p, t, u))
updateTeamMember t u p = do

This comment has been minimized.

Copy link
@tiago-loureiro

tiago-loureiro May 4, 2020

Contributor

When we call updateTeamMember we always have the previous permissions, right? Can we check that instead of always deleting?

retry x5 $ batch $ do
setType BatchLogged
setConsistency Quorum
addPrepQuery Cql.updatePermissions (p, t, u)
if (SetBilling `Set.member` (view self p))
then addPrepQuery Cql.insertBillingTeamMember (t, u)
else addPrepQuery Cql.deleteBillingTeamMember (t, u)

removeTeamMember :: MonadClient m => TeamId -> UserId -> m ()
removeTeamMember t m =
Expand All @@ -385,18 +392,10 @@ removeTeamMember t m =
addPrepQuery Cql.deleteUserTeam (m, t)
addPrepQuery Cql.deleteBillingTeamMember (t, m)

addBillingTeamMember :: MonadClient m => TeamId -> UserId -> m ()
addBillingTeamMember tid uid =
retry x5 $ write Cql.insertBillingTeamMember (params Quorum (tid, uid))

listBillingTeamMembers :: MonadClient m => TeamId -> m [UserId]
listBillingTeamMembers tid =
fmap runIdentity
<$> (retry x1 $ query Cql.listBillingTeamMembers (params Quorum (Identity tid)))

deleteBillingTeamMember :: MonadClient m => TeamId -> UserId -> m ()
deleteBillingTeamMember tid uid =
retry x5 $ write Cql.deleteBillingTeamMember (params Quorum (tid, uid))
<$> retry x1 (query Cql.listBillingTeamMembers (params Quorum (Identity tid)))

removeTeamConv :: MonadClient m => TeamId -> ConvId -> m ()
removeTeamConv tid cid = do
Expand Down Expand Up @@ -586,7 +585,7 @@ createOne2OneConversation a b name ti = do
updateConversation :: MonadClient m => ConvId -> Range 1 256 Text -> m ()
updateConversation cid name = retry x5 $ write Cql.updateConvName (params Quorum (fromRange name, cid))

updateConversationAccess :: MonadClient m => ConvId -> Data.Set.Set Access -> AccessRole -> m ()
updateConversationAccess :: MonadClient m => ConvId -> Set.Set Access -> AccessRole -> m ()
updateConversationAccess cid acc role = retry x5 $ write Cql.updateConvAccess (params Quorum (Set (toList acc), role, cid))

updateConversationReceiptMode :: MonadClient m => ConvId -> ReceiptMode -> m ()
Expand Down

0 comments on commit d37b290

Please sign in to comment.