Merge branch 'develop' into feature/splitscreen-pickup-shelf-design

This commit is contained in:
Nino
2023-09-22 10:49:31 +02:00
4 changed files with 129 additions and 10 deletions

View File

@@ -1,18 +1,27 @@
import { ComponentStore, tapResponse } from '@ngrx/component-store';
import { PickupShelfDetailsState } from './pickup-shelf-details.state';
import { Observable } from 'rxjs';
import { ListResponseArgsOfDBHOrderItemListItemDTO, OrderItemProcessingStatusValue, ResponseArgsOfOrderDTO } from '@swagger/oms';
import {
DBHOrderItemListItemDTO,
ListResponseArgsOfDBHOrderItemListItemDTO,
OrderDTO,
OrderItemProcessingStatusValue,
ResponseArgsOfOrderDTO,
} from '@swagger/oms';
import { PickupShelfIOService, PickupShelfService } from '@domain/pickup-shelf';
import { Injectable, inject } from '@angular/core';
import { filter, switchMap, tap } from 'rxjs/operators';
import { UiModalService } from '@ui/modal';
import { CrmCustomerService } from '@domain/crm';
import * as Selectors from './pickup-shelf-details.selectors';
import { ListResponseArgsOfCustomerInfoDTO } from '@swagger/crm';
import { CustomerInfoDTO, ListResponseArgsOfCustomerInfoDTO } from '@swagger/crm';
import { uniq } from 'lodash';
import { CacheService } from '@core/cache';
@Injectable()
export class PickupShelfDetailsStore extends ComponentStore<PickupShelfDetailsState> {
private _cacheService = inject(CacheService);
private _pickupShelfService = inject(PickupShelfService);
private _pickupShelfIOService = inject(PickupShelfIOService);
@@ -94,19 +103,32 @@ export class PickupShelfDetailsStore extends ComponentStore<PickupShelfDetailsSt
fetchOrder = this.effect((trigger$: Observable<{ orderId: number }>) =>
trigger$.pipe(
filter(({ orderId }) => this.order?.id !== orderId),
tap(this.beforeFetchOrder),
tap(({ orderId }) => this.beforeFetchOrder(orderId)),
switchMap(({ orderId }) =>
this._pickupShelfService.getOrderByOrderId(orderId).pipe(tapResponse(this.fetchOrderSuccess, this.fetchOrderFailed))
)
)
);
private beforeFetchOrder = () => {
this.patchState({ fetchingOrder: true, order: undefined, customer: undefined });
private beforeFetchOrder = (orderId) => {
const order = this._cacheService.get<OrderDTO>({ name: 'order', orderId, store: 'PickupShelfDetailsStore' });
const customer = this._cacheService.get<CustomerInfoDTO>({
name: 'customer',
orderId,
store: 'PickupShelfDetailsStore',
});
this.patchState({ fetchingOrder: true, order, customer });
};
private fetchOrderSuccess = (res: ResponseArgsOfOrderDTO) => {
this.patchState({ fetchingOrder: false, order: res.result });
this._cacheService.set<OrderDTO>({ name: 'order', orderId: res.result.id, store: 'PickupShelfDetailsStore' }, res.result, {
persist: true,
ttl: 3600000,
});
this.fetchCustomer({ buyerNumber: res.result.buyer.buyerNumber });
};
@@ -121,16 +143,26 @@ export class PickupShelfDetailsStore extends ComponentStore<PickupShelfDetailsSt
switchMap(({ compartmentCode, orderNumber }) =>
this._pickupShelfIOService
.getOrderItemsByOrderNumberOrCompartmentCode({ compartmentCode, orderNumber })
.pipe(tapResponse(this.fetchOrderItemsSuccess, this.fetchOrderItemsFailed))
.pipe(tapResponse((r) => this.fetchOrderItemsSuccess({ compartmentCode })(r), this.fetchOrderItemsFailed))
)
)
);
private beforeFetchOrderItems = ({ compartmentCode }: { orderNumber?: string; compartmentCode?: string }) => {
this.patchState({ fetchingOrderItems: true, orderItems: [], selectedCompartmentCode: compartmentCode });
const orderItems =
this._cacheService.get<DBHOrderItemListItemDTO[]>({
name: 'orderItems',
orderId: this.order.id,
compartmentCode,
store: 'PickupShelfDetailsStore',
}) ?? [];
this.patchState({ fetchingOrderItems: true, orderItems, selectedCompartmentCode: compartmentCode });
};
private fetchOrderItemsSuccess = (res: ListResponseArgsOfDBHOrderItemListItemDTO) => {
private fetchOrderItemsSuccess = ({ compartmentCode }: { compartmentCode?: string }) => (
res: ListResponseArgsOfDBHOrderItemListItemDTO
) => {
// check if order items belong to the same customer
const buyerNumbers = uniq(res.result.map((i) => i.buyerNumber));
if (buyerNumbers.length > 1) {
@@ -138,6 +170,16 @@ export class PickupShelfDetailsStore extends ComponentStore<PickupShelfDetailsSt
return;
}
this._cacheService.set<DBHOrderItemListItemDTO[]>(
{
name: 'orderItems',
orderId: this.order.id,
compartmentCode,
store: 'PickupShelfDetailsStore',
},
res.result
);
this.patchState({ fetchingOrderItems: false, orderItems: res.result });
};
@@ -157,7 +199,12 @@ export class PickupShelfDetailsStore extends ComponentStore<PickupShelfDetailsSt
);
beforeFetchCustomer = () => {
this.patchState({ fetchingCustomer: true, customer: undefined });
const customer = this._cacheService.get<CustomerInfoDTO>({
name: 'customer',
orderId: this.order.id,
store: 'PickupShelfDetailsStore',
});
this.patchState({ fetchingCustomer: true, customer });
};
fetchCustomerSuccess = (res: ListResponseArgsOfCustomerInfoDTO) => {
@@ -175,6 +222,16 @@ export class PickupShelfDetailsStore extends ComponentStore<PickupShelfDetailsSt
return;
}
this._cacheService.set<CustomerInfoDTO>(
{
name: 'customer',
orderId: this.order.id,
store: 'PickupShelfDetailsStore',
},
customer,
{ persist: true, ttl: 3600000 }
);
this.patchState({ fetchingCustomer: false, customer });
};

