All files / helpers/reports sequelize.js

59.37% Statements 38/64
43.66% Branches 31/71
100% Functions 4/4
59.64% Lines 34/57

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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166          99x       5x   5x                             5x           1x     4x       5x 5x   5x 5x       4x       93x 93x   93x 93x 32x 32x     32x 32x 32x   5x   5x 3x   3x                               3x                         3x     3x 2x 2x   2x                               2x                         2x     2x                                                   93x    
import Sequelize from 'sequelize';
import _ from 'lodash';
import moment from 'moment-timezone';
import db from '../../database/dao/index';
 
const { Op } = Sequelize;
 
function checkAndParseDate(value) {
  // Validando o padrĂ£o de data ISO8601, YYYY-MM-DDTHH:mm:ss
  const ISO_8601 = `${moment.HTML5_FMT.DATETIME_LOCAL_SECONDS}Z`;
 
  Iif (
    value instanceof Array &&
    value.length &&
    value.length <= 2 &&
    (moment.isMoment(value[0]) ||
      moment.isDate(value[0]) ||
      moment(value[0], ISO_8601, true).isValid())
  ) {
    for (let index = 0; index < value.length; index++) {
      if (index === 0)
        value[index] = moment.utc(value[index]).startOf('day').format();
      else value[index] = moment.utc(value[index]).endOf('day').format();
    }
    return value;
  }
  if (
    !(value instanceof Array) &&
    (moment.isMoment(value) ||
      moment.isDate(value) ||
      moment(value, ISO_8601, true).isValid())
  ) {
    return moment.utc(value).startOf('day').format();
  }
 
  return false;
}
 
function parseValuesBasedType(value) {
  let retParsedValue = null;
  try {
    // DATE
    retParsedValue = checkAndParseDate(value);
    if (retParsedValue) return retParsedValue;
    // END DATE
  } catch (e) {}
 
  return value;
}
 
export function mountWhereBasedFilter(filters = {}) {
  const where = {};
  let hasFilter = false;
 
  try {
    _.forEach(filters, (value, key) => {
      const hasIntance = typeof value.instance === 'string';
      const instance = hasIntance
        ? db[value.instance]
        : { sequelize: Sequelize };
      const as = typeof value.as === 'string' ? value.as : '';
      Iif (!key) return;
      if (value.value === '' || value.value === undefined) return;
 
      key = key.replace(/[$]/g, '');
 
      if (value.type == 'and') {
        if (!where[Op.and]) where[Op.and] = [];
 
        Iif (value.functionOnColumn) {
          const field = as
            ? `${as}.${instance.rawAttributes[key].field}`
            : `${instance.name ? `${instance.name}.` : ''}${
                instance.rawAttributes[key].field
              }`;
          const newOp = Op[value.functionOnColumn.type];
          where[Op.and].push(
            Sequelize.where(
              Sequelize.fn(
                value.functionOnColumn.name,
                instance.sequelize.col(field)
              ),
              { [newOp]: parseValuesBasedType(value.value) }
            )
          );
        } else Iif (hasIntance) {
          const field = as
            ? `${as}.${instance.rawAttributes[key].field}`
            : `${instance.name ? `${instance.name}.` : ''}${
                instance.rawAttributes[key].field
              }`;
          where[Op.and].push(
            Sequelize.where(
              instance.sequelize.col(field),
              parseValuesBasedType(value.value)
            )
          );
        } else {
          where[Op.and].push({ [key]: parseValuesBasedType(value.value) });
        }
 
        hasFilter = true;
      } else if (value.type == 'or') {
        if (!where[Op.or]) where[Op.or] = [];
 
        Iif (value.functionOnColumn) {
          const field = as
            ? `${as}.${instance.rawAttributes[key].field}`
            : `${instance.name ? `${instance.name}.` : ''}${
                instance.rawAttributes[key].field
              }`;
          const newOp = Op[value.functionOnColumn.type];
          where[Op.and].push(
            Sequelize.where(
              Sequelize.fn(
                value.functionOnColumn.name,
                instance.sequelize.col(field)
              ),
              { [newOp]: parseValuesBasedType(value.value) }
            )
          );
        } else Iif (hasIntance) {
          const field = as
            ? `${as}.${instance.rawAttributes[key].field}`
            : `${instance.name ? `${instance.name}.` : ''}${
                instance.rawAttributes[key].field
              }`;
          where[Op.or].push(
            Sequelize.where(
              instance.sequelize.col(field),
              parseValuesBasedType(value.value)
            )
          );
        } else {
          where[Op.or].push({ [key]: parseValuesBasedType(value.value) });
        }
 
        hasFilter = true;
      } else E{
        const newOp = Op[value.type];
 
        if (hasIntance) {
          const field = as
            ? `${as}.${instance.rawAttributes[key].field}`
            : `${instance.name ? `${instance.name}.` : ''}${
                instance.rawAttributes[key].field
              }`;
          if (!where[Op.and]) where[Op.and] = [];
          where[Op.and].push(
            Sequelize.where(instance.sequelize.col(field), {
              [newOp]: parseValuesBasedType(value.value),
            })
          );
        } else {
          where[key] = { [newOp]: parseValuesBasedType(value.value) };
        }
        hasFilter = true;
      }
    });
  } catch (e) {
    console.log(e);
  }
 
  return hasFilter ? { [Op.and]: where } : {};
}