All files / source/productEans import.js

50% Statements 22/44
56.52% Branches 13/23
100% Functions 2/2
50% Lines 22/44

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                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 { t } from '../../helpers/i18n';
import ProductInformation from '../products/information';
import ProductEanCreate from './create';
import ProductEanInformation from './information';
import LabelInformation from '../labels/information';
 
async function validProductEan(productEan) {
  let isValid = true;
  let code = '';
  let message = '';
 
  Iif (!productEan.productCode) {
    isValid = false;
    code = 9000;
    message = t('BEE1288' /* Código do produto deve ser informado */);
  }
 
  const existProduct = await ProductInformation.getProduct({
    productCode: productEan.productCode,
  });
 
  Eif (!existProduct) {
    isValid = false;
    code = 9001;
    message = t(
      'BEE1345',
      { 0: productEan.productCode } /* Produto código %{0} não encontrado */
    );
  }
 
  Iif (![1, 2, 3, 4].includes(parseInt(productEan.barCodeType, 10))) {
    isValid = false;
    code = 9004;
    message = t('BEE2598' /* Tipo inválido */);
  } else Iif (
    parseInt(productEan.barCodeType, 10) === 1 &&
    productEan.factor &&
    productEan.factor !== 1
  ) {
    isValid = false;
    code = 9005;
    message = t('BEE2597' /* O (Fator) não corresponde com o (Tipo) */);
  }
 
  Iif (!productEan.ean) {
    isValid = false;
    code = 9002;
    message = t('BEE980' /* Código EAN deve ser informado */);
  } else {
    const existEan = await ProductEanInformation.getProductEan({
      ean: productEan.ean,
    });
 
    Iif (existEan) {
      isValid = false;
      code = 9003;
      message = t('BEE2821' /* Código de Barras já cadastrado! */);
    }
  }
 
  Iif (productEan.labelCode && isValid) {
    const existLabel = await LabelInformation.getLabel(
      {
        code: productEan.labelCode,
        active: true,
      },
      { attributes: ['id'], include: [], raw: true, nest: true }
    );
 
    if (!existLabel) {
      isValid = false;
      code = 9007;
      message = t(
        'BEE4110',
        {
          0: productEan.labelCode,
        } /* Não foi encontrado um modelo da etiqueta ativa do código %{0}. */
      );
    }
  }
 
  return {
    isValid,
    code,
    message,
  };
}
 
async function importProductEans(productEans, userId) {
  const listProductEansErrors = [];
 
  for (const productEan of productEans) {
    const valid = await validProductEan(productEan);
 
    if (!valid.isValid) {
      listProductEansErrors.push({
        code: {
          productCode: productEan.productCode,
          ean: productEan.ean,
        },
        error: valid.code,
        message: valid.message,
      });
    } else E{
      const newProductEan = {
        productCode: productEan.productCode,
        barCodeType: productEan.barCodeType,
        factor: productEan.factor,
        retentionFactor: productEan.retentionFactor,
        barCode: productEan.ean,
        main: productEan.main,
        packageCode: productEan.packageCode,
        note: productEan.note,
        labelCode: productEan.labelCode,
        createdUser: userId,
        updatedUser: userId,
      };
 
      await ProductEanCreate.createProductEan(newProductEan);
    }
  }
 
  return { listProductEansErrors };
}
 
export default {
  importProductEans,
};