153 lines
3.4 KiB
JavaScript
153 lines
3.4 KiB
JavaScript
|
|
// src/infra/db/repositories/hubglpi/sync.repository.js
|
||
|
|
|
||
|
|
const db = require('../../connections/hubglpi.pg.js');
|
||
|
|
const { logError } = require('../../../../shared/utils/logger');
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Insere registros em sync_data para tickets novos vindos do HubSoft
|
||
|
|
* @param {number[]} hubsoftIds
|
||
|
|
*/
|
||
|
|
async function insertSyncData(hubsoftIds) {
|
||
|
|
if (!hubsoftIds?.length) return [];
|
||
|
|
|
||
|
|
const query = `
|
||
|
|
INSERT INTO sync_data (hubsoft_ticket_id, status_sync)
|
||
|
|
SELECT id, 'pending_create'
|
||
|
|
FROM unnest($1::bigint[]) id
|
||
|
|
ON CONFLICT (hubsoft_ticket_id) DO NOTHING
|
||
|
|
RETURNING id, hubsoft_ticket_id;
|
||
|
|
`;
|
||
|
|
|
||
|
|
try {
|
||
|
|
const { rows } = await db.query(query, [hubsoftIds]);
|
||
|
|
return rows;
|
||
|
|
} catch (err) {
|
||
|
|
logError('Erro ao inserir sync_data', err);
|
||
|
|
throw err;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Atualiza sync_data após criação do ticket no GLPI
|
||
|
|
* @param {number, number[]} hubsoftid, glpiID
|
||
|
|
*/
|
||
|
|
async function updateSyncDataCreated(hubsoftId, glpiId) {
|
||
|
|
const query = `
|
||
|
|
UPDATE sync_data
|
||
|
|
SET glpi_ticket_id = $2,
|
||
|
|
status_sync = 'created',
|
||
|
|
updated_at = NOW()
|
||
|
|
WHERE hubsoft_ticket_id = $1
|
||
|
|
RETURNING *;
|
||
|
|
`;
|
||
|
|
|
||
|
|
try {
|
||
|
|
const { rows } = await db.query(query, [hubsoftId, glpiId]);
|
||
|
|
return rows[0] || null;
|
||
|
|
} catch (err) {
|
||
|
|
logError('Erro ao atualizar sync_data para created', err);
|
||
|
|
throw err;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Busca sync_data pelo id do HubSoft
|
||
|
|
*/
|
||
|
|
async function getSyncIdByHubsoftId(hubsoftTicketId) {
|
||
|
|
const query = `
|
||
|
|
SELECT id, glpi_ticket_id
|
||
|
|
FROM sync_data
|
||
|
|
WHERE hubsoft_ticket_id = $1;
|
||
|
|
`;
|
||
|
|
|
||
|
|
try {
|
||
|
|
const { rows } = await db.query(query, [hubsoftTicketId]);
|
||
|
|
return rows[0] || null;
|
||
|
|
} catch (err) {
|
||
|
|
logError('Erro ao buscar sync_data por hubsoft_ticket_id', err);
|
||
|
|
throw err;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function lockTicketForClosing(syncId) {
|
||
|
|
const query = `
|
||
|
|
UPDATE sync_data
|
||
|
|
SET status_sync = 'processing_close',
|
||
|
|
updated_at = NOW()
|
||
|
|
WHERE id = $1
|
||
|
|
AND status_sync NOT IN ('processing_close', 'closed')
|
||
|
|
RETURNING *;
|
||
|
|
`;
|
||
|
|
|
||
|
|
try {
|
||
|
|
const { rows } = await db.query(query, [syncId]);
|
||
|
|
return rows[0] || null;
|
||
|
|
} catch (err) {
|
||
|
|
logError(`Erro ao travar ticket para fechamento (${syncId})`, err);
|
||
|
|
throw err;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function updateSyncDataError(syncErrorMessage, syncId) {
|
||
|
|
const query = `
|
||
|
|
UPDATE sync_data
|
||
|
|
SET sync_error_message = $1,
|
||
|
|
status_sync = 'sync_error',
|
||
|
|
last_sync_attempt = NOW()
|
||
|
|
WHERE id = $2
|
||
|
|
RETURNING *;
|
||
|
|
`;
|
||
|
|
|
||
|
|
try {
|
||
|
|
const { rows } = await db.query(query, [syncErrorMessage, syncId]);
|
||
|
|
return rows[0];
|
||
|
|
} catch (err) {
|
||
|
|
logError('Erro ao atualizar erro de sync_data', err);
|
||
|
|
throw err;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* Coleta info de sync a partir do GLPI ID
|
||
|
|
* @param {number[]} glpiTicketId
|
||
|
|
*/
|
||
|
|
async function getByGlpiId(glpiTicketId) {
|
||
|
|
const query = `
|
||
|
|
SELECT
|
||
|
|
id,
|
||
|
|
glpi_ticket_id,
|
||
|
|
hubsoft_ticket_id,
|
||
|
|
status_sync
|
||
|
|
FROM sync_data
|
||
|
|
WHERE glpi_ticket_id = $1
|
||
|
|
LIMIT 1;
|
||
|
|
`
|
||
|
|
|
||
|
|
const { rows } = await db.query(query, [glpiTicketId])
|
||
|
|
return rows[0] || null
|
||
|
|
}
|
||
|
|
|
||
|
|
async function markAsClosed(syncId) {
|
||
|
|
const query = `
|
||
|
|
UPDATE sync_data
|
||
|
|
SET
|
||
|
|
status_sync = 'closed',
|
||
|
|
updated_at = NOW()
|
||
|
|
WHERE id = $1
|
||
|
|
RETURNING hubsoft_ticket_id;
|
||
|
|
`
|
||
|
|
|
||
|
|
const { rows } = await db.query(query, [syncId])
|
||
|
|
return rows[0]?.hubsoft_ticket_id
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
module.exports = {
|
||
|
|
insertSyncData,
|
||
|
|
updateSyncDataCreated,
|
||
|
|
getSyncIdByHubsoftId,
|
||
|
|
lockTicketForClosing,
|
||
|
|
updateSyncDataError,
|
||
|
|
getByGlpiId,
|
||
|
|
markAsClosed
|
||
|
|
};
|