From 14526f92f226f2384e662fc67dc40b6802ab6cf0 Mon Sep 17 00:00:00 2001 From: urieljareth Date: Mon, 26 Oct 2026 15:16:44 -0600 Subject: [PATCH] Correos: leer la URL publica en tiempo de ejecucion (APP_URL) Next incrusta process.env.NEXT_PUBLIC_* en el bundle al compilar, tambien en codigo de servidor y aun con clave calculada. La imagen de produccion se construye sin NEXT_PUBLIC_APP_URL, asi que los links de los correos de cobro quedaban fijos en http://localhost:3000. Ahora se lee APP_URL (variable de servidor) al enviar. Compose de Coolify, .env.example y AGENTS.md actualizados. Co-Authored-By: Claude Opus 5.5 --- .env.example | 3 ++- AGENTS.md | 2 +- docker-compose.coolify.yml | 3 ++- docker-compose.yaml | 3 ++- src/lib/notificaciones.ts | 20 +++++++++++++------- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/.env.example b/.env.example index 406ae40..0e20c16 100644 --- a/.env.example +++ b/.env.example @@ -43,7 +43,8 @@ MINIMAX_MODEL=MiniMax-M3 VAPID_PUBLIC_KEY= VAPID_PRIVATE_KEY= # URL publica (links de correo y notificaciones). En Coolify: el dominio del servicio web. -NEXT_PUBLIC_APP_URL=http://localhost:3000 +# Variable de servidor: un NEXT_PUBLIC_* quedaria fijo en el bundle al compilar. +APP_URL=http://localhost:3000 # SMTP de correos (opcionales: sin SMTP_HOST no se envia correo, solo avisa en log) SMTP_HOST= diff --git a/AGENTS.md b/AGENTS.md index 5b62ad8..40a91ff 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -90,7 +90,7 @@ A quoted line item is `fijo` (default), `horas`, `retainer`, or `demanda` — se - Production stack: postgres + web + api. The live Coolify app (`cotizador`, uuid `e6vie34d83iv5vw3eyzinl3s`, https://cotizador.urieljareth.org) has **Docker Compose Location `/docker-compose.yaml`** — the byte-identical copy — so both files must stay in sync. Local dev keeps using `docker-compose.yml` + `start.bat`. - **Deploys come from GitHub `main` (`origin`), not Gitea.** Auto-deploy is on: a push to GitHub `main` *is* the deploy (~15 min full build). Pushing to `gitea` only mirrors the code. Don't also trigger the deploy API after a push, or two deploys race. -- `NEXT_PUBLIC_APP_URL` must be set in Coolify: email and push links fall back to `http://localhost:3000` without it. +- `APP_URL` must be set in Coolify (`https://cotizador.urieljareth.org`): email links fall back to `http://localhost:3000` without it. **Never read a runtime value through a `NEXT_PUBLIC_*` name on the server** — Next inlines those at build time (even via a computed key), and the production image is built without them. - The `web` container runs `prisma migrate deploy` on every start, and the seed only when `RUN_SEED=true`. Seed users/passwords come from `SEED_*` env vars (see `.env.example`) — `prisma/seed.ts` never overwrites an existing password unless the env var is set. - `DATABASE_URL` is only used by the Prisma CLI (migrations); the apps use individual `DB_*` vars. Both must point to the same DB. - Healthchecks: web `GET /api/health` (public in middleware, pings DB), api `GET /health`. diff --git a/docker-compose.coolify.yml b/docker-compose.coolify.yml index b8fe870..f3dd7f4 100644 --- a/docker-compose.coolify.yml +++ b/docker-compose.coolify.yml @@ -56,7 +56,8 @@ services: - SEED_ASESOR_PASSWORD=${SEED_ASESOR_PASSWORD} - SEED_ASESOR_NAME=${SEED_ASESOR_NAME} # URL publica: la usan los links de correo y de las notificaciones push. - - NEXT_PUBLIC_APP_URL=${NEXT_PUBLIC_APP_URL} + # Se lee en tiempo de ejecucion (no NEXT_PUBLIC_*: Next lo fijaria al compilar). + - APP_URL=${APP_URL} # Web Push (VAPID). Generar con: node -e "console.log(require('web-push').generateVAPIDKeys())" # La app funciona sin ellas: las notificaciones se omiten con aviso en log. - VAPID_PUBLIC_KEY=${VAPID_PUBLIC_KEY} diff --git a/docker-compose.yaml b/docker-compose.yaml index b8fe870..f3dd7f4 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -56,7 +56,8 @@ services: - SEED_ASESOR_PASSWORD=${SEED_ASESOR_PASSWORD} - SEED_ASESOR_NAME=${SEED_ASESOR_NAME} # URL publica: la usan los links de correo y de las notificaciones push. - - NEXT_PUBLIC_APP_URL=${NEXT_PUBLIC_APP_URL} + # Se lee en tiempo de ejecucion (no NEXT_PUBLIC_*: Next lo fijaria al compilar). + - APP_URL=${APP_URL} # Web Push (VAPID). Generar con: node -e "console.log(require('web-push').generateVAPIDKeys())" # La app funciona sin ellas: las notificaciones se omiten con aviso en log. - VAPID_PUBLIC_KEY=${VAPID_PUBLIC_KEY} diff --git a/src/lib/notificaciones.ts b/src/lib/notificaciones.ts index 783bf1a..1b45352 100644 --- a/src/lib/notificaciones.ts +++ b/src/lib/notificaciones.ts @@ -28,8 +28,14 @@ import type { PagoRecurrente } from "@/generated/prisma/client"; // - al aprobar/rechazar: notificarVerificacion(pagoId) / notificarRechazo(pagoId) // - cobranza de lo que sigue por pagar: enviarRecordatoriosPendientes() -const BASE_URL = process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000"; -const PANEL_PAGOS = `${BASE_URL}/cliente/pagos`; +// URL publica para los links de correo, leida AL ENVIAR. No usar un nombre +// NEXT_PUBLIC_*: Next lo incrusta en el bundle durante el build (tambien en el +// servidor, incluso con clave calculada) y la imagen de produccion se construye +// sin ese valor, asi que el link quedaba fijo en "http://localhost:3000". +function panelPagos(): string { + const base = process.env.APP_URL || "http://localhost:3000"; + return `${base.replace(/\/+$/, "")}/cliente/pagos`; +} // Nombres, conceptos y motivos los captura una persona: no deben poder // inyectar HTML en el correo. @@ -144,7 +150,7 @@ export async function notificarCobrosNuevos(pagos: PagoRecurrente[]): Promise { userId, destino, titulo, - plantilla(titulo, cuerpo, { texto: "Ver mis pagos", url: PANEL_PAGOS }), + plantilla(titulo, cuerpo, { texto: "Ver mis pagos", url: panelPagos() }), `${programa.concepto}: pago del ${formatFechaCorta(pago.fecha)} verificado.`, { titulo: "Pago verificado ✓", @@ -223,7 +229,7 @@ export async function notificarRechazo(pagoId: string, motivo: string): Promise< userId, destino, titulo, - plantilla(titulo, cuerpo, { texto: "Subir otro comprobante", url: PANEL_PAGOS }), + plantilla(titulo, cuerpo, { texto: "Subir otro comprobante", url: panelPagos() }), `${programa.concepto}: comprobante del ${formatFechaCorta(pago.fecha)} rechazado. ${motivo || ""}`.trim(), { titulo: "Comprobante rechazado", @@ -340,11 +346,11 @@ function mensajeRecordatorio( .slice(0, 5) .map((p) => `${formatFechaCorta(p.fecha)} ${p.programa.concepto} ${formatoMoneda(p.monto, p.programa.moneda)}`) .join("; ") + - `. Sube tu comprobante en ${PANEL_PAGOS}`; + `. Sube tu comprobante en ${panelPagos()}`; return { titulo, - html: plantilla(titulo, cuerpo, { texto: "Subir comprobante", url: PANEL_PAGOS }), + html: plantilla(titulo, cuerpo, { texto: "Subir comprobante", url: panelPagos() }), texto, push: { titulo: n === 1 ? "Tienes un pago pendiente" : `Tienes ${n} pagos pendientes`,