All files / src/app/core/models/cost-center cost-center.mapper.ts

90.9% Statements 10/11
66.66% Branches 2/3
100% Functions 4/4
90% Lines 9/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 668x         8x           2x   2x 2x                             2x             7x 7x   2x                                                    
import { AttributeHelper } from 'ish-core/models/attribute/attribute.helper';
 
import { CostCenterData, CostCenterListData } from './cost-center.interface';
import { CostCenter, CostCenters } from './cost-center.model';
 
export class CostCenterMapper {
  /**
   * map cost center data from link list attributes,
   * some data are missing like orders or buyer assignments that have to be fetched with the details call
   */
  static fromListData(data: CostCenterListData): CostCenters {
    let costCenters: CostCenter[] = [];
 
    if (data.data?.length) {
      costCenters = data.data.map(cc => ({
        id: cc.itemId,
        costCenterId: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'costCenterId'),
        name: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'name'),
        active: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'active'),
        costCenterOwner: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'costCenterOwner'),
        budget: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'budget'),
        budgetPeriod: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'budgetPeriod'),
        pendingOrders: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'pendingOrders'),
        approvedOrders: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'approvedOrders'),
        spentBudget: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'spentBudget'),
        remainingBudget: AttributeHelper.getAttributeValueByAttributeName(cc.attributes, 'remainingBudget'),
      }));
    }
 
    return {
      costCenters,
      paging: data.info,
    };
  }
 
  static fromData(data: CostCenterData): CostCenter {
    if (data) {
      return {
        ...data,
        orders: data.orders?.map(order => ({
          documentNo: order.orderNo,
          status: order.orderStatus,
          creationDate: order.orderDate.toString(),
          user: {
            firstName: AttributeHelper.getAttributeValueByAttributeName(order.buyer?.attributes, 'firstName'),
            lastName: AttributeHelper.getAttributeValueByAttributeName(order.buyer?.attributes, 'lastName'),
          },
          totalProductQuantity: order.items,
          totals: {
            total: {
              type: 'PriceItem',
              gross: order.orderTotalGross.value,
              net: order.orderTotalNet.value,
              currency: order.orderTotalGross.currency,
            },
            itemTotal: undefined,
            isEstimated: false,
          },
        })),
      };
    } else E{
      throw new Error(`'costCenterData' is required for the mapping`);
    }
  }
}