All files / source/productionOrders information.js

100% Statements 21/21
100% Branches 21/21
100% Functions 4/4
100% Lines 21/21

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 167 168 169 170 171 172 173                          7x           7x     6x   2x         4x           7x 5x   2x                   2x     1x                   1x     1x         1x     1x     1x           7x                                                                                       7x       6x                                 6x       1x                           1x                
import Sequelize, { Op } from 'sequelize';
import ProductionOrderDao from '../../database/dao/productionOrder';
import { models, sequelize } from '../../../config/database';
 
async function getAllProductionOrders(
  startDateFormat,
  endDateFormat,
  page = 0,
  pageSize = 10,
  sorted = [],
  filtered = [],
  opts = {}
) {
  const query = {
    [Op.and]: [
      { createdAt: { [Op.between]: [startDateFormat, endDateFormat] } },
    ],
  };
 
  const sortParams = sorted.length
    ? {
        order: sorted.map((it) => {
          switch (it.id) {
            case 'product.name':
              return [
                Sequelize.col('product.str_name'),
                it.desc ? 'DESC' : 'ASC',
              ];
            default:
              return [it.id, it.desc ? 'DESC' : 'ASC'];
          }
        }),
      }
    : {};
 
  for (const element of filtered) {
    switch (element.id) {
      case 'createdAt': {
        query[Op.and].push(
          sequelize.where(
            Sequelize.fn(
              'DATE_FORMAT',
              Sequelize.col('ProductionOrders.dte_created'),
              '%d/%m/%Y %H:%i:%s'
            ),
            { [Op.like]: `%${element.value}%` }
          )
        );
        break;
      }
      case 'expirationDate': {
        query[Op.and].push(
          sequelize.where(
            Sequelize.fn(
              'DATE_FORMAT',
              Sequelize.col('ProductionOrders.dte_expiration'),
              '%d/%m/%Y %H:%i:%s'
            ),
            { [Op.like]: `%${element.value}%` }
          )
        );
        break;
      }
      case 'product.name': {
        query[Op.and].push(
          sequelize.where(Sequelize.col('product.str_name'), {
            [Op.like]: `%${element.value}%`,
          })
        );
        break;
      }
      default: {
        query[Op.and].push({
          [element.id]: { [Op.like]: `%${element.value}%` },
        });
        break;
      }
    }
  }
 
  const productionOrder =
    await ProductionOrderDao.findAndCountAllProductionOrder(query, {
      include: [
        {
          required: true,
          model: models.Product,
          as: 'product',
          attributes: ['name', 'fullName', 'unitMeasure'],
        },
        {
          required: false,
          model: models.UserTimeTracking,
          as: 'productionOrderTracking',
          where: {
            status: 2,
          },
          attributes: ['totalTime'],
        },
        {
          required: false,
          model: models.ProductionOrderReserve,
          as: 'productionOrderReserve',
          attributes: [
            'productCode',
            'lineNumber',
            'quantity',
            'reserved',
            'lotNumber',
          ],
          include: [
            {
              required: false,
              model: models.Product,
              as: 'product',
              attributes: ['name', 'unitMeasure'],
            },
          ],
        },
      ],
      offset: page * pageSize,
      limit: pageSize,
      ...sortParams,
      ...opts,
    });
 
  return productionOrder;
}
 
async function getProductionOrder(query = {}, opts = {}) {
  const productionOrder = await ProductionOrderDao.findProductionOrder(query, {
    include: [
      {
        required: true,
        model: models.Product,
        as: 'product',
        attributes: ['name', 'fullName', 'controlExpirationDate'],
      },
      {
        required: false,
        model: models.SerialGroup,
        as: 'serialGroup',
        attributes: ['id'],
      },
    ],
    ...opts,
  });
  return productionOrder;
}
 
async function countProductionOrder(query = {}, opts = {}) {
  const productionOrder = await ProductionOrderDao.findProductionOrder(query, {
    attributes: [
      [sequelize.fn('COUNT', sequelize.col('ProductionOrders.id')), 'total'],
    ],
    include: [
      {
        required: true,
        model: models.Product,
        as: 'product',
        attributes: [],
      },
    ],
    ...opts,
  });
  return productionOrder.dataValues.total;
}
 
export default {
  getAllProductionOrders,
  getProductionOrder,
  countProductionOrder,
};