This commit is contained in:
40
dist/controllers/webhook.controller.js
vendored
Normal file
40
dist/controllers/webhook.controller.js
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.handleTinyOrderUpdate = void 0;
|
||||
const n8n_service_1 = require("../services/n8n.service");
|
||||
const handleTinyOrderUpdate = async (req, res) => {
|
||||
try {
|
||||
let payload = req.body;
|
||||
// Parse 'dados' if it comes as a JSON string
|
||||
if (typeof payload.dados === 'string') {
|
||||
try {
|
||||
payload.dados = JSON.parse(payload.dados);
|
||||
}
|
||||
catch (e) {
|
||||
console.error('Could not parse "dados" as JSON string.', e);
|
||||
}
|
||||
}
|
||||
console.log('Received webhook from Tiny:', JSON.stringify(payload, null, 2));
|
||||
// Acknowledge Tiny immediately
|
||||
res.status(200).send('OK');
|
||||
if (!payload || Object.keys(payload).length === 0) {
|
||||
console.warn('Received empty payload from Tiny webhook.');
|
||||
return;
|
||||
}
|
||||
const transformedData = {
|
||||
source: 'tiny_webhook_middleware',
|
||||
event: payload.tipo || 'atualizacao_pedido',
|
||||
dados: payload.dados,
|
||||
raw_payload: payload
|
||||
};
|
||||
// Forward to n8n asynchronously
|
||||
await (0, n8n_service_1.sendToN8n)(transformedData);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error handling Tiny webhook:', error);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).json({ error: 'Internal server error processing webhook.' });
|
||||
}
|
||||
}
|
||||
};
|
||||
exports.handleTinyOrderUpdate = handleTinyOrderUpdate;
|
||||
Reference in New Issue
Block a user