All files / middlewares typeOrderValidations.js

100% Statements 23/23
100% Branches 19/19
100% Functions 6/6
100% Lines 23/23

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                2x   2x         2x         2x 1x                         2x   2x 1x               2x   2x 1x               2x   2x 1x               3x   3x   2x           2x 1x                     2x   2x           2x 1x                                          
import { Op } from 'sequelize';
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
import inboundOrdersInformation from '../source/inboundOrders/information';
import OutboundOrdersInformation from '../source/outboundOrders/information';
import TypeOrderInformation from '../source/typeOrders/information';
 
async function validOrder(req) {
  const { type, branchCode } = req.body.order;
 
  const outboundOrder = await OutboundOrdersInformation.getOutboundOrder({
    orderType: type,
    branchCode,
  });
 
  const inboundOrder = await inboundOrdersInformation.getInboundOrder({
    orderType: type,
    branchCode,
  });
 
  if (outboundOrder || inboundOrder) {
    throw new APIError(
      'EXIST_TYPE_CODE_ORDER_USED',
      t(
        'BEE2811',
        {
          0: type,
        } /* Documentos de Entrada e/ou Saída estão usando o Tipo de Documento %{0} */
      )
    );
  }
}
 
async function validType(req) {
  const { type } = req.body;
 
  if (!type) {
    throw new APIError(
      'INVALID_TYPE_CODE',
      t('BEE2810' /* Tipo deve ser informado ! */)
    );
  }
}
 
async function validCode(req) {
  const { branchCode } = req.body;
 
  if (!branchCode) {
    throw new APIError(
      'INVALID_BRANCH_CODE',
      t('BEE1171' /* Código deve ser informado ! */)
    );
  }
}
 
async function validDesc(req) {
  const { name } = req.body;
 
  if (!name) {
    throw new APIError(
      'INVALID_DESCRIPTION_NAME',
      t('BEE2812' /* Descrição deve ser informada ! */)
    );
  }
}
 
async function validRequestManual(req) {
  const { branchCode, usesRequestManual, id } = req.body;
 
  if (usesRequestManual) {
    const existTypeOrderRequestManual =
      await TypeOrderInformation.getTypeOrderValid({
        branchCode,
        usesRequestManual: true,
        [Op.and]: [id && { id: { [Op.ne]: id } }],
      });
 
    if (existTypeOrderRequestManual) {
      throw new APIError(
        'INVALID_REQUEST_MANUAL_MODE',
        t(
          'BEE2975' /* A Filial tem um Tipo de Documento com o parâmetro "Requisição Manual" ativo. */
        )
      );
    }
  }
}
 
async function validTypeExist(req) {
  const { branchCode, type, id = null } = req.body;
 
  const existTypeOrder = await TypeOrderInformation.getTypeOrder({
    branchCode,
    type,
    id: { [Op.ne]: id },
  });
 
  if (existTypeOrder) {
    throw new APIError(
      'EXISTS_TYPE_ORDER',
      t(
        'BEE2884',
        {
          0: type,
          1: branchCode,
        } /* O tipo %{0} já está cadastrada para a filial %{1}! */
      )
    );
  }
}
 
export default {
  validOrder,
  validType,
  validCode,
  validDesc,
  validTypeExist,
  validRequestManual,
};