All files / projects/requisition-management/src/app/models/requisition requisition.mapper.ts

82.6% Statements 19/23
60.86% Branches 14/23
75% Functions 6/8
81.81% Lines 18/22

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 994x     4x   4x             4x   5x 4x   4x 4x 4x   4x                     1x           1x                             1x                 1x                                                 4x 3x   1x         1x      
import { Injectable } from '@angular/core';
 
import { BasketData } from 'ish-core/models/basket/basket.interface';
import { BasketMapper } from 'ish-core/models/basket/basket.mapper';
import { OrderData } from 'ish-core/models/order/order.interface';
import { PriceItemMapper } from 'ish-core/models/price-item/price-item.mapper';
import { Price } from 'ish-core/models/price/price.model';
 
import { RequisitionData } from './requisition.interface';
import { Requisition, RequisitionUserBudget } from './requisition.model';
 
@Injectable({ providedIn: 'root' })
export class RequisitionMapper {
  fromData(payload: RequisitionData, orderPayload?: OrderData): Requisition {
    if (!Array.isArray(payload.data)) {
      const { data } = payload;
 
      if (data) {
        const payloadData = (orderPayload ? orderPayload : payload) as BasketData;
        payloadData.data.calculated = true;
 
        return {
          ...BasketMapper.fromData(payloadData),
          id: data.id,
          requisitionNo: data.requisitionNo,
          orderNo: data.orderNo,
          creationDate: data.creationDate,
          userBudget: this.fromUserBudgets(data.userBudgets, data.purchaseCurrency),
          lineItemCount: data.lineItemCount,
          user: data.userInformation,
          approval: {
            ...data.approvalStatus,
            approvers: data.approvalStatuses?.map(status => status.approver),
            customerApproval: {
              ...data.approval?.customerApproval,
              statusCode:
                data.approvalStatuses?.length &&
                data.approval?.customerApproval?.approvers?.some(
                  appr => appr.email === data.approvalStatuses[0]?.approver.email
                )
                  ? data.approvalStatuses[0].statusCode
                  : 'PENDING',
            },
            costCenterApproval: {
              ...data.approval?.costCenterApproval,
              statusCode:
                data.approvalStatuses?.length &&
                data.approval?.costCenterApproval?.approvers[0].email === data.approvalStatuses[0]?.approver.email
                  ? data.approvalStatuses[0].statusCode
                  : 'PENDING',
            },
          },
          systemRejected: data.systemRejected,
          systemRejectErrors: data.systemRejectErrors?.map(error => error.message || error.code),
        };
      } else E{
        throw new Error(`requisitionData is required`);
      }
    }
  }
 
  fromListData(payload: RequisitionData): Requisition[] {
    Iif (Array.isArray(payload.data)) {
      return (
        payload.data
          /* filter requisitions that didn't need an approval */
          .filter(data => data.requisitionNo)
          .map(data => ({
            ...this.fromData({ ...payload, data }),
            totals: {
              itemTotal: data.totals ? PriceItemMapper.fromPriceItem(data.totals.itemTotal) : undefined,
              total: data.totals
                ? PriceItemMapper.fromPriceItem(data.totals.grandTotal)
                : {
                    type: 'PriceItem',
                    gross: data.totalGross.value,
                    net: data.totalNet.value,
                    currency: data.totalGross.currency,
                  },
              isEstimated: false,
            },
          }))
      );
    }
  }
 
  private fromUserBudgets(userBudgets: RequisitionUserBudget, purchaseCurrency: string): RequisitionUserBudget {
    if (!userBudgets?.budgetPeriod) {
      return;
    }
    const emptyPrice: Price = {
      type: 'Money',
      value: 0,
      currency: purchaseCurrency,
    };
    return { ...userBudgets, spentBudget: userBudgets.spentBudget || emptyPrice };
  }
}