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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 20x 20x 20x 20x 20x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { HttpHeaders, HttpParams } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable, of, throwError } from 'rxjs'; import { catchError, concatMap, map } from 'rxjs/operators'; import { AddressData } from 'ish-core/models/address/address.interface'; import { AddressMapper } from 'ish-core/models/address/address.mapper'; import { Address } from 'ish-core/models/address/address.model'; import { Attribute } from 'ish-core/models/attribute/attribute.model'; import { BasketInfoMapper } from 'ish-core/models/basket-info/basket-info.mapper'; import { BasketInfo } from 'ish-core/models/basket-info/basket-info.model'; import { BasketMergeHelper } from 'ish-core/models/basket-merge/basket-merge.helper'; import { BasketMergeData } from 'ish-core/models/basket-merge/basket-merge.interface'; import { BasketValidationData } from 'ish-core/models/basket-validation/basket-validation.interface'; import { BasketValidationMapper } from 'ish-core/models/basket-validation/basket-validation.mapper'; import { BasketValidation, BasketValidationScopeType } from 'ish-core/models/basket-validation/basket-validation.model'; import { BasketBaseData, BasketData } from 'ish-core/models/basket/basket.interface'; import { BasketMapper } from 'ish-core/models/basket/basket.mapper'; import { Basket } from 'ish-core/models/basket/basket.model'; import { ShippingMethodData } from 'ish-core/models/shipping-method/shipping-method.interface'; import { ShippingMethodMapper } from 'ish-core/models/shipping-method/shipping-method.mapper'; import { ShippingMethod } from 'ish-core/models/shipping-method/shipping-method.model'; import { ApiService, unpackEnvelope } from 'ish-core/services/api/api.service'; import { OrderService } from 'ish-core/services/order/order.service'; export type BasketUpdateType = | { commonShippingMethod: string } | { commonShipToAddress: string } | { calculated: boolean } | { costCenter: string } | { externalOrderReference: string } | { invoiceToAddress: string } | { messageToMerchant: string }; /** * The Basket Service handles the interaction with the 'baskets' REST API. * Methods related to basket-items are handled in the basket-items.service. * Methods related to the payment are handled in the payment.service. */ @Injectable({ providedIn: 'root' }) export class BasketService { constructor(private apiService: ApiService, private orderService: OrderService) {} /** * http header for Basket API v1 */ private basketHeaders = new HttpHeaders({ 'content-type': 'application/json', Accept: 'application/vnd.intershop.basket.v1+json', }); private readonly allBasketIncludes = [ 'invoiceToAddress', 'commonShipToAddress', 'commonShippingMethod', 'discounts', 'lineItems', 'lineItems_discounts', 'lineItems_warranty', 'payments', 'payments_paymentMethod', 'payments_paymentInstrument', ]; private readonly allTargetBasketIncludes = [ 'targetBasket', 'targetBasket_invoiceToAddress', 'targetBasket_commonShipToAddress', 'targetBasket_commonShippingMethod', 'targetBasket_discounts', 'targetBasket_lineItems', 'targetBasket_lineItems_discounts', 'targetBasket_lineItems_warranty', 'targetBasket_payments', 'targetBasket_payments_paymentMethod', 'targetBasket_payments_paymentInstrument', ]; private readonly allBasketValidationIncludes = [ 'basket', 'basket_invoiceToAddress', 'basket_commonShipToAddress', 'basket_commonShippingMethod', 'basket_discounts', 'basket_lineItems', 'basket_lineItems_discounts', 'basket_lineItems_warranty', 'basket_payments', 'basket_payments_paymentMethod', 'basket_payments_paymentInstrument', ]; /** * Gets the currently used basket for the current user. * * @returns The basket. */ getBasket(): Observable<Basket> { const params = new HttpParams().set('include', this.allBasketIncludes.join()); return this.apiService .currentBasketEndpoint() .get<BasketData>('', { headers: this.basketHeaders, params, }) .pipe(map(BasketMapper.fromData)); } /** * Gets the basket for the current user with the given basket id if available. * * @param basketId The basket id for the basket to be fetched. * @returns The basket. */ getBasketWithId(basketId: string): Observable<Basket> { const params = new HttpParams().set('include', this.allBasketIncludes.join()); return this.apiService .get<BasketData>(`baskets/${basketId}`, { headers: this.basketHeaders, params, }) .pipe(map(BasketMapper.fromData)); } /** * Gets the 'current' default basket for the current user authenticated by the given apiToken. * * @param apiToken The authentication token. * @returns The basket. */ getBasketByToken(apiToken: string): Observable<Basket> { const params = new HttpParams().set('include', this.allBasketIncludes.join()); return this.apiService .get<BasketData>(`baskets/current`, { headers: this.basketHeaders.set(ApiService.TOKEN_HEADER_KEY, apiToken), params, }) .pipe(map(BasketMapper.fromData)); } /** * Creates a new basket for the current user. * * @returns The basket. */ createBasket(): Observable<Basket> { return this.apiService .post<BasketData>( `baskets`, {}, { headers: this.basketHeaders, } ) .pipe(map(BasketMapper.fromData)); } /** * Merge the source basket into the target basket. * * @param sourceBasketId The id of the source basket. * @param sourceBasketAuthToken The token value of the source basket owner. * @param targetBasketId The id of the target basket. * @returns The merged basket. */ mergeBasket(sourceBasketId: string, sourceBasketAuthToken: string, targetBasketId: string): Observable<Basket> { Iif (!sourceBasketId) { return throwError(() => new Error('mergeBasket() called without sourceBasketId')); } Iif (!sourceBasketAuthToken) { return throwError(() => new Error('mergeBasket() called without sourceBasketAuthToken')); } Iif (!targetBasketId) { return throwError(() => new Error('mergeBasket() called without targetBasketId')); } Iif (targetBasketId === sourceBasketId) { return throwError(() => new Error('mergeBasket() cannot be called when targetBasketId === sourceBasketId')); } const params = new HttpParams().set('include', this.allTargetBasketIncludes.join()); return this.apiService .post<BasketMergeData>( `baskets/${this.apiService.encodeResourceId(targetBasketId)}/merges`, { sourceBasket: sourceBasketId, sourceAuthenticationToken: sourceBasketAuthToken }, { headers: this.basketHeaders, params, } ) .pipe(map(mergeBasketData => BasketMapper.fromData(BasketMergeHelper.transform(mergeBasketData)))); } /** * Updates the currently used basket. * * @param body Basket related data (invoice address, shipping address, shipping method ...), which should be changed * @returns The changed basket. */ updateBasket(body: BasketUpdateType): Observable<Basket> { Iif (!body) { return throwError(() => new Error('updateBasket() called without body')); } const params = new HttpParams().set('include', this.allBasketIncludes.join()); return this.apiService .currentBasketEndpoint() .patch<BasketData>('', body, { headers: this.basketHeaders, params, }) .pipe(map(BasketMapper.fromData)); } /** * Validates the currently used basket. * * @param scopes Basket scopes which should be validated ( see also BasketValidationScopeType ), default: minimal scope (max items limit, empty basket) * @returns The (adjusted) basket and the validation results. */ validateBasket(scopes: BasketValidationScopeType[] = ['']): Observable<BasketValidation> { const body = { adjustmentsAllowed: !scopes.some(scope => scope === 'All'), // don't allow adjustments for 'All' validation steps, because you cannot show them to the user at once scopes, }; const params = new HttpParams().set('include', this.allBasketValidationIncludes.join()); return this.apiService .currentBasketEndpoint() .post<BasketValidationData>('validations', body, { headers: this.basketHeaders, params, }) .pipe(map(BasketValidationMapper.fromData)); } /** * Returns the list of active baskets for the current user. The first basket is the last modified basket. * Use this method to check if the user has at least one active basket * * @returns An array of basket base data. */ getBaskets(): Observable<BasketBaseData[]> { return this.apiService .get(`baskets`, { headers: this.basketHeaders, }) .pipe(unpackEnvelope<BasketBaseData>('data')); } /** * Add a promotion code to currently used basket. * * @param codeStr The code string of the promotion code that should be added to basket. * @returns The info message after creation. */ addPromotionCodeToBasket(codeStr: string): Observable<string> { const body = { code: codeStr }; return this.apiService .currentBasketEndpoint() .post<{ infos: BasketInfo[] }>('promotioncodes', body, { headers: this.basketHeaders, }) .pipe(map(({ infos }) => infos?.[0]?.message)); } /** * Remove a promotion code from the currently used basket. * * @param codeStr The code string of the promotion code that should be removed from basket. */ removePromotionCodeFromBasket(codeStr: string): Observable<string> { return this.apiService .currentBasketEndpoint() .delete<string>(`promotioncodes/${this.apiService.encodeResourceId(codeStr)}`, { headers: this.basketHeaders, }); } /** * Get eligible addresses for the currently used basket. * * @returns The eligible addresses. */ getBasketEligibleAddresses(): Observable<Address[]> { return this.apiService .currentBasketEndpoint() .get('eligible-addresses', { headers: this.basketHeaders, }) .pipe( unpackEnvelope<AddressData>('data'), map(addressesData => addressesData.map(AddressMapper.fromData)) ); } /** * Create a basket address for the currently used basket of an anonymous user. * * @param address The address which should be created * @returns The new basket address. */ createBasketAddress(address: Address): Observable<Address> { Iif (!address) { return throwError(() => new Error('createBasketAddress() called without address')); } return this.apiService .currentBasketEndpoint() .post(`addresses`, address, { headers: this.basketHeaders, }) .pipe( map(({ data }) => data), map(AddressMapper.fromData) ); } /** * Update partly or completely an address for the currently used basket of an anonymous user. * * @param address The address data which should be updated * @returns The new basket address. */ updateBasketAddress(address: Address): Observable<Address> { Iif (!address) { return throwError(() => new Error('updateBasketAddress() called without address')); } Iif (!address.id) { return throwError(() => new Error('updateBasketAddress() called without addressId')); } return this.apiService .currentBasketEndpoint() .patch<{ data: Address }>(`addresses/${this.apiService.encodeResourceId(address.id)}`, address, { headers: this.basketHeaders, }) .pipe( map(({ data }) => data), map(AddressMapper.fromData) ); } /** * Get eligible shipping methods for the currently used basket or for the selected bucket of a basket. * * @param bucketId The bucket id. * @returns The eligible shipping methods. */ getBasketEligibleShippingMethods(bucketId?: string): Observable<ShippingMethod[]> { return bucketId ? this.apiService .currentBasketEndpoint() .get(`buckets/${this.apiService.encodeResourceId(bucketId)}/eligible-shipping-methods`, { headers: this.basketHeaders, }) .pipe( unpackEnvelope<ShippingMethodData>('data'), map(data => data.map(ShippingMethodMapper.fromData)) ) : this.apiService .currentBasketEndpoint() .get('eligible-shipping-methods', { headers: this.basketHeaders, }) .pipe( unpackEnvelope<ShippingMethodData>('data'), map(data => data.map(ShippingMethodMapper.fromData)) ); } /** * Creates a requisition of a certain basket that has to be approved. * * @param basketId Basket id. * @returns nothing */ createRequisition(basketId: string): Observable<void> { Iif (!basketId) { return throwError(() => new Error('createRequisition() called without required basketId')); } return this.orderService.createOrder(basketId, true).pipe( concatMap(() => of(undefined)), catchError(err => { if (err.status === 422) { return of(undefined); } return throwError(() => err); }) ); } /** * Creates a custom attribute on the currently used basket. Default attribute type is 'String'. * * @param attr The custom attribute * @returns The custom attribute */ createBasketAttribute(attr: Attribute): Observable<Attribute> { Iif (!attr) { return throwError(() => new Error('createBasketAttribute() called without attribute')); } // if no type is provided save it as string const attribute = { ...attr, type: attr.type ?? 'String' }; return this.apiService.currentBasketEndpoint().post<Attribute>('attributes', attribute, { headers: this.basketHeaders, }); } /** * Updates a custom attribute on the currently used basket. Default attribute type is 'String'. * * @param attribute The custom attribute * @returns The custom attribute */ updateBasketAttribute(attr: Attribute): Observable<Attribute> { Iif (!attr) { return throwError(() => new Error('updateBasketAttribute() called without attribute')); } // if no type is provided save it as string const attribute = { ...attr, type: attr.type ?? 'String' }; return this.apiService .currentBasketEndpoint() .patch<Attribute>(`attributes/${this.apiService.encodeResourceId(attribute.name)}`, attribute, { headers: this.basketHeaders, }); } /** * Deletes a custom attribute from the currently used basket. * * @param attributeName The name of the custom attribute */ deleteBasketAttribute(attributeName: string): Observable<unknown> { Iif (!attributeName) { return throwError(() => new Error('deleteBasketAttribute() called without attributeName')); } return this.apiService .currentBasketEndpoint() .delete(`attributes/${this.apiService.encodeResourceId(attributeName)}`, { headers: this.basketHeaders, }); } /** * Adds all items of a quote to the current basket. * * @param quoteId The id of the quote that should be added to the basket. * @returns The info message if present. */ addQuoteToBasket(quoteId: string): Observable<BasketInfo[]> { Iif (!quoteId) { return throwError(() => new Error('addQuoteToBasket() called without quoteId')); } return this.apiService .currentBasketEndpoint() .post( `quotes`, { id: quoteId, calculated: true }, { headers: this.basketHeaders, } ) .pipe(map(BasketInfoMapper.fromInfo)); } /** * Deletes all items of a quote from the current basket. * * @param quoteId The id of the quote whose items should be deleted from the basket. * @returns The info message if present. */ deleteQuoteFromBasket(quoteId: string): Observable<BasketInfo[]> { Iif (!quoteId) { return throwError(() => new Error('deleteQuoteFromBasket() called without quoteId')); } return this.apiService .currentBasketEndpoint() .delete(`quotes/${this.apiService.encodeResourceId(quoteId)}`, { headers: this.basketHeaders, }) .pipe(map(BasketInfoMapper.fromInfo)); } } |