Merged PR 445: #1322 Cart Service implementation of canAddItem based on activeCustomer and c...

#1322 Cart Service implementation of canAddItem based on activeCustomer and customer features
This commit is contained in:
Nino Righi
2021-01-25 10:43:23 +00:00
committed by Lorenz Hilpert
3 changed files with 57 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
import { Injectable } from '@angular/core';
import { OLAAvailabilityDTO } from '@swagger/checkout';
import { Observable } from 'rxjs';
import { CartItem } from './defs/cart-item.model';
@@ -19,4 +20,8 @@ export class CartService {
getRequiredCustomerTypes(processId: number): Observable<('branch' | 'guest' | 'online' | 'b2b')[]> {
throw new Error('Not Implemented');
}
canAddItem(processId: number, availability: OLAAvailabilityDTO): Promise<boolean> {
throw new Error('Not Implemented');
}
}

View File

@@ -26,6 +26,8 @@ import { SearchDropdownComponent } from '@libs/ui/lib/search-dropdown';
import { AddBranchesIfNotLoaded } from 'apps/sales/src/app/core/store/actions/branch.actions';
import { allowedAvailabilityStatusCodes } from 'apps/sales/src/app/core/utils/product.util';
import { OrderService } from '@swagger/oms';
import { CartService } from '@domain/cart';
import { UiDebugModalComponent, UiModalService } from '@ui/modal';
@Component({
selector: 'app-checkout',
@@ -220,6 +222,8 @@ export class ProductCheckoutComponent implements OnInit, OnDestroy {
private productAvailabilityService: ProductAvailabilityService,
private branchService: BranchService,
private datePipe: DatePipe,
private shoppingCartService: CartService,
private uiModal: UiModalService,
private cdrf: ChangeDetectorRef
) {}
@@ -514,6 +518,8 @@ export class ProductCheckoutComponent implements OnInit, OnDestroy {
this._pikUpPrice = preferredSupplier.price.value.value;
}
this.shoppingCartService.canAddItem(undefined, this.currentAvailability);
const eligibleAvailabilities = response.av
.filter((availability) => allowedAvailabilityStatusCodes(availability.status) && availability.qty)
.map((availability) => availability.qty);
@@ -560,7 +566,9 @@ export class ProductCheckoutComponent implements OnInit, OnDestroy {
} else if (this.deliveryType === DeliveryOption.DELIVERY_B2B) {
// TODO: Logik für Verfügbarkeit prüfen
}
this.shoppingCartService
.canAddItem(undefined, this.currentAvailability)
.then((result) => this.uiModal.open({ content: UiDebugModalComponent, data: { canAddItem: result } }));
return of(numberOfItems).toPromise();
}

View File

@@ -1,14 +1,17 @@
import { Injectable } from '@angular/core';
import { CartService, CartItem } from '@domain/cart';
import { Store } from '@ngxs/store';
import { Observable, combineLatest } from 'rxjs';
import { map, switchMap, tap } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { map, switchMap, tap, first } from 'rxjs/operators';
import { SharedSelectors } from '../core/store/selectors/shared.selectors';
import { ProcessSelectors } from '../core/store/selectors/process.selectors';
import { StoreCheckoutService } from '@swagger/checkout';
import { OLAAvailabilityDTO, StoreCheckoutService } from '@swagger/checkout';
import { CustomerSelectors } from '../core/store/selectors/customer.selectors';
import { StringDictionary } from '@cmf/core';
import { ApplicationService } from '@core/application';
@Injectable()
export class CartRefactImp implements CartService {
constructor(private store: Store, private checkoutService: StoreCheckoutService) {}
constructor(private store: Store, private checkoutService: StoreCheckoutService, private applicationService: ApplicationService) {}
getItems(processId: number): Observable<CartItem[]> {
return this.store.select(SharedSelectors.getCart).pipe(
@@ -39,12 +42,7 @@ export class CartRefactImp implements CartService {
}
getCardId(processId: number): Observable<number> {
return this.store.select(ProcessSelectors.getCurrentProcess).pipe(
map((process) => {
console.log(process);
return process.cartId;
})
);
return this.store.select(ProcessSelectors.getCurrentProcess).pipe(map((process) => process.cartId));
}
getRequiredCustomerTypes(processId: number): Observable<any> {
@@ -94,4 +92,39 @@ export class CartRefactImp implements CartService {
})
);
}
async canAddItem(processId: number, availability: OLAAvailabilityDTO): Promise<boolean> {
const customer$ = await this.store.select(CustomerSelectors.getActiveUser).pipe(first()).toPromise();
const features$ = await this.store.select(CustomerSelectors.getCustomerFeatures).pipe(first()).toPromise();
console.log({ processId }, { activatedProcessId: this.applicationService.activatedProcessId });
const customerId = customer$(processId || this.applicationService.activatedProcessId)?.id;
console.log({ customerId });
const customerFeatures: StringDictionary<string> = {};
if (customerId) {
const features = features$(customerId);
console.log({ features });
for (const feature of features) {
customerFeatures[feature.key] = feature.key;
}
}
this.getCardId(processId || this.applicationService.activatedProcessId)
.pipe(
first(),
switchMap((cartId) => {
return this.checkoutService
.StoreCheckoutCanAddItem({ shoppingCartId: cartId, payload: { customerFeatures, availabilities: [availability] } })
.pipe(tap((res) => console.log(res)));
})
)
.toPromise();
return false;
}
}