All files / middlewares rangesValidations.js

100% Statements 36/36
100% Branches 40/40
100% Functions 7/7
100% Lines 36/36

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              3x   3x 1x   2x             2x 1x               3x   3x 1x         2x       2x 1x                 2x   2x 1x         3x   3x 2x             2x 1x               3x   3x 2x             2x 1x                 3x   3x 1x   2x             2x 1x                 3x   3x 2x       2x 1x                                      
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
import RangeInformation from '../source/ranges/information';
import BranchInformation from '../source/branches/information';
import AddressInformation from '../source/storageAddresses/information';
 
async function validRangeId(req) {
  const rangeId = req.body.rangeId || req.query.rangeId;
 
  if (!rangeId) {
    throw new APIError('INVALID_ID', t('BEE3421' /* ID inválido */));
  } else {
    const existRange = await RangeInformation.getRange(
      {
        id: rangeId,
      },
      { userBranches: req.userBranches }
    );
 
    if (!existRange) {
      throw new APIError(
        'NOT_EXIST_ID',
        t('BEE1236', { 0: rangeId } /* Armazém %{0} não localizada */)
      );
    }
  }
}
async function validCode(req) {
  const code = req.body.code || req.query.code;
 
  if (!code) {
    throw new APIError(
      'INVALID_RANGE_CODE',
      t('BEE3430' /* Código inválido */)
    );
  } else {
    const existRange = await RangeInformation.getRange({
      code,
    });
 
    if (existRange) {
      throw new APIError(
        'EXIST_CODE',
        t('BEE1173', { 0: code } /* Codigo %{0} já cadastrado  */)
      );
    }
  }
}
 
function validRangeName(req) {
  const name = req.body.name || req.query.name;
 
  if (!name) {
    throw new APIError('INVALID_NAME', t('BEE3562' /* Nome inválido */));
  }
}
 
async function validBranch(req) {
  const branch = req.body.branchCode || req.query.branchCode;
 
  if (branch) {
    const existBranch = await BranchInformation.getBranch(
      {
        code: branch,
      },
      { userBranches: req.userBranches }
    );
 
    if (!existBranch)
      throw new APIError(
        '',
        t('BEE3575', { 0: branch } /* Filial %{0} inválida! */)
      );
  }
}
 
async function validRangeCode(req) {
  const rangeCode = req.body.rangeCode || req.query.rangeCode;
 
  if (rangeCode !== undefined) {
    const existRange = await RangeInformation.getRange(
      {
        code: rangeCode,
      },
      { userBranches: req.userBranches }
    );
 
    if (!existRange) {
      throw new APIError(
        'INEXIST_RANGE_CODE',
        t('BEE1192', { 0: rangeCode } /* Código %{0} não cadastrado */)
      );
    }
  }
}
 
async function validRangeCode2(req) {
  const rangeCode = req.body.rangeCode || req.query.rangeCode;
 
  if (!rangeCode) {
    throw new APIError('INVALID_CODE', t('BEE3430' /* Código inválido */));
  } else {
    const existRange = await RangeInformation.getRange(
      {
        code: rangeCode,
      },
      { userBranches: req.userBranches }
    );
 
    if (!existRange) {
      throw new APIError(
        'THE_CODE_DOES_NOT_EXIST',
        t('BEE1774' /* código do Range não localizado */)
      );
    }
  }
}
 
async function validDeleteHasAssociatedAddress(req) {
  const rangeCode = req.body.rangeCode || req.query.rangeCode;
 
  if (rangeCode) {
    const addressData = await AddressInformation.getStorageAddress({
      rangeCode,
    });
 
    if (addressData) {
      throw new APIError(
        '',
        t(
          'BEE4231' /* Não é possível excluir este range, pois ele possui endereços vinculados */
        )
      );
    }
  }
}
 
export default {
  validRangeId,
  validCode,
  validRangeName,
  validBranch,
  validRangeCode,
  validRangeCode2,
  validDeleteHasAssociatedAddress,
};