This commit is contained in:
2026-05-14 14:14:07 +02:00
parent fac8a6e412
commit 06c7603451
8 changed files with 685 additions and 357 deletions

View File

@@ -111,8 +111,8 @@ api.get('/download', async (c) => {
const contentType = mime.lookup(doc.Extension) || 'application/octet-stream'
c.header('Content-Type', contentType)
c.header('Content-Disposition', `${download === 'true' ? 'attachment' : 'inline'}; filename="${doc.Nombre}"`)
return c.body(doc.Archivo)
return c.body(doc.Archivo)
}
return c.json(docs.map((doc) => {
@@ -129,4 +129,60 @@ api.get('/download', async (c) => {
}
})
// --- RUTA DE BÚSQUEDA DE PÓLIZAS ---
api.post('/search', async (c) => {
try {
const body = await c.req.json();
const q = body.q?.trim() || '';
if (!q || q.length < 3) {
return c.json({ success: true, results: [] });
}
const searchTerm = `%${q}%`;
const results = await db.raw(`
SELECT
CodigoPoliza, CodigoMediador, FechaAlta, FechaBaja,
Riesgo, Ramo, Nombre, Apellidos, Documento,
Domicilio, Localidad, CodigoPostal, Provincia
FROM vPolizas
WHERE
CodigoPoliza LIKE :search OR
Nombre LIKE :search OR
Apellidos LIKE :search OR
Documento LIKE :search OR
CodigoMediador LIKE :search
LIMIT 50
`, { search: searchTerm });
// 3. Serialización limpia (Manejo de BigInt y Dates)
const cleanResults = results.map(row => {
const entry = { ...row };
for (let key in entry) {
if (typeof entry[key] === 'bigint') entry[key] = entry[key].toString();
// Formatear fechas a YYYY-MM-DD
if (entry[key] instanceof Date) {
entry[key] = entry[key].toISOString().split('T')[0];
}
}
return entry;
});
return c.json({
success: true,
results: cleanResults,
count: cleanResults.length
});
} catch (error) {
console.error('Error en búsqueda:', error);
return c.json({
success: false,
error: 'Error interno del servidor',
results: []
}, 500);
}
});
export default api