All files / source/reports volumesDocument.js

100% Statements 41/41
81.25% Branches 26/32
100% Functions 7/7
100% Lines 37/37

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                          30x 3x 3x 3x 3x 3x 3x 3x   3x 3x                                                                                         5x   3x 30x   3x 30x     3x 5x       5x       5x       5x     5x 5x 5x         5x 5x   5x 5x 46x   46x   46x 10x   46x 3x     5x         21x 21x 14x      
import moment from 'moment-timezone';
import { models } from '../../../config/database';
import outboundVolumeDao from '../../database/dao/outboundVolume';
import OutboundVolumeStatus from '../../database/enumerators/outboundVolume';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
 
export default async function generateData(
  columns,
  filters,
  headers = [],
  rows = [],
  opts = {}
) {
  let columnsValuesToFind = columns.map((column) => column.value);
  remove(columnsValuesToFind, 'erpOrderCode');
  remove(columnsValuesToFind, 'orderNumber');
  remove(columnsValuesToFind, 'orderType');
  remove(columnsValuesToFind, 'carrierCode');
  remove(columnsValuesToFind, 'carrierName');
  remove(columnsValuesToFind, 'waybills');
  remove(columnsValuesToFind, 'dockCode');
 
  const where = mountWhereBasedFilter(filters);
  const dados = await outboundVolumeDao.findOutboundVolumes(where, {
    attributes: columnsValuesToFind,
    include: [
      {
        required: false,
        model: models.User,
        as: 'userCreated',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.User,
        as: 'userUpdated',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.OutboundOrder,
        as: 'outboundOrder',
        attributes: ['erpOrderCode', 'orderNumber', 'orderType'],
        include: [
          {
            required: false,
            model: models.Carrier,
            as: 'carrier',
            attributes: ['code', 'name'],
          },
          {
            required: false,
            model: models.DockControl,
            as: 'dockControl',
            attributes: ['dockCode'],
          },
          {
            require: false,
            model: models.Waybill,
            as: 'waybill',
            attributes: ['waybillNumber'],
          },
        ],
      },
    ],
    ...opts,
  });
 
  const volumeList = dados.length ? dados.map((o) => o.toJSON()) : [];
 
  columnsValuesToFind = null;
  columnsValuesToFind = columns.map((it) => it.value);
 
  columns.forEach((it) => {
    headers.push(it.label);
  });
 
  volumeList.forEach((it) => {
    it.carrierCode =
      it.outboundOrder.carrier && it.outboundOrder.carrier.code
        ? it.outboundOrder.carrier.code
        : '';
    it.carrierName =
      it.outboundOrder.carrier && it.outboundOrder.carrier.name
        ? it.outboundOrder.carrier.name
        : '';
    it.dockCode =
      it.outboundOrder.dockControl && it.outboundOrder.dockControl.dockCode
        ? it.outboundOrder.dockControl.dockCode
        : '';
    it.erpOrderCode = it.outboundOrder.erpOrderCode
      ? it.outboundOrder.erpOrderCode
      : '';
    it.orderNumber = it.outboundOrder.orderNumber;
    it.orderType = it.outboundOrder.orderType;
    it.waybills =
      it.outboundOrder.waybill && it.outboundOrder.waybill.waybillNumber
        ? it.outboundOrder.waybill.waybillNumber
        : '';
 
    it.createdUser = it.userCreated.login;
    it.updatedUser = it.userUpdated.login;
 
    const row = [];
    for (let index = 0; index < columnsValuesToFind.length; index++) {
      const column = columnsValuesToFind[index];
 
      row.push(it[column]);
 
      if ((column === 'createdAt' || column === 'updatedAt') && row[index]) {
        row[index] = moment(row[index]).format('L LTS');
      }
      if (column === 'status') {
        row[index] = OutboundVolumeStatus.getStatusLabel(it.status);
      }
    }
    rows.push(row);
  });
}
 
function remove(headersValue, value) {
  const index = headersValue.indexOf(value);
  if (index !== -1) {
    headersValue.splice(index, 1);
  }
}