diff --git a/server.js b/server.js index 5068bd7..635d7f4 100644 --- a/server.js +++ b/server.js @@ -1412,16 +1412,34 @@ app.get("/public/portal/:token", async (req, res) => { client_name: b.client_name, client_phone: b.client_phone, client_address: b.client_address, - status: b.status // 🛑 FIX: Faltaba enviar el estado al móvil + status: b.status })); } + // 🚀 5. NUEVO: BUSCAR SI TIENE PLAN DE PROTECCIÓN ACTIVO + let activeSubscription = null; + if (cleanPhoneSearch.length >= 9) { + const subQ = await pool.query(` + SELECT s.status, s.payment_status, s.bricos_used, s.urgencies_used, s.renewal_date, + p.name as plan_name, p.bricos_limit, p.urgencies_limit + FROM protection_subscriptions s + JOIN protection_plans p ON s.plan_id = p.id + WHERE s.company_id = $1 AND s.client_phone LIKE $2 AND s.status != 'expirado' + ORDER BY s.created_at DESC LIMIT 1 + `, [ownerId, `%${cleanPhoneSearch}%`]); + + if (subQ.rowCount > 0) { + activeSubscription = subQ.rows[0]; + } + } + res.json({ ok: true, client: { name: client.full_name }, company, services: formattedServices, - quotes: formattedQuotes + quotes: formattedQuotes, + subscription: activeSubscription // <-- 🟢 Lo pasamos al frontend del cliente }); } catch (e) {