All files / source/productsGrouping information.js

73.33% Statements 11/15
85.71% Branches 12/14
66.66% Functions 4/6
73.33% Lines 11/15

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        3x 3x     5x         5x                   3x       1x 1x                                 1x               3x             2x           3x                                                        
import Sequelize from 'sequelize';
import { models } from '../../../config/database';
import ProductGroupingDao from '../../database/dao/productGrouping';
 
const { Op } = Sequelize;
const ProductGroupingModel = models.ProductGrouping;
 
async function getProductGrouping(query, opts = {}) {
  const product = await ProductGroupingDao.findProductGrouping(query, {
    attributes: ['id', 'code', 'name', 'description', 'sequence'],
    ...opts,
  });
 
  return product;
}
 
async function getProductsGroupingList(
  page = 0,
  pageSize = 0,
  sorted = [],
  filtered = [],
  opts = {}
) {
  const productsGroupingQuery = {
    ...(filtered.length
      ? filtered.reduce(
          (acum, it) => {
            try {
              acum[Op.and].push(
                Sequelize.where(
                  Sequelize.cast(
                    Sequelize.col(
                      `${ProductGroupingModel.name}.${
                        ProductGroupingModel.rawAttributes[it.id].field
                      }`
                    ),
                    'char'
                  ),
                  { [Op.like]: `%${it.value}%` }
                )
              );
            } catch (e) {
              // empty
            }
 
            return acum;
          },
          { [Op.and]: [] }
        )
      : {}),
  };
 
  const productsGrouping =
    await ProductGroupingDao.findAndCountProductsGrouping(
      productsGroupingQuery,
      {
        attributes: ['id', 'code', 'name', 'description', 'sequence'],
        offset: page * pageSize,
        limit: pageSize,
        ...(sorted.length
          ? { order: sorted.map((it) => [it.id, it.desc ? 'DESC' : 'ASC']) }
          : {}),
        ...opts,
      }
    );
 
  return productsGrouping;
}
 
async function getOptionsProductsGrouping(query = {}, opts = {}) {
  const productsGrouping = await ProductGroupingDao.findProductsGrouping(
    query,
    {
      attributes: ['id', 'code', 'name'],
      raw: true,
      ...opts,
    }
  );
 
  const result = productsGrouping.map((productGrouping) => {
    return {
      value: productGrouping.code,
      label: productGrouping.name,
    };
  });
 
  return result;
}
 
export default {
  getProductGrouping,
  getProductsGroupingList,
  getOptionsProductsGrouping,
};