All files / middlewares carrierValidations.js

100% Statements 72/72
100% Branches 89/89
100% Functions 19/19
100% Lines 72/72

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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248                    4x   4x 1x   3x       3x 2x               4x   4x 1x   3x       3x 2x                 2x   2x 1x         5x   5x 2x       3x 2x               2x   2x 1x         6x   6x 2x     4x   4x 2x                 2x   2x 1x         2x   2x 1x         2x   2x 1x         2x   2x 1x         2x   2x 1x         2x   2x 1x         2x   2x 1x         2x   2x 1x         2x   2x 1x         2x   2x 1x         2x   2x 1x         3x   3x       3x       3x 2x                   3x   3x       3x       3x 2x                                                            
import validator from 'validator';
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
import CarrierInformation from '../source/carriers/information';
import validateCNPJ from '../helpers/validators/cnpj';
import validateCPF from '../helpers/validators/cpf';
import InboundOrderInformation from '../source/inboundOrders/information';
import OutboundOrderInformation from '../source/outboundOrders/information';
 
async function validCarrierId(req) {
  const carrierId = req.body.carrierId || req.query.carrierId;
 
  if (!carrierId) {
    throw new APIError('INVALID_CARRIER_ID');
  } else {
    const existCarrier = await CarrierInformation.getCarrier({
      id: carrierId,
    });
 
    if (!existCarrier) {
      throw new APIError(
        'NOT_EXIST_CARRIER_ID',
        t('BEE1181', { 0: carrierId } /* Transportadora %{0} não localizada */)
      );
    }
  }
}
async function validCode(req) {
  const code = req.body.code || req.query.code;
 
  if (!code) {
    throw new APIError('INVALID_CARRIER_CODE');
  } else {
    const existCarrier = await CarrierInformation.getCarrier({
      code,
    });
 
    if (existCarrier) {
      throw new APIError(
        'EXIST_CARRIER_CODE',
        t('BEE1173', { 0: code } /* Codigo %{0} já cadastrado  */)
      );
    }
  }
}
 
function validCarrierName(req) {
  const name = req.body.name || req.query.name;
 
  if (!name) {
    throw new APIError('INVALID_CARRIER_NAME');
  }
}
 
function validMode(req) {
  const mode = req.body.modeTransport || req.query.modeTransport;
 
  if (!mode) {
    throw new APIError(
      '',
      t('BEE1182' /* Via de transporte deve ser informada ! */)
    );
  } else if (mode > 4) {
    throw new APIError(
      '',
      t('BEE1183', { 0: mode } /* Via de transporte %{0} inválida! */)
    );
  }
}
 
function validEmail(req) {
  const email = req.body.email || req.query.email;
 
  if (email && !validator.isEmail(email)) {
    throw new APIError('INVALID_CARRIER_EMAIL');
  }
}
 
function validCnpjOrCpf(req) {
  const cnpj = req.body.cnpj || req.query.cnpj;
 
  if (!cnpj) {
    throw new APIError('', t('BEE1305' /* CNPJ / CPF deve ser informado ! */));
  }
 
  const valid = cnpj && (validateCNPJ(cnpj) || validateCPF(cnpj));
 
  if (!valid) {
    throw new APIError(
      '',
      t('BEE4219' /* O CPF ou CNPJ informado é inválido. */)
    );
  }
}
 
function validStateRegistration(req) {
  const stateRegistration =
    req.body.stateRegistration || req.query.stateRegistration;
 
  if (!stateRegistration) {
    throw new APIError('INVALID_CARRIER_STATE_REGISTRATION');
  }
}
 
function validNote(req) {
  const note = req.body.note || req.query.note;
 
  if (!note) {
    throw new APIError('INVALID_CARRIER_NOTE');
  }
}
 
function validPostalCode(req) {
  const postalCode = req.body.postalCode || req.query.postalCode;
 
  if (!postalCode) {
    throw new APIError('INVALID_CARRIER_POSTAL_CODE');
  }
}
 
function validIbge(req) {
  const ibge = req.body.ibge || req.query.ibge;
 
  if (!ibge) {
    throw new APIError('INVALID_CARRIER_IBGE');
  }
}
 
function validStreet(req) {
  const street = req.body.street || req.query.street;
 
  if (!street) {
    throw new APIError('INVALID_CARRIER_STREET');
  }
}
 
function validNumber(req) {
  const number = req.body.number || req.query.number;
 
  if (!number) {
    throw new APIError('INVALID_CARRIER_NUMBER');
  }
}
 
function validComplement(req) {
  const complement = req.body.complement || req.query.complement;
 
  if (!complement) {
    throw new APIError('INVALID_CARRIER_COMPLEMENT');
  }
}
 
function validDistrict(req) {
  const district = req.body.district || req.query.district;
 
  if (!district) {
    throw new APIError('INVALID_CARRIER_DISTRICT');
  }
}
 
function validCity(req) {
  const city = req.body.city || req.query.city;
 
  if (!city) {
    throw new APIError('INVALID_CARRIER_CITY');
  }
}
 
function validState(req) {
  const state = req.body.state || req.query.state;
 
  if (!state) {
    throw new APIError('INVALID_CARRIER_STATE');
  }
}
 
function validCountry(req) {
  const country = req.body.country || req.query.country;
 
  if (!country) {
    throw new APIError('INVALID_CARRIER_COUNTRY');
  }
}
 
async function validDeleteHasAssociatedInboundOrder(req) {
  const carrierId = req.body.carrierId || req.query.carrierId;
 
  const carrierData = await CarrierInformation.getCarrier({
    id: carrierId,
  });
 
  const existInboundOrder = await InboundOrderInformation.getInboundOrder({
    carrierCode: carrierData.code,
  });
 
  if (existInboundOrder) {
    throw new APIError(
      '',
      t(
        'BEE4238' /* Não é possível excluir esta transportadora, pois ela está vinculada a outros registros no sistema */
      )
    );
  }
}
 
async function validDeleteHasAssociatedOutboundOrder(req) {
  const carrierId = req.body.carrierId || req.query.carrierId;
 
  const carrierData = await CarrierInformation.getCarrier({
    id: carrierId,
  });
 
  const existOutboundOrder = await OutboundOrderInformation.getOutboundOrder({
    carrierCode: carrierData.code,
  });
 
  if (existOutboundOrder) {
    throw new APIError(
      '',
      t(
        'BEE4238' /* Não é possível excluir esta transportadora, pois ela está vinculada a outros registros no sistema */
      )
    );
  }
}
 
export default {
  validCarrierId,
  validCode,
  validCarrierName,
  validMode,
  validEmail,
  validCnpjOrCpf,
  validStateRegistration,
  validNote,
  validPostalCode,
  validIbge,
  validNumber,
  validStreet,
  validComplement,
  validDistrict,
  validCity,
  validState,
  validCountry,
  validDeleteHasAssociatedInboundOrder,
  validDeleteHasAssociatedOutboundOrder,
};