All files / projects/organization-management/src/app/store/users users.reducer.ts

63.63% Statements 14/22
100% Branches 0/0
50% Functions 5/10
63.63% Lines 14/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 12049x 49x     49x         49x                                             49x 76x                 49x           49x                                         11x   11x     34x       4x   4x                                                     1x                        
import { EntityState, createEntityAdapter } from '@ngrx/entity';
import { createReducer, on } from '@ngrx/store';
 
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { setErrorOn, setLoadingOn, unsetLoadingAndErrorOn } from 'ish-core/utils/ngrx-creators';
 
import { B2bRole } from '../../models/b2b-role/b2b-role.model';
import { B2bUser } from '../../models/b2b-user/b2b-user.model';
 
import {
  addUser,
  addUserFail,
  addUserSuccess,
  deleteUser,
  deleteUserFail,
  deleteUserSuccess,
  loadSystemUserRolesSuccess,
  loadUserFail,
  loadUserSuccess,
  loadUsers,
  loadUsersFail,
  loadUsersSuccess,
  setUserBudget,
  setUserBudgetFail,
  setUserBudgetSuccess,
  setUserRolesFail,
  setUserRolesSuccess,
  updateUser,
  updateUserFail,
  updateUserSuccess,
} from './users.actions';
 
export const usersAdapter = createEntityAdapter<B2bUser>({
  selectId: user => user.login,
});
 
export interface UsersState extends EntityState<B2bUser> {
  loading: boolean;
  error: HttpError;
  roles: B2bRole[];
}
 
const initialState: UsersState = usersAdapter.getInitialState({
  loading: false,
  error: undefined,
  roles: [],
});
 
export const usersReducer = createReducer(
  initialState,
  setLoadingOn(loadUsers, addUser, updateUser, deleteUser, setUserBudget),
  unsetLoadingAndErrorOn(
    loadUsersSuccess,
    loadUserSuccess,
    addUserSuccess,
    updateUserSuccess,
    deleteUserSuccess,
    setUserBudgetSuccess
  ),
  setErrorOn(
    loadUsersFail,
    loadUserFail,
    addUserFail,
    updateUserFail,
    deleteUserFail,
    setUserRolesFail,
    setUserBudgetFail
  ),
  on(loadUsersSuccess, (state, action) => {
    const { users } = action.payload;
 
    return {
      ...usersAdapter.upsertMany(users, state),
      // preserve order from API
      ids: users.map(u => u.login),
    };
  }),
  on(loadUserSuccess, (state, action) => {
    const { user } = action.payload;
 
    return {
      ...usersAdapter.upsertOne(user, state),
    };
  }),
  on(addUserSuccess, (state, action) => {
    const { user } = action.payload;
 
    return {
      ...usersAdapter.addOne(user, state),
    };
  }),
  on(updateUserSuccess, (state, action) => {
    const { user } = action.payload;
 
    return {
      ...usersAdapter.upsertOne(user, state),
    };
  }),
  on(deleteUserSuccess, (state, action) => {
    const { login } = action.payload;
 
    return {
      ...usersAdapter.removeOne(login, state),
    };
  }),
  on(
    loadSystemUserRolesSuccess,
    (state, action): UsersState => ({
      ...state,
      roles: action.payload.roles,
    })
  ),
  on(setUserRolesSuccess, (state, action) =>
    usersAdapter.updateOne({ id: action.payload.login, changes: { roleIDs: action.payload.roles } }, state)
  ),
  on(setUserBudgetSuccess, (state, action) => ({
    ...usersAdapter.updateOne({ id: action.payload.login, changes: { userBudget: action.payload.budget } }, state),
  }))
);