fix: safely parse n8n stringified numbers and add ID_Pedido to API response
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m16s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m16s
This commit is contained in:
@@ -38,6 +38,9 @@ const initDB = async () => {
|
|||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
);
|
);
|
||||||
`);
|
`);
|
||||||
|
|
||||||
|
await pool.query(`ALTER TABLE orders ADD COLUMN IF NOT EXISTS pedido_id VARCHAR(100);`).catch(() => {});
|
||||||
|
|
||||||
console.log("Database initialized successfully.");
|
console.log("Database initialized successfully.");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Failed to initialize database:", err);
|
console.error("Failed to initialize database:", err);
|
||||||
@@ -81,7 +84,8 @@ const formatRow = (row) => ({
|
|||||||
Descricao_Produto: row.produto_descricao,
|
Descricao_Produto: row.produto_descricao,
|
||||||
Quantidade: row.quantidade,
|
Quantidade: row.quantidade,
|
||||||
Valor_Unitario: parseFloat(row.valor_unitario),
|
Valor_Unitario: parseFloat(row.valor_unitario),
|
||||||
Recebido_Em: row.created_at
|
Recebido_Em: row.created_at,
|
||||||
|
ID_Pedido: row.pedido_id
|
||||||
});
|
});
|
||||||
|
|
||||||
// GET data (for the frontend)
|
// GET data (for the frontend)
|
||||||
@@ -135,11 +139,11 @@ app.post('/api/data', authenticateAPIKey, async (req, res) => {
|
|||||||
const values = [
|
const values = [
|
||||||
item.Nome_Cliente || 'Unknown',
|
item.Nome_Cliente || 'Unknown',
|
||||||
item.Data_Pedido || '',
|
item.Data_Pedido || '',
|
||||||
item.Valor_Pedido || 0,
|
parseFloat(item.Valor_Pedido) || 0,
|
||||||
item.ID_Produto || '',
|
item.ID_Produto || '',
|
||||||
item.Descricao_Produto || '',
|
item.Descricao_Produto || '',
|
||||||
item.Quantidade || 0,
|
parseInt(item.Quantidade) || 0,
|
||||||
item.Valor_Unitario || 0,
|
parseFloat(item.Valor_Unitario) || 0,
|
||||||
String(orderId)
|
String(orderId)
|
||||||
];
|
];
|
||||||
await client.query(insertQuery, values);
|
await client.query(insertQuery, values);
|
||||||
@@ -158,5 +162,4 @@ app.post('/api/data', authenticateAPIKey, async (req, res) => {
|
|||||||
app.listen(PORT, '0.0.0.0', () => {
|
app.listen(PORT, '0.0.0.0', () => {
|
||||||
console.log(`Nexstar Backend running at http://localhost:${PORT}`);
|
console.log(`Nexstar Backend running at http://localhost:${PORT}`);
|
||||||
console.log(`Endpoint for n8n: POST http://localhost:${PORT}/api/data`);
|
console.log(`Endpoint for n8n: POST http://localhost:${PORT}/api/data`);
|
||||||
});ORT}/api/data`);
|
|
||||||
});
|
});
|
||||||
32
test-payload.json
Normal file
32
test-payload.json
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"Nome_Cliente": "Fabrcio Araujo leme",
|
||||||
|
"Data_Pedido": "13/02/2025",
|
||||||
|
"Valor_Pedido": 94.95,
|
||||||
|
"ID_Pedido": "942384335",
|
||||||
|
"ID_Produto": "942384336",
|
||||||
|
"Descricao_Produto": "Camiseta Plus Size Premium Algodão Estampada Paris Tira G4 Grafite",
|
||||||
|
"Quantidade": "1.0000",
|
||||||
|
"Valor_Unitario": "31.650000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Nome_Cliente": "Fabrcio Araujo leme",
|
||||||
|
"Data_Pedido": "13/02/2025",
|
||||||
|
"Valor_Pedido": 94.95,
|
||||||
|
"ID_Pedido": "942384335",
|
||||||
|
"ID_Produto": "942384339",
|
||||||
|
"Descricao_Produto": "Camiseta Plus Size Premium Algodão Estampada Fichas Rolando G4 Preto",
|
||||||
|
"Quantidade": "1.0000",
|
||||||
|
"Valor_Unitario": "31.650000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Nome_Cliente": "Fabrcio Araujo leme",
|
||||||
|
"Data_Pedido": "13/02/2025",
|
||||||
|
"Valor_Pedido": 94.95,
|
||||||
|
"ID_Pedido": "942384335",
|
||||||
|
"ID_Produto": "942384342",
|
||||||
|
"Descricao_Produto": "Camiseta Plus Size Unissex T-Shirt Premium Sorte Nas Cartas G4 Preto",
|
||||||
|
"Quantidade": "1.0000",
|
||||||
|
"Valor_Unitario": "31.650000"
|
||||||
|
}
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user