feat: add cliente_fone column for WhatsApp marketing and safely implement database idempotency
This commit is contained in:
@@ -132,13 +132,24 @@ app.post('/api/data', authenticateAPIKey, async (req, res) => {
|
|||||||
const insertQuery = `
|
const insertQuery = `
|
||||||
INSERT INTO orders (
|
INSERT INTO orders (
|
||||||
cliente_nome, data_pedido, valor_pedido,
|
cliente_nome, data_pedido, valor_pedido,
|
||||||
produto_id, produto_descricao, quantidade, valor_unitario, pedido_id
|
produto_id, produto_descricao, quantidade, valor_unitario, pedido_id, cliente_fone
|
||||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||||
|
ON CONFLICT (pedido_id, produto_id) DO UPDATE SET
|
||||||
|
cliente_nome = EXCLUDED.cliente_nome,
|
||||||
|
data_pedido = EXCLUDED.data_pedido,
|
||||||
|
valor_pedido = EXCLUDED.valor_pedido,
|
||||||
|
produto_descricao = EXCLUDED.produto_descricao,
|
||||||
|
quantidade = EXCLUDED.quantidade,
|
||||||
|
valor_unitario = EXCLUDED.valor_unitario,
|
||||||
|
cliente_fone = EXCLUDED.cliente_fone,
|
||||||
|
created_at = CURRENT_TIMESTAMP
|
||||||
`;
|
`;
|
||||||
|
|
||||||
for (const item of payload) {
|
for (const item of payload) {
|
||||||
// Handle potential missing fields gracefully
|
// Handle potential missing fields gracefully
|
||||||
const orderId = item.id || item.ID_Pedido || (item.json && item.json.body && item.json.body.id) || '';
|
const fallbackId = `${item.Nome_Cliente}_${item.Data_Pedido}_${item.Valor_Pedido}`;
|
||||||
|
const orderId = item.id || item.ID_Pedido || (item.json && item.json.body && item.json.body.id) || fallbackId;
|
||||||
|
const fone = item.Fone_Cliente || item.fone || item.celular || '';
|
||||||
|
|
||||||
const values = [
|
const values = [
|
||||||
item.Nome_Cliente || 'Unknown',
|
item.Nome_Cliente || 'Unknown',
|
||||||
@@ -148,7 +159,8 @@ app.post('/api/data', authenticateAPIKey, async (req, res) => {
|
|||||||
item.Descricao_Produto || '',
|
item.Descricao_Produto || '',
|
||||||
parseInt(item.Quantidade) || 0,
|
parseInt(item.Quantidade) || 0,
|
||||||
parseFloat(item.Valor_Unitario) || 0,
|
parseFloat(item.Valor_Unitario) || 0,
|
||||||
String(orderId)
|
String(orderId),
|
||||||
|
String(fone)
|
||||||
];
|
];
|
||||||
await client.query(insertQuery, values);
|
await client.query(insertQuery, values);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user