All files / source/ranges import.js

100% Statements 40/40
100% Branches 21/21
100% Functions 2/2
100% Lines 40/40

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                  8x 8x 8x   8x 1x 1x 1x     8x 7x       7x 1x 1x 1x             8x 1x 1x 1x   7x       7x 1x 1x 1x             8x 1x 1x 1x     8x               8x   8x 8x   8x 5x           3x                   3x   3x 2x 2x         2x 1x                   8x            
/* eslint-disable no-restricted-syntax */
import { t } from '../../helpers/i18n';
import BranchInformation from '../branches/information';
import AddressInformation from '../storageAddresses/information';
import AddressUpdate from '../storageAddresses/update';
import RangeCreate from './create';
import RangeInformation from './information';
 
async function validRange(range) {
  let isValid = true;
  let code = '';
  let message = '';
 
  if (!range.code) {
    isValid = false;
    code = 9000;
    message = t('BEE1171' /* Código deve ser informado ! */);
  }
 
  if (isValid) {
    const existRange = await RangeInformation.getRange({
      code: range.code,
    });
 
    if (existRange) {
      isValid = false;
      code = 9001;
      message = t(
        'BEE1173',
        { 0: range.code } /* Codigo %{0} já cadastrado  */
      );
    }
  }
 
  if (!range.branchCode) {
    isValid = false;
    code = 9002;
    message = t('BEE1285' /* Código da filial deve ser informado */);
  } else {
    const existBranch = await BranchInformation.getBranch({
      code: range.branchCode,
    });
 
    if (!existBranch) {
      isValid = false;
      code = 9003;
      message = t(
        'BEE1347',
        { 0: range.branchCode } /* Filial código %{0} não encontrado */
      );
    }
  }
 
  if (!range.name) {
    isValid = false;
    code = 9004;
    message = t('BEE1259' /* Nome deve ser informado ! */);
  }
 
  return {
    isValid,
    code,
    message,
  };
}
 
async function importRanges(ranges, userId) {
  const listRangesErrors = [];
 
  for (const range of ranges) {
    const valid = await validRange(range);
 
    if (!valid.isValid) {
      listRangesErrors.push({
        code: range.code,
        error: valid.code,
        message: valid.message,
      });
    } else {
      const newRange = {
        code: range.code,
        name: range.name,
        branchCode: range.branchCode,
        note: range.note,
        limitProductsWave: range.limitProductsWave,
        createdUser: userId,
        updatedUser: userId,
      };
 
      const result = await RangeCreate.createRange(newRange);
 
      if (result && range.addresses && range.addresses.length) {
        for (const address of range.addresses) {
          const existAddress = await AddressInformation.getStorageAddress({
            code: address.code,
            branchCode: range.branchCode,
          });
 
          if (existAddress) {
            await AddressUpdate.updateStorageAddress(existAddress.id, {
              rangeCode: newRange.code,
              updatedUser: userId,
            });
          }
        }
      }
    }
  }
 
  return { listRangesErrors };
}
 
export default {
  importRanges,
};