Skip to content

Commit

Permalink
fix: add items to basket with common shipping method (#1419)
Browse files Browse the repository at this point in the history
* fix: add items to basket with common shipping method

* test: fix e2e test due to shipping restrictions
  • Loading branch information
SGrueber committed Apr 24, 2023
1 parent 61c4b0f commit 6ee9909
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
Expand Up @@ -17,7 +17,7 @@ const _ = {
},
category: 'networks',
subcategory: 'networks.firewalls',
product1: '859910',
product1: '1451744',
product2: '3459777',
product3: '3542794',
};
Expand Down
7 changes: 5 additions & 2 deletions src/app/core/services/basket/basket.service.spec.ts
Expand Up @@ -10,7 +10,7 @@ import { LineItemData } from 'ish-core/models/line-item/line-item.interface';
import { LineItem } from 'ish-core/models/line-item/line-item.model';
import { ApiService } from 'ish-core/services/api/api.service';
import { OrderService } from 'ish-core/services/order/order.service';
import { getBasketIdOrCurrent } from 'ish-core/store/customer/basket';
import { getBasketIdOrCurrent, getCurrentBasket } from 'ish-core/store/customer/basket';
import { makeHttpError } from 'ish-core/utils/dev/api-service-utils';
import { BasketMockData } from 'ish-core/utils/dev/basket-mock-data';

Expand Down Expand Up @@ -59,7 +59,10 @@ describe('Basket Service', () => {
{ provide: ApiService, useFactory: () => instance(apiService) },
{ provide: OrderService, useFactory: () => instance(orderService) },
provideMockStore({
selectors: [{ selector: getBasketIdOrCurrent, value: 'current' }],
selectors: [
{ selector: getBasketIdOrCurrent, value: 'current' },
{ selector: getCurrentBasket, value: { id: '123' } },
],
}),
],
});
Expand Down
42 changes: 27 additions & 15 deletions src/app/core/services/basket/basket.service.ts
Expand Up @@ -2,7 +2,7 @@ import { HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { EMPTY, Observable, forkJoin, iif, of, throwError } from 'rxjs';
import { catchError, concatMap, map, take } from 'rxjs/operators';
import { catchError, concatMap, first, map, take } from 'rxjs/operators';

import { AddressMapper } from 'ish-core/models/address/address.mapper';
import { Address } from 'ish-core/models/address/address.model';
Expand All @@ -27,7 +27,7 @@ import { ShippingMethodMapper } from 'ish-core/models/shipping-method/shipping-m
import { ShippingMethod } from 'ish-core/models/shipping-method/shipping-method.model';
import { ApiService, AvailableOptions, unpackEnvelope } from 'ish-core/services/api/api.service';
import { OrderService } from 'ish-core/services/order/order.service';
import { getBasketIdOrCurrent } from 'ish-core/store/customer/basket';
import { getBasketIdOrCurrent, getCurrentBasket } from 'ish-core/store/customer/basket';

export type BasketUpdateType =
| { invoiceToAddress: string }
Expand Down Expand Up @@ -341,26 +341,38 @@ export class BasketService {
if (!items) {
return throwError(() => new Error('addItemsToBasket() called without items'));
}
return this.store.pipe(select(getCurrentBasket)).pipe(
first(),
concatMap(basket =>
this.currentBasketEndpoint()
.post<{ data: LineItemData[]; infos: BasketInfo[]; errors?: ErrorFeedback[] }>(
'items',
this.mapItemsToAdd(basket, items),
{
headers: this.basketHeaders,
}
)
.pipe(
map(payload => ({
lineItems: payload.data.map(item => LineItemMapper.fromData(item)),
info: BasketInfoMapper.fromInfo({ infos: payload.infos }),
errors: payload.errors,
}))
)
)
);
}

const body = items.map(item => ({
private mapItemsToAdd(basket: Basket, items: SkuQuantityType[]) {
return items.map(item => ({
product: item.sku,
quantity: {
value: item.quantity,
unit: item.unit,
},
// ToDo: if multi buckets will be supported setting the shipping method to the common shipping method has to be reworked
shippingMethod: basket?.commonShippingMethod?.id,
}));

return this.currentBasketEndpoint()
.post<{ data: LineItemData[]; infos: BasketInfo[]; errors?: ErrorFeedback[] }>('items', body, {
headers: this.basketHeaders,
})
.pipe(
map(payload => ({
lineItems: payload.data.map(item => LineItemMapper.fromData(item)),
info: BasketInfoMapper.fromInfo({ infos: payload.infos }),
errors: payload.errors,
}))
);
}

/**
Expand Down

0 comments on commit 6ee9909

Please sign in to comment.