All files / middlewares storageAddressTypeValidations.js

100% Statements 27/27
100% Branches 32/32
100% Functions 6/6
100% Lines 27/27

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            3x   3x 1x     2x       2x 1x                       2x   2x 1x         3x   3x 1x   2x 1x         2x   2x 1x               3x   3x 1x     2x 2x 1x               2x   2x       2x 1x                                  
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
import StorageAddressTypeInformation from '../source/storageAddressTypes/information';
import AddressInformation from '../source/storageAddresses/information';
 
async function validStorageAddressTypeId(req) {
  const addressTypeId = req.body.addressTypeId || req.query.addressTypeId;
 
  if (!addressTypeId) {
    throw new APIError('INVALID_WAREHOUSE_ID');
  } else {
    const existWarehouse =
      await StorageAddressTypeInformation.getStorageAddressType({
        id: addressTypeId,
      });
 
    if (!existWarehouse) {
      throw new APIError(
        'NOT_EXIST_STORAGE_ADDRESS_TYPE_ID',
        t(
          'BEE1243',
          { 0: addressTypeId } /* Tipo endereço %{0} não localizada */
        )
      );
    }
  }
}
 
function validStorageAddressTypeName(req) {
  const name = req.body.name || req.query.name;
 
  if (!name) {
    throw new APIError('INVALID_STORAGE_ADDRESS_TYPE_NAME');
  }
}
 
function validStorageAddressTypeStatus(req) {
  const status = req.body.status || req.query.status;
 
  if (!status) {
    throw new APIError('INVALID_STORAGE_ADDRESS_TYPE_STATUS');
  }
  if (status < 1 || status > 10) {
    throw new APIError('INVALID_STORAGE_ADDRESS_TYPE_STATUS');
  }
}
 
async function validLimitCharacters(req) {
  const code = req.body.code || req.query.code;
 
  if (code.length > 10) {
    throw new APIError(
      '',
      t('BEE3330' /* Código excede o limite de 10 caracteres. */)
    );
  }
}
 
async function validStorageAddressTypeCode(req) {
  const code = req.body.code || req.query.code;
 
  if (!code) {
    throw new APIError('', t('BEE1746' /* Código não preenchido */));
  } else {
    const storageAddressType =
      await StorageAddressTypeInformation.getStorageAddressType({ code });
    if (storageAddressType)
      throw new APIError(
        '',
        t('BEE1992' /* O código informado já tem cadastro */)
      );
  }
}
 
async function validDeleteHasAssociatedAddress(req) {
  const addressTypeId = req.body.addressTypeId || req.query.addressTypeId;
 
  const addressData = await AddressInformation.getStorageAddress({
    type: addressTypeId,
  });
 
  if (addressData) {
    throw new APIError(
      '',
      t(
        'BEE4237' /* Não é possível excluir este tipo de endereço, pois ele está vinculado a outros registros no sistema */
      )
    );
  }
}
 
export default {
  validStorageAddressTypeId,
  validStorageAddressTypeName,
  validStorageAddressTypeStatus,
  validStorageAddressTypeCode,
  validLimitCharacters,
  validDeleteHasAssociatedAddress,
};