diff --git a/src/user-groups/__tests__/userGroupsReducer-test.js b/src/user-groups/__tests__/userGroupsReducer-test.js index 37aebe9bf85..b909838b50e 100644 --- a/src/user-groups/__tests__/userGroupsReducer-test.js +++ b/src/user-groups/__tests__/userGroupsReducer-test.js @@ -4,12 +4,14 @@ import deepFreeze from 'deep-freeze'; import * as eg from '../../__tests__/lib/exampleData'; import { + EVENT, EVENT_USER_GROUP_ADD, EVENT_USER_GROUP_REMOVE, EVENT_USER_GROUP_UPDATE, EVENT_USER_GROUP_ADD_MEMBERS, EVENT_USER_GROUP_REMOVE_MEMBERS, } from '../../actionConstants'; +import { EventTypes } from '../../api/eventTypes'; import userGroupsReducer from '../userGroupsReducer'; describe('userGroupsReducer', () => { @@ -192,4 +194,35 @@ describe('userGroupsReducer', () => { ).toEqual([{ ...group1, members: [user1.user_id, user4.user_id] }, group2]); }); }); + + describe('realm_user op: update', () => { + test('a user is deactivated', () => { + const user1 = eg.makeUser(); + const user2 = eg.makeUser(); + const user3 = eg.makeUser(); + + const group1 = eg.makeUserGroup({ members: [user1.user_id, user2.user_id, user3.user_id] }); + const group2 = eg.makeUserGroup({ members: [user2.user_id, user1.user_id] }); + const group3 = eg.makeUserGroup({ members: [user1.user_id] }); + + const person = { userToDeactivate: user1 }; + const action = deepFreeze({ + type: EVENT, + event: { id: 1, type: EventTypes.realm_user, op: 'update', person }, + }); + + const prevState = deepFreeze([group1, group2, group3]); + + const actualState = userGroupsReducer(prevState, action); + + expect(actualState).toEqual([ + { ...group1, members: [user2.user_id, user3.user_id] }, + { ...group2, members: [user2.user_id] }, + + // A newly-empty group is not pruned; when a group is deactivated, + // we expect a user_group/remove event. + { ...group3, members: [] }, + ]); + }); + }); }); diff --git a/src/user-groups/userGroupsReducer.js b/src/user-groups/userGroupsReducer.js index 7ee86da4c7f..41b450de20f 100644 --- a/src/user-groups/userGroupsReducer.js +++ b/src/user-groups/userGroupsReducer.js @@ -2,6 +2,7 @@ import type { UserGroupsState, PerAccountApplicableAction } from '../types'; import { REGISTER_COMPLETE, + EVENT, EVENT_USER_GROUP_ADD, EVENT_USER_GROUP_REMOVE, EVENT_USER_GROUP_UPDATE, @@ -9,6 +10,7 @@ import { EVENT_USER_GROUP_REMOVE_MEMBERS, RESET_ACCOUNT_DATA, } from '../actionConstants'; +import { EventTypes } from '../api/eventTypes'; import { NULL_ARRAY } from '../nullObjects'; const initialState: UserGroupsState = NULL_ARRAY; @@ -69,6 +71,31 @@ export default ( case EVENT_USER_GROUP_REMOVE_MEMBERS: return eventUserGroupRemoveMembers(state, action); + case EVENT: { + const { event } = action; + switch (event.type) { + case EventTypes.realm_user: { + switch (event.op) { + case 'update': { + const { person } = event; + if (person.userToDeactivate != null) { + const userId = person.userToDeactivate.user_id; + return state.map(g => ({ ...g, members: g.members.filter(m => m !== userId) })); + } + + return state; + } + + default: + return state; + } + } + + default: + return state; + } + } + default: return state; }