-- ============================================================ -- Fix: Remove templates com keys em inglês (WhatsApp/SMS) -- Agência PSI — 2026-04-22 -- ============================================================ -- Ambiente de desenvolvimento sem dados reais: DELETE físico. -- Mantém apenas as keys canônicas em português definidas -- pelo seed_014_global_data.sql. Se alguma key PT estiver -- faltando após rodar esta migration, rode o Step 3 de reseed. -- -- Idempotente: rodar de novo não causa erro (DELETE simples -- encontra 0 linhas). -- ============================================================ BEGIN; -- ── Step 1: Snapshot ANTES ──────────────────────────────────────────── -- Útil pra conferir o que vai ser apagado SELECT 'BEFORE' AS stage, key, channel, event_type, tenant_id IS NULL AS is_global, is_default, is_active, deleted_at FROM public.notification_templates WHERE channel IN ('whatsapp', 'sms') ORDER BY channel, event_type, tenant_id NULLS FIRST, key; -- ── Step 2: DELETE físico de todas as keys em inglês ───────────────── DELETE FROM public.notification_templates WHERE key IN ( -- WhatsApp — variantes em inglês 'session.reminder.whatsapp', 'session.reminder_2h.whatsapp', 'session.confirmation.whatsapp', 'session.cancellation.whatsapp', 'session.reschedule.whatsapp', 'session.rescheduled.whatsapp', 'billing.pending.whatsapp', 'system.welcome.whatsapp', -- SMS — variantes em inglês 'session.reminder.sms', 'session.reminder_2h.sms', 'session.confirmation.sms', 'session.cancellation.sms', 'session.reschedule.sms', 'session.rescheduled.sms', 'billing.pending.sms', 'system.welcome.sms' ); -- ── Step 3: Re-seed (inserção idempotente) das keys PT canônicas ───── -- Garante que todas as keys esperadas existem como globais ativas. -- Usa INSERT … ON CONFLICT DO UPDATE para ser idempotente. -- Os body_text são placeholders padrão; se quiser textos diferentes, -- edite depois via /configuracoes/whatsapp-templates ou /saas/notification-templates. INSERT INTO public.notification_templates (tenant_id, owner_id, key, domain, channel, event_type, body_text, variables, is_default, is_active) VALUES -- ── WhatsApp ───────────────────────────────────────────────────────── (NULL, NULL, 'session.lembrete.whatsapp', 'session', 'whatsapp', 'lembrete_sessao', 'Olá {{patient_name}}! Lembrete: sua sessão com {{therapist_name}} é amanhã às {{session_time}}. Até lá!', '["patient_name","therapist_name","session_date","session_time"]'::jsonb, true, true), (NULL, NULL, 'session.lembrete_2h.whatsapp', 'session', 'whatsapp', 'lembrete_sessao', 'Olá {{patient_name}}! Sua sessão com {{therapist_name}} começa em 2 horas ({{session_time}}). Até já!', '["patient_name","therapist_name","session_time"]'::jsonb, true, true), (NULL, NULL, 'session.confirmacao.whatsapp', 'session', 'whatsapp', 'confirmacao_sessao', 'Olá {{patient_name}}! Sua sessão com {{therapist_name}} foi confirmada para {{session_date}} às {{session_time}}.', '["patient_name","therapist_name","session_date","session_time"]'::jsonb, true, true), (NULL, NULL, 'session.cancelamento.whatsapp', 'session', 'whatsapp', 'cancelamento_sessao', 'Olá {{patient_name}}. Sua sessão de {{session_date}} às {{session_time}} foi cancelada. Entre em contato para remarcar.', '["patient_name","session_date","session_time"]'::jsonb, true, true), (NULL, NULL, 'session.reagendamento.whatsapp', 'session', 'whatsapp', 'reagendamento', 'Olá {{patient_name}}! Sua sessão foi reagendada para {{session_date}} às {{session_time}} com {{therapist_name}}.', '["patient_name","therapist_name","session_date","session_time"]'::jsonb, true, true), (NULL, NULL, 'cobranca.pendente.whatsapp', 'billing', 'whatsapp', 'cobranca_pendente', 'Olá {{patient_name}}! Identificamos um pagamento pendente de {{valor}} com vencimento em {{vencimento}}. Qualquer dúvida, estou à disposição.', '["patient_name","valor","vencimento"]'::jsonb, true, true), (NULL, NULL, 'sistema.boas_vindas.whatsapp', 'system', 'whatsapp', 'boas_vindas_paciente', 'Olá {{patient_name}}! Bem-vindo(a) à {{clinic_name}}. Seu terapeuta {{therapist_name}} está à disposição.', '["patient_name","clinic_name","therapist_name"]'::jsonb, true, true), -- ── SMS ────────────────────────────────────────────────────────────── (NULL, NULL, 'session.lembrete.sms', 'session', 'sms', 'lembrete_sessao', 'Lembrete: sua sessao com {{therapist_name}} e amanha as {{session_time}}.', '["therapist_name","session_date","session_time"]'::jsonb, true, true), (NULL, NULL, 'session.lembrete_2h.sms', 'session', 'sms', 'lembrete_sessao', 'Sua sessao com {{therapist_name}} comeca em 2h ({{session_time}}).', '["therapist_name","session_time"]'::jsonb, true, true), (NULL, NULL, 'session.confirmacao.sms', 'session', 'sms', 'confirmacao_sessao', 'Sua sessao foi confirmada para {{session_date}} as {{session_time}}.', '["session_date","session_time"]'::jsonb, true, true), (NULL, NULL, 'session.cancelamento.sms', 'session', 'sms', 'cancelamento_sessao', 'Sua sessao de {{session_date}} as {{session_time}} foi cancelada.', '["session_date","session_time"]'::jsonb, true, true), (NULL, NULL, 'session.reagendamento.sms', 'session', 'sms', 'reagendamento', 'Sua sessao foi reagendada para {{session_date}} as {{session_time}}.', '["session_date","session_time"]'::jsonb, true, true), (NULL, NULL, 'cobranca.pendente.sms', 'billing', 'sms', 'cobranca_pendente', 'Pagamento pendente: {{valor}}, venc. {{vencimento}}.', '["valor","vencimento"]'::jsonb, true, true), (NULL, NULL, 'sistema.boas_vindas.sms', 'system', 'sms', 'boas_vindas_paciente', 'Bem-vindo a {{clinic_name}}! Seu terapeuta e {{therapist_name}}.', '["clinic_name","therapist_name"]'::jsonb, true, true) ON CONFLICT (tenant_id, owner_id, key, deleted_at) DO UPDATE SET body_text = EXCLUDED.body_text, variables = EXCLUDED.variables, is_default = EXCLUDED.is_default, is_active = EXCLUDED.is_active, domain = EXCLUDED.domain, event_type = EXCLUDED.event_type, updated_at = now(); -- ── Step 4: Snapshot DEPOIS ────────────────────────────────────────── SELECT 'AFTER' AS stage, key, channel, event_type, tenant_id IS NULL AS is_global, is_default, is_active FROM public.notification_templates WHERE channel IN ('whatsapp', 'sms') AND deleted_at IS NULL ORDER BY channel, event_type, tenant_id NULLS FIRST, key; -- ── Step 5: Verificação — esperado 0 linhas ativas em EN ───────────── SELECT count(*) AS remaining_english_keys FROM public.notification_templates WHERE deleted_at IS NULL AND key IN ( 'session.reminder.whatsapp', 'session.reminder_2h.whatsapp', 'session.confirmation.whatsapp', 'session.cancellation.whatsapp', 'session.reschedule.whatsapp', 'session.rescheduled.whatsapp', 'billing.pending.whatsapp', 'system.welcome.whatsapp', 'session.reminder.sms', 'session.reminder_2h.sms', 'session.confirmation.sms', 'session.cancellation.sms', 'session.reschedule.sms', 'session.rescheduled.sms', 'billing.pending.sms', 'system.welcome.sms' ); COMMIT;