mirror of
https://dev.azure.com/hugendubel/ISA/_git/ISA-Frontend
synced 2025-12-28 22:42:11 +01:00
#1631 Update Availabilities
This commit is contained in:
@@ -4,10 +4,11 @@ import { AvailabilityDTO, BranchDTO, OLAAvailabilityDTO, StoreCheckoutService, S
|
||||
import { Observable } from 'rxjs';
|
||||
import { AvailabilityService as SwaggerAvailabilityService } from '@swagger/availability';
|
||||
import { StockService } from '@swagger/cat';
|
||||
import { map, shareReplay, switchMap, withLatestFrom, mergeMap } from 'rxjs/operators';
|
||||
import { map, shareReplay, switchMap, withLatestFrom, mergeMap, tap } from 'rxjs/operators';
|
||||
import { isArray, isNullOrUndefined, memorize } from '@utils/common';
|
||||
import { OrderService } from '@swagger/oms';
|
||||
import { ItemData } from './defs/item-data.model';
|
||||
import { SsoService } from 'sso';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class AvailabilityService {
|
||||
@@ -15,7 +16,8 @@ export class AvailabilityService {
|
||||
private swaggerAvailabilityService: SwaggerAvailabilityService,
|
||||
private stockService: StockService,
|
||||
private storeCheckoutService: StoreCheckoutService,
|
||||
private orderService: OrderService
|
||||
private orderService: OrderService,
|
||||
private sso: SsoService
|
||||
) {}
|
||||
|
||||
@memorize()
|
||||
@@ -26,6 +28,22 @@ export class AvailabilityService {
|
||||
);
|
||||
}
|
||||
|
||||
@memorize()
|
||||
getBranches(): Observable<BranchDTO[]> {
|
||||
return this.storeCheckoutService.StoreCheckoutGetBranches({}).pipe(
|
||||
map((response) => response.result),
|
||||
shareReplay()
|
||||
);
|
||||
}
|
||||
|
||||
@memorize()
|
||||
getCurrentBranch(): Observable<BranchDTO> {
|
||||
return this.getBranches().pipe(
|
||||
map((branches) => branches.find((branch) => branch.branchNumber === this.sso.getClaimByKey('branch_no'))),
|
||||
shareReplay()
|
||||
);
|
||||
}
|
||||
|
||||
@memorize({ ttl: 10000 })
|
||||
getTakeAwayAvailability({
|
||||
item,
|
||||
@@ -176,31 +194,20 @@ export class AvailabilityService {
|
||||
}
|
||||
|
||||
@memorize({ ttl: 10000 })
|
||||
getB2bDeliveryAvailability({
|
||||
item,
|
||||
quantity,
|
||||
branch,
|
||||
}: {
|
||||
item: ItemData;
|
||||
quantity: number;
|
||||
branch: BranchDTO;
|
||||
}): Observable<AvailabilityDTO> {
|
||||
// Als funktion auslagern und hier aufrufen (getCurrentBranch im DomainCheckoutService anlegen)
|
||||
|
||||
// const branchNo = this.sso.getClaimByKey('branch_no');
|
||||
// const branchId = shoppingCartItem?.destination?.data?.targetBranch?.id;
|
||||
|
||||
// const branch = await this.domainCheckoutService
|
||||
// .getBranches()
|
||||
// .pipe(map((branches) => branches.find((branch) => (branchId ? branch.id === branchId : branch.branchNumber === branchNo))))
|
||||
// .toPromise();
|
||||
|
||||
getB2bDeliveryAvailability({ item, quantity }: { item: ItemData; quantity: number }): Observable<AvailabilityDTO> {
|
||||
const logistician$ = this.orderService
|
||||
.OrderGetLogisticians({})
|
||||
.pipe(map((response) => response.result.find((l) => l.logisticianNumber === '2470')));
|
||||
return this.getPickUpAvailability({ item, quantity, branch }).pipe(
|
||||
mergeMap((availability) => logistician$.pipe(map((logistician) => ({ ...availability, logistician: { id: logistician.id } })))),
|
||||
shareReplay()
|
||||
|
||||
const currentBranch$ = this.getCurrentBranch();
|
||||
|
||||
return currentBranch$.pipe(
|
||||
mergeMap((branch) =>
|
||||
this.getPickUpAvailability({ item, quantity, branch }).pipe(
|
||||
mergeMap((availability) => logistician$.pipe(map((logistician) => ({ ...availability, logistician: { id: logistician.id } })))),
|
||||
shareReplay()
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import { StringDictionary } from '@cmf/core';
|
||||
import { Store } from '@ngrx/store';
|
||||
import {
|
||||
AddToShoppingCartDTO,
|
||||
AvailabilityDTO,
|
||||
BranchDTO,
|
||||
BuyerDTO,
|
||||
BuyerResult,
|
||||
@@ -23,7 +24,7 @@ import {
|
||||
import { InputDTO } from '@swagger/crm';
|
||||
import { DisplayOrderDTO, OrderCheckoutService } from '@swagger/oms';
|
||||
import { isNullOrUndefined, memorize } from '@utils/common';
|
||||
import { combineLatest, Observable, of, concat, zip, BehaviorSubject } from 'rxjs';
|
||||
import { combineLatest, Observable, of, concat, zip, BehaviorSubject, isObservable } from 'rxjs';
|
||||
import { bufferCount, filter, first, map, mergeMap, shareReplay, switchMap, tap, withLatestFrom } from 'rxjs/operators';
|
||||
|
||||
import * as DomainCheckoutSelectors from './store/domain-checkout.selectors';
|
||||
@@ -334,84 +335,67 @@ export class DomainCheckoutService {
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: Lieferungen DIG und B2B Availabilities aktualisieren, anhand Logistician
|
||||
validateAvailabilities({ processId }: { processId: number }): Observable<any> {
|
||||
updateAvailabilities({ processId }: { processId: number }): Observable<any> {
|
||||
const shoppingCart$ = this.getShoppingCart({ processId }).pipe(first());
|
||||
// Lieferanten DIG ID === 1021 und B2B ID === 1022
|
||||
const itemsToUpdate$ = shoppingCart$.pipe(
|
||||
map((cart) =>
|
||||
cart.items.filter((item) => item.data.availability.logistician.id === 1021 || item.data.availability.logistician.id === 1022)
|
||||
cart?.items?.filter(
|
||||
(item) => item?.data?.features?.orderType === 'DIG-Versand' || item?.data?.features?.orderType === 'B2B-Versand'
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
const branches$ = itemsToUpdate$.pipe(
|
||||
mergeMap((items) => {
|
||||
let branches: Observable<BranchDTO>[];
|
||||
branches = items.map((item) => this.getCurrentBranch({ branchId: item?.data?.destination?.data?.targetBranch?.id }));
|
||||
return zip(branches).pipe(map((b) => b));
|
||||
return itemsToUpdate$.pipe(
|
||||
mergeMap((itemsToUpdate) => {
|
||||
if (!(itemsToUpdate?.length > 0)) {
|
||||
return of(undefined);
|
||||
}
|
||||
|
||||
return concat(
|
||||
...itemsToUpdate.map((itemToUpdate) => {
|
||||
const orderType = itemToUpdate?.data?.features?.orderType;
|
||||
let availability$: Observable<AvailabilityDTO>;
|
||||
if (orderType === 'DIG-Versand') {
|
||||
availability$ = this.availabilityService.getDigDeliveryAvailability({
|
||||
item: {
|
||||
ean: itemToUpdate.data.product.ean,
|
||||
itemId: Number(itemToUpdate.data.product.catalogProductNumber),
|
||||
price: itemToUpdate.data.availability.price,
|
||||
},
|
||||
quantity: itemToUpdate.data.quantity,
|
||||
});
|
||||
} else if (orderType === 'B2B-Versand') {
|
||||
availability$ = this.availabilityService.getB2bDeliveryAvailability({
|
||||
item: {
|
||||
ean: itemToUpdate.data.product.ean,
|
||||
itemId: Number(itemToUpdate.data.product.catalogProductNumber),
|
||||
price: itemToUpdate.data.availability.price,
|
||||
},
|
||||
quantity: itemToUpdate.data.quantity,
|
||||
});
|
||||
}
|
||||
|
||||
if (!isObservable(availability$)) {
|
||||
return of(undefined);
|
||||
}
|
||||
|
||||
return availability$.pipe(
|
||||
mergeMap((availability) => {
|
||||
return this.updateItemInShoppingCart({
|
||||
processId,
|
||||
shoppingCartItemId: itemToUpdate.id,
|
||||
update: {
|
||||
availability,
|
||||
},
|
||||
});
|
||||
})
|
||||
);
|
||||
})
|
||||
).pipe(bufferCount(itemsToUpdate.length));
|
||||
})
|
||||
);
|
||||
|
||||
// TODO Daily: CatalogProductNumber statt searchItemsByEANs
|
||||
// const items$ = itemsToUpdate$.pipe(mergeMap((items) =>
|
||||
// this.searchItemsByEANs({ eans: items.map((item) => item.data.product.ean) })));
|
||||
|
||||
combineLatest([itemsToUpdate$, branches$]).pipe(
|
||||
mergeMap(([itemsToUpdate, branches]) => {
|
||||
itemsToUpdate.map((itemToUpdate) => {
|
||||
const logisticianId = itemToUpdate.data.availability.logistician.id;
|
||||
const branchNo = this.sso.getClaimByKey('branch_no');
|
||||
const branchId = itemToUpdate?.data?.destination?.data?.targetBranch?.id;
|
||||
if (logisticianId === 1021) {
|
||||
return this.availabilityService.getDigDeliveryAvailability({
|
||||
item: {
|
||||
ean: itemToUpdate.data.product.ean,
|
||||
itemId: Number(itemToUpdate.data.product.catalogProductNumber),
|
||||
price: itemToUpdate.data.availability.price,
|
||||
},
|
||||
quantity: itemToUpdate.data.quantity,
|
||||
});
|
||||
} else if (logisticianId === 1022) {
|
||||
return this.availabilityService.getB2bDeliveryAvailability({
|
||||
item: {
|
||||
ean: itemToUpdate.data.product.ean,
|
||||
itemId: Number(itemToUpdate.data.product.catalogProductNumber),
|
||||
price: itemToUpdate.data.availability.price,
|
||||
},
|
||||
quantity: itemToUpdate.data.quantity,
|
||||
branch: branches.find((branch) => (branchId ? branch.id === branchId : branch.branchNumber === branchNo)),
|
||||
});
|
||||
}
|
||||
});
|
||||
return null;
|
||||
})
|
||||
);
|
||||
return shoppingCart$;
|
||||
}
|
||||
|
||||
getCurrentBranch({ branchId }: { branchId: number }): Observable<BranchDTO> {
|
||||
const branchNo = this.sso.getClaimByKey('branch_no');
|
||||
return this.getBranches().pipe(
|
||||
map((branches) => branches.find((branch) => (branchId ? branch.id === branchId : branch.branchNumber === branchNo)))
|
||||
);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @deprecated The method should not be used
|
||||
// * @internal
|
||||
// */
|
||||
// searchItemsByEANs({ eans }: { eans: string[] }): Observable<StringDictionary<ItemDTO>> {
|
||||
// return this.searchService.SearchByEAN(eans).pipe(
|
||||
// map((response) => {
|
||||
// const dict: StringDictionary<ItemDTO> = {};
|
||||
// for (const item of response.result) {
|
||||
// dict[item.product.ean] = item;
|
||||
// }
|
||||
// return dict;
|
||||
// })
|
||||
// );
|
||||
// }
|
||||
|
||||
completeCheckout({ processId }: { processId: number }): Observable<DisplayOrderDTO[]> {
|
||||
const refreshShoppingCart$ = this.getShoppingCart({ processId, latest: true }).pipe(first());
|
||||
const refreshCheckout$ = this.getCheckout({ processId, refresh: true }).pipe(first());
|
||||
@@ -498,8 +482,7 @@ export class DomainCheckoutService {
|
||||
})
|
||||
);
|
||||
|
||||
// TODO
|
||||
const validateAvailabilities$ = this.validateAvailabilities({ processId }).pipe();
|
||||
const updateAvailabilities$ = this.updateAvailabilities({ processId });
|
||||
|
||||
const setPaymentType$ = itemOrderOptions$.pipe(
|
||||
mergeMap(({ hasDownload, hasDelivery, hasDigDelivery, hasB2BDelivery }) => {
|
||||
@@ -551,7 +534,7 @@ export class DomainCheckoutService {
|
||||
mergeMap((_) => setSpecialComment$.pipe(tap(console.log.bind(window, 'setSpecialComment$')))),
|
||||
mergeMap((_) => refreshCheckout$.pipe(tap(console.log.bind(window, 'refreshCheckout$')))),
|
||||
mergeMap((_) => updateDestination$.pipe(tap(console.log.bind(window, 'updateDestination$')))),
|
||||
mergeMap((_) => validateAvailabilities$.pipe(tap(console.log.bind(window, 'validateAvailabilities$')))),
|
||||
mergeMap((_) => updateAvailabilities$.pipe(tap(console.log.bind(window, 'updateAvailabilities$')))),
|
||||
mergeMap((_) => setBuyer$.pipe(tap(console.log.bind(window, 'setBuyer$')))),
|
||||
mergeMap((_) => setNotificationChannels$.pipe(tap(console.log.bind(window, 'setNotificationChannels$')))),
|
||||
mergeMap((_) => setPayer$.pipe(tap(console.log.bind(window, 'setPayer$')))),
|
||||
|
||||
@@ -403,7 +403,6 @@ export class PurchasingOptionsModalStore extends ComponentStore<PurchasingOption
|
||||
availability$ = this.availabilityService.getB2bDeliveryAvailability({
|
||||
item: { itemId: item.id, ean: item.product.ean, price: item.catalogAvailability.price },
|
||||
quantity,
|
||||
branch,
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user