All files / helpers/sequelize filteredQuery.js

78.72% Statements 37/47
49.01% Branches 25/51
100% Functions 2/2
80.95% Lines 34/42

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        3x     13x 13x   13x 13x 19x 19x 19x 19x 19x   18x   18x 4x   4x 2x     2x 2x               2x 2x     1x                   3x 14x 3x   3x                         3x 3x     3x                   3x   11x   11x                       11x   11x     1x   13x  
import Sequelize from 'sequelize';
import _ from 'lodash';
import db from '../../database/dao/index';
 
const Op = Sequelize.Op;
 
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 === '') return;
 
      key = key.replace(/[$]/g, '');
      
      if (value.type == 'and') {
        Eif (!where[Op.and]) where[Op.and] = [];
  
        if (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]: value.value },
            )
          );
        } else if (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),
              value.value
            )
          );
        } else E{
          where[Op.and].push({ [key]: value.value });
        }
  
        hasFilter = true;
      } else if (value.type == 'or') {
        Eif (!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]: value.value },
            )
          );
        } else if (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),
              value.value
            )
          );
        } else E{
          where[Op.or].push({ [key]: value.value });
        }
  
        hasFilter = true;
      } else {
        const newOp = Op[value.type];
 
        Iif (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]: value.value },
            )
          );
        } else {
          where[key] = { [newOp]: value.value }
        }
        hasFilter = true;
      }
    });
  } catch (e) { console.log(e) }
 
  return hasFilter ? { [Op.and]: where } : {};
}