Skip to content

Commit

Permalink
fix: add basket ship-to-address to the product prices call (#1362)
Browse files Browse the repository at this point in the history
...to make the product price call more precise regarding the resulting taxes
  • Loading branch information
SGrueber authored Jan 30, 2023
1 parent c88fcca commit 0e3c633
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
9 changes: 8 additions & 1 deletion src/app/core/services/prices/prices.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
},
],
}),
],
Expand All @@ -46,7 +53,7 @@ describe('Prices Service', () => {
verify(apiServiceMock.get(`productprices`, anything())).once();
expect(
capture<string, AvailableOptions>(apiServiceMock.get).last()?.[1]?.params?.toString()
).toMatchInlineSnapshot(`"sku=abc&sku=123&customerID=customer"`);
).toMatchInlineSnapshot(`"sku=abc&sku=123&customerID=customer&shipToAddress=urn:4711"`);
done();
});
});
Expand Down
29 changes: 19 additions & 10 deletions src/app/core/services/prices/prices.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand All @@ -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<ProductPriceDetails[]> {
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))));
})
);
}
}

0 comments on commit 0e3c633

Please sign in to comment.