Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add items to basket with common shipping method #1419

Merged
merged 2 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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