All files / middlewares curvesValidation.js

100% Statements 21/21
100% Branches 20/20
100% Functions 3/3
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                  6x   6x 2x   4x       4x 2x                 2x   2x 1x         8x   8x 1x     7x 7x 2x               5x     5x 2x               3x 3x 2x                            
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
import CurveInformation from '../source/curves/information';
 
import ProductsBranchInformation from '../source/productBranches/information';
import StoraAddressSizeInformation from '../source/storageAddressSizes/information';
import StorageAddressInformation from '../source/storageAddresses/information';
 
async function validCurveCode(req) {
  const code = req.body.code || req.query.code;
 
  if (!code) {
    throw new APIError('INVALID_CODE', t('BEE3430' /* Código inválido */));
  } else {
    const existCode = await CurveInformation.getCurve({
      code,
    });
 
    if (existCode) {
      throw new APIError(
        'CODE_EXISTS',
        t('BEE262' /* O código informado já existe! */)
      );
    }
  }
}
 
function validCurveName(req) {
  const name = req.body.name || req.query.name;
 
  if (!name) {
    throw new APIError('INVALID_NAME', t('BEE3562' /* Nome inválido */));
  }
}
 
async function existCurveInTables(req) {
  const code = req.body.code || req.query.code;
 
  if (!code) {
    throw new APIError('', t('BEE2479' /* Código de curva inexistente! */));
  }
 
  const existProduct = await ProductsBranchInformation.getCurvesInTable(code);
  if (existProduct) {
    throw new APIError(
      '',
      t(
        'BEE2480' /* Impossível excluir uma curva que tenha um ou mais produtos vinculados! */
      )
    );
  }
 
  const existAddressSize = await StoraAddressSizeInformation.getCurvesInTable(
    code
  );
  if (existAddressSize) {
    throw new APIError(
      '',
      t(
        'BEE2481' /* Impossível excluir uma curva que tenha um ou mais tamanho de endereços vinculados! */
      )
    );
  }
 
  const existAdress = await StorageAddressInformation.getCurvesInTable(code);
  if (existAdress) {
    throw new APIError(
      '',
      t(
        'BEE2482' /* Impossível excluir uma curva que tenha um ou endereços vinculados! */
      )
    );
  }
}
 
export default {
  validCurveCode,
  validCurveName,
  existCurveInTables,
};