-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added user administration (#28)
Closes: #3
- Loading branch information
Showing
26 changed files
with
1,160 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { inject } from '@angular/core'; | ||
import { on } from '@ngrx/store'; | ||
import { switchMap } from 'rxjs'; | ||
|
||
import { ApiCreateUserResponse } from '../../../api/models'; | ||
import { UserAdministrationService } from '../../../api/services'; | ||
import { User } from '../../../models/parsed-models'; | ||
import { assertBody } from '../../../utils/http.utils'; | ||
import { createHttpAction, handleHttpAction, onHttpAction, toHttpAction } from '../../action-state'; | ||
import { createFunctionalEffect } from '../../functional-effect'; | ||
import { Effects, Reducers } from '../../utils'; | ||
import { USERS_ACTION_SCOPE } from '../consts'; | ||
import { selectUsersActionState } from '../users.selectors'; | ||
import { userEntityAdapter, UsersFeatureState } from '../users.state'; | ||
|
||
export const addUserAction = createHttpAction<Omit<User, 'id' | 'loginToken'>, User>()( | ||
USERS_ACTION_SCOPE, | ||
'Add User' | ||
); | ||
|
||
export const addUserReducers: Reducers<UsersFeatureState> = [ | ||
on(addUserAction.success, (state, { response }) => userEntityAdapter.upsertOne(response, state)), | ||
handleHttpAction('add', addUserAction), | ||
]; | ||
|
||
export const addUserEffects: Effects = { | ||
addUser$: createFunctionalEffect.dispatching((api = inject(UserAdministrationService)) => | ||
onHttpAction(addUserAction, selectUsersActionState('add')).pipe( | ||
switchMap(({ props }) => toHttpAction(createUser(api, props), addUserAction, props)) | ||
) | ||
), | ||
}; | ||
|
||
async function createUser( | ||
api: UserAdministrationService, | ||
props: ReturnType<typeof addUserAction>['props'] | ||
) { | ||
const response = await api.createUser({ | ||
body: { | ||
alias: props.alias, | ||
roles: props.roles, | ||
playerPreferences: props.playerPreferences, | ||
}, | ||
}); | ||
return response.ok | ||
? addUserAction.success(props, toUser(assertBody(response))) | ||
: addUserAction.error(props, response); | ||
} | ||
|
||
function toUser(body: ApiCreateUserResponse): User { | ||
return { | ||
...body.user, | ||
loginToken: body.loginToken, | ||
}; | ||
} |
54 changes: 54 additions & 0 deletions
54
client/src/app/+state/users/actions/load-user-login-token.action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { inject } from '@angular/core'; | ||
import { on } from '@ngrx/store'; | ||
import { produce } from 'immer'; | ||
import { switchMap } from 'rxjs'; | ||
|
||
import { UserAdministrationService } from '../../../api/services'; | ||
import { assertBody } from '../../../utils/http.utils'; | ||
import { createHttpAction, handleHttpAction, onHttpAction, toHttpAction } from '../../action-state'; | ||
import { createFunctionalEffect } from '../../functional-effect'; | ||
import { Effects, Reducers } from '../../utils'; | ||
import { USERS_ACTION_SCOPE } from '../consts'; | ||
import { selectUsersActionState } from '../users.selectors'; | ||
import { userEntityAdapter, UsersFeatureState } from '../users.state'; | ||
|
||
export const loadUserLoginTokenAction = createHttpAction< | ||
{ userId: string }, | ||
{ loginToken: string } | ||
>()(USERS_ACTION_SCOPE, 'Load User Login Token'); | ||
|
||
export const loadUserLoginTokenReducers: Reducers<UsersFeatureState> = [ | ||
on(loadUserLoginTokenAction.success, (state, { props, response }) => | ||
userEntityAdapter.mapOne( | ||
{ | ||
id: props.userId, | ||
map: produce(draft => { | ||
draft.loginToken = response.loginToken; | ||
}), | ||
}, | ||
state | ||
) | ||
), | ||
handleHttpAction('loadLoginToken', loadUserLoginTokenAction), | ||
]; | ||
|
||
export const loadUserLoginTokenEffects: Effects = { | ||
loadUserLoginToken$: createFunctionalEffect.dispatching( | ||
(api = inject(UserAdministrationService)) => | ||
onHttpAction(loadUserLoginTokenAction, selectUsersActionState('loadLoginToken')).pipe( | ||
switchMap(({ props }) => | ||
toHttpAction(getUserLoginToken(api, props), loadUserLoginTokenAction, props) | ||
) | ||
) | ||
), | ||
}; | ||
|
||
async function getUserLoginToken( | ||
api: UserAdministrationService, | ||
props: ReturnType<typeof loadUserLoginTokenAction>['props'] | ||
) { | ||
const response = await api.getUserLoginToken({ userId: props.userId }); | ||
return response.ok | ||
? loadUserLoginTokenAction.success(props, assertBody(response)) | ||
: loadUserLoginTokenAction.error(props, response); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { inject } from '@angular/core'; | ||
import { on } from '@ngrx/store'; | ||
import { switchMap } from 'rxjs'; | ||
|
||
import { UserAdministrationService } from '../../../api/services'; | ||
import { createHttpAction, handleHttpAction, onHttpAction, toHttpAction } from '../../action-state'; | ||
import { createFunctionalEffect } from '../../functional-effect'; | ||
import { Effects, Reducers } from '../../utils'; | ||
import { USERS_ACTION_SCOPE } from '../consts'; | ||
import { selectUsersActionState } from '../users.selectors'; | ||
import { userEntityAdapter, UsersFeatureState } from '../users.state'; | ||
|
||
export const removeUserAction = createHttpAction<{ userId: string }>()( | ||
USERS_ACTION_SCOPE, | ||
'Remove User' | ||
); | ||
|
||
export const removeUserReducers: Reducers<UsersFeatureState> = [ | ||
on(removeUserAction.success, (state, { props }) => | ||
userEntityAdapter.removeOne(props.userId, state) | ||
), | ||
handleHttpAction('remove', removeUserAction), | ||
]; | ||
|
||
export const removeUserEffects: Effects = { | ||
removeUser$: createFunctionalEffect.dispatching((api = inject(UserAdministrationService)) => | ||
onHttpAction(removeUserAction, selectUsersActionState('remove')).pipe( | ||
switchMap(({ props }) => toHttpAction(deleteUser(api, props), removeUserAction, props)) | ||
) | ||
), | ||
}; | ||
|
||
async function deleteUser( | ||
api: UserAdministrationService, | ||
props: ReturnType<typeof removeUserAction>['props'] | ||
) { | ||
const response = await api.deleteUser({ userId: props.userId }); | ||
return response.ok | ||
? removeUserAction.success(props, undefined) | ||
: removeUserAction.error(props, response); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { inject } from '@angular/core'; | ||
import { concatLatestFrom } from '@ngrx/effects'; | ||
import { on, Store } from '@ngrx/store'; | ||
import { filter, switchMap } from 'rxjs'; | ||
|
||
import { ApiUpdateUserRequest } from '../../../api/models'; | ||
import { UserAdministrationService } from '../../../api/services'; | ||
import { User } from '../../../models/parsed-models'; | ||
import { createHttpAction, handleHttpAction, onHttpAction, toHttpAction } from '../../action-state'; | ||
import { createFunctionalEffect } from '../../functional-effect'; | ||
import { Reducers } from '../../utils'; | ||
import { USERS_ACTION_SCOPE } from '../consts'; | ||
import { selectUser, selectUsersActionState } from '../users.selectors'; | ||
import { userEntityAdapter, UsersFeatureState } from '../users.state'; | ||
|
||
export const updateUserAction = createHttpAction<User>()(USERS_ACTION_SCOPE, 'Update User'); | ||
|
||
export const updateUserReducers: Reducers<UsersFeatureState> = [ | ||
on(updateUserAction.success, (state, { props }) => userEntityAdapter.upsertOne(props, state)), | ||
handleHttpAction('update', updateUserAction), | ||
]; | ||
|
||
export const updateUserEffects = { | ||
updateUser$: createFunctionalEffect.dispatching( | ||
(store = inject(Store), api = inject(UserAdministrationService)) => | ||
onHttpAction(updateUserAction, selectUsersActionState('update')).pipe( | ||
concatLatestFrom(({ props }) => store.select(selectUser(props.id))), | ||
filter(([, oldUser]) => !!oldUser), | ||
switchMap(([{ props }, oldUser]) => | ||
toHttpAction(updateUser(api, props, oldUser!), updateUserAction, props) | ||
) | ||
) | ||
), | ||
}; | ||
|
||
async function updateUser( | ||
api: UserAdministrationService, | ||
props: ReturnType<typeof updateUserAction>['props'], | ||
oldUser: User | ||
) { | ||
const request: ApiUpdateUserRequest = { | ||
alias: props.alias !== oldUser.alias ? props.alias : undefined, | ||
addRoles: props.roles.filter(role => !oldUser.roles.includes(role)), | ||
removeRoles: oldUser.roles.filter(role => !props.roles.includes(role)), | ||
playerPreferences: { | ||
addAvoid: props.playerPreferences.avoid.filter( | ||
avoid => !oldUser.playerPreferences.avoid.includes(avoid) | ||
), | ||
removeAvoid: oldUser.playerPreferences.avoid.filter( | ||
avoid => !props.playerPreferences.avoid.includes(avoid) | ||
), | ||
addPrefer: props.playerPreferences.prefer.filter( | ||
prefer => !oldUser.playerPreferences.prefer.includes(prefer) | ||
), | ||
removePrefer: oldUser.playerPreferences.prefer.filter( | ||
prefer => !props.playerPreferences.prefer.includes(prefer) | ||
), | ||
}, | ||
}; | ||
|
||
const response = await api.updateUser({ | ||
userId: oldUser.id, | ||
body: request, | ||
}); | ||
return response.ok | ||
? updateUserAction.success(props, undefined) | ||
: updateUserAction.error(props, response); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
export { addUserAction } from './actions/add-user.action'; | ||
export { loadUserLoginTokenAction } from './actions/load-user-login-token.action'; | ||
export { loadUsersAction } from './actions/load-users.action'; | ||
export { removeUserAction } from './actions/remove-user.action'; | ||
export { updateUserAction } from './actions/update-user.action'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
import { addUserEffects } from './actions/add-user.action'; | ||
import { loadUserLoginTokenEffects } from './actions/load-user-login-token.action'; | ||
import { loadUsersEffects } from './actions/load-users.action'; | ||
import { removeUserEffects } from './actions/remove-user.action'; | ||
import { updateUserEffects } from './actions/update-user.action'; | ||
import { Effects } from '../utils'; | ||
|
||
export const usersFeatureEffects: Effects[] = [loadUsersEffects]; | ||
export const usersFeatureEffects: Effects[] = [ | ||
addUserEffects, | ||
loadUserLoginTokenEffects, | ||
loadUsersEffects, | ||
removeUserEffects, | ||
updateUserEffects, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
import { createReducer } from '@ngrx/store'; | ||
|
||
import { addUserReducers } from './actions/add-user.action'; | ||
import { loadUserLoginTokenReducers } from './actions/load-user-login-token.action'; | ||
import { loadUsersReducers } from './actions/load-users.action'; | ||
import { removeUserReducers } from './actions/remove-user.action'; | ||
import { updateUserReducers } from './actions/update-user.action'; | ||
import { UsersFeatureState, initialUsersFeatureState } from './users.state'; | ||
|
||
export const usersReducer = createReducer<UsersFeatureState>( | ||
initialUsersFeatureState, | ||
|
||
...loadUsersReducers | ||
...addUserReducers, | ||
...loadUserLoginTokenReducers, | ||
...loadUsersReducers, | ||
...removeUserReducers, | ||
...updateUserReducers | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.