diff --git a/server.js b/server.js index d9c85f4..aa6b3b9 100644 --- a/server.js +++ b/server.js @@ -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 {