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

90% Statements 9/10
66.66% Branches 2/3
100% Functions 4/4
88.88% Lines 8/9

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 598x           8x       2x 1x   2x                               7x 7x   2x                                                    
import { AttributeHelper } from 'ish-core/models/attribute/attribute.helper';
import { Link } from 'ish-core/models/link/link.model';
 
import { CostCenterData } from './cost-center.interface';
import { CostCenter } 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: Link[]): CostCenter[] {
    if (!data?.length) {
      return [];
    }
    return 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'),
    }));
  }
 
  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`);
    }
  }
}