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 | 11x 11x 11x 11x 1x 1x 1x 11x 1x 1x 1x 11x 1x 1x 1x 10x 10x 1x 1x 1x 11x 1x 1x 1x 10x 3x 3x 3x 11x 1x 1x 1x 10x 2x 2x 2x 11x 1x 1x 1x 11x 11x 11x 11x 11x 8x 3x 3x 2x 1x 19x 1x 1x 10x | /* eslint-disable no-restricted-syntax */
import validator from 'validator';
import { t } from '../../helpers/i18n';
import validateCNPJ from '../../helpers/validators/cnpj';
import CompanyInformation from '../companies/information';
import BranchCreate from './create';
import BranchInformation from './information';
import BranchUpdate from './update';
async function validBranch(branch) {
let isValid = true;
let code = '';
let message = '';
if (!branch.code) {
isValid = false;
code = 9000;
message = t('BEE1171' /* Código deve ser informado ! */);
}
if (!branch.name) {
isValid = false;
code = 9002;
message = t('BEE1259' /* Nome deve ser informado ! */);
}
if (!branch.companyCode) {
isValid = false;
code = 9003;
message = t('BEE1291' /* Código da empresa deve ser informado */);
} else {
const existCompany = await CompanyInformation.getCompany({
code: branch.companyCode,
});
if (!existCompany) {
isValid = false;
code = 9004;
message = t(
'BEE1292',
{ 0: branch.companyCode } /* Código da empresa %{0} inválido */
);
}
}
if (!branch.email) {
isValid = false;
code = 9005;
message = t('BEE1293' /* Email deve ser informado ! */);
} else if (!validator.isEmail(branch.email)) {
isValid = false;
code = 9006;
message = t('BEE1254', { 0: branch.email } /* Email %{0} inválido ! */);
}
if (!branch.cnpj) {
isValid = false;
code = 9007;
message = t('BEE1294' /* CNPJ deve ser informado ! */);
} else if (!validateCNPJ(branch.cnpj)) {
isValid = false;
code = 9008;
message = t('BEE1295', { 0: branch.cnpj } /* CNPJ %{0} inválido ! */);
}
if (!branch.postalCode) {
isValid = false;
code = 9009;
message = t('BEE1296' /* CEP deve ser informado ! */);
}
return {
isValid,
code,
message,
};
}
async function importBranches(branches, userId) {
const listBranchesErrors = [];
for (const branch of branches) {
const valid = await validBranch(branch);
if (!valid.isValid) {
listBranchesErrors.push({
code: branch.code,
error: valid.code,
message: valid.message,
});
} else {
const newBranch = {
code: branch.code,
name: branch.name,
companyCode: branch.companyCode,
email: branch.email,
phone: branch.phone || '',
cnpj: branch.cnpj,
stateRegistration: branch.stateRegistration || '',
ibge: '',
postalCode: branch.postalCode,
street: branch.street || '',
number: branch.number || '',
complement: branch.complement || '',
district: branch.district || '',
city: branch.city || '',
state: branch.state || '',
country: branch.country || 'Brasil',
note: branch.note || '',
createdUser: userId,
updatedUser: userId,
};
const branchExistente = await BranchInformation.getBranch({
code: branch.code,
});
if (branchExistente) {
Object.keys(newBranch).forEach(
(key) => newBranch[key] == null && delete newBranch[key]
);
await BranchUpdate.updateBranch(branchExistente.id, newBranch);
} else {
await BranchCreate.createBranch(newBranch);
}
}
}
return { listBranchesErrors };
}
export default {
importBranches,
};
|