From 1f73e3b6ada992e507550ccd909f07dca507c076 Mon Sep 17 00:00:00 2001 From: Silke Date: Fri, 27 Jan 2023 12:19:04 +0100 Subject: [PATCH] fix: add basket shipTo address to product prices call to make it more precise --- .../services/prices/prices.service.spec.ts | 9 +++++- .../core/services/prices/prices.service.ts | 29 ++++++++++++------- 2 files changed, 27 insertions(+), 11 deletions(-) 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)))); + }) ); } }