fix: Corregir funciones json_agg y json_build_object en PostgreSQL para pre-órdenes y escaneo QR
This commit is contained in:
+9
-2
@@ -975,6 +975,11 @@ async function loadPreOrders() {
|
||||
const res = await fetch('/api/ordenes?tipo=preorden');
|
||||
const orders = await res.json();
|
||||
|
||||
if (!Array.isArray(orders)) {
|
||||
console.warn('Respuesta no válida al cargar pre-órdenes:', orders);
|
||||
return;
|
||||
}
|
||||
|
||||
const pendingCount = orders.filter(o => o.estado !== 'entregado' && o.estado !== 'cancelado').length;
|
||||
document.getElementById('badgePreordenesCount').textContent = pendingCount;
|
||||
|
||||
@@ -986,7 +991,8 @@ async function loadPreOrders() {
|
||||
|
||||
list.innerHTML = orders.map(ord => {
|
||||
const hora = new Date(ord.creado_en).toLocaleTimeString('es-CR', { hour: '2-digit', minute: '2-digit' });
|
||||
const itemsList = ord.items.map(i => `${i.cantidad}x ${i.icono || '🥪'} ${i.nombre}`).join(', ');
|
||||
const items = Array.isArray(ord.items) ? ord.items : [];
|
||||
const itemsList = items.map(i => `${i.cantidad}x ${i.icono || '🥪'} ${i.nombre}`).join(', ');
|
||||
|
||||
return `
|
||||
<div style="background: #1e293b; border: 1px solid #334155; border-radius: 12px; padding: 14px; display: flex; justify-content: space-between; align-items: center;">
|
||||
@@ -1063,7 +1069,8 @@ async function dispatchPreOrderExpress(qrToken) {
|
||||
|
||||
if (window.sounds) window.sounds.playSuccess();
|
||||
|
||||
alert(`🎉 ¡ENTREGA EXPRESS EXITOSA!\n${data.mensaje}\nAlumno: ${data.estudiante.nombre_completo}\nGrado: ${data.estudiante.grado}\nProductos a entregar:\n${data.orden.items.map(i => `• ${i.cantidad}x ${i.nombre}`).join('\n')}`);
|
||||
const itemsList = (data.orden && Array.isArray(data.orden.items) ? data.orden.items : []).map(i => `• ${i.cantidad}x ${i.nombre}`).join('\n');
|
||||
alert(`¡ENTREGA EXPRESS EXITOSA!\n${data.mensaje}\nAlumno: ${data.estudiante.nombre_completo}\nGrado: ${data.estudiante.grado}\nProductos a entregar:\n${itemsList}`);
|
||||
|
||||
loadPreOrders();
|
||||
} catch (err) {
|
||||
|
||||
@@ -32,6 +32,10 @@ if (isPg) {
|
||||
s = s.replace(/date\s*\(\s*['"]now['"][^)]*\)/gi, 'CURRENT_DATE');
|
||||
s = s.replace(/date\s*\(\s*([^,)]+)(?:\s*,\s*['"]localtime['"])?\s*\)/gi, 'CAST($1 AS DATE)');
|
||||
|
||||
// Compatibilidad SQLite -> PostgreSQL para funciones JSON
|
||||
s = s.replace(/json_group_array\s*\(/gi, 'json_agg(');
|
||||
s = s.replace(/json_object\s*\(/gi, 'json_build_object(');
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
+6
-2
@@ -1397,7 +1397,9 @@ app.get('/api/estudiantes/qr/:token', (req, res) => {
|
||||
|
||||
const preordenesFormateadas = preordenesPendientes.map(o => ({
|
||||
...o,
|
||||
items: JSON.parse(o.items_json || '[]')
|
||||
items: Array.isArray(o.items_json)
|
||||
? o.items_json
|
||||
: (typeof o.items_json === 'string' ? JSON.parse(o.items_json || '[]') : [])
|
||||
}));
|
||||
|
||||
res.json({
|
||||
@@ -1687,7 +1689,9 @@ app.get('/api/ordenes', (req, res) => {
|
||||
const ordenes = db.prepare(query).all(...params);
|
||||
const resultado = ordenes.map(o => ({
|
||||
...o,
|
||||
items: JSON.parse(o.items_json || '[]')
|
||||
items: Array.isArray(o.items_json)
|
||||
? o.items_json
|
||||
: (typeof o.items_json === 'string' ? JSON.parse(o.items_json || '[]') : [])
|
||||
}));
|
||||
|
||||
res.json(resultado);
|
||||
|
||||
Reference in New Issue
Block a user