61 lines
1.8 KiB
JavaScript
61 lines
1.8 KiB
JavaScript
const apiConfig = require('../config/apiConfig');
|
|
const axios = require('axios');
|
|
const qs = require('qs');
|
|
const { logError, logInfo } = require('../utils/logger');
|
|
|
|
const getAuthToken = async () => {
|
|
try {
|
|
const response = await axios.post(apiConfig.hubsoft.authUrl, qs.stringify(apiConfig.hubsoft.authPayload), {
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
}
|
|
});
|
|
console.log('Token de autenticação obtido com sucesso.');
|
|
return response.data.access_token;
|
|
} catch (error) {
|
|
logError('Erro ao obter token de autenticação:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
const updateAtendimentoStatus = async (id_atendimento, newStatus) => {
|
|
try {
|
|
|
|
const token = await getAuthToken();
|
|
|
|
if (newStatus === 3) {
|
|
|
|
const response = await axios.put(`${apiConfig.hubsoft.atendimentosUrl}${id_atendimento}`, {
|
|
"fechar_atendimento": true
|
|
}, {
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`,
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
return response.data;
|
|
} else {
|
|
|
|
const response = await axios.put(`${apiConfig.hubsoft.atendimentosUrl}${id_atendimento}`, {
|
|
"id_atendimento_status": newStatus,
|
|
"fechar_atendimento": false
|
|
}, {
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`,
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
return response.data;
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
logError(`Erro ao atualizar status do atendimento ID ${id_atendimento}:`, error.response ? error.response.data : error.message);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
updateAtendimentoStatus
|
|
};
|