All files / middlewares wizardValidation.js

100% Statements 20/20
100% Branches 13/13
100% Functions 6/6
100% Lines 20/20

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                  2x   2x 1x                                                     2x   2x                                         2x 1x               2x   2x 1x               2x   2x   2x 1x               2x   2x 1x               2x   2x 1x                              
import APIError from '../helpers/error';
import validateCNPJ from '../helpers/validators/cnpj';
import emptyFields from '../helpers/validators/emptyFields';
import validEmail from '../helpers/validators/validEmail';
import passwordFormat from '../helpers/validators/passwordFormat';
import CompanyInformation from '../source/companies/information';
import { t } from '../helpers/i18n';
 
async function cnpjValidator(req) {
  const { cnpj } = req.body;
 
  if (!validateCNPJ(cnpj)) {
    throw new APIError(
      'CNPJ_INVALID',
      t('BEE1295' /* CNPJ %{0} inválido ! */, { 0: cnpj })
    );
  }
}
 
async function verifyEmptyField(req) {
  const {
    email,
    login,
    name,
    password,
    code,
    corporateName,
    companyName,
    companyEmail,
    phone,
    cnpj,
    postalCode,
    street,
    number,
    district,
    city,
    state,
    country,
    ibge = 0,
  } = req.body;
 
  const formData = {
    email,
    login,
    name,
    password,
    code,
    corporateName,
    companyName,
    companyEmail,
    phone,
    cnpj,
    postalCode,
    street,
    number,
    district,
    city,
    state,
    country,
    ibge,
  };
 
  if (!emptyFields(formData)) {
    throw new APIError(
      'EMPTY_FIELDS',
      t('BEE1483' /* Todos os campos são obrigatórios!! */)
    );
  }
}
 
async function checkFirstAccess() {
  const companyCount = await CompanyInformation.countCompanies();
 
  if (companyCount !== 0) {
    throw new APIError(
      'NOT_FIRST_ACCESS',
      t('BEE2920' /* Rota indisponível */)
    );
  }
}
 
async function verifyEmail(req) {
  const { email, companyEmail } = req.body;
 
  const emailFields = { email, companyEmail };
 
  if (!validEmail(emailFields)) {
    throw new APIError(
      'INVALID_EMAIL',
      t('BEE2892' /* Formato de e-mail inválido. */)
    );
  }
}
 
async function verifyPasswordFormat(req) {
  const { password } = req.body;
 
  if (!passwordFormat(password)) {
    throw new APIError(
      'INVALID_PASSWORD',
      t('BEE2899' /* A senha deve ter no mínimo 6 caracteres... */)
    );
  }
}
 
async function passwordNotMatch(req) {
  const { password, confirmPassword } = req.body;
 
  if (password !== confirmPassword) {
    throw new APIError(
      'INVALID_PASSWORD',
      t('BEE2898' /* As senhas não conferem. */)
    );
  }
}
 
export default {
  cnpjValidator,
  verifyEmptyField,
  verifyEmail,
  verifyPasswordFormat,
  passwordNotMatch,
  checkFirstAccess,
};