All files / middlewares stockGroupValidations.js

100% Statements 22/22
100% Branches 18/18
100% Functions 6/6
100% Lines 22/22

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            2x   2x 1x               2x   2x 1x               2x   2x   2x 1x                           2x   2x 1x                     2x   2x       2x 1x                     2x   2x       2x       2x 1x                                  
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
import StockGroupInformation from '../source/stockGroup/information';
import ProductInformation from '../source/products/information';
 
async function validCode(req) {
  const { code } = req.body;
 
  if (!code) {
    throw new APIError(
      '',
      t('BEE3740', { 0: t('BEE82' /* Código */) } /* %{0} deve ser informado */)
    );
  }
}
 
async function validStockGroupName(req) {
  const { name } = req.body;
 
  if (!name) {
    throw new APIError(
      '',
      t('BEE3740', { 0: t('BEE83' /* Nome */) } /* %{0} deve ser informado */)
    );
  }
}
 
async function validStockGroupExists(req) {
  const { code } = req.body;
 
  const stockGroup = await StockGroupInformation.getStockGroup({ code });
 
  if (stockGroup) {
    throw new APIError(
      '',
      t(
        'BEE4026',
        {
          0: t('BEE4009' /* Grupo de Estoque */),
          1: code,
        } /* %{0} %{1} já cadastrado */
      )
    );
  }
}
 
async function validStockGroupId(req) {
  const stockGroupId = req.body.stockGroupId || req.query.stockGroupId;
 
  if (!stockGroupId) {
    throw new APIError(
      '',
      t(
        'BEE4031',
        { 0: t('BEE4009' /* Grupo de Estoque */) } /* %{0} não encontrado */
      )
    );
  }
}
 
async function validStockGroupIdExists(req) {
  const stockGroupId = req.body.stockGroupId || req.query.stockGroupId;
 
  const stockGroup = await StockGroupInformation.getStockGroup({
    id: stockGroupId,
  });
 
  if (!stockGroup) {
    throw new APIError(
      '',
      t(
        'BEE4031',
        { 0: t('BEE4009' /* Grupo de Estoque */) } /* %{0} não encontrado */
      )
    );
  }
}
 
async function validDeleteHasAssociatedProduct(req) {
  const stockGroupId = req.body.stockGroupId || req.query.stockGroupId;
 
  const stockGroup = await StockGroupInformation.getStockGroup({
    id: stockGroupId,
  });
 
  const existProduct = await ProductInformation.getProduct({
    stockGroup: stockGroup.code,
  });
 
  if (existProduct) {
    throw new APIError(
      '',
      t(
        'BEE4251' /* Não é possível excluir este grupo de estoque, pois ele está vinculado a outros registros no sistema */
      )
    );
  }
}
 
export default {
  validCode,
  validStockGroupName,
  validStockGroupExists,
  validStockGroupId,
  validStockGroupIdExists,
  validDeleteHasAssociatedProduct,
};