All files / middlewares materialFamilyValidations.js

100% Statements 26/26
100% Branches 26/26
100% Functions 5/5
100% Lines 26/26

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                3x   3x 1x           2x       2x 1x                     3x   3x 1x           2x       2x 1x                 2x   2x 1x               3x   3x 1x           2x       2x 1x               2x   2x       2x       2x 1x                                
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
import MaterialFamilyInformation from '../source/materialsFamily/information';
import UnitMeasureInformation from '../source/unitsMeasure/information';
import ProductInformation from '../source/products/information';
 
async function validMaterialFamilyId(req) {
  const materialFamilyId =
    req.body.materialFamilyId || req.query.materialFamilyId;
 
  if (!materialFamilyId) {
    throw new APIError(
      t('BEE3085' /* ID não pode ser vazio! */),
      'INVALID_MATERIAL_FAMILY_ID'
    );
  } else {
    const existMaterialFamily =
      await MaterialFamilyInformation.getMaterialFamily({
        id: materialFamilyId,
      });
 
    if (!existMaterialFamily) {
      throw new APIError(
        t(
          'BEE1210',
          { 0: materialFamilyId } /* Família material %{0} não localizado ! */
        ),
        'NOT_EXIST_MATERIAL_FAMILY_ID'
      );
    }
  }
}
async function validCode(req) {
  const code = req.body.code || req.query.code;
 
  if (!code) {
    throw new APIError(
      t('BEE1171' /* Código deve ser informado ! */),
      'INVALID_MATERIAL_FAMILY_CODE'
    );
  } else {
    const existMaterialFamily =
      await MaterialFamilyInformation.getMaterialFamily({
        code,
      });
 
    if (existMaterialFamily) {
      throw new APIError(
        t('BEE1173', { 0: code } /* Codigo %{0} já cadastrado  */),
        'EXIST_MATERIAL_FAMILY_CODE'
      );
    }
  }
}
 
function validMaterialFamilyName(req) {
  const name = req.body.name || req.query.name;
 
  if (!name) {
    throw new APIError(
      t('BEE1234' /* Nome não pode ser vazio ! */),
      'INVALID_MATERIAL_FAMILY_NAME'
    );
  }
}
 
async function validUnitMeasure(req) {
  const unitMeasure = req.body.unitMeasureCode || req.query.unitMeasureCode;
 
  if (!unitMeasure) {
    throw new APIError(
      t('BEE1235' /* Selecione uma unidade de medida ! */),
      'INVALID_MATERIAL_FAMILY_UNIT_MEASURE'
    );
  }
 
  const existUnitMeasure = await UnitMeasureInformation.getUnitMeasure({
    code: unitMeasure,
  });
 
  if (!existUnitMeasure)
    throw new APIError(
      '',
      t('BEE1211', { 0: unitMeasure } /* Unidade de medida %{0} inválida ! */)
    );
}
 
async function validDeleteHasAssociatedProduct(req) {
  const materialFamilyId =
    req.body.materialFamilyId || req.query.materialFamilyId;
 
  const materialFamilyData = await MaterialFamilyInformation.getMaterialFamily({
    id: materialFamilyId,
  });
 
  const existProduct = await ProductInformation.getProduct({
    materialFamily: materialFamilyData.code,
  });
 
  if (existProduct) {
    throw new APIError(
      '',
      t(
        'BEE4249' /* Não é possível excluir esta família de materiais, pois ela está vinculada a outros registros no sistema */
      )
    );
  }
}
 
export default {
  validMaterialFamilyId,
  validCode,
  validMaterialFamilyName,
  validUnitMeasure,
  validDeleteHasAssociatedProduct,
};