Skip to content

Commit

Permalink
chore: account mutation and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
yosvelquintero committed Sep 13, 2023
1 parent 361129b commit 1ba10a5
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 9 deletions.
57 changes: 53 additions & 4 deletions packages/core/state/src/lib/+state/account/account.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ export class AccountEffects {
switchMap(({ payload }) =>
this.accountService.userFindByToken(payload).pipe(
map((result) => {
const data = result?.data?.identity?.user?.FindByToken?.details;
const identity = result?.data?.identity;
const status = identity?.user?.FindByToken?.details?.status;
const data = identity?.user?.FindByToken?.details?.payload;

if (data?.status?.code !== 200 || !data?.payload) {
if (status?.code !== 200 || !data) {
this.authnFacade.signOut();
}

return accountActions.userFindByTokenSuccess({
payload: data?.payload as IIoRestorecommerceUserUser,
payload: data as IIoRestorecommerceUserUser,
});
}),
catchError((error: Error) =>
Expand All @@ -45,8 +47,14 @@ export class AccountEffects {
this.accountService.userMutate(payload).pipe(
map((result) => {
const identity = result?.data?.identity;
const operationStatus =
identity?.user?.Mutate?.details?.operationStatus;
const users = identity?.user?.Mutate?.details?.items;

if (operationStatus?.code !== 200) {
throw new Error(operationStatus?.message || 'unknown error');
}

if (!users?.length) {
throw new Error('user not found');
}
Expand All @@ -63,12 +71,53 @@ export class AccountEffects {
);
});

userDeleteRequest$ = createEffect(() => {
return this.actions$.pipe(
ofType(accountActions.userDeleteRequest),
switchMap(({ payload }) =>
this.accountService.userDelete(payload).pipe(
map((result) => {
const identity = result?.data?.identity;
const operationStatus =
identity?.user?.Delete?.details?.operationStatus;

if (operationStatus?.code !== 200) {
throw new Error(operationStatus?.message || 'unknown error');
}

return accountActions.userDeleteSuccess();
}),
catchError((error: Error) =>
of(accountActions.userDeleteFail({ error: error.message }))
)
)
)
);
});

userDeleteSuccess$ = createEffect(
() => {
return this.actions$.pipe(
ofType(accountActions.userDeleteSuccess),
tap(() => {
this.appFacade.addNotification({
content: 'account deleted',
type: ENotificationTypes.SUCCESS,
});
this.authnFacade.signOut();
})
);
},
{ dispatch: false }
);

handleNotificationErrors$ = createEffect(
() => {
return this.actions$.pipe(
ofType(
accountActions.userFindByTokenFail,
accountActions.userMutateFail
accountActions.userMutateFail,
accountActions.userDeleteFail
),
tap(({ error }) => {
this.appFacade.addNotification({
Expand Down
12 changes: 11 additions & 1 deletion packages/core/state/src/lib/+state/account/account.facade.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';

import { IIoRestorecommerceUserFindByTokenRequest } from '@console-core/graphql';
import {
IIoRestorecommerceResourcebaseDeleteRequest,
IIoRestorecommerceUserFindByTokenRequest,
IIoRestorecommerceUserUserList,
} from '@console-core/graphql';

import * as accountActions from './account.actions';
import * as accountSelectors from './account.selectors';
Expand All @@ -11,13 +15,19 @@ export class AccountFacade {
// Selectors
profile$ = this.store.select(accountSelectors.selectProfile);
isLoading$ = this.store.select(accountSelectors.selectIsLoading);
isUpdating$ = this.store.select(accountSelectors.selectIsUpdating);
isDeleting$ = this.store.select(accountSelectors.selectIsDeleting);
actionStatus$ = this.store.select(accountSelectors.selectActionStatus);
error$ = this.store.select(accountSelectors.selectError);

// Actions
userFindByTokenRequest = (
payload: IIoRestorecommerceUserFindByTokenRequest
) => this.store.dispatch(accountActions.userFindByTokenRequest({ payload }));
userMutateRequest = (payload: IIoRestorecommerceUserUserList) =>
this.store.dispatch(accountActions.userMutateRequest({ payload }));
userDeleteRequest = (payload: IIoRestorecommerceResourcebaseDeleteRequest) =>
this.store.dispatch(accountActions.userDeleteRequest({ payload }));

constructor(private readonly store: Store) {}
}
8 changes: 4 additions & 4 deletions packages/core/state/src/lib/+state/account/account.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const reducer = createReducer<IAccountState>(
accountActions.userMutateRequest,
(state): IAccountState => ({
...state,
actionStatus: EActionStatus.REQUESTING,
actionStatus: EActionStatus.UPDATING,
error: null,
})
),
Expand All @@ -52,7 +52,7 @@ const reducer = createReducer<IAccountState>(
(state, { payload }): IAccountState => ({
...state,
user: getUser(payload),
actionStatus: EActionStatus.SUCCEEDED,
actionStatus: EActionStatus.UPDATED,
error: null,
})
),
Expand All @@ -68,15 +68,15 @@ const reducer = createReducer<IAccountState>(
accountActions.userDeleteRequest,
(state): IAccountState => ({
...state,
actionStatus: EActionStatus.REQUESTING,
actionStatus: EActionStatus.DELETING,
error: null,
})
),
on(
accountActions.userDeleteSuccess,
(state): IAccountState => ({
...state,
actionStatus: EActionStatus.SUCCEEDED,
actionStatus: EActionStatus.DELETED,
error: null,
})
),
Expand Down
10 changes: 10 additions & 0 deletions packages/core/state/src/lib/+state/account/account.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ export const selectIsLoading = createSelector(
(state: IAccountState) => state.actionStatus === EActionStatus.REQUESTING
);

export const selectIsUpdating = createSelector(
selectAccount,
(state: IAccountState) => state.actionStatus === EActionStatus.UPDATING
);

export const selectIsDeleting = createSelector(
selectAccount,
(state: IAccountState) => state.actionStatus === EActionStatus.DELETING
);

export const selectActionStatus = createSelector(
selectAccount,
(state: IAccountState) => state.actionStatus
Expand Down

0 comments on commit 1ba10a5

Please sign in to comment.