All files / src/app/core/models/basket basket.mapper.ts

95.23% Statements 20/21
79.62% Branches 43/54
88.88% Functions 8/9
95.23% Lines 20/21

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 13538x   38x     38x 38x 38x 38x       38x     24x   24x     24x 13x     24x                                             11x                                           9x     1x                           27x   27x                             9x                         14x             11x                      
import { AddressMapper } from 'ish-core/models/address/address.mapper';
import { BasketRebateData } from 'ish-core/models/basket-rebate/basket-rebate.interface';
import { BasketRebateMapper } from 'ish-core/models/basket-rebate/basket-rebate.mapper';
import { BasketTotal } from 'ish-core/models/basket-total/basket-total.model';
import { BasketBaseData, BasketData } from 'ish-core/models/basket/basket.interface';
import { LineItemMapper } from 'ish-core/models/line-item/line-item.mapper';
import { PaymentMapper } from 'ish-core/models/payment/payment.mapper';
import { PriceItemMapper } from 'ish-core/models/price-item/price-item.mapper';
import { ShippingMethodMapper } from 'ish-core/models/shipping-method/shipping-method.mapper';
 
import { Basket } from './basket.model';
 
export class BasketMapper {
  // eslint-disable-next-line complexity
  static fromData(payload: BasketData): Basket {
    const { data, included, infos } = payload;
 
    const totals = data.calculated
      ? BasketMapper.getTotals(data, included ? included.discounts : undefined)
      : undefined;
    if (totals) {
      totals.isEstimated = !data.invoiceToAddress || !data.commonShipToAddress || !data.commonShippingMethod;
    }
 
    return {
      id: data.id,
      bucketId: (data.buckets?.length === 1 && data.buckets[0]) || undefined,
      purchaseCurrency: data.purchaseCurrency,
      dynamicMessages: data.discounts ? data.discounts.dynamicMessages : undefined,
      invoiceToAddress:
        included?.invoiceToAddress && data.invoiceToAddress
          ? AddressMapper.fromData(included.invoiceToAddress[data.invoiceToAddress])
          : undefined,
      commonShipToAddress:
        included?.commonShipToAddress && data.commonShipToAddress
          ? AddressMapper.fromData(included.commonShipToAddress[data.commonShipToAddress])
          : undefined,
      commonShippingMethod:
        included?.commonShippingMethod && data.commonShippingMethod
          ? ShippingMethodMapper.fromData(included.commonShippingMethod[data.commonShippingMethod])
          : undefined,
      costCenter: data.costCenter,
      customerNo: data.customer,
      email: data.user,
      lineItems:
        included?.lineItems && data.lineItems?.length
          ? data.lineItems.map(lineItemId =>
              LineItemMapper.fromData(
                included.lineItems[lineItemId],
                included.lineItems_discounts,
                included.lineItems_warranty
              )
            )
          : [],
      totalProductQuantity: data.totalProductQuantity,
      payment:
        included?.payments && data.payments?.length && included.payments[data.payments[0]]
          ? PaymentMapper.fromIncludeData(
              included.payments[data.payments[0]],
              included.payments_paymentMethod?.[included.payments[data.payments[0]].paymentMethod]
                ? included.payments_paymentMethod[included.payments[data.payments[0]].paymentMethod]
                : undefined,
              included.payments[data.payments[0]].paymentInstrument && included.payments_paymentInstrument
                ? included.payments_paymentInstrument[included.payments[data.payments[0]].paymentInstrument]
                : undefined
            )
          : undefined,
      promotionCodes: data.promotionCodes,
      totals,
      infos: infos?.filter(info => info.code !== 'include.not_resolved.error'),
      approval: data.approval,
      attributes: data.attributes,
      taxationId: data.attributes?.find(attr => attr.name === 'taxationID')?.value as string,
      user: data.buyer,
      externalOrderReference: data.externalOrderReference,
      messageToMerchant: data.messageToMerchant,
      recurrence: data.recurrence,
    };
  }
 
  /**
   * Helper method to determine a basket(order) total on the base of row data.
   *
   * @returns         The basket total.
   */
  static getTotals(data: BasketBaseData, discounts?: { [id: string]: BasketRebateData }): BasketTotal {
    const totalsData = data.totals;
 
    return totalsData
      ? {
          itemTotal: PriceItemMapper.fromPriceItem(totalsData.itemTotal),
          undiscountedItemTotal: PriceItemMapper.fromPriceItem(totalsData.undiscountedItemTotal),
          shippingTotal: PriceItemMapper.fromPriceItem(totalsData.shippingTotal),
          undiscountedShippingTotal: PriceItemMapper.fromPriceItem(totalsData.undiscountedShippingTotal),
          paymentCostsTotal: PriceItemMapper.fromPriceItem(totalsData.paymentCostsTotal),
          dutiesAndSurchargesTotal: PriceItemMapper.fromPriceItem(totalsData.surchargeTotal),
          taxTotal: PriceItemMapper.fromSpecificPriceItem(totalsData.grandTotal, 'tax'),
          total: PriceItemMapper.fromPriceItem(totalsData.grandTotal),
 
          itemRebatesTotal: PriceItemMapper.fromPriceItem(totalsData.itemValueDiscountsTotal),
          valueRebatesTotal: PriceItemMapper.fromPriceItem(totalsData.basketValueDiscountsTotal),
          valueRebates:
            data.discounts?.valueBasedDiscounts && discounts
              ? data.discounts.valueBasedDiscounts.map(discountId => BasketRebateMapper.fromData(discounts[discountId]))
              : undefined,
 
          itemShippingRebatesTotal: PriceItemMapper.fromPriceItem(totalsData.itemShippingDiscountsTotal),
          shippingRebatesTotal: PriceItemMapper.fromPriceItem(totalsData.basketShippingDiscountsTotal),
          shippingRebates:
            data.discounts?.shippingBasedDiscounts && discounts
              ? data.discounts.shippingBasedDiscounts.map(discountId =>
                  BasketRebateMapper.fromData(discounts[discountId])
                )
              : undefined,
 
          itemSurchargeTotalsByType: data.surcharges?.itemSurcharges
            ? data.surcharges.itemSurcharges.map(surcharge => ({
                amount: PriceItemMapper.fromPriceItem(surcharge.amount),
                displayName: surcharge.name,
                description: surcharge.description,
              }))
            : undefined,
          bucketSurchargeTotalsByType: data.surcharges?.bucketSurcharges
            ? data.surcharges.bucketSurcharges.map(surcharge => ({
                amount: PriceItemMapper.fromPriceItem(surcharge.amount),
                displayName: surcharge.name,
                description: surcharge.description,
              }))
            : undefined,
          isEstimated: false,
        }
      : undefined;
  }
}