All files / source/suppliers import.js

100% Statements 29/29
100% Branches 20/20
100% Functions 3/3
100% Lines 29/29

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              6x 6x 6x   6x 1x 1x 1x     6x 1x 1x 1x     6x 1x 1x 1x     6x               6x   6x 6x   6x 3x           3x                                                   3x       3x 2x 38x   2x   1x         6x            
/* eslint-disable no-restricted-syntax */
import { t } from '../../helpers/i18n';
import SupplierCreate from './create';
import SupplierUpdate from './update';
import SupplierInformation from './information';
 
async function validSupplier(supplier) {
  let isValid = true;
  let code = '';
  let message = '';
 
  if (!supplier.code) {
    isValid = false;
    code = 9000;
    message = t('BEE1171' /* Código deve ser informado ! */);
  }
 
  if (!supplier.name) {
    isValid = false;
    code = 9002;
    message = t('BEE1259' /* Nome deve ser informado ! */);
  }
 
  if (!supplier.businessName) {
    isValid = false;
    code = 9003;
    message = t('BEE1298' /* Nome fantasia deve ser informado */);
  }
 
  return {
    isValid,
    code,
    message,
  };
}
 
async function importSuppliers(suppliers, userId) {
  const listSuppliersErrors = [];
 
  for (const supplier of suppliers) {
    const valid = await validSupplier(supplier);
 
    if (!valid.isValid) {
      listSuppliersErrors.push({
        code: supplier.code,
        error: valid.code,
        message: valid.message,
      });
    } else {
      const newSupplier = {
        code: supplier.code,
        name: supplier.name,
        businessName: supplier.businessName,
        email: supplier.email,
        phone: supplier.phone ? supplier.phone.replace(/\D/g, '') : null,
        cnpj: supplier.cnpj ? supplier.cnpj.replace(/\D/g, '') : null,
        stateRegistration: supplier.stateRegistration
          ? supplier.stateRegistration.replace(/\D/g, '')
          : null,
        ibge: '',
        postalCode: supplier.postalCode
          ? supplier.postalCode.replace(/\D/g, '')
          : null,
        street: supplier.street,
        number: supplier.number,
        complement: supplier.complement,
        district: supplier.district,
        city: supplier.city,
        state: supplier.state,
        country: supplier.country,
        note: supplier.note,
        createdUser: userId,
        updatedUser: userId,
      };
 
      const supplierExistente = await SupplierInformation.getSupplier({
        code: supplier.code,
      });
 
      if (supplierExistente) {
        Object.keys(newSupplier).forEach(
          (key) => newSupplier[key] == null && delete newSupplier[key]
        );
        await SupplierUpdate.updateSupplier(supplierExistente.id, newSupplier);
      } else {
        await SupplierCreate.createSupplier(newSupplier);
      }
    }
  }
 
  return { listSuppliersErrors };
}
 
export default {
  importSuppliers,
};