All files / source/reports carrier.js

100% Statements 17/17
57.14% Branches 4/7
100% Functions 4/4
100% Lines 14/14

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              3x 18x 3x                               18x   3x   3x 3x 3x 3x 18x   18x     18x 6x     3x    
import CarrierDao from '../../database/dao/carrier';
import moment from 'moment-timezone';
import { models } from '../../../config/database';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
 
// 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 CarrierDao.findCarriers(where, { 
    attributes: headersValue,
    include: [{
      required: false,
      model: models.User,
      as: 'userCreated',
      attributes: ['login'],
    }, {
      required: false,
      model: models.User,
      as: 'userUpdated',
      attributes: ['login'],
    }],
    ...opts
  });
 
  columns.forEach(it => { headers.push(it.label); });
 
  dados.forEach(it => {
   // it.main = (it.main) ? I18n.t('BEE172' /* Sim */) : I18n.t('BEE173' /* Não */)
    it.createdUser = it.userCreated.dataValues.login
    it.updatedUser = it.userUpdated.dataValues.login
    const row = [];
    for (let index = 0; index < headersValue.length; index++) {
      const column = headersValue[index];
 
      row.push(it[column]);
 
      // Traduz o tempo do banco para um formato mais bacana para o usuário (moment.js)
      if(column === 'createdAt' || column === 'updatedAt') {
        row[index] = moment(row[index]).format('l LTS')
      }
    }
    rows.push(row);
  });
};