feat: parse and display n8n order ID instead of data do pedido in client details
This commit is contained in:
@@ -124,12 +124,14 @@ 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
|
produto_id, produto_descricao, quantidade, valor_unitario, pedido_id
|
||||||
) VALUES ($1, $2, $3, $4, $5, $6, $7)
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||||
`;
|
`;
|
||||||
|
|
||||||
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 values = [
|
const values = [
|
||||||
item.Nome_Cliente || 'Unknown',
|
item.Nome_Cliente || 'Unknown',
|
||||||
item.Data_Pedido || '',
|
item.Data_Pedido || '',
|
||||||
@@ -137,7 +139,8 @@ app.post('/api/data', authenticateAPIKey, async (req, res) => {
|
|||||||
item.ID_Produto || '',
|
item.ID_Produto || '',
|
||||||
item.Descricao_Produto || '',
|
item.Descricao_Produto || '',
|
||||||
item.Quantidade || 0,
|
item.Quantidade || 0,
|
||||||
item.Valor_Unitario || 0
|
item.Valor_Unitario || 0,
|
||||||
|
String(orderId)
|
||||||
];
|
];
|
||||||
await client.query(insertQuery, values);
|
await client.query(insertQuery, values);
|
||||||
}
|
}
|
||||||
@@ -155,4 +158,5 @@ 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`);
|
||||||
});
|
});
|
||||||
@@ -16,7 +16,7 @@ const ClientDetails = () => {
|
|||||||
return clientName === decodedName;
|
return clientName === decodedName;
|
||||||
});
|
});
|
||||||
|
|
||||||
const groupedOrdersMap: Record<string, { date: string, orderTotal: number, items: OrderData[] }> = {};
|
const groupedOrdersMap: Record<string, { date: string, orderId: string, orderTotal: number, items: OrderData[] }> = {};
|
||||||
let totalSpent = 0;
|
let totalSpent = 0;
|
||||||
let totalItems = 0;
|
let totalItems = 0;
|
||||||
|
|
||||||
@@ -24,11 +24,12 @@ const ClientDetails = () => {
|
|||||||
totalSpent += (order.Quantidade * order.Valor_Unitario);
|
totalSpent += (order.Quantidade * order.Valor_Unitario);
|
||||||
totalItems += order.Quantidade;
|
totalItems += order.Quantidade;
|
||||||
|
|
||||||
// Use date and total order value as a unique cart/order identifier
|
// Use ID_Pedido if available, otherwise fallback to date and total order value
|
||||||
const key = `${order.Data_Pedido}_${order.Valor_Pedido}`;
|
const key = order.ID_Pedido || `${order.Data_Pedido}_${order.Valor_Pedido}`;
|
||||||
if (!groupedOrdersMap[key]) {
|
if (!groupedOrdersMap[key]) {
|
||||||
groupedOrdersMap[key] = {
|
groupedOrdersMap[key] = {
|
||||||
date: order.Data_Pedido,
|
date: order.Data_Pedido,
|
||||||
|
orderId: order.ID_Pedido || key,
|
||||||
orderTotal: order.Valor_Pedido,
|
orderTotal: order.Valor_Pedido,
|
||||||
items: []
|
items: []
|
||||||
};
|
};
|
||||||
@@ -89,16 +90,16 @@ const ClientDetails = () => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{/* Orders List */}
|
||||||
{/* Orders List */}
|
<div className="flex flex-col gap-6">
|
||||||
<div className="flex flex-col gap-6">
|
{groupedOrders.map((group, groupIndex) => (
|
||||||
{groupedOrders.map((group, groupIndex) => (
|
<div key={groupIndex} className="bg-white dark:bg-dark-card border border-zinc-200 dark:border-dark-border rounded-2xl overflow-hidden shadow-sm">
|
||||||
<div key={groupIndex} className="bg-white dark:bg-dark-card border border-zinc-200 dark:border-dark-border rounded-2xl overflow-hidden shadow-sm">
|
<div className="p-4 border-b border-zinc-100 dark:border-dark-border bg-zinc-50/50 dark:bg-dark-header flex justify-between items-center">
|
||||||
<div className="p-4 border-b border-zinc-100 dark:border-dark-border bg-zinc-50/50 dark:bg-dark-header flex justify-between items-center">
|
<h2 className="text-sm font-bold uppercase tracking-wider text-zinc-500 dark:text-dark-muted flex items-center gap-2">
|
||||||
<h2 className="text-sm font-bold uppercase tracking-wider text-zinc-500 dark:text-dark-muted">
|
<Tag className="w-4 h-4 text-brand-primary" />
|
||||||
Data do Pedido: {group.date}
|
Pedido ID: {group.orderId}
|
||||||
</h2>
|
</h2>
|
||||||
<span className="text-sm font-bold text-brand-primary">
|
<span className="text-sm font-bold text-brand-primary">
|
||||||
Total do Pedido: {formatCurrency(group.orderTotal)}
|
Total do Pedido: {formatCurrency(group.orderTotal)}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ export interface OrderData {
|
|||||||
Quantidade: number;
|
Quantidade: number;
|
||||||
Valor_Unitario: number;
|
Valor_Unitario: number;
|
||||||
Recebido_Em?: string;
|
Recebido_Em?: string;
|
||||||
|
ID_Pedido?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DateRange {
|
export interface DateRange {
|
||||||
|
|||||||
Reference in New Issue
Block a user