Skip to content

Commit

Permalink
on the way to admin recurring orders listing - part six
Browse files Browse the repository at this point in the history
  • Loading branch information
shauke committed Sep 19, 2024
1 parent 658d561 commit 9ee5d14
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 42 deletions.
35 changes: 7 additions & 28 deletions src/app/core/services/recurring-orders/recurring-orders.service.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { combineLatest, filter, iif, map, switchMap, take, throwError } from 'rxjs';
import { combineLatest, filter, map, switchMap, take, throwError } from 'rxjs';

import { RecurringOrderData, RecurringOrderListData } from 'ish-core/models/recurring-order/recurring-order.interface';
import { RecurringOrderMapper } from 'ish-core/models/recurring-order/recurring-order.mapper';
import { ApiService, unpackEnvelope } from 'ish-core/services/api/api.service';
import { getLoggedInCustomer, getLoggedInUser } from 'ish-core/store/customer/user';
import { log } from 'ish-core/utils/dev/operators';
import { whenTruthy } from 'ish-core/utils/operators';

@Injectable({ providedIn: 'root' })
export class RecurringOrdersService {
constructor(private apiService: ApiService, private store: Store) {}

private recurringOrderHeaderV2 = new HttpHeaders({ Accept: 'application/vnd.intershop.recurringorder.v2+json' });

private currentCustomer$ = this.store.pipe(select(getLoggedInCustomer), whenTruthy(), take(1));

private currentCustomerAndUser$ = combineLatest([
this.store.pipe(select(getLoggedInCustomer)),
this.store.pipe(select(getLoggedInUser)),
Expand Down Expand Up @@ -53,7 +49,6 @@ export class RecurringOrdersService {

getRecurringOrders(context?: string) {
return this.getRecurringOrdersEndpoint(context).pipe(
log('API ENDPOINT'),
switchMap(apiEndpoint =>
this.apiService.get(apiEndpoint, { headers: this.recurringOrderHeaderV2 }).pipe(
unpackEnvelope<RecurringOrderListData>('data'),
Expand All @@ -65,7 +60,6 @@ export class RecurringOrdersService {

getRecurringOrder(recurringOrderId: string, context?: string) {
return this.getRecurringOrdersEndpoint(context).pipe(
log('API ENDPOINT'),
switchMap(apiEndpoint =>
this.apiService
.get<{ data: RecurringOrderData }>(`${apiEndpoint}/${this.apiService.encodeResourceId(recurringOrderId)}`, {
Expand All @@ -77,14 +71,11 @@ export class RecurringOrdersService {
}

updateRecurringOrder(recurringOrderId: string, active: boolean, context?: string) {
console.log('updateRecurringOrder', recurringOrderId, active, context);

if (!recurringOrderId) {
return throwError(() => new Error('updateRecurringOrder() called without recurringOrderId'));
}

return this.getRecurringOrdersEndpoint(context).pipe(
log('API ENDPOINT'),
switchMap(apiEndpoint =>
this.apiService
.patch<{ data: RecurringOrderData }>(
Expand All @@ -97,28 +88,16 @@ export class RecurringOrdersService {
);
}

deleteRecurringOrder(recurringOrderId: string) {
deleteRecurringOrder(recurringOrderId: string, context?: string) {
if (!recurringOrderId) {
return throwError(() => new Error('deleteRecurringOrder() called without recurringOrderId'));
}

return this.currentCustomer$.pipe(
switchMap(customer =>
iif(
() => customer.isBusinessCustomer,
this.apiService
.b2bUserEndpoint()
.delete(`recurringorders/${this.apiService.encodeResourceId(recurringOrderId)}`, {
headers: this.recurringOrderHeaderV2,
}),

this.apiService.delete(
`privatecustomers/${this.apiService.encodeResourceId(
customer.customerNo
)}/recurringorders/${this.apiService.encodeResourceId(recurringOrderId)}`,
{ headers: this.recurringOrderHeaderV2 }
)
)
return this.getRecurringOrdersEndpoint(context).pipe(
switchMap(apiEndpoint =>
this.apiService.delete(`${apiEndpoint}/${this.apiService.encodeResourceId(recurringOrderId)}`, {
headers: this.recurringOrderHeaderV2,
})
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,9 @@ export class RecurringOrdersEffects {
ofType(recurringOrdersActions.updateRecurringOrder),
mapToPayload(),
whenTruthy(),

concatLatestFrom(payload => this.store.pipe(select(getRecurringOrder(payload.recurringOrderId)))),
concatLatestFrom(() => this.store.pipe(select(selectQueryParam('context')))),

log('WARUM FEUERT updateRecurringOrder'),
log('THE SWITCH COMPONENT NEEDS FIXING'),
mergeMap(([[payload, recurringOrder], context]) => {
if (payload.active !== recurringOrder.active) {
return this.recurringOrdersService
Expand All @@ -83,11 +81,12 @@ export class RecurringOrdersEffects {
deleteRecurringOrder$ = createEffect(() =>
this.actions$.pipe(
ofType(recurringOrdersActions.deleteRecurringOrder),
mapToPayload(),
mergeMap(payload =>
this.recurringOrdersService.deleteRecurringOrder(payload.recurringOrderId).pipe(
mapToPayloadProperty('recurringOrderId'),
concatLatestFrom(() => this.store.pipe(select(selectQueryParam('context')))),
mergeMap(([recurringOrderId, context]) =>
this.recurringOrdersService.deleteRecurringOrder(recurringOrderId, context).pipe(
mergeMap(() => [
recurringOrdersApiActions.deleteRecurringOrderSuccess({ recurringOrderId: payload.recurringOrderId }),
recurringOrdersApiActions.deleteRecurringOrderSuccess({ recurringOrderId }),
displaySuccessMessage({
message: 'account.recurring_order.delete.confirmation',
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const recurringOrdersAdapter = createEntityAdapter<RecurringOrder>();
export interface RecurringOrdersState extends EntityState<RecurringOrder> {
loading: boolean;
error: HttpError;
contexts: { [id: string]: string[] };
contexts: { [key: string]: string[] };
}

export const initialState: RecurringOrdersState = recurringOrdersAdapter.getInitialState({
Expand All @@ -29,22 +29,31 @@ export const recurringOrdersReducer = createReducer(
recurringOrdersApiActions.loadRecurringOrdersSuccess,
recurringOrdersApiActions.deleteRecurringOrderSuccess
),
on(recurringOrdersApiActions.loadRecurringOrdersSuccess, (state, action) =>
recurringOrdersAdapter.upsertMany(action.payload.recurringOrders, {
on(recurringOrdersApiActions.loadRecurringOrdersSuccess, (state, action) => {
const { recurringOrders } = action.payload;
return recurringOrdersAdapter.upsertMany(recurringOrders, {
...state,
contexts: {
...state.contexts,
[action.payload.context || 'MY']: action.payload.recurringOrders.map(recurringOrder => recurringOrder.id),
[action.payload.context || 'MY']: recurringOrders.map(recurringOrder => recurringOrder.id),
},
})
),
});
}),
on(
recurringOrdersApiActions.loadRecurringOrderSuccess,
recurringOrdersApiActions.updateRecurringOrderSuccess,
(state, action) => recurringOrdersAdapter.upsertOne(action.payload.recurringOrder, state)
),
on(recurringOrdersApiActions.deleteRecurringOrderSuccess, (state, action) => {
const { recurringOrderId } = action.payload;
return recurringOrdersAdapter.removeOne(recurringOrderId, state);
return recurringOrdersAdapter.removeOne(recurringOrderId, {
...state,
contexts: {
...Object.entries(state.contexts).reduce(
(acc, [key, value]) => ({ ...acc, [key]: value.filter(id => id !== recurringOrderId) }),
{}
),
},
});
})
);

0 comments on commit 9ee5d14

Please sign in to comment.