All files / middlewares carrierScheduleValidations.js

100% Statements 21/21
100% Branches 24/24
100% Functions 4/4
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95                  5x   5x 2x     3x       3x 2x                       4x   4x 3x             3x 2x                 4x   4x 3x       3x 2x                       4x   4x 3x       3x 2x                            
import APIError from '../helpers/error';
import I18n from '../helpers/i18n';
import CarrierScheduleInformation from '../source/carrierSchedules/information';
import ScheduleInformation from '../source/schedules/information';
import CarrierInformation from '../source/carriers/information';
import CustomerInformation from '../source/customers/information';
 
async function validCarrierScheduleId(req) {
  const carrierScheduleId =
    req.body.carrierScheduleId || req.query.carrierScheduleId;
 
  if (!carrierScheduleId) {
    throw new APIError('INVALID_CARRIER_SCHEDULE_ID');
  } else {
    const existCarrierSchedule =
      await CarrierScheduleInformation.getCarrierSchedule({
        id: carrierScheduleId,
      });
 
    if (!existCarrierSchedule) {
      throw new APIError(
        'NOT_EXIST_CARRIER_SCHEDULE_ID',
        I18n.t(
          'BEE1175',
          { 0: carrierScheduleId } /* Carrier Schedule %{0} não localizada */
        )
      );
    }
  }
}
 
async function validScheduleCode(req) {
  const scheduleCode = req.body.scheduleCode || req.query.scheduleCode;
 
  if (scheduleCode) {
    const existSchedule = await ScheduleInformation.getSchedule(
      {
        code: scheduleCode,
      },
      { userBranches: req.userBranches }
    );
 
    if (!existSchedule)
      throw new APIError(
        '',
        'INVALID_SCHEDULE_CODE',
        I18n.t('BEE1177', { 0: scheduleCode } /* Agendamento %{0} inválida! */)
      );
  }
}
 
async function validCarrierCode(req) {
  const carrierCode = req.body.carrierCode || req.query.carrierCode;
 
  if (carrierCode) {
    const existCarrier = await CarrierInformation.getCarrier({
      code: carrierCode,
    });
 
    if (!existCarrier)
      throw new APIError(
        '',
        'INVALID_CARRIER_CODE',
        I18n.t(
          'BEE1179',
          { 0: carrierCode } /* Transportadora %{0} inválida ! */
        )
      );
  }
}
 
async function validCustomerCode(req) {
  const customerCode = req.body.customerCode || req.query.customerCode;
 
  if (customerCode) {
    const existCarrier = await CustomerInformation.getCustomer({
      code: customerCode,
    });
 
    if (!existCarrier)
      throw new APIError(
        '',
        'INVALID_CUSTOMER_CODE',
        I18n.t('BEE1180', { 0: customerCode } /* Cliente %{0} inválida ! */)
      );
  }
}
 
export default {
  validCarrierScheduleId,
  validScheduleCode,
  validCarrierCode,
  validCustomerCode,
};