Skip to content

Commit

Permalink
fix: add quote to cart in case no cart exists (#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
SGrueber authored Dec 16, 2021
1 parent 93870ee commit 114c8d5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ describe('Quoting Service', () => {

describe('addQuoteToBasket', () => {
beforeEach(() => {
when(apiService.post(anything(), anything(), anything())).thenReturn(of({}));
when(apiService.post(anything(), anything())).thenReturn(of({}));
});

it('should use basket API for adding quotes to basket', done => {
quotingService.addQuoteToBasket('quoteID').subscribe(
quotingService.addQuoteToBasket('basketId', 'quoteID').subscribe(
() => {
verify(apiService.post(anything(), anything(), anything())).once();
verify(apiService.post(anything(), anything())).once();
const [path, body] = capture(apiService.post).last();
expect(path).toMatchInlineSnapshot(`"baskets/current/items"`);
expect(path).toMatchInlineSnapshot(`"baskets/basketId/items"`);
expect(body).toMatchInlineSnapshot(`
Object {
"quoteID": "quoteID",
Expand Down
12 changes: 4 additions & 8 deletions src/app/extensions/quoting/services/quoting/quoting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { concatMap, defaultIfEmpty, expand, filter, last, map, mapTo, take } fro

import { Link } from 'ish-core/models/link/link.model';
import { ApiService, unpackEnvelope } from 'ish-core/services/api/api.service';
import { BasketService } from 'ish-core/services/basket/basket.service';

import { QuoteRequestUpdate } from '../../models/quote-request-update/quote-request-update.model';
import { QuotingHelper } from '../../models/quoting/quoting.helper';
Expand All @@ -22,11 +21,7 @@ import {

@Injectable({ providedIn: 'root' })
export class QuotingService {
constructor(
private apiService: ApiService,
private basketService: BasketService,
private quoteMapper: QuotingMapper
) {}
constructor(private apiService: ApiService, private quoteMapper: QuotingMapper) {}

getQuotes() {
return forkJoin([
Expand Down Expand Up @@ -100,8 +95,9 @@ export class QuotingService {
.pipe(map(data => this.quoteMapper.fromData(data, 'Quote')));
}

addQuoteToBasket(quoteID: string) {
return this.basketService.currentBasketEndpoint().post('items', { quoteID }).pipe(mapTo(quoteID));
addQuoteToBasket(basketId: string, quoteID: string) {
// ToDo: remove parameter basketId and delegate addQuoteToBasket to the basket service if the basket REST api 1.0 provides this functionality, see #70533
return this.apiService.post(`baskets/${basketId}/items`, { quoteID }).pipe(mapTo(quoteID));
}

createQuoteRequestFromQuote(quoteID: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('Quoting Effects', () => {

describe('addQuoteToBasket$', () => {
beforeEach(() => {
when(quotingService.addQuoteToBasket(anything())).thenReturn(of(''));
when(quotingService.addQuoteToBasket(anything(), anything())).thenReturn(of(''));
});

describe('with basket', () => {
Expand All @@ -192,7 +192,7 @@ describe('Quoting Effects', () => {
actions$ = of(addQuoteToBasket({ id: 'quoteID' }));

effects.addQuoteToBasket$.subscribe(() => {
verify(quotingService.addQuoteToBasket('quoteID')).once();
verify(quotingService.addQuoteToBasket('basketID', 'quoteID')).once();
verify(basketService.createBasket()).never();
done();
});
Expand All @@ -209,7 +209,7 @@ describe('Quoting Effects', () => {
actions$ = of(addQuoteToBasket({ id: 'quoteID' }));

effects.addQuoteToBasket$.subscribe(() => {
verify(quotingService.addQuoteToBasket('quoteID')).once();
verify(quotingService.addQuoteToBasket('basketID', 'quoteID')).once();
verify(basketService.createBasket()).once();
done();
});
Expand Down
6 changes: 3 additions & 3 deletions src/app/extensions/quoting/store/quoting/quoting.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ export class QuotingEffects {
this.actions$.pipe(
ofType(addQuoteToBasket),
mapToPayloadProperty('id'),
concatMap(id =>
concatMap(quoteId =>
this.store.pipe(
select(getCurrentBasketId),
first(),
switchMap(basketId =>
!basketId ? this.basketService.createBasket().pipe(map(basket => basket.id)) : of(basketId)
),
concatMap(() => this.quotingService.addQuoteToBasket(id)),
mergeMapTo([updateBasket({ update: { calculated: true } }), addQuoteToBasketSuccess({ id })]),
concatMap(basketId => this.quotingService.addQuoteToBasket(basketId, quoteId)),
mergeMapTo([updateBasket({ update: { calculated: true } }), addQuoteToBasketSuccess({ id: quoteId })]),
mapErrorToAction(loadQuotingFail)
)
)
Expand Down

0 comments on commit 114c8d5

Please sign in to comment.