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

72.72% Statements 8/11
70% Branches 14/20
80% Functions 4/5
70% Lines 7/10

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                                                                                        349x         52x       22x       1x                         1x   1x     1x    
import { Address } from 'ish-core/models/address/address.model';
import { Attribute } from 'ish-core/models/attribute/attribute.model';
import { BasketApproval } from 'ish-core/models/basket-approval/basket-approval.model';
import { BasketInfo } from 'ish-core/models/basket-info/basket-info.model';
import { BasketTotal } from 'ish-core/models/basket-total/basket-total.model';
import { BasketValidationResultType } from 'ish-core/models/basket-validation/basket-validation.model';
import { LineItem, LineItemView } from 'ish-core/models/line-item/line-item.model';
import { Payment } from 'ish-core/models/payment/payment.model';
import { ShippingMethod } from 'ish-core/models/shipping-method/shipping-method.model';
 
export interface AbstractBasket<T> {
  id: string;
  purchaseCurrency?: string;
  dynamicMessages?: string[];
  invoiceToAddress?: Address;
  commonShipToAddress?: Address;
  commonShippingMethod?: ShippingMethod;
  customerNo?: string;
  email?: string;
  lineItems?: T[];
  payment?: Payment;
  costCenter?: string;
  promotionCodes?: string[];
  totals: BasketTotal;
  totalProductQuantity?: number;
  bucketId?: string;
  infos?: BasketInfo[];
  approval?: BasketApproval;
  attributes?: Attribute[];
  taxationId?: string;
  user?: {
    companyName?: string;
    companyName2?: string;
    firstName: string;
    lastName: string;
  };
  externalOrderReference?: string;
  messageToMerchant?: string;
}
 
export type Basket = AbstractBasket<LineItem>;
 
export type BasketView = AbstractBasket<LineItemView>;
 
export const createBasketView = (
  basket: Basket,
  validationResults: BasketValidationResultType,
  basketInfo: BasketInfo[]
): BasketView =>
  basket && {
    ...basket,
    lineItems: basket.lineItems
      ? basket.lineItems
          .map(li => ({
            ...li,
            validationError:
              validationResults && !validationResults.valid && validationResults.errors
                ? validationResults.errors.find(error => error.parameters && error.parameters.lineItemId === li.id)
                : undefined,
            info:
              basketInfo?.length && basketInfo[0].causes
                ? basketInfo[0].causes.find(cause => cause.parameters && cause.parameters.lineItemId === li.id)
                : undefined,
          }))
          .sort(comparePosition)
      : [],
  };
 
/* Sort basket line items by position as long as the REST request returns them unsorted */
function comparePosition(a: LineItem, b: LineItem) {
  Iif (a.position < b.position) {
    return -1;
  } else Iif (a.position < b.position) {
    return 1;
  }
  return 0;
}