All files / source/reports branch.js

89.65% Statements 26/29
52.94% Branches 9/17
100% Functions 6/6
87.5% Lines 21/24

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          61x         4x 28x 4x 4x 4x     4x 4x           4x                                           28x 28x 4x 4x 4x 4x 4x 28x   32x 28x 8x     4x    
import Sequelize from 'sequelize';
import moment from 'moment-timezone';
import BranchDao from '../../database/dao/branch';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
import { models } from '../../../config/database';
const Op = Sequelize.Op;
 
// 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);
  var headersValue = columns.map(it => it.value);
  const index = headersValue.indexOf('company.companyName');
  Eif (index != -1) {
    headersValue.splice(index, 1);
  }
 
  let whereCompany = null;
  Iif (where[Op.and] && where[Op.and]['companyName']) {
    const companyName = where[Op.and]['companyName'];
    delete where[Op.and]['companyName'];
    whereCompany = { [Op.and]: [{ companyName }] };
  }
 
  const dados = await BranchDao.findBranches(where, {
    attributes: headersValue,
    include: [{
      required: whereCompany ? true : false,
      model: models.Company,
      as: 'company',
      attributes: ['companyName'],
      where: whereCompany
    }, {
      required: false,
      model: models.User,
      as: 'userCreated',
      attributes: ['login'],
    }, {
      required: false,
      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.dataValues.login
    it.updatedUser = it.userUpdated.dataValues.login
    const row = [];
    for (let index = 0; index < headersValueRes.length; index++) {
      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')
      }
    }
    rows.push(row);
  });
}