All files / source/inboundDocuments import.js

56.41% Statements 22/39
22.22% Branches 4/18
100% Functions 2/2
56.41% Lines 22/39

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                    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 InboundDocumentCreate from './create';
import InboundDocumentInformation from './information';
import BranchInformation from '../branches/information';
import SupplierInformation from '../suppliers/information';
import { t } from '../../helpers/i18n';
 
async function validDocument(document) {
  let isValid = true;
  let code = '';
  let message = '';
 
  if (!document.branchCode) {
    isValid = false;
    code = 9000;
    message = t('BEE1285' /* Código da filial deve ser informada */);
  } else E{
    const existBranch = await BranchInformation.getBranch({
      code: document.branchCode,
    });
 
    if (!existBranch) {
      isValid = false;
      code = 9001;
      message = t(
        'BEE1307',
        { 0: document.branchCode } /* Código da filial %{0} inválido */
      );
    }
  }
 
  if (!document.supplierCode) {
    isValid = false;
    code = 9002;
    message = t('BEE1308' /* Código do fornecedor deve ser informado */);
  } else E{
    const existSupplier = await SupplierInformation.getSupplier({
      code: document.supplierCode,
    });
 
    if (!existSupplier) {
      isValid = false;
      code = 9003;
      message = t(
        'BEE1309',
        { 0: document.supplierCode } /* Código do fornecedor %{0} inválido */
      );
    }
  }
 
  if (!document.accessKey) {
    isValid = false;
    code = 9004;
    message = t('BEE1313' /* Chave de Acesso deve ser informado */);
  } else E{
    const existDocument = await InboundDocumentInformation.getInboundDocument({
      accessKey: document.accessKey,
    });
 
    if (existDocument) {
      isValid = false;
      code = 9005;
      message = `Já existe documento com a Chave de Acesso ${document.accessKey}`;
    }
  }
 
  return {
    isValid,
    code,
    message,
  };
}
 
async function importDocuments(documents, userId) {
  const listDocumentsErrors = [];
 
  for (const document of documents) {
    const valid = await validDocument(document);
 
    if (!valid.isValid) {
      listDocumentsErrors.push({
        code: document.accessKey,
        error: valid.code,
        message: valid.message,
      });
    } else E{
      const newDocument = {
        branchCode: document.branchCode,
        supplierCode: document.supplierCode,
        orderNumber: document.orderNumber,
        serie: document.serie,
        issueDate:
          document.issueDate &&
          moment(document.issueDate, 'DD/MM/YYYY', true).format()
            ? moment(document.issueDate, 'DD/MM/YYYY', true).format()
            : new Date(moment().format('MM/DD/YYYY')),
        accessKey: document.accessKey,
        licensePlate: document.licensePlate,
        expectedVolumes: document.expectedVolumes,
        note: document.note,
        createdUser: userId,
        updatedUser: userId,
      };
 
      await InboundDocumentCreate.createInboundDocument(newDocument);
    }
  }
 
  return { listDocumentsErrors };
}
 
export default {
  importDocuments,
};