From fa3fae2202e1e0d240d2693ff438b0ce853e75c8 Mon Sep 17 00:00:00 2001 From: Rafael Lopes Date: Mon, 6 Oct 2025 13:36:15 -0300 Subject: [PATCH] Primeiro Commit --- .env | 7 ++++++ config/apiConfig.js | 15 +++++++++++++ controller/hubsoftController.js | 39 +++++++++++++++++++++++++++++++++ package.json | 11 ++++++++++ services/hubsoftServices.js | 37 +++++++++++++++++++++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 .env create mode 100644 config/apiConfig.js create mode 100644 controller/hubsoftController.js create mode 100644 package.json create mode 100644 services/hubsoftServices.js diff --git a/.env b/.env new file mode 100644 index 0000000..e2732b3 --- /dev/null +++ b/.env @@ -0,0 +1,7 @@ +HUBSOFT_AUTH_URL='https://api.sothis.hubsoft.com.br/oauth/token' +HUBSOFT_CLIENT_ID=35 +HUBSOFT_CLIENT_SECRET=1s62YsDijmzLz17NaKbnK1AUCKAx10RSDPMOtVcN +HUBSOFT_USERNAME=contato@vexvigilancia.com.br +HUBSOFT_PASSWORD="19T(6Jnp*" +HUBSOFT_GRANT_TYPE=password +HUBSOFT_CONSULTAR_ATENDIMENTO_URL="https://api.sothis.hubsoft.com.br/api/v1/integracao/atendimento/todos?pagina=0&itens_por_pagina=500&" \ No newline at end of file diff --git a/config/apiConfig.js b/config/apiConfig.js new file mode 100644 index 0000000..aadd40e --- /dev/null +++ b/config/apiConfig.js @@ -0,0 +1,15 @@ +require('dotenv').config(); + +module.exports = { + hubsoft: { + authUrl: process.env.HUBSOFT_AUTH_URL, + authPayload: { + client_id: process.env.HUBSOFT_CLIENT_ID, + client_secret: process.env.HUBSOFT_CLIENT_SECRET, + username: process.env.HUBSOFT_USERNAME, + password: process.env.HUBSOFT_PASSWORD, + grant_type: process.env.HUBSOFT_GRANT_TYPE + }, + consultarAtendimentoUrl: process.env.HUBSOFT_CONSULTAR_ATENDIMENTO_URL + } +}; \ No newline at end of file diff --git a/controller/hubsoftController.js b/controller/hubsoftController.js new file mode 100644 index 0000000..a858c81 --- /dev/null +++ b/controller/hubsoftController.js @@ -0,0 +1,39 @@ +const hubsoftService = require('../services/hubsoftServices.js'); + + +const processaAtendimentos = async () => { + try { + const atendimentos = await hubsoftService.consultarAtendimento(); + + console.log('Resposta atendimentos:', { + type: typeof atendimentos, + isArray: Array.isArray(atendimentos), + length: Array.isArray(atendimentos) ? atendimentos.length : undefined + }); + + if (!Array.isArray(atendimentos) || atendimentos.length === 0) { + console.warn('Nenhum atendimento disponível para o filtro.'); + return; + } + + // Converter para number antes da comparação para evitar falhas por string/número + const consultarAtendimentos = atendimentos.find(atendimento => { + const status = Number(atendimento.id_atendimento_status); + const tipo = Number(atendimento.id_atendimento_tipo); + return status === 33 && tipo === 4; + }); + + if (!consultarAtendimentos) { + const candidatos = atendimentos.filter(a => + Number(a.id_atendimento_status) === 33 || Number(a.id_atendimento_tipo) === 4 + ); + console.log('Nenhum atendimento com status 33 e tipo 4. Exemplos próximos:', candidatos.slice(0, 5)); + } else { + console.log('Atendimento encontrado:', consultarAtendimentos); + } + } catch (error) { + console.error('Erro no controller ao consultar atendimentos:', error); + } +}; + +processaAtendimentos(); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..95dfcd4 --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "integra-hubsoft-glpi", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "GabrielPereira", + "license": "ISC", + "description": "" +} diff --git a/services/hubsoftServices.js b/services/hubsoftServices.js new file mode 100644 index 0000000..29ade98 --- /dev/null +++ b/services/hubsoftServices.js @@ -0,0 +1,37 @@ +const apiConfig = require('../config/apiConfig.js'); +const axios = require('axios'); + +async function getAuthToken() { + try { + const response = await axios.post(apiConfig.hubsoft.authUrl, apiConfig.hubsoft.authPayload); + return response.data.access_token; + } + catch (error) { + console.error('Error fetching auth token:', error); + throw error; + } +} + +const consultarAtendimento = async () => { + try { + const token = await getAuthToken(); + const today = new Date(); + const date = new Date(today.getFullYear(), today.getMonth(), today.getDate()).toISOString().split('T')[0]; + console.log('Consulting atendimentos for date:', date); + const url = `${apiConfig.hubsoft.consultarAtendimentoUrl}data_inicio=2025-10-03&data_fim=2025-10-06`; + + const response = await axios.get(url, { + headers: { Authorization: `Bearer ${token}` } + }); + + return response.data.atendimentos; + + }catch (error) { + console.error('Error consulting atendimentos:', error); + throw error; + } +}; + +module.exports = { + consultarAtendimento, getAuthToken +}; \ No newline at end of file