View File

@@ -63,3 +63,7 @@ export function selectList(state: PickupShelfState) {
export function selectListHits(state: PickupShelfState) {
return state.listHits ?? 0;
}
export function selectSelectedListItems(state: PickupShelfState) {
return state.selectedListItems ?? [];
}

View File

@@ -11,4 +11,6 @@ export interface PickupShelfState {
fetchingList?: boolean;
list?: DBHOrderItemListItemDTO[];
listHits?: number;
selectedListItems?: DBHOrderItemListItemDTO[];
}

View File

@@ -5,6 +5,7 @@ import { delay, delayWhen, filter, map, switchMap, takeUntil, tap, withLatestFro
import {
DBHOrderItemListItemDTO,
ListResponseArgsOfDBHOrderItemListItemDTO,
QuerySettingsDTO,
QueryTokenDTO,
ResponseArgsOfQuerySettingsDTO,
} from '@swagger/oms';
@@ -77,6 +78,18 @@ export class PickupShelfStore extends ComponentStore<PickupShelfState> implement
map(([fetching, hits]) => (hits === 0 && !fetching ? 'Keine Suchergebnisse' : ''))
);
readonly fetchingList$ = this.select(Selectors.selectFetchingList);
get fetchingList() {
return this.get(Selectors.selectFetchingList);
}
readonly selectedListItems$ = this.select(Selectors.selectSelectedListItems);
get selectedListItems() {
return this.get(Selectors.selectSelectedListItems);
}
constructor() {
// Nicht entfernen sonst wird der Store nicht initialisiert
super({});
@@ -107,21 +120,64 @@ export class PickupShelfStore extends ComponentStore<PickupShelfState> implement
this.patchState({ fetchingList: false });
}
addSelectedListItem = this.updater((state, listItem: DBHOrderItemListItemDTO) => ({
selectedListItems: [...(state.selectedListItems ?? []), listItem],
}));
addSelectedListItems = this.updater((state, listItems: DBHOrderItemListItemDTO[]) => ({
selectedListItems: [...(state.selectedListItems ?? []), ...listItems],
}));
removeSelectedListItem = this.updater((state, listItem: DBHOrderItemListItemDTO) => ({
selectedListItems: state.selectedListItems?.filter((li) => li.orderItemId !== listItem.orderItemId) ?? [],
}));
removeSelectedListItems = this.updater((state, listItems: DBHOrderItemListItemDTO[]) => ({
selectedListItems: state.selectedListItems?.filter((li) => !listItems.some((l) => l.orderItemId === li.orderItemId)) ?? [],
}));
resetSelectedListItems = this.updater((state) => ({ selectedListItems: [] }));
ngrxOnStoreInit = () => {
this.fetchQuerySettings();
};
fetchQuerySettings = this.effect((trigger$) =>
trigger$.pipe(
tap(() => this.patchState({ fetchingQuerySettings: true })),
map(() => this.beforeFetchQuerySettings()),
filter((shouldFetch) => shouldFetch),
switchMap(() =>
this._pickupShelfIOService.getQuerySettings().pipe(tapResponse(this.fetchQuerySettingsDone, this.fetchQuerySettingsError))
)
)
);
/**
* Before fetching query settings
* @returns true if query settings should be fetched
*/
private beforeFetchQuerySettings = () => {
const cachedQuerySettings = this._cacheService.get<QuerySettingsDTO>({
name: 'pickup-shelf',
providerName: this._pickupShelfIOService.constructor.name,
});
if (!!cachedQuerySettings) {
this.patchState({ fetchingQuerySettings: false, querySettings: cachedQuerySettings });
return false;
} else {
this.patchState({ fetchingQuerySettings: true });
return true;
}
};
private fetchQuerySettingsDone = (resp: ResponseArgsOfQuerySettingsDTO) => {
this.patchState({ fetchingQuerySettings: false, querySettings: resp.result });
this._cacheService.set<QuerySettingsDTO>(
{ name: 'pickup-shelf', providerName: this._pickupShelfIOService.constructor.name },
resp.result
);
};
private fetchQuerySettingsError = (err: any) => {