first commit
Some checks failed
Build and Deploy / build-and-push (push) Failing after 4m14s

This commit is contained in:
Cauê Faleiros
2026-04-09 09:45:02 -03:00
commit c687fd2bd1
12 changed files with 305 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
import { Request, Response } from 'express';
import { sendToN8n } from '../services/n8n.service';
export const handleTinyOrderUpdate = async (req: Request, res: Response): Promise<void> => {
try {
// 1. Security Check: Verify token from Tiny
const expectedToken = process.env.TINY_WEBHOOK_SECRET;
const providedToken = req.query.token;
if (expectedToken && providedToken !== expectedToken) {
console.warn('Unauthorized webhook attempt. Invalid or missing token.');
res.status(401).json({ error: 'Unauthorized' });
return;
}
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 sendToN8n(transformedData);
} catch (error) {
console.error('Error handling Tiny webhook:', error);
if (!res.headersSent) {
res.status(500).json({ error: 'Internal server error processing webhook.' });
}
}
};

21
src/index.ts Normal file
View File

@@ -0,0 +1,21 @@
import express, { Express, Request, Response } from 'express';
import dotenv from 'dotenv';
import webhookRoutes from './routes/webhook.route';
dotenv.config();
const app: Express = express();
const port = process.env.PORT || 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/api/webhooks', webhookRoutes);
app.get('/health', (req: Request, res: Response) => {
res.status(200).json({ status: 'OK', message: 'Tiny-n8n middleware is running.' });
});
app.listen(port, () => {
console.log(`[server]: Middleware server is running at http://localhost:${port}`);
});

View File

@@ -0,0 +1,8 @@
import { Router } from 'express';
import { handleTinyOrderUpdate } from '../controllers/webhook.controller';
const router = Router();
router.post('/tiny/order-status', handleTinyOrderUpdate);
export default router;

View File

@@ -0,0 +1,25 @@
import axios from 'axios';
export const sendToN8n = async (data: any): Promise<void> => {
const n8nWebhookUrl = process.env.N8N_WEBHOOK_URL;
if (!n8nWebhookUrl) {
console.error('N8N_WEBHOOK_URL is not defined in environment variables.');
return;
}
try {
console.log(`Forwarding payload to n8n at ${n8nWebhookUrl}...`);
const response = await axios.post(n8nWebhookUrl, data, {
headers: {
'Content-Type': 'application/json'
}
});
console.log(`Successfully forwarded to n8n. Status: ${response.status}`);
} catch (error: any) {
console.error('Failed to forward payload to n8n.', error.message);
if (error.response) {
console.error('n8n Response:', error.response.status, error.response.data);
}
}
};