All files / controllers integration.js

97.91% Statements 47/48
83.87% Branches 26/31
100% Functions 3/3
97.91% Lines 47/48

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                      3x 3x 3x 3x   3x       3x   3x 2x         1x         2x   2x 2x                           2x     1x 1x     1x         5x   5x 5x 4x 2x           1x 1x 2x         2x       2x 2x 2x   1x       1x   1x       1x                 4x     1x 1x     1x         4x   4x 4x   4x 3x       2x 1x       3x     1x 1x     1x                  
/* eslint-disable no-restricted-syntax */
import moment from 'moment-timezone';
import { Op } from 'sequelize';
 
import Totvs from '../services/totvs';
import IntegrationQueueInformation from '../source/integrationQueue/information';
import IntegrationQueueErpInformation from '../source/integrationQueueErp/information';
import InboundOrderImport from '../source/inboundOrders/import';
import OutboundOrderImport from '../source/outboundOrders/import';
 
async function getListIntegrationQueue(req, res) {
  const { filterStartDate, filterEndDate, isBeeStock } = req.query;
  const startDateFormat = moment(filterStartDate).startOf('day').format();
  const endDateFormat = moment(filterEndDate).endOf('day').format();
  const typeIntegration = isBeeStock === 'true';
 
  const query = {
    createdAt: { [Op.between]: [startDateFormat, endDateFormat] },
  };
 
  try {
    let integrationQueue;
    if (typeIntegration) {
      integrationQueue =
        await IntegrationQueueInformation.getAllIntegrationQueue(query, {
          userBranches: req.userBranches,
        });
    } else {
      integrationQueue =
        await IntegrationQueueErpInformation.getAllIntegrationQueue(query, {
          userBranches: req.userBranches,
        });
    }
    const data = [];
 
    for (const element of integrationQueue) {
      data.push({
        idIntegrationQueue: element.id,
        integrationTable: element.code,
        tableKey: element.tableKey,
        branchCode: element.branchCode,
        branchName: element.branch.name,
        status: element.status,
        message: element.message,
        integrationDate: element.integratedAt,
        creationDate: element.createdAt,
        body: element.body ? element.body : '',
        response: element.response ? element.response : '',
      });
    }
    res.json({ success: true, data });
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function resyncQueue(req, res) {
  const { queueIds, isBeeStock } = req.body || req.query;
 
  try {
    if (queueIds && queueIds.length > 0) {
      if (isBeeStock) {
        const queues = await IntegrationQueueInformation.getAllIntegrationQueue(
          {
            id: queueIds,
          }
        );
 
        Eif (queues && queues.length > 0) {
          for (const queue of queues) {
            await Totvs.resyncQueue(queue, req.userId);
          }
        }
      } else {
        const queues =
          await IntegrationQueueErpInformation.getAllIntegrationQueue({
            id: queueIds,
          });
 
        Eif (queues && queues.length > 0) {
          for (const queue of queues) {
            switch (queue.code) {
              case 'inboundOrder':
                await InboundOrderImport.importInboundOrders(
                  [JSON.parse(queue.body)],
                  req.userId
                );
                break;
              case 'outboundOrder':
                await OutboundOrderImport.importOutboundOrders(
                  [JSON.parse(queue.body)],
                  req.userId
                );
                break;
              default:
                break;
            }
          }
        }
      }
    }
 
    res.json({ success: true });
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
async function cancelInboundOrder(req, res) {
  const { queueId } = req.body || req.query;
 
  try {
    let result = false;
 
    if (queueId) {
      const queue = await IntegrationQueueErpInformation.getIntegrationQueue(
        queueId
      );
 
      if (queue) {
        result = await Totvs.cancelInboundIntegration(queue, req.userId);
      }
    }
 
    res.json({ success: result });
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
    const response = {
      success: false,
    };
    res.json(response);
  }
}
 
export default {
  getListIntegrationQueue,
  resyncQueue,
  cancelInboundOrder,
};