All files / source/customers import.js

100% Statements 32/32
100% Branches 36/36
100% Functions 2/2
100% Lines 32/32

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              6x 6x 6x   6x 1x 1x 1x   5x       5x 1x 1x 1x       6x 1x 1x 1x     6x 1x 1x 1x     6x               6x   6x 6x   6x 4x           2x                                           2x       2x 1x   1x         6x            
/* eslint-disable no-restricted-syntax */
import { t } from '../../helpers/i18n';
import CustomerCreate from './create';
import CustomerInformation from './information';
import CustomerUpdate from './update';
 
async function validCustomer(customer) {
  let isValid = true;
  let code = '';
  let message = '';
 
  if (!customer.code) {
    isValid = false;
    code = 9000;
    message = t('BEE1171' /* Código deve ser informado ! */);
  } else {
    const existCustomer = await CustomerInformation.getCustomer({
      code: customer.code,
    });
 
    if (existCustomer && customer.cnpj !== existCustomer.cnpj) {
      isValid = false;
      code = 9001;
      message = `Codigo ${customer.code} já cadastrado para outro CNPJ`;
    }
  }
 
  if (!customer.name) {
    isValid = false;
    code = 9002;
    message = t('BEE1259' /* Nome deve ser informado ! */);
  }
 
  if (!customer.businessName) {
    isValid = false;
    code = 9003;
    message = t('BEE1298' /* Nome fantasia deve ser informado */);
  }
 
  return {
    isValid,
    code,
    message,
  };
}
 
async function importCustomers(customers, userId) {
  const listCustomersErrors = [];
 
  for (const customer of customers) {
    const valid = await validCustomer(customer);
 
    if (!valid.isValid) {
      listCustomersErrors.push({
        code: customer.code,
        error: valid.code,
        message: valid.message,
      });
    } else {
      const newCustomer = {
        code: customer.code,
        name: customer.name,
        businessName: customer.businessName,
        email: customer.email || '',
        phone: customer.phone.replace(/\D/g, '') || '',
        cnpj: customer.cnpj.replace(/\D/g, ''),
        stateRegistration: customer.stateRegistration.replace(/\D/g, '') || '',
        ibge: '',
        postalCode: customer.postalCode.replace(/\D/g, ''),
        street: customer.street || '',
        number: customer.number || '',
        complement: customer.complement || '',
        district: customer.district || '',
        city: customer.city || '',
        state: customer.state || '',
        country: customer.country || 'Brasil',
        note: customer.note || '',
        createdUser: userId,
        updatedUser: userId,
      };
 
      const existCustomer = await CustomerInformation.getCustomer({
        code: customer.code,
      });
 
      if (existCustomer) {
        await CustomerUpdate.updateCustomer(existCustomer.id, newCustomer);
      } else {
        await CustomerCreate.createCustomer(newCustomer);
      }
    }
  }
 
  return { listCustomersErrors };
}
 
export default {
  importCustomers,
};