Actualizar server.js

This commit is contained in:
2026-03-04 08:20:17 +00:00
parent 4ce5fa7ba2
commit 2bf09791f7

View File

@@ -404,7 +404,8 @@ app.get("/public/portal/:token", async (req, res) => {
// 3. Obtenemos los servicios.
// AHORA BUSCAMOS POR client_id. Si el client_id es nulo (expedientes viejos), buscamos por teléfono en el JSON.
const cleanPhoneToMatch = String(client.phone || "").replace(/\D/g, "").slice(-9);
const cleanPhoneToMatch = String(client.phone || "").replace(/\D/g, "").slice(-9);
const requestedServiceId = req.query.service || null; // <-- Capturamos el ID del enlace si existe
const qServices = await pool.query(`
SELECT
@@ -419,16 +420,14 @@ app.get("/public/portal/:token", async (req, res) => {
WHERE s.owner_id = $1
AND s.provider != 'SYSTEM_BLOCK'
AND (
s.client_id = $2
OR
(s.client_id IS NULL AND REPLACE(s.raw_data->>'Teléfono', ' ', '') LIKE $3)
OR
(s.client_id IS NULL AND REPLACE(s.raw_data->>'TELEFONO', ' ', '') LIKE $3)
OR
(s.client_id IS NULL AND REPLACE(s.raw_data->>'TELEFONOS', ' ', '') LIKE $3)
s.id::text = $4 -- <-- LA MAGIA: Forzamos a que traiga el expediente del enlace
OR s.client_id = $2
OR (s.client_id IS NULL AND REPLACE(s.raw_data->>'Teléfono', ' ', '') LIKE $3)
OR (s.client_id IS NULL AND REPLACE(s.raw_data->>'TELEFONO', ' ', '') LIKE $3)
OR (s.client_id IS NULL AND REPLACE(s.raw_data->>'TELEFONOS', ' ', '') LIKE $3)
)
ORDER BY s.created_at DESC
`, [ownerId, clientId, `%${cleanPhoneToMatch}%`]);
`, [ownerId, clientId, `%${cleanPhoneToMatch}%`, requestedServiceId]);
const formattedServices = qServices.rows.map(s => {
return {