Merged PR 430: #1322 Cart service erweitern um den benötigten Kunden zu ermitteln

#1322 Cart service erweitern um den benötigten Kunden zu ermitteln
This commit is contained in:
Nino Righi
2021-01-18 16:17:42 +00:00
committed by Lorenz Hilpert
5 changed files with 98 additions and 21 deletions

View File

@@ -1,5 +1,6 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { CartItem } from './defs/cart-item.model';
@Injectable({
providedIn: 'root',
@@ -7,7 +8,11 @@ import { Observable } from 'rxjs';
export class CartService {
constructor() {}
getItems(processId: number): number[] {
getItems(processId: number): Observable<CartItem[]> {
throw new Error('Not Implemented');
}
getRequiredCustomerTypes(processId: number): Observable<('branch' | 'guest' | 'online' | 'b2b')[]> {
throw new Error('Not Implemented');
}
}

View File

@@ -1,9 +1,24 @@
import { AvailabilityDTO, BranchDTO, ProductDTO } from '@swagger/checkout';
import { AvailabilityDTO, BranchDTO, ItemDTO, Price, ProductDTO } from '@swagger/checkout';
import { BranchInfoDTO } from '@swagger/isa';
export interface CartItem {
product: ProductDTO;
quantity: number;
availability: AvailabilityDTO;
target: 'shipping' | 'pickup' | 'download' | 'reserve';
targetBranch?: BranchDTO;
// cartId: number;
// cartEntryId: number;
// book: ItemDTO;
// deliveryType: string;
// branch?: BranchInfoDTO;
// orderTypeChanged: boolean;
// avaMessage: string;
// pickUpPrice?: number;
// deliveryPrice?: number;
// downloadPrice?: number;
// deliveryDate?: string;
// pickUpDate?: string;
// quantity: number;
// shopItemId?: number;
// product?: ProductDTO;
// availability?: AvailabilityDTO;
target?: 'shipping' | 'shippingB2B' | 'pickup' | 'download' | 'reserve';
// targetBranch?: BranchDTO;
}

View File

@@ -4,3 +4,4 @@
export * from './lib/cart.service';
export * from './lib/cart.module';
export * from './lib/defs/cart-item.model';

View File

@@ -27,9 +27,9 @@ export class CustomerDetailsComponent implements OnInit {
selectedPayer?: number = undefined;
selectedAddress?: number = undefined;
cartExists: boolean;
isB2b$: Observable<boolean>;
cartExists$: Observable<boolean>;
private currentBreadcrumb: Breadcrumb;
@@ -46,11 +46,11 @@ export class CustomerDetailsComponent implements OnInit {
) {}
ngOnInit() {
if (this.cartService.getItems(this.application.activatedProcessId).length > 0) {
this.cartExists = true;
} else {
this.cartExists = false;
}
this.cartExists$ = this.cartService.getItems(this.application.activatedProcessId).pipe(
map((items) => {
return items.length > 0;
})
);
this.customerId$ = this.activatedRoute.params.pipe(map((params) => Number(params['customerId'])));
@@ -146,7 +146,7 @@ export class CustomerDetailsComponent implements OnInit {
}
// Navigate To Catalog Or Cart
if (this.cartExists) {
if (await this.cartExists$.pipe(first()).toPromise()) {
this.router.navigate(['/cart/review']);
} else {
this.router.navigate(['/product/search']);

View File

@@ -1,15 +1,71 @@
import { Injectable } from '@angular/core';
import { CartService } from '@domain/cart';
import { CartService, CartItem } from '@domain/cart';
import { Store } from '@ngxs/store';
import { ProcessSelectors } from '../core/store/selectors/process.selectors';
import { CartState } from '../core/store/state/cart.state';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { SharedSelectors } from '../core/store/selectors/shared.selectors';
@Injectable()
export class CartRefactImp implements CartService {
constructor(private store: Store) {}
getItems(processId: number): number[] {
const currentProcess = this.store.selectSnapshot(ProcessSelectors.getCurrentProcess);
const existingCartEntriesIds = this.store.selectSnapshot(CartState.getCartEntries)(currentProcess.cartId);
return existingCartEntriesIds || [];
getItems(processId: number): Observable<CartItem[]> {
return this.store.select(SharedSelectors.getCart).pipe(
map((processCart) => processCart.cart),
map((items) => {
return items.map<CartItem>((item) => {
switch (item.deliveryType) {
case 'Versand':
return { target: 'shipping' };
case 'Abholung':
return { target: 'pickup' };
case 'Rücklage':
return { target: 'reserve' };
case 'Download':
return { target: 'download' };
}
});
})
);
}
getRequiredCustomerTypes(processId: number): Observable<('branch' | 'guest' | 'online' | 'b2b')[]> {
return this.getItems(processId).pipe(
map((items) => {
const itemsSet = new Set(items.map((i) => i.target));
const itemsArr = Array.from(itemsSet);
if (itemsArr.length === 1) {
switch (itemsArr[0]) {
// TODO bei Einzelprodukt im Warenkorb: Versand Artikel wenn Abholung auch möglich
case 'reserve':
case 'pickup':
return ['branch', 'online', 'b2b'];
case 'shipping':
return ['guest', 'online'];
case 'download':
return ['online'];
case 'shippingB2B':
return ['b2b'];
}
}
// TODO bei Mischkörben: Versand Artikel wenn Abholung auch möglich
// TODO Mischkörbe Sonderfall: reserve wird nicht gehandled
if (itemsSet.has('shipping') && itemsSet.has('download')) {
return ['online'];
}
if (itemsSet.has('shipping') && itemsSet.has('shippingB2B')) {
return ['b2b'];
}
if (itemsSet.has('pickup') && itemsSet.has('shipping')) {
return ['guest', 'online'];
}
if (itemsSet.has('pickup') && itemsSet.has('shippingB2B')) {
return ['b2b'];
}
})
);
}
}