Compare commits

..

2 Commits

Author SHA1 Message Date
Cauê Faleiros
8be7dbbe06 feat: make database idempotent by adding unique index and using UPSERT for order insertions
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m2s
2026-05-08 15:48:37 -03:00
Cauê Faleiros
d4fad9f6c1 style: replace insertion timestamp with original purchase date in client details 2026-05-08 15:33:48 -03:00
2 changed files with 11 additions and 13 deletions

View File

@@ -41,6 +41,10 @@ const initDB = async () => {
await pool.query(`ALTER TABLE orders ADD COLUMN IF NOT EXISTS pedido_id VARCHAR(100);`).catch(() => {});
await pool.query(`CREATE UNIQUE INDEX IF NOT EXISTS unique_order_product ON orders (pedido_id, produto_id);`).catch(err => {
console.error("Notice: Could not create unique index (might already exist or there are duplicates):", err.message);
});
console.log("Database initialized successfully.");
} catch (err) {
console.error("Failed to initialize database:", err);
@@ -159,6 +163,11 @@ app.post('/api/data', authenticateAPIKey, async (req, res) => {
})();
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`Nexstar Backend running at http://localhost:${PORT}`);
console.log(`Endpoint for n8n: POST http://localhost:${PORT}/api/data`);
});;
app.listen(PORT, '0.0.0.0', () => {
console.log(`Nexstar Backend running at http://localhost:${PORT}`);
console.log(`Endpoint for n8n: POST http://localhost:${PORT}/api/data`);

View File

@@ -113,22 +113,11 @@ const ClientDetails = () => {
<Tag className="w-3 h-3 text-zinc-400 dark:text-dark-muted" />
<span className="text-[10px] font-bold text-zinc-400 dark:text-dark-muted">ID: {order.ID_Produto}</span>
</div>
{order.Recebido_Em && (
{order.Data_Pedido && (
<div className="flex items-center gap-1 ml-2">
<Clock className="w-3 h-3 text-zinc-400 dark:text-dark-muted" />
<span className="text-[10px] font-bold text-zinc-400 dark:text-dark-muted">
Recebido: {
(() => {
const d = new Date(order.Recebido_Em);
const day = String(d.getDate()).padStart(2, '0');
const month = String(d.getMonth() + 1).padStart(2, '0');
const year = d.getFullYear();
const hours = String(d.getHours()).padStart(2, '0');
const minutes = String(d.getMinutes()).padStart(2, '0');
const seconds = String(d.getSeconds()).padStart(2, '0');
return `${day}/${month}/${year} ${hours}:${minutes}:${seconds}`;
})()
}
Comprado: {order.Data_Pedido}
</span>
</div>
)}