All files / source/averageCosts import.js

100% Statements 54/54
93.33% Branches 28/30
100% Functions 3/3
100% Lines 54/54

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 156 157 158 159 160 161 162                        5x 5x 5x   5x 1x 1x 1x   4x       4x 1x 1x 1x             5x 1x 1x 1x   4x       4x 1x 1x 1x             5x 1x 1x 1x   4x       4x 1x 1x 1x             5x   4x         4x 1x 1x 1x             5x 3x 3x 3x     5x               5x   5x 5x   5x 3x                   2x                             2x           2x 1x 8x   1x         1x         5x            
/* eslint-disable no-restricted-syntax */
import moment from 'moment-timezone';
import { t } from '../../helpers/i18n';
import BranchInformtion from '../branches/information';
import ProductBranchInformation from '../productBranches/information';
import ProductInformation from '../products/information';
import WarehouseInformation from '../warehouses/information';
import AverageCostCreate from './create';
import AverageCostInformation from './information';
import AvergaCostUpdate from './update';
 
async function validAverageCost(averageCost) {
  let isValid = true;
  let code = '';
  let message = '';
 
  if (!averageCost.branchCode) {
    isValid = false;
    code = 9000;
    message = t('BEE1285' /* Código da filial deve ser informada */);
  } else {
    const existBranch = await BranchInformtion.getBranch({
      code: averageCost.branchCode,
    });
 
    if (!existBranch) {
      isValid = false;
      code = 9001;
      message = t(
        'BEE1228',
        { 0: averageCost.branchCode } /* Filial %{0} não cadastrado */
      );
    }
  }
 
  if (!averageCost.warehouseCode) {
    isValid = false;
    code = 9002;
    message = t('BEE1286' /* Código do armazém deve ser informado */);
  } else {
    const existWarehouse = await WarehouseInformation.getWarehouse({
      code: averageCost.warehouseCode,
    });
 
    if (!existWarehouse) {
      isValid = false;
      code = 9003;
      message = t(
        'BEE1287',
        { 0: averageCost.warehouseCode } /* Armazém %{0} não cadastrado */
      );
    }
  }
 
  if (!averageCost.productCode) {
    isValid = false;
    code = 9004;
    message = t('BEE1288' /* Código do produto deve ser informado */);
  } else {
    const existProduct = await ProductInformation.validProduct({
      productCode: averageCost.productCode,
    });
 
    if (!existProduct) {
      isValid = false;
      code = 9005;
      message = t(
        'BEE1186',
        { 0: averageCost.productCode } /* Produto %{0} não cadastrado */
      );
    }
  }
 
  if (averageCost.productCode && averageCost.branchCode) {
    const existProductBranch =
      await ProductBranchInformation.validProductBranch({
        productCode: averageCost.productCode,
        branchCode: averageCost.branchCode,
      });
 
    if (!existProductBranch) {
      isValid = false;
      code = 9006;
      message = t(
        'BEE1186',
        { 0: averageCost.productCode } /* Produto %{0} não cadastrado */
      );
    }
  }
 
  if (!averageCost.averageCost) {
    isValid = false;
    code = 9007;
    message = t('BEE1289' /* Custo médio deve ser informado */);
  }
 
  return {
    isValid,
    code,
    message,
  };
}
 
async function importAverageCosts(averageCosts, userId) {
  const listAverageCostsErrors = [];
 
  for (const averageCost of averageCosts) {
    const valid = await validAverageCost(averageCost);
 
    if (!valid.isValid) {
      listAverageCostsErrors.push({
        code: {
          branchCode: averageCost.branchCode,
          warehouseCode: averageCost.warehouseCode,
          productCode: averageCost.productCode,
        },
        error: valid.code,
        message: valid.message,
      });
    } else {
      const newAverageCost = {
        branchCode: averageCost.branchCode,
        warehouseCode: averageCost.warehouseCode,
        productCode: averageCost.productCode,
        averageCost: averageCost.averageCost,
        costDate:
          averageCost.costDate &&
          moment(averageCost.costDate, 'DD/MM/YYYY', true).format()
            ? moment(averageCost.costDate, 'DD/MM/YYYY', true).format()
            : new Date(moment().format('MM/DD/YYYY')),
        note: averageCost.note,
        createdUser: userId,
        updatedUser: userId,
      };
 
      const averageCostExist = await AverageCostInformation.getAverageCost({
        branchCode: newAverageCost.branchCode,
        warehouseCode: newAverageCost.warehouseCode,
        productCode: newAverageCost.productCode,
      });
 
      if (averageCostExist) {
        Object.keys(newAverageCost).forEach(
          (key) => newAverageCost[key] == null && delete newAverageCost[key]
        );
        await AvergaCostUpdate.updateAverageCost(
          averageCostExist.id,
          newAverageCost
        );
      } else {
        await AverageCostCreate.createAverageCost(newAverageCost);
      }
    }
  }
 
  return { listAverageCostsErrors };
}
 
export default {
  importAverageCosts,
};