All files / source/productBranches import.js

100% Statements 35/35
100% Branches 14/14
100% Functions 3/3
100% Lines 35/35

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                  6x 6x 6x   6x 1x 1x 1x   5x       5x 1x 1x 1x                 6x 1x 1x 1x   5x       5x 1x 1x 1x             6x               6x   6x 6x   6x 4x           2x                                         2x         2x 1x 17x   1x         1x         6x            
/* eslint-disable no-restricted-syntax */
import { t } from '../../helpers/i18n';
import BranchInformation from '../branches/information';
import ProductInformation from '../products/information';
import ProductBranchCreate from './create';
import ProductBranchInformation from './information';
import ProductBranchUpdate from './update';
 
async function validProductBranch(productBranch) {
  let isValid = true;
  let code = '';
  let message = '';
 
  if (!productBranch.productCode) {
    isValid = false;
    code = 9000;
    message = t('BEE1288' /* Código do produto deve ser informado */);
  } else {
    const existProduct = await ProductInformation.getProduct({
      productCode: productBranch.productCode,
    });
 
    if (!existProduct) {
      isValid = false;
      code = 9001;
      message = t(
        'BEE1345',
        {
          0: productBranch.productCode,
        } /* Produto código %{0} não encontrado */
      );
    }
  }
 
  if (!productBranch.branchCode) {
    isValid = false;
    code = 9002;
    message = t('BEE1285' /* Código da filial deve ser informado */);
  } else {
    const existBranch = await BranchInformation.getBranch({
      code: productBranch.branchCode,
    });
 
    if (!existBranch) {
      isValid = false;
      code = 9003;
      message = t(
        'BEE1347',
        { 0: productBranch.branchCode } /* Filial código %{0} não encontrado */
      );
    }
  }
 
  return {
    isValid,
    code,
    message,
  };
}
 
async function importProductBranches(productBranches, userId) {
  const listProductBranchesErrors = [];
 
  for (const productBranch of productBranches) {
    const valid = await validProductBranch(productBranch);
 
    if (!valid.isValid) {
      listProductBranchesErrors.push({
        code: productBranch.code,
        error: valid.code,
        message: valid.message,
      });
    } else {
      const newProductBranch = {
        productCode: productBranch.productCode,
        branchCode: productBranch.branchCode,
        averageCost: productBranch.averageCost,
        restriction: productBranch.restriction,
        leadTime: productBranch.leadTime,
        storable: productBranch.storable,
        demandClass: productBranch.demandClass,
        flag: productBranch.flag,
        defaultAddress: productBranch.defaultAddress,
        shareAddress: productBranch.shareAddress,
        curve: productBranch.curve,
        receivesExpired: productBranch.receivesExpired,
        pickingExpired: productBranch.pickingExpired,
        addressType: productBranch.addressType,
        note: productBranch.note,
        createdUser: userId,
        updatedUser: userId,
      };
 
      const productBranchExistente =
        await ProductBranchInformation.getProductBranch({
          productCode: newProductBranch.productCode,
          branchCode: newProductBranch.branchCode,
        });
 
      if (productBranchExistente) {
        Object.keys(newProductBranch).forEach(
          (key) => newProductBranch[key] == null && delete newProductBranch[key]
        );
        await ProductBranchUpdate.updateProductBranch(
          productBranchExistente.id,
          newProductBranch
        );
      } else {
        await ProductBranchCreate.createProductBranch(newProductBranch);
      }
    }
  }
 
  return { listProductBranchesErrors };
}
 
export default {
  importProductBranches,
};