freemium F1: enforcement de limite de pacientes (schema-per-tenant)

- therapist_free ganha max_patients=20 (clinic_free ja tinha 30)
- trigger BEFORE INSERT em patients le plan_features.limits em runtime,
  resolve tenant por TG_TABLE_SCHEMA, plano ativo (clinica via tenant_id +
  pessoal via owner user_id), conta vivos (status<>Arquivado) e da RAISE
  PLAN_LIMIT_REACHED|patients|<n>
- helpers tenant_active_plan_id / plan_feature_limit (globais, sobrevivem F6.3)
- wiring: tenants novos ganham via trg_attach_business_triggers; 9 existentes backfill
- testado: clinic_free bloqueia em 30, therapist_free em 20, PRO ilimitado (rollback)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Leonardo
2026-06-13 18:05:19 -03:00
parent 98fe183bac
commit a73b82fa86
6 changed files with 376 additions and 338 deletions
@@ -0,0 +1,34 @@
-- =============================================================================
-- Freemium F1 — limite de pacientes do plano therapist_free
--
-- clinic_free já traz max_patients=30 (em plan_features.limits da feature
-- clinic_calendar, semeado). O therapist_free não tinha limite de pacientes.
-- Pendura max_patients=20 na feature 'patients.manage' (a que o therapist_free
-- já possui, enabled).
--
-- REGRA DE OURO: referenciar plano/feature POR KEY via subquery, nunca por uuid
-- hardcoded (uuids divergem entre ambientes). Idempotente (merge no jsonb).
-- O enforcement em runtime (trigger) está em manual/freemium_f1_plan_limits.
-- =============================================================================
BEGIN;
UPDATE public.plan_features pf
SET limits = COALESCE(pf.limits, '{}'::jsonb) || jsonb_build_object('max_patients', 20)
WHERE pf.plan_id = (SELECT id FROM public.plans WHERE key = 'therapist_free')
AND pf.feature_id = (SELECT id FROM public.features WHERE key = 'patients.manage');
-- Sanidade: garante que o limite ficou gravado (1 linha afetada esperada).
DO $$
DECLARE v int;
BEGIN
SELECT (pf.limits->>'max_patients')::int INTO v
FROM public.plan_features pf
WHERE pf.plan_id = (SELECT id FROM public.plans WHERE key = 'therapist_free')
AND pf.feature_id = (SELECT id FROM public.features WHERE key = 'patients.manage');
IF v IS DISTINCT FROM 20 THEN
RAISE EXCEPTION 'therapist_free max_patients esperado 20, obtido %', v;
END IF;
END $$;
COMMIT;