Fase 1a: microservicio tcef-notifications (workers FCM v1 + email)

Sustituye el envío de push por la API legacy de GCM (muerta desde jun-2024) y
saca el envío del observer de Meteor a una cola BullMQ controlada.

- poller: consume 'notifications' pendientes (campos correctos; el cron viejo
  tenía un typo nofitied/emailNofitied que nunca casaba)
- fcm-worker: firebase-admin (FCM HTTP v1), payload compatible con la app
  Flutter (FLUTTER_NOTIFICATION_CLICK, collapseKey=_id, data); purga tokens
  muertos sin reintentar
- email-worker: nodemailer + plantillas new-fire portadas verbatim
- idempotencia persistente: notification_sends, indice unico (notificationId,
  channel) -> sobrevive reinicios y replays
- salvaguardas anti-spam: modos dry-run/shadow/canary/full, exclusion mutua por
  canal (OWNED_CHANNELS + notifDisabledChannels en Meteor), kill switch,
  limites por usuario/dia y circuit breaker por batch
- docs/legacy-behavior.md (caracterizacion) + docs/rollout.md
- 47 tests (vitest + mongodb-memory-server), typecheck y build OK
- deploy: systemd + pm2 (Node 22 standalone)

Falta el service account de Firebase para envio real de push (pedir al usuario).
This commit is contained in:
vjrj 2026-07-13 00:10:29 +02:00
commit d20e168c90
39 changed files with 8378 additions and 0 deletions

50
.env.example Normal file
View file

@ -0,0 +1,50 @@
# tcef-notifications — ejemplo de configuración por entorno.
# Copia a .env (o usa config.json). Los SECRETOS salen del repo privado
# tcef-private-config / de shiva — NUNCA se commitean aquí.
# --- Modo y salvaguardas ---
MODE=dry-run # dry-run | shadow | canary | full
KILL_SWITCH=0 # 1 = parada total de envíos
OWNED_CHANNELS= # fcm,email (vacío = no procesa nada)
CANARY_USER_IDS= # userIds separados por coma (modo canary)
CANARY_SUBS_IDS= # subsIds (ObjectId hex) separados por coma
# --- Límites ---
LIMIT_PER_USER_DAY=20 # máx envíos/usuario/24h por canal (0 = sin límite)
LIMIT_MAX_PER_BATCH=2000 # circuit breaker: para y alerta si un ciclo supera esto
# --- Poller ---
POLL_INTERVAL_MS=10000
POLL_BATCH_SIZE=500
# --- Reintentos ---
RETRY_ATTEMPTS=3
RETRY_BACKOFF_MS=5000
# --- Mongo (rsmain, db fuegos) — SECRETO ---
MONGO_URL=mongodb://USER:PASS@simone:27017,rbg:27017,rosaparks:27017/fuegos?replicaSet=rsmain
MONGO_DB=fuegos
# --- Redis ---
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
# REDIS_PASSWORD=
REDIS_DB=0
# --- FCM (HTTP v1) — SECRETO: service account JSON del proyecto org.comunes.fires ---
FCM_SERVICE_ACCOUNT=/opt/tcef-notifications/secrets/firebase-service-account.json
FCM_PROJECT_ID=angular-cosmos-108908
# --- Email — SECRETO: MAIL_URL de los settings de producción de Meteor ---
MAIL_URL=smtps://USER:PASS@smtp.host:465
MAIL_FROM=Todos contra el Fuego <no-reply@comunes.org>
# MAIL_REDIRECT_TO=buzon-de-test@example.com # canary/staging: redirige todo el email aquí
# --- Enlaces / mapas ---
ROOT_URL=https://fuegos.comunes.org/
GMAPS_KEY= # Meteor.settings.gmaps.key
FIRE_ICON_URL= # Meteor.settings.private.fireIconUrl
# --- Logs ---
LOG_LEVEL=info
SERVICE_NAME=tcef-notifications