All files / source/reports storageAddress.js

95.83% Statements 23/24
75% Branches 15/20
100% Functions 6/6
95% Lines 19/20

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                            4x 12x   4x                                                 12x 4x 12x   4x 4x 4x   4x 4x 12x   12x 12x 6x   12x 2x       12x                       4x      
import moment from 'moment-timezone';
import { models } from '../../../config/database';
import StorageAddressDao from '../../database/dao/storageAddress';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
import NumberFormatter from '../../helpers/number/format';
 
// Funcao criada para popular o array headers e rows enviados por parametro
export default async function generateData(
  columns,
  filters,
  headers = [],
  rows = [],
  opts = {}
) {
  const where = mountWhereBasedFilter(filters);
  const headersValue = columns.map((it) => it.value);
 
  const dados = await StorageAddressDao.findStorageAddresses(where, {
    attributes: headersValue,
    include: [
      {
        required: false,
        model: models.Branch,
        as: 'branch',
        attributes: ['code', 'name'],
      },
      {
        required: true,
        model: models.User,
        as: 'userCreated',
        attributes: ['login'],
      },
      {
        required: true,
        model: models.User,
        as: 'userUpdated',
        attributes: ['login'],
      },
    ],
    ...opts,
  });
 
  const headersValueRes = columns.map((it) => it.value);
  columns.forEach((it) => {
    headers.push(it.label);
  });
  dados.forEach((it) => {
    it.createdUser = it.userCreated.login;
    it.updatedUser = it.userUpdated.login;
 
    const row = [];
    for (let index = 0; index < headersValueRes.length; index += 1) {
      const column = headersValueRes[index];
 
      row.push(column.split('.').reduce((o, i) => o[i] || '', it));
      if (column === 'createdAt' || column === 'updatedAt') {
        row[index] = moment(row[index]).format('L LTS');
      }
      if (column === 'branchCode') {
        row[
          index
        ] = `${it.branch.dataValues.code} - ${it.branch.dataValues.name}`;
      }
      Iif (
        (column === 'size' ||
          column === 'height' ||
          column === 'width' ||
          column === 'length' ||
          column === 'capacityVolume' ||
          column === 'capacityWeight') &&
        row[index] !== null
      ) {
        row[index] = NumberFormatter.format(row[index], 3);
      }
    }
    rows.push(row);
  });
}