All files / middlewares customerValidations.js

100% Statements 63/63
100% Branches 81/81
100% Functions 17/17
100% Lines 63/63

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                  3x   3x 1x   2x       2x 1x               3x   3x 1x   2x       2x 1x                 2x   2x 1x         3x   3x 1x         5x   5x 1x     4x   4x 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         2x   2x 1x         4x   4x       4x 3x       3x 1x                                                          
import validator from 'validator';
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
import CustomerInformation from '../source/customers/information';
import validateCNPJ from '../helpers/validators/cnpj';
import validateCPF from '../helpers/validators/cpf';
import OutboundOrdersInformation from '../source/outboundOrders/information';
 
async function validCustomerId(req) {
  const customerId = req.body.customerId || req.query.customerId;
 
  if (!customerId) {
    throw new APIError('INVALID_ID', t('BEE3421' /* ID inválido */));
  } else {
    const existCustomer = await CustomerInformation.getCustomer({
      id: customerId,
    });
 
    if (!existCustomer) {
      throw new APIError(
        'NOT_EXIST_CUSTOMER_ID',
        t('BEE1190', { 0: customerId } /* Cliente %{0} não localizado */)
      );
    }
  }
}
async function validCode(req) {
  const code = req.body.code || req.query.code;
 
  if (!code) {
    throw new APIError('INVALID_CODE', t('BEE3430' /* Código inválido */));
  } else {
    const existCustomer = await CustomerInformation.getCustomer({
      code,
    });
 
    if (existCustomer) {
      throw new APIError(
        'EXIST_CUSTOMER_CODE',
        t('BEE1173', { 0: code } /* Codigo %{0} já cadastrado  */)
      );
    }
  }
}
 
function validCustomerName(req) {
  const name = req.body.name || req.query.name;
 
  if (!name) {
    throw new APIError('INVALID_NAME', t('BEE3562' /* Nome inválido */));
  }
}
 
function validEmail(req) {
  const email = req.body.email || req.query.email;
 
  if (email && !validator.isEmail(email)) {
    throw new APIError('INVALID_EMAIL', t('BEE3305' /* E-mail inválido */));
  }
}
 
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_STATE_REGISTRATION');
  }
}
 
function validNote(req) {
  const note = req.body.note || req.query.note;
 
  if (!note) {
    throw new APIError('INVALID_NOTE');
  }
}
 
function validPostalCode(req) {
  const postalCode = req.body.postalCode || req.query.postalCode;
 
  if (!postalCode) {
    throw new APIError('INVALID_POSTAL_CODE');
  }
}
 
function validIbge(req) {
  const ibge = req.body.ibge || req.query.ibge;
 
  if (!ibge) {
    throw new APIError('INVALID_IBGE');
  }
}
 
function validStreet(req) {
  const street = req.body.street || req.query.street;
 
  if (!street) {
    throw new APIError('INVALID_STREET');
  }
}
 
function validNumber(req) {
  const number = req.body.number || req.query.number;
 
  if (!number) {
    throw new APIError('INVALID_NUMBER');
  }
}
 
function validComplement(req) {
  const complement = req.body.complement || req.query.complement;
 
  if (!complement) {
    throw new APIError('INVALID_COMPLEMENT');
  }
}
 
function validDistrict(req) {
  const district = req.body.district || req.query.district;
 
  if (!district) {
    throw new APIError('INVALID_DISTRICT');
  }
}
 
function validCity(req) {
  const city = req.body.city || req.query.city;
 
  if (!city) {
    throw new APIError('INVALID_CITY');
  }
}
 
function validState(req) {
  const state = req.body.state || req.query.state;
 
  if (!state) {
    throw new APIError('INVALID_STATE');
  }
}
 
function validCountry(req) {
  const country = req.body.country || req.query.country;
 
  if (!country) {
    throw new APIError('INVALID_COUNTRY');
  }
}
 
async function validDeleteOnSeparation(req) {
  const customerId = req.body.customerId || req.query.customerId;
 
  const customerData = await CustomerInformation.getCustomer({
    id: customerId,
  });
 
  if (customerData) {
    const outboundOrder = await OutboundOrdersInformation.getOutboundOrder({
      customerCode: customerData.code,
    });
 
    if (outboundOrder) {
      throw new APIError(
        '',
        t(
          'BEE4227' /* Não é possível excluir este cliente, pois há documentos de saída vinculados */
        )
      );
    }
  }
}
 
export default {
  validCustomerId,
  validCode,
  validCustomerName,
  validEmail,
  validCnpjOrCpf,
  validStateRegistration,
  validNote,
  validPostalCode,
  validIbge,
  validNumber,
  validStreet,
  validComplement,
  validDistrict,
  validCity,
  validState,
  validCountry,
  validDeleteOnSeparation,
};