Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 6x 6x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x 2x 2x 4x 3x 2x | import { HttpHeaders } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Store, select } from '@ngrx/store'; import { Observable, forkJoin, iif, of, throwError } from 'rxjs'; import { concatMap, first, map } from 'rxjs/operators'; import { BasketInfoMapper } from 'ish-core/models/basket-info/basket-info.mapper'; import { BasketInfo } from 'ish-core/models/basket-info/basket-info.model'; import { Basket } from 'ish-core/models/basket/basket.model'; import { ErrorFeedback } from 'ish-core/models/http-error/http-error.model'; import { LineItemData } from 'ish-core/models/line-item/line-item.interface'; import { LineItemMapper } from 'ish-core/models/line-item/line-item.mapper'; import { AddLineItemType, LineItem, LineItemView } from 'ish-core/models/line-item/line-item.model'; import { ApiService } from 'ish-core/services/api/api.service'; import { getCurrentBasket } from 'ish-core/store/customer/basket'; export type BasketItemUpdateType = | { quantity?: { value: number; unit: string }; product?: string } | { shippingMethod?: { id: string } } | { desiredDelivery?: string } | { calculated: boolean } | { warranty?: string }; /** * The Basket-Items Service handles basket line-item related calls for the 'baskets/items' REST API. */ @Injectable({ providedIn: 'root' }) export class BasketItemsService { constructor(private apiService: ApiService, private store: Store) {} /** * http header for Basket API v1 */ private basketHeaders = new HttpHeaders({ 'content-type': 'application/json', Accept: 'application/vnd.intershop.basket.v1+json', }); /** * Adds a list of items with the given sku and quantity to the currently used basket. * * @param items The list of product SKU and quantity pairs to be added to the basket. * @returns lineItems The list of line items, that have been added to the basket. * info Info responded by the server. * errors Errors responded by the server. */ addItemsToBasket( items: AddLineItemType[] ): Observable<{ lineItems: LineItem[]; info: BasketInfo[]; errors: ErrorFeedback[] }> { Iif (!items) { return throwError(() => new Error('addItemsToBasket() called without items')); } return this.store.pipe(select(getCurrentBasket)).pipe( first(), concatMap(basket => this.apiService .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, })) ) ) ); } private mapItemsToAdd(basket: Basket, items: AddLineItemType[]) { 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, warranty: item.warrantySku, })); } /** * Updates a specific line item (quantity/shipping method) of the currently used basket. * * @param itemId The id of the line item that should be updated. * @param body request body * @returns The line item and possible infos after the update. */ updateBasketItem(itemId: string, body: BasketItemUpdateType): Observable<{ lineItem: LineItem; info: BasketInfo[] }> { return this.apiService .currentBasketEndpoint() .patch<{ data: LineItemData; infos: BasketInfo[] }>(`items/${this.apiService.encodeResourceId(itemId)}`, body, { headers: this.basketHeaders, }) .pipe( map(payload => ({ lineItem: LineItemMapper.fromData(payload.data), info: BasketInfoMapper.fromInfo({ infos: payload.infos }), })) ); } /** * Removes a specific line item from the currently used basket. * * @param itemId The id of the line item that should be deleted. */ deleteBasketItem(itemId: string): Observable<BasketInfo[]> { return this.apiService .currentBasketEndpoint() .delete<{ infos: BasketInfo[] }>(`items/${this.apiService.encodeResourceId(itemId)}`, { headers: this.basketHeaders, }) .pipe( map(payload => ({ ...payload, itemId })), map(BasketInfoMapper.fromInfo) ); } /** * Removes all line items from the currently used basket. */ deleteBasketItems(): Observable<BasketInfo[]> { return this.apiService .currentBasketEndpoint() .delete<{ infos: BasketInfo[] }>(`items`, { headers: this.basketHeaders, }) .pipe( map(payload => ({ ...payload })), map(BasketInfoMapper.fromInfo) ); } /** * Updates the desired delivery date at all those line items of the current basket, whose desired delivery date differs from the given date. * * @param desiredDeliveryDate Desired delivery date in iso format, i.e. yyyy-mm-dd. * @param lineItems Array of basket line items * @returns Array of updated line items and basket */ updateBasketItemsDesiredDeliveryDate( desiredDeliveryDate: string, lineItems: LineItemView[] ): Observable<{ lineItem: LineItem; info: BasketInfo[] }[]> { Iif (desiredDeliveryDate && !new RegExp(/\d{4}-\d{2}-\d{2}/).test(desiredDeliveryDate)) { return throwError( () => new Error('updateBasketItemsDesiredDeliveryDate() called with an invalid desiredDeliveryDate') ); } const obsArray = lineItems ?.filter(item => item.desiredDeliveryDate !== desiredDeliveryDate) ?.map(item => this.updateBasketItem(item.id, { desiredDelivery: desiredDeliveryDate })); return iif(() => !!obsArray.length, forkJoin(obsArray), of([])); } } |