All files / source/customerProducts import.js

57.69% Statements 30/52
27.27% Branches 6/22
66.66% Functions 2/3
57.69% Lines 30/52

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                      1x 1x 1x   1x 1x 1x 1x                               1x 1x 1x 1x                               1x 1x 1x 1x         1x 1x 1x 1x         1x 1x 1x 1x                                   1x               1x   1x 1x   1x 1x                                                                                       1x            
/* eslint-disable no-restricted-syntax */
import moment from 'moment-timezone';
import { t } from '../../helpers/i18n';
import CustomerInformtion from '../customers/information';
import ProductInformation from '../products/information';
import UnitMeasureInformation from '../unitsMeasure/information';
import CustomerProductCreate from './create';
import CustomerProductInformation from './information';
import CustomerProductUpdate from './update';
 
async function validCustomerProduct(customerProduct) {
  let isValid = true;
  let code = '';
  let message = '';
 
  if (!customerProduct.customerCode) {
    isValid = false;
    code = 9000;
    message = t('BEE1299' /* Código do cliente deve ser informado */);
  } else E{
    const existCustomer = await CustomerInformtion.getCustomer({
      code: customerProduct.customerCode,
    });
 
    if (!existCustomer) {
      isValid = false;
      code = 9001;
      message = t(
        'BEE1187',
        { 0: customerProduct.customerCode } /* Cliente %{0} não cadastrado */
      );
    }
  }
 
  if (!customerProduct.productCode) {
    isValid = false;
    code = 9002;
    message = t('BEE1288' /* Código do produto deve ser informado */);
  } else E{
    const existProduct = await ProductInformation.getProduct({
      productCode: customerProduct.productCode,
    });
 
    if (!existProduct) {
      isValid = false;
      code = 9003;
      message = t(
        'BEE1186',
        { 0: customerProduct.productCode } /* Produto %{0} não cadastrado */
      );
    }
  }
 
  Eif (!customerProduct.productCustomerCode) {
    isValid = false;
    code = 9004;
    message = t(
      'BEE1300' /* Código do produto do cliente deve ser informado */
    );
  }
 
  Eif (!customerProduct.productCustomerName) {
    isValid = false;
    code = 9004;
    message = t(
      'BEE1302' /* Descrição do produto do cliente deve ser informada */
    );
  }
 
  if (!customerProduct.unitMeasure) {
    isValid = false;
    code = 9006;
    message = t('BEE1303' /* Unidade de medida deve ser informada */);
  } else E{
    const existUnit = await UnitMeasureInformation.getUnitMeasure({
      code: customerProduct.unitMeasure,
    });
 
    if (!existUnit) {
      isValid = false;
      code = 9007;
      message = t(
        'BEE1211',
        {
          0: customerProduct.unitMeasure,
        } /* Unidade de medida %{0} inválida ! */
      );
    }
  }
 
  return {
    isValid,
    code,
    message,
  };
}
 
async function importCustomerProducts(customerProducts, userId) {
  const listCustomerProductsErrors = [];
 
  for (const customerProduct of customerProducts) {
    const valid = await validCustomerProduct(customerProduct);
 
    if (!valid.isValid) {
      listCustomerProductsErrors.push({
        code: {
          productCode: customerProduct.productCode,
          customerCode: customerProduct.customerCode,
          productCustomerCode: customerProduct.productCustomerCode,
        },
        error: valid.code,
        message: valid.message,
      });
    } else E{
      const newCustomerProduct = {
        productCode: customerProduct.productCode,
        customerCode: customerProduct.customerCode,
        productCustomerCode: customerProduct.productCustomerCode,
        productCustomerName: customerProduct.productCustomerName,
        unitMeasure: customerProduct.unitMeasure,
        note: customerProduct.note,
        createdUser: userId,
        createdAt: moment().format(),
        updatedUser: userId,
        updatedAt: moment().format(),
      };
 
      const customerProductExistente =
        await CustomerProductInformation.getCustomerProduct({
          productCode: newCustomerProduct.productCode,
          customerCode: newCustomerProduct.customerCode,
        });
 
      if (customerProductExistente) {
        Object.keys(newCustomerProduct).forEach(
          (key) =>
            newCustomerProduct[key] == null && delete newCustomerProduct[key]
        );
        await CustomerProductUpdate.updateCustomerProduct(
          customerProductExistente.id,
          newCustomerProduct
        );
      } else {
        await CustomerProductCreate.createCustomerProduct(newCustomerProduct);
      }
    }
  }
 
  return { listCustomerProductsErrors };
}
 
export default {
  importCustomerProducts,
};