All files / services/totvs stockMirror.js

0% Statements 0/27
0% Branches 0/16
0% Functions 0/2
0% Lines 0/27

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                                                                                                                                                                                                                                       
import moment from 'moment-timezone';
import { Op } from 'sequelize';
import QueueCreate from '../../source/integrationQueue/create';
import QueueInformation from '../../source/integrationQueue/information';
import QueueUpdate from '../../source/integrationQueue/update';
import RestService from './rest';
import isJson from '../../helpers/validators/totvs';
 
async function sendStockMirrorRequest(
  stockMirrors,
  branchCode,
  depositCode,
  userId
) {
  let queueId = null;
 
  if (!stockMirrors.length) {
    return false;
  }
 
  const json = {
    branchCode,
    depositCode,
    stockMirror: [],
    queueId: 0,
  };
 
  const existQueue = await QueueInformation.getIntegrationQueue({
    code: 'stockMirror',
    tableKey: `${branchCode} | ${depositCode}`,
    status: { [Op.ne]: 200 },
  });
 
  if (existQueue) {
    queueId = existQueue.id;
  } else {
    const newQueue = await QueueCreate.createIntegrationQueue({
      code: 'stockMirror',
      tableId: 0,
      branchCode,
      type: 1,
      body: JSON.stringify(json),
      tableKey: `${branchCode} | ${depositCode}`,
      response: '',
      message: '',
      status: 999,
      createdUser: userId,
      updatedUser: userId,
    });
    queueId = newQueue.id;
  }
 
  json.queueId = queueId;
 
  stockMirrors.forEach((stockMirror) => {
    json.stockMirror.push({
      productCode: stockMirror.productCode,
      quantity: parseFloat(stockMirror.quantity),
    });
  });
 
  try {
    const result = await RestService.requestRest(
      branchCode,
      json,
      'stockMirror'
    );
 
    if (isJson(result.data)) {
      await QueueUpdate.updateIntegrationQueue(queueId, {
        response: JSON.stringify(result.data),
        body: JSON.stringify(json),
        status: result.status,
        message: result.data.retorno || '',
        integratedAt: moment().format(),
        updatedUser: userId,
      });
 
      return result;
    }
    await QueueUpdate.updateIntegrationQueue(queueId, {
      body: JSON.stringify(json),
      response: 'Resposta Inválida',
      status: 998,
      message: 'Erro Integração',
      updatedUser: userId,
    });
 
    return false;
  } catch (error) {
    const integrated = error.message.includes('já integrado');
 
    let status = 998;
 
    if (error.response) {
      status = error.response.status;
    } else if (integrated) {
      status = 200;
    }
 
    await QueueUpdate.updateIntegrationQueue(queueId, {
      body: JSON.stringify(json),
      response: error.response ? error.response.data : '',
      status,
      message: error.message,
      integratedAt: integrated ? moment().format() : null,
      updatedUser: userId,
    });
    return error;
  }
}
 
export default {
  sendStockMirrorRequest,
};