diff --git a/src/app/core/services/prices/prices.service.spec.ts b/src/app/core/services/prices/prices.service.spec.ts index 500e9f4bf9..6f165ee934 100644 --- a/src/app/core/services/prices/prices.service.spec.ts +++ b/src/app/core/services/prices/prices.service.spec.ts @@ -3,9 +3,12 @@ import { provideMockStore } from '@ngrx/store/testing'; import { of } from 'rxjs'; import { anything, capture, instance, mock, verify, when } from 'ts-mockito'; +import { Address } from 'ish-core/models/address/address.model'; +import { BasketView } from 'ish-core/models/basket/basket.model'; import { Customer } from 'ish-core/models/customer/customer.model'; import { ProductPriceDetailsData } from 'ish-core/models/product-prices/product-prices.interface'; import { ApiService, AvailableOptions } from 'ish-core/services/api/api.service'; +import { getCurrentBasket } from 'ish-core/store/customer/basket'; import { getLoggedInCustomer } from 'ish-core/store/customer/user'; import { PricesService } from './prices.service'; @@ -22,6 +25,10 @@ describe('Prices Service', () => { provideMockStore({ selectors: [ { selector: getLoggedInCustomer, value: { customerNo: 'customer', isBusinessCustomer: true } as Customer }, + { + selector: getCurrentBasket, + value: { commonShipToAddress: { urn: 'urn:4711' } as Address } as BasketView, + }, ], }), ], @@ -46,7 +53,7 @@ describe('Prices Service', () => { verify(apiServiceMock.get(`productprices`, anything())).once(); expect( capture(apiServiceMock.get).last()?.[1]?.params?.toString() - ).toMatchInlineSnapshot(`"sku=abc&sku=123&customerID=customer"`); + ).toMatchInlineSnapshot(`"sku=abc&sku=123&customerID=customer&shipToAddress=urn:4711"`); done(); }); }); diff --git a/src/app/core/services/prices/prices.service.ts b/src/app/core/services/prices/prices.service.ts index 8e0e6de3b0..a0366359d1 100644 --- a/src/app/core/services/prices/prices.service.ts +++ b/src/app/core/services/prices/prices.service.ts @@ -2,12 +2,13 @@ import { HttpHeaders, HttpParams } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Store, select } from '@ngrx/store'; import { Observable, throwError } from 'rxjs'; -import { map, switchMap, take, tap } from 'rxjs/operators'; +import { map, switchMap, take, withLatestFrom } from 'rxjs/operators'; import { ProductPriceDetailsData } from 'ish-core/models/product-prices/product-prices.interface'; import { ProductPricesMapper } from 'ish-core/models/product-prices/product-prices.mapper'; import { ProductPriceDetails } from 'ish-core/models/product-prices/product-prices.model'; import { ApiService } from 'ish-core/services/api/api.service'; +import { getCurrentBasket } from 'ish-core/store/customer/basket'; import { getLoggedInCustomer } from 'ish-core/store/customer/user'; @Injectable({ providedIn: 'root' }) @@ -20,25 +21,33 @@ export class PricesService { private currentCustomer$ = this.store.pipe(select(getLoggedInCustomer), take(1)); + /** + * Gets the prices for an array of products. Prices might be customer specific or depend on the user location (basket ship to address). + * + * @param Array of product skus. + * @returns Product Prices. + */ getProductPrices(skus: string[]): Observable { if (!skus || skus.length === 0) { return throwError(() => new Error('getProductPrices() called without skus')); } - let params = new HttpParams(); - skus.map(sku => (params = params.append('sku', sku))); - return this.currentCustomer$.pipe( - tap(customer => { + withLatestFrom(this.store.pipe(select(getCurrentBasket))), + switchMap(([customer, basket]) => { + let params = new HttpParams(); + skus.map(sku => (params = params.append('sku', sku))); + if (customer?.customerNo) { params = params.set('customerID', customer.customerNo); } - }), - switchMap(() => - this.apiService + if (basket?.commonShipToAddress) { + params = params.set('shipToAddress', basket.commonShipToAddress.urn); + } + return this.apiService .get<{ data: ProductPriceDetailsData[] }>(`productprices`, { headers: this.priceHeaders, params }) - .pipe(map(element => element?.data?.map(prices => ProductPricesMapper.fromData(prices)))) - ) + .pipe(map(element => element?.data?.map(prices => ProductPricesMapper.fromData(prices)))); + }) ); } }