Agenda, Agendador, Configurações
This commit is contained in:
220
DBS/2026-03-11/Novo-DB/fix_missing_subscriptions.sql
Normal file
220
DBS/2026-03-11/Novo-DB/fix_missing_subscriptions.sql
Normal file
@@ -0,0 +1,220 @@
|
||||
-- =============================================================================
|
||||
-- FIX: Atribuir plano free a usuários/tenants sem assinatura ativa
|
||||
-- =============================================================================
|
||||
-- Execute no SQL Editor do Supabase (service_role)
|
||||
-- Idempotente: só insere onde não existe assinatura ativa.
|
||||
--
|
||||
-- Regras:
|
||||
-- • tenant kind = 'therapist' → therapist_free (por user_id do admin)
|
||||
-- • tenant kind IN (clinic_*) → clinic_free (por tenant_id)
|
||||
-- • profiles.account_type = 'patient' / portal_user → patient_free (por user_id)
|
||||
-- =============================================================================
|
||||
|
||||
BEGIN;
|
||||
|
||||
-- ──────────────────────────────────────────────────────────────────────────────
|
||||
-- DIAGNÓSTICO — mostra o estado atual antes de corrigir
|
||||
-- ──────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
r RECORD;
|
||||
BEGIN
|
||||
RAISE NOTICE '=== DIAGNÓSTICO DE ASSINATURAS ===';
|
||||
RAISE NOTICE '';
|
||||
|
||||
-- Terapeutas sem plano
|
||||
RAISE NOTICE '--- Terapeutas SEM assinatura ativa ---';
|
||||
FOR r IN
|
||||
SELECT
|
||||
tm.user_id,
|
||||
p.full_name,
|
||||
t.id AS tenant_id,
|
||||
t.name AS tenant_name
|
||||
FROM public.tenant_members tm
|
||||
JOIN public.tenants t ON t.id = tm.tenant_id
|
||||
JOIN public.profiles p ON p.id = tm.user_id
|
||||
WHERE t.kind = 'therapist'
|
||||
AND tm.role = 'tenant_admin'
|
||||
AND tm.status = 'active'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM public.subscriptions s
|
||||
WHERE s.user_id = tm.user_id
|
||||
AND s.status = 'active'
|
||||
)
|
||||
LOOP
|
||||
RAISE NOTICE ' FALTANDO: % (%) — tenant %', r.full_name, r.user_id, r.tenant_id;
|
||||
END LOOP;
|
||||
|
||||
-- Clínicas sem plano
|
||||
RAISE NOTICE '';
|
||||
RAISE NOTICE '--- Clínicas SEM assinatura ativa ---';
|
||||
FOR r IN
|
||||
SELECT t.id, t.name, t.kind
|
||||
FROM public.tenants t
|
||||
WHERE t.kind IN ('clinic_coworking', 'clinic_reception', 'clinic_full', 'clinic')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM public.subscriptions s
|
||||
WHERE s.tenant_id = t.id
|
||||
AND s.status = 'active'
|
||||
)
|
||||
LOOP
|
||||
RAISE NOTICE ' FALTANDO: % (%) — kind %', r.name, r.id, r.kind;
|
||||
END LOOP;
|
||||
|
||||
-- Pacientes sem plano
|
||||
RAISE NOTICE '';
|
||||
RAISE NOTICE '--- Pacientes SEM assinatura ativa ---';
|
||||
FOR r IN
|
||||
SELECT p.id, p.full_name
|
||||
FROM public.profiles p
|
||||
WHERE p.account_type = 'patient'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM public.subscriptions s
|
||||
WHERE s.user_id = p.id
|
||||
AND s.status = 'active'
|
||||
)
|
||||
LOOP
|
||||
RAISE NOTICE ' FALTANDO: % (%)', r.full_name, r.id;
|
||||
END LOOP;
|
||||
|
||||
RAISE NOTICE '';
|
||||
RAISE NOTICE '=== FIM DO DIAGNÓSTICO — aplicando correções... ===';
|
||||
END;
|
||||
$$;
|
||||
|
||||
|
||||
-- ──────────────────────────────────────────────────────────────────────────────
|
||||
-- CORREÇÃO 1: Terapeutas sem assinatura → therapist_free
|
||||
-- Escopo: user_id do tenant_admin do tenant kind='therapist'
|
||||
-- ──────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
INSERT INTO public.subscriptions (
|
||||
user_id, plan_id, plan_key, status, interval,
|
||||
current_period_start, current_period_end,
|
||||
source, started_at, activated_at
|
||||
)
|
||||
SELECT
|
||||
tm.user_id,
|
||||
p.id,
|
||||
p.key,
|
||||
'active',
|
||||
'month',
|
||||
now(),
|
||||
now() + interval '30 days',
|
||||
'fix_seed',
|
||||
now(),
|
||||
now()
|
||||
FROM public.tenant_members tm
|
||||
JOIN public.tenants t ON t.id = tm.tenant_id
|
||||
JOIN public.plans p ON p.key = 'therapist_free'
|
||||
WHERE t.kind = 'therapist'
|
||||
AND tm.role = 'tenant_admin'
|
||||
AND tm.status = 'active'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM public.subscriptions s
|
||||
WHERE s.user_id = tm.user_id
|
||||
AND s.status = 'active'
|
||||
);
|
||||
|
||||
|
||||
-- ──────────────────────────────────────────────────────────────────────────────
|
||||
-- CORREÇÃO 2: Clínicas sem assinatura → clinic_free
|
||||
-- Escopo: tenant_id
|
||||
-- ──────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
INSERT INTO public.subscriptions (
|
||||
tenant_id, plan_id, plan_key, status, interval,
|
||||
current_period_start, current_period_end,
|
||||
source, started_at, activated_at
|
||||
)
|
||||
SELECT
|
||||
t.id,
|
||||
p.id,
|
||||
p.key,
|
||||
'active',
|
||||
'month',
|
||||
now(),
|
||||
now() + interval '30 days',
|
||||
'fix_seed',
|
||||
now(),
|
||||
now()
|
||||
FROM public.tenants t
|
||||
JOIN public.plans p ON p.key = 'clinic_free'
|
||||
WHERE t.kind IN ('clinic_coworking', 'clinic_reception', 'clinic_full', 'clinic')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM public.subscriptions s
|
||||
WHERE s.tenant_id = t.id
|
||||
AND s.status = 'active'
|
||||
);
|
||||
|
||||
|
||||
-- ──────────────────────────────────────────────────────────────────────────────
|
||||
-- CORREÇÃO 3: Pacientes sem assinatura → patient_free
|
||||
-- Escopo: user_id
|
||||
-- ──────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
INSERT INTO public.subscriptions (
|
||||
user_id, plan_id, plan_key, status, interval,
|
||||
current_period_start, current_period_end,
|
||||
source, started_at, activated_at
|
||||
)
|
||||
SELECT
|
||||
pr.id,
|
||||
p.id,
|
||||
p.key,
|
||||
'active',
|
||||
'month',
|
||||
now(),
|
||||
now() + interval '30 days',
|
||||
'fix_seed',
|
||||
now(),
|
||||
now()
|
||||
FROM public.profiles pr
|
||||
JOIN public.plans p ON p.key = 'patient_free'
|
||||
WHERE pr.account_type = 'patient'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM public.subscriptions s
|
||||
WHERE s.user_id = pr.id
|
||||
AND s.status = 'active'
|
||||
);
|
||||
|
||||
|
||||
-- ──────────────────────────────────────────────────────────────────────────────
|
||||
-- CONFIRMAÇÃO — mostra o que foi inserido (source = 'fix_seed')
|
||||
-- ──────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
r RECORD;
|
||||
total INT := 0;
|
||||
BEGIN
|
||||
RAISE NOTICE '';
|
||||
RAISE NOTICE '=== ASSINATURAS CRIADAS NESTA EXECUÇÃO ===';
|
||||
|
||||
FOR r IN
|
||||
SELECT
|
||||
s.plan_key,
|
||||
COALESCE(pr.full_name, t.name) AS nome,
|
||||
COALESCE(s.user_id::text, s.tenant_id::text) AS owner_id
|
||||
FROM public.subscriptions s
|
||||
LEFT JOIN public.profiles pr ON pr.id = s.user_id
|
||||
LEFT JOIN public.tenants t ON t.id = s.tenant_id
|
||||
WHERE s.source = 'fix_seed'
|
||||
AND s.started_at >= now() - interval '5 seconds'
|
||||
ORDER BY s.plan_key, nome
|
||||
LOOP
|
||||
RAISE NOTICE ' ✅ % → % (%)', r.plan_key, r.nome, r.owner_id;
|
||||
total := total + 1;
|
||||
END LOOP;
|
||||
|
||||
IF total = 0 THEN
|
||||
RAISE NOTICE ' (nenhuma nova assinatura criada — todos já tinham plano ativo)';
|
||||
ELSE
|
||||
RAISE NOTICE '';
|
||||
RAISE NOTICE ' Total: % assinatura(s) criada(s).', total;
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
|
||||
COMMIT;
|
||||
50
DBS/2026-03-11/Novo-DB/fix_subscriptions_validate_scope.sql
Normal file
50
DBS/2026-03-11/Novo-DB/fix_subscriptions_validate_scope.sql
Normal file
@@ -0,0 +1,50 @@
|
||||
-- Fix: subscriptions_validate_scope — adiciona suporte a target='patient'
|
||||
|
||||
CREATE OR REPLACE FUNCTION public.subscriptions_validate_scope()
|
||||
RETURNS trigger
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
DECLARE
|
||||
v_target text;
|
||||
BEGIN
|
||||
SELECT lower(p.target) INTO v_target
|
||||
FROM public.plans p
|
||||
WHERE p.id = NEW.plan_id;
|
||||
|
||||
IF v_target IS NULL THEN
|
||||
RAISE EXCEPTION 'Plano inválido (target nulo).';
|
||||
END IF;
|
||||
|
||||
IF v_target = 'clinic' THEN
|
||||
IF NEW.tenant_id IS NULL THEN
|
||||
RAISE EXCEPTION 'Assinatura clinic exige tenant_id.';
|
||||
END IF;
|
||||
IF NEW.user_id IS NOT NULL THEN
|
||||
RAISE EXCEPTION 'Assinatura clinic não pode ter user_id (XOR).';
|
||||
END IF;
|
||||
|
||||
ELSIF v_target = 'therapist' THEN
|
||||
IF NEW.tenant_id IS NOT NULL THEN
|
||||
RAISE EXCEPTION 'Assinatura therapist não deve ter tenant_id.';
|
||||
END IF;
|
||||
IF NEW.user_id IS NULL THEN
|
||||
RAISE EXCEPTION 'Assinatura therapist exige user_id.';
|
||||
END IF;
|
||||
|
||||
ELSIF v_target = 'patient' THEN
|
||||
IF NEW.tenant_id IS NOT NULL THEN
|
||||
RAISE EXCEPTION 'Assinatura patient não deve ter tenant_id.';
|
||||
END IF;
|
||||
IF NEW.user_id IS NULL THEN
|
||||
RAISE EXCEPTION 'Assinatura patient exige user_id.';
|
||||
END IF;
|
||||
|
||||
ELSE
|
||||
RAISE EXCEPTION 'Target de plano inválido: %', v_target;
|
||||
END IF;
|
||||
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
|
||||
ALTER FUNCTION public.subscriptions_validate_scope() OWNER TO supabase_admin;
|
||||
296
DBS/2026-03-11/Novo-DB/migration_001.sql
Normal file
296
DBS/2026-03-11/Novo-DB/migration_001.sql
Normal file
@@ -0,0 +1,296 @@
|
||||
-- =============================================================================
|
||||
-- SEED 001 — Usuários fictícios para teste
|
||||
-- =============================================================================
|
||||
-- Execute APÓS migration_001.sql
|
||||
--
|
||||
-- Usuários criados:
|
||||
-- paciente@agenciapsi.com.br senha: Teste@123 → patient
|
||||
-- terapeuta@agenciapsi.com.br senha: Teste@123 → therapist
|
||||
-- clinica1@agenciapsi.com.br senha: Teste@123 → clinic_coworking
|
||||
-- clinica2@agenciapsi.com.br senha: Teste@123 → clinic_reception
|
||||
-- clinica3@agenciapsi.com.br senha: Teste@123 → clinic_full
|
||||
-- saas@agenciapsi.com.br senha: Teste@123 → saas_admin
|
||||
-- =============================================================================
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- Limpeza de seeds anteriores
|
||||
-- ============================================================
|
||||
|
||||
ALTER TABLE public.patient_groups DISABLE TRIGGER ALL;
|
||||
|
||||
DELETE FROM public.tenant_members
|
||||
WHERE user_id IN (
|
||||
SELECT id FROM auth.users
|
||||
WHERE email IN (
|
||||
'paciente@agenciapsi.com.br',
|
||||
'terapeuta@agenciapsi.com.br',
|
||||
'clinica1@agenciapsi.com.br',
|
||||
'clinica2@agenciapsi.com.br',
|
||||
'clinica3@agenciapsi.com.br',
|
||||
'saas@agenciapsi.com.br'
|
||||
)
|
||||
);
|
||||
|
||||
DELETE FROM public.tenants WHERE id IN (
|
||||
'bbbbbbbb-0002-0002-0002-000000000002',
|
||||
'bbbbbbbb-0003-0003-0003-000000000003',
|
||||
'bbbbbbbb-0004-0004-0004-000000000004',
|
||||
'bbbbbbbb-0005-0005-0005-000000000005'
|
||||
);
|
||||
|
||||
DELETE FROM auth.users WHERE email IN (
|
||||
'paciente@agenciapsi.com.br',
|
||||
'terapeuta@agenciapsi.com.br',
|
||||
'clinica1@agenciapsi.com.br',
|
||||
'clinica2@agenciapsi.com.br',
|
||||
'clinica3@agenciapsi.com.br',
|
||||
'saas@agenciapsi.com.br'
|
||||
);
|
||||
|
||||
ALTER TABLE public.patient_groups ENABLE TRIGGER ALL;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 1. Usuários no auth.users
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO auth.users (
|
||||
id, email, encrypted_password, email_confirmed_at,
|
||||
created_at, updated_at, raw_user_meta_data, role, aud
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
'aaaaaaaa-0001-0001-0001-000000000001',
|
||||
'paciente@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(),
|
||||
'{"name": "Ana Paciente"}'::jsonb,
|
||||
'authenticated', 'authenticated'
|
||||
),
|
||||
(
|
||||
'aaaaaaaa-0002-0002-0002-000000000002',
|
||||
'terapeuta@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(),
|
||||
'{"name": "Bruno Terapeuta"}'::jsonb,
|
||||
'authenticated', 'authenticated'
|
||||
),
|
||||
(
|
||||
'aaaaaaaa-0003-0003-0003-000000000003',
|
||||
'clinica1@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(),
|
||||
'{"name": "Clinica Espaco Psi"}'::jsonb,
|
||||
'authenticated', 'authenticated'
|
||||
),
|
||||
(
|
||||
'aaaaaaaa-0004-0004-0004-000000000004',
|
||||
'clinica2@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(),
|
||||
'{"name": "Clinica Mente Sa"}'::jsonb,
|
||||
'authenticated', 'authenticated'
|
||||
),
|
||||
(
|
||||
'aaaaaaaa-0005-0005-0005-000000000005',
|
||||
'clinica3@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(),
|
||||
'{"name": "Clinica Bem Estar"}'::jsonb,
|
||||
'authenticated', 'authenticated'
|
||||
),
|
||||
(
|
||||
'aaaaaaaa-0006-0006-0006-000000000006',
|
||||
'saas@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(),
|
||||
'{"name": "Admin Plataforma"}'::jsonb,
|
||||
'authenticated', 'authenticated'
|
||||
);
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 2. Profiles
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.profiles (id, role, account_type, full_name)
|
||||
VALUES
|
||||
('aaaaaaaa-0001-0001-0001-000000000001', 'portal_user', 'patient', 'Ana Paciente'),
|
||||
('aaaaaaaa-0002-0002-0002-000000000002', 'portal_user', 'therapist', 'Bruno Terapeuta'),
|
||||
('aaaaaaaa-0003-0003-0003-000000000003', 'portal_user', 'clinic', 'Clinica Espaco Psi'),
|
||||
('aaaaaaaa-0004-0004-0004-000000000004', 'portal_user', 'clinic', 'Clinica Mente Sa'),
|
||||
('aaaaaaaa-0005-0005-0005-000000000005', 'portal_user', 'clinic', 'Clinica Bem Estar'),
|
||||
('aaaaaaaa-0006-0006-0006-000000000006', 'saas_admin', 'free', 'Admin Plataforma')
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
role = EXCLUDED.role,
|
||||
account_type = EXCLUDED.account_type,
|
||||
full_name = EXCLUDED.full_name;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 3. SaaS Admin
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.saas_admins (user_id, created_at)
|
||||
VALUES ('aaaaaaaa-0006-0006-0006-000000000006', now())
|
||||
ON CONFLICT (user_id) DO NOTHING;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 4. Tenant do terapeuta
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.tenants (id, name, kind, created_at)
|
||||
VALUES ('bbbbbbbb-0002-0002-0002-000000000002', 'Bruno Terapeuta', 'therapist', now())
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES ('bbbbbbbb-0002-0002-0002-000000000002', 'aaaaaaaa-0002-0002-0002-000000000002', 'tenant_admin', 'active', now())
|
||||
ON CONFLICT (tenant_id, user_id) DO NOTHING;
|
||||
|
||||
DO $$ BEGIN
|
||||
PERFORM public.seed_determined_commitments('bbbbbbbb-0002-0002-0002-000000000002');
|
||||
END; $$;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 5. Tenant Clinica 1 — Coworking
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.tenants (id, name, kind, created_at)
|
||||
VALUES ('bbbbbbbb-0003-0003-0003-000000000003', 'Clinica Espaco Psi', 'clinic_coworking', now())
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES ('bbbbbbbb-0003-0003-0003-000000000003', 'aaaaaaaa-0003-0003-0003-000000000003', 'tenant_admin', 'active', now())
|
||||
ON CONFLICT (tenant_id, user_id) DO NOTHING;
|
||||
|
||||
DO $$ BEGIN
|
||||
PERFORM public.seed_determined_commitments('bbbbbbbb-0003-0003-0003-000000000003');
|
||||
END; $$;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 6. Tenant Clinica 2 — Recepcao
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.tenants (id, name, kind, created_at)
|
||||
VALUES ('bbbbbbbb-0004-0004-0004-000000000004', 'Clinica Mente Sa', 'clinic_reception', now())
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES ('bbbbbbbb-0004-0004-0004-000000000004', 'aaaaaaaa-0004-0004-0004-000000000004', 'tenant_admin', 'active', now())
|
||||
ON CONFLICT (tenant_id, user_id) DO NOTHING;
|
||||
|
||||
DO $$ BEGIN
|
||||
PERFORM public.seed_determined_commitments('bbbbbbbb-0004-0004-0004-000000000004');
|
||||
END; $$;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 7. Tenant Clinica 3 — Full
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.tenants (id, name, kind, created_at)
|
||||
VALUES ('bbbbbbbb-0005-0005-0005-000000000005', 'Clinica Bem Estar', 'clinic_full', now())
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES ('bbbbbbbb-0005-0005-0005-000000000005', 'aaaaaaaa-0005-0005-0005-000000000005', 'tenant_admin', 'active', now())
|
||||
ON CONFLICT (tenant_id, user_id) DO NOTHING;
|
||||
|
||||
DO $$ BEGIN
|
||||
PERFORM public.seed_determined_commitments('bbbbbbbb-0005-0005-0005-000000000005');
|
||||
END; $$;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 8. Subscriptions ativas
|
||||
-- ============================================================
|
||||
|
||||
-- Paciente → patient_free
|
||||
INSERT INTO public.subscriptions (
|
||||
user_id, plan_id, plan_key, status, interval,
|
||||
current_period_start, current_period_end, source, started_at, activated_at
|
||||
)
|
||||
SELECT
|
||||
'aaaaaaaa-0001-0001-0001-000000000001',
|
||||
p.id, p.key, 'active', 'month',
|
||||
now(), now() + interval '30 days', 'seed', now(), now()
|
||||
FROM public.plans p WHERE p.key = 'patient_free';
|
||||
|
||||
-- Terapeuta → therapist_free
|
||||
INSERT INTO public.subscriptions (
|
||||
user_id, plan_id, plan_key, status, interval,
|
||||
current_period_start, current_period_end, source, started_at, activated_at
|
||||
)
|
||||
SELECT
|
||||
'aaaaaaaa-0002-0002-0002-000000000002',
|
||||
p.id, p.key, 'active', 'month',
|
||||
now(), now() + interval '30 days', 'seed', now(), now()
|
||||
FROM public.plans p WHERE p.key = 'therapist_free';
|
||||
|
||||
-- Clinica 1 → clinic_free
|
||||
INSERT INTO public.subscriptions (
|
||||
tenant_id, plan_id, plan_key, status, interval,
|
||||
current_period_start, current_period_end, source, started_at, activated_at
|
||||
)
|
||||
SELECT
|
||||
'bbbbbbbb-0003-0003-0003-000000000003',
|
||||
p.id, p.key, 'active', 'month',
|
||||
now(), now() + interval '30 days', 'seed', now(), now()
|
||||
FROM public.plans p WHERE p.key = 'clinic_free';
|
||||
|
||||
-- Clinica 2 → clinic_free
|
||||
INSERT INTO public.subscriptions (
|
||||
tenant_id, plan_id, plan_key, status, interval,
|
||||
current_period_start, current_period_end, source, started_at, activated_at
|
||||
)
|
||||
SELECT
|
||||
'bbbbbbbb-0004-0004-0004-000000000004',
|
||||
p.id, p.key, 'active', 'month',
|
||||
now(), now() + interval '30 days', 'seed', now(), now()
|
||||
FROM public.plans p WHERE p.key = 'clinic_free';
|
||||
|
||||
-- Clinica 3 → clinic_free
|
||||
INSERT INTO public.subscriptions (
|
||||
tenant_id, plan_id, plan_key, status, interval,
|
||||
current_period_start, current_period_end, source, started_at, activated_at
|
||||
)
|
||||
SELECT
|
||||
'bbbbbbbb-0005-0005-0005-000000000005',
|
||||
p.id, p.key, 'active', 'month',
|
||||
now(), now() + interval '30 days', 'seed', now(), now()
|
||||
FROM public.plans p WHERE p.key = 'clinic_free';
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 9. Vincula terapeuta à Clinica 3 (exemplo de associacao)
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES (
|
||||
'bbbbbbbb-0005-0005-0005-000000000005',
|
||||
'aaaaaaaa-0002-0002-0002-000000000002',
|
||||
'therapist', 'active', now()
|
||||
)
|
||||
ON CONFLICT (tenant_id, user_id) DO NOTHING;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- Confirmacao
|
||||
-- ============================================================
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
RAISE NOTICE '✅ Seed aplicado com sucesso.';
|
||||
RAISE NOTICE ' paciente@agenciapsi.com.br → patient';
|
||||
RAISE NOTICE ' terapeuta@agenciapsi.com.br → therapist';
|
||||
RAISE NOTICE ' clinica1@agenciapsi.com.br → clinic_coworking';
|
||||
RAISE NOTICE ' clinica2@agenciapsi.com.br → clinic_reception';
|
||||
RAISE NOTICE ' clinica3@agenciapsi.com.br → clinic_full';
|
||||
RAISE NOTICE ' saas@agenciapsi.com.br → saas_admin';
|
||||
RAISE NOTICE ' Senha: Teste@123';
|
||||
END;
|
||||
$$;
|
||||
13
DBS/2026-03-11/Novo-DB/migration_002_layout_variant.sql
Normal file
13
DBS/2026-03-11/Novo-DB/migration_002_layout_variant.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
-- =============================================================================
|
||||
-- Migration 002 — Adiciona layout_variant em user_settings
|
||||
-- =============================================================================
|
||||
-- Execute no SQL Editor do Supabase (ou via Docker psql).
|
||||
-- Tolerante: usa IF NOT EXISTS / DEFAULT para não quebrar dados existentes.
|
||||
-- =============================================================================
|
||||
|
||||
ALTER TABLE public.user_settings
|
||||
ADD COLUMN IF NOT EXISTS layout_variant TEXT NOT NULL DEFAULT 'classic';
|
||||
|
||||
-- =============================================================================
|
||||
RAISE NOTICE '✅ Coluna layout_variant adicionada a user_settings.';
|
||||
-- =============================================================================
|
||||
334
DBS/2026-03-11/Novo-DB/seed_001.sql
Normal file
334
DBS/2026-03-11/Novo-DB/seed_001.sql
Normal file
@@ -0,0 +1,334 @@
|
||||
-- =============================================================================
|
||||
-- SEED — Usuários fictícios para teste
|
||||
-- =============================================================================
|
||||
-- IMPORTANTE: Execute APÓS a migration_001.sql
|
||||
-- IMPORTANTE: Requer extensão pgcrypto (já ativa no Supabase)
|
||||
--
|
||||
-- Cria os seguintes usuários de teste:
|
||||
--
|
||||
-- paciente@agenciapsi.com.br senha: Teste@123 → paciente
|
||||
-- terapeuta@agenciapsi.com.br senha: Teste@123 → terapeuta solo
|
||||
-- clinica1@agenciapsi.com.br senha: Teste@123 → clínica coworking
|
||||
-- clinica2@agenciapsi.com.br senha: Teste@123 → clínica com secretaria
|
||||
-- clinica3@agenciapsi.com.br senha: Teste@123 → clínica full
|
||||
-- saas@agenciapsi.com.br senha: Teste@123 → admin da plataforma
|
||||
--
|
||||
-- =============================================================================
|
||||
|
||||
BEGIN;
|
||||
|
||||
-- ============================================================
|
||||
-- Helper: cria usuário no auth.users + profile
|
||||
-- (Supabase não expõe auth.users diretamente, mas em SQL Editor
|
||||
-- com acesso de service_role podemos inserir diretamente)
|
||||
-- ============================================================
|
||||
|
||||
-- Limpa seeds anteriores se existirem
|
||||
DELETE FROM auth.users
|
||||
WHERE email IN (
|
||||
'paciente@agenciapsi.com.br',
|
||||
'terapeuta@agenciapsi.com.br',
|
||||
'clinica1@agenciapsi.com.br',
|
||||
'clinica2@agenciapsi.com.br',
|
||||
'clinica3@agenciapsi.com.br',
|
||||
'saas@agenciapsi.com.br'
|
||||
);
|
||||
|
||||
-- ============================================================
|
||||
-- 1. Cria usuários no auth.users
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO auth.users (
|
||||
instance_id,
|
||||
id,
|
||||
email,
|
||||
encrypted_password,
|
||||
email_confirmed_at,
|
||||
confirmed_at,
|
||||
created_at,
|
||||
updated_at,
|
||||
raw_user_meta_data,
|
||||
raw_app_meta_data,
|
||||
role,
|
||||
aud,
|
||||
is_sso_user,
|
||||
is_anonymous,
|
||||
confirmation_token,
|
||||
recovery_token,
|
||||
email_change_token_new,
|
||||
email_change_token_current,
|
||||
email_change
|
||||
)
|
||||
VALUES
|
||||
-- Paciente
|
||||
(
|
||||
'00000000-0000-0000-0000-000000000000',
|
||||
'aaaaaaaa-0001-0001-0001-000000000001',
|
||||
'paciente@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(), now(),
|
||||
'{"name": "Ana Paciente"}'::jsonb,
|
||||
'{"provider": "email", "providers": ["email"]}'::jsonb,
|
||||
'authenticated', 'authenticated', false, false, '', '', '', '', ''
|
||||
),
|
||||
-- Terapeuta
|
||||
(
|
||||
'00000000-0000-0000-0000-000000000000',
|
||||
'aaaaaaaa-0002-0002-0002-000000000002',
|
||||
'terapeuta@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(), now(),
|
||||
'{"name": "Bruno Terapeuta"}'::jsonb,
|
||||
'{"provider": "email", "providers": ["email"]}'::jsonb,
|
||||
'authenticated', 'authenticated', false, false, '', '', '', '', ''
|
||||
),
|
||||
-- Clínica 1 — Coworking
|
||||
(
|
||||
'00000000-0000-0000-0000-000000000000',
|
||||
'aaaaaaaa-0003-0003-0003-000000000003',
|
||||
'clinica1@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(), now(),
|
||||
'{"name": "Clínica Espaço Psi"}'::jsonb,
|
||||
'{"provider": "email", "providers": ["email"]}'::jsonb,
|
||||
'authenticated', 'authenticated', false, false, '', '', '', '', ''
|
||||
),
|
||||
-- Clínica 2 — Recepção
|
||||
(
|
||||
'00000000-0000-0000-0000-000000000000',
|
||||
'aaaaaaaa-0004-0004-0004-000000000004',
|
||||
'clinica2@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(), now(),
|
||||
'{"name": "Clínica Mente Sã"}'::jsonb,
|
||||
'{"provider": "email", "providers": ["email"]}'::jsonb,
|
||||
'authenticated', 'authenticated', false, false, '', '', '', '', ''
|
||||
),
|
||||
-- Clínica 3 — Full
|
||||
(
|
||||
'00000000-0000-0000-0000-000000000000',
|
||||
'aaaaaaaa-0005-0005-0005-000000000005',
|
||||
'clinica3@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(), now(),
|
||||
'{"name": "Clínica Bem Estar"}'::jsonb,
|
||||
'{"provider": "email", "providers": ["email"]}'::jsonb,
|
||||
'authenticated', 'authenticated', false, false, '', '', '', '', ''
|
||||
),
|
||||
-- SaaS Admin
|
||||
(
|
||||
'00000000-0000-0000-0000-000000000000',
|
||||
'aaaaaaaa-0006-0006-0006-000000000006',
|
||||
'saas@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(), now(),
|
||||
'{"name": "Admin Plataforma"}'::jsonb,
|
||||
'{"provider": "email", "providers": ["email"]}'::jsonb,
|
||||
'authenticated', 'authenticated', false, false, '', '', '', '', ''
|
||||
);
|
||||
|
||||
-- auth.identities (obrigatório para GoTrue reconhecer login email/senha)
|
||||
INSERT INTO auth.identities (id, user_id, provider_id, provider, identity_data, created_at, updated_at, last_sign_in_at)
|
||||
VALUES
|
||||
(gen_random_uuid(), 'aaaaaaaa-0001-0001-0001-000000000001', 'paciente@agenciapsi.com.br', 'email', '{"sub": "aaaaaaaa-0001-0001-0001-000000000001", "email": "paciente@agenciapsi.com.br", "email_verified": true}'::jsonb, now(), now(), now()),
|
||||
(gen_random_uuid(), 'aaaaaaaa-0002-0002-0002-000000000002', 'terapeuta@agenciapsi.com.br', 'email', '{"sub": "aaaaaaaa-0002-0002-0002-000000000002", "email": "terapeuta@agenciapsi.com.br", "email_verified": true}'::jsonb, now(), now(), now()),
|
||||
(gen_random_uuid(), 'aaaaaaaa-0003-0003-0003-000000000003', 'clinica1@agenciapsi.com.br', 'email', '{"sub": "aaaaaaaa-0003-0003-0003-000000000003", "email": "clinica1@agenciapsi.com.br", "email_verified": true}'::jsonb, now(), now(), now()),
|
||||
(gen_random_uuid(), 'aaaaaaaa-0004-0004-0004-000000000004', 'clinica2@agenciapsi.com.br', 'email', '{"sub": "aaaaaaaa-0004-0004-0004-000000000004", "email": "clinica2@agenciapsi.com.br", "email_verified": true}'::jsonb, now(), now(), now()),
|
||||
(gen_random_uuid(), 'aaaaaaaa-0005-0005-0005-000000000005', 'clinica3@agenciapsi.com.br', 'email', '{"sub": "aaaaaaaa-0005-0005-0005-000000000005", "email": "clinica3@agenciapsi.com.br", "email_verified": true}'::jsonb, now(), now(), now()),
|
||||
(gen_random_uuid(), 'aaaaaaaa-0006-0006-0006-000000000006', 'saas@agenciapsi.com.br', 'email', '{"sub": "aaaaaaaa-0006-0006-0006-000000000006", "email": "saas@agenciapsi.com.br", "email_verified": true}'::jsonb, now(), now(), now())
|
||||
ON CONFLICT (provider, provider_id) DO NOTHING;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 2. Profiles (o trigger handle_new_user não dispara em inserts
|
||||
-- diretos no auth.users via SQL, então criamos manualmente)
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.profiles (id, role, account_type, full_name)
|
||||
VALUES
|
||||
('aaaaaaaa-0001-0001-0001-000000000001', 'portal_user', 'patient', 'Ana Paciente'),
|
||||
('aaaaaaaa-0002-0002-0002-000000000002', 'tenant_member', 'therapist', 'Bruno Terapeuta'),
|
||||
('aaaaaaaa-0003-0003-0003-000000000003', 'tenant_member', 'clinic', 'Clínica Espaço Psi'),
|
||||
('aaaaaaaa-0004-0004-0004-000000000004', 'tenant_member', 'clinic', 'Clínica Mente Sã'),
|
||||
('aaaaaaaa-0005-0005-0005-000000000005', 'tenant_member', 'clinic', 'Clínica Bem Estar'),
|
||||
('aaaaaaaa-0006-0006-0006-000000000006', 'saas_admin', 'free', 'Admin Plataforma')
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
role = EXCLUDED.role,
|
||||
account_type = EXCLUDED.account_type,
|
||||
full_name = EXCLUDED.full_name;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 3. SaaS Admin na tabela saas_admins
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.saas_admins (user_id, created_at)
|
||||
VALUES ('aaaaaaaa-0006-0006-0006-000000000006', now())
|
||||
ON CONFLICT (user_id) DO NOTHING;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 4. Tenant do terapeuta
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.tenants (id, name, kind, created_at)
|
||||
VALUES ('bbbbbbbb-0002-0002-0002-000000000002', 'Bruno Terapeuta', 'therapist', now())
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES ('bbbbbbbb-0002-0002-0002-000000000002', 'aaaaaaaa-0002-0002-0002-000000000002', 'tenant_admin', 'active', now())
|
||||
ON CONFLICT (tenant_id, user_id) DO NOTHING;
|
||||
|
||||
DO $$ BEGIN PERFORM public.seed_determined_commitments('bbbbbbbb-0002-0002-0002-000000000002'); END $$;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 5. Tenant da Clínica 1 — Coworking
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.tenants (id, name, kind, created_at)
|
||||
VALUES ('bbbbbbbb-0003-0003-0003-000000000003', 'Clínica Espaço Psi', 'clinic_coworking', now())
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES ('bbbbbbbb-0003-0003-0003-000000000003', 'aaaaaaaa-0003-0003-0003-000000000003', 'tenant_admin', 'active', now())
|
||||
ON CONFLICT (tenant_id, user_id) DO NOTHING;
|
||||
|
||||
DO $$ BEGIN PERFORM public.seed_determined_commitments('bbbbbbbb-0003-0003-0003-000000000003'); END $$;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 6. Tenant da Clínica 2 — Recepção
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.tenants (id, name, kind, created_at)
|
||||
VALUES ('bbbbbbbb-0004-0004-0004-000000000004', 'Clínica Mente Sã', 'clinic_reception', now())
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES ('bbbbbbbb-0004-0004-0004-000000000004', 'aaaaaaaa-0004-0004-0004-000000000004', 'tenant_admin', 'active', now())
|
||||
ON CONFLICT (tenant_id, user_id) DO NOTHING;
|
||||
|
||||
DO $$ BEGIN PERFORM public.seed_determined_commitments('bbbbbbbb-0004-0004-0004-000000000004'); END $$;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 7. Tenant da Clínica 3 — Full
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.tenants (id, name, kind, created_at)
|
||||
VALUES ('bbbbbbbb-0005-0005-0005-000000000005', 'Clínica Bem Estar', 'clinic_full', now())
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES ('bbbbbbbb-0005-0005-0005-000000000005', 'aaaaaaaa-0005-0005-0005-000000000005', 'tenant_admin', 'active', now())
|
||||
ON CONFLICT (tenant_id, user_id) DO NOTHING;
|
||||
|
||||
DO $$ BEGIN PERFORM public.seed_determined_commitments('bbbbbbbb-0005-0005-0005-000000000005'); END $$;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 8. Subscriptions ativas para cada conta
|
||||
-- ============================================================
|
||||
|
||||
-- Terapeuta → plano therapist_free
|
||||
INSERT INTO public.subscriptions (
|
||||
user_id, plan_id, plan_key, status, interval,
|
||||
current_period_start, current_period_end,
|
||||
source, started_at, activated_at
|
||||
)
|
||||
SELECT
|
||||
'aaaaaaaa-0002-0002-0002-000000000002',
|
||||
p.id, p.key, 'active', 'month',
|
||||
now(), now() + interval '30 days',
|
||||
'seed', now(), now()
|
||||
FROM public.plans p WHERE p.key = 'therapist_free';
|
||||
|
||||
-- Clínica 1 → plano clinic_free
|
||||
INSERT INTO public.subscriptions (
|
||||
tenant_id, plan_id, plan_key, status, interval,
|
||||
current_period_start, current_period_end,
|
||||
source, started_at, activated_at
|
||||
)
|
||||
SELECT
|
||||
'bbbbbbbb-0003-0003-0003-000000000003',
|
||||
p.id, p.key, 'active', 'month',
|
||||
now(), now() + interval '30 days',
|
||||
'seed', now(), now()
|
||||
FROM public.plans p WHERE p.key = 'clinic_free';
|
||||
|
||||
-- Clínica 2 → plano clinic_free
|
||||
INSERT INTO public.subscriptions (
|
||||
tenant_id, plan_id, plan_key, status, interval,
|
||||
current_period_start, current_period_end,
|
||||
source, started_at, activated_at
|
||||
)
|
||||
SELECT
|
||||
'bbbbbbbb-0004-0004-0004-000000000004',
|
||||
p.id, p.key, 'active', 'month',
|
||||
now(), now() + interval '30 days',
|
||||
'seed', now(), now()
|
||||
FROM public.plans p WHERE p.key = 'clinic_free';
|
||||
|
||||
-- Clínica 3 → plano clinic_free
|
||||
INSERT INTO public.subscriptions (
|
||||
tenant_id, plan_id, plan_key, status, interval,
|
||||
current_period_start, current_period_end,
|
||||
source, started_at, activated_at
|
||||
)
|
||||
SELECT
|
||||
'bbbbbbbb-0005-0005-0005-000000000005',
|
||||
p.id, p.key, 'active', 'month',
|
||||
now(), now() + interval '30 days',
|
||||
'seed', now(), now()
|
||||
FROM public.plans p WHERE p.key = 'clinic_free';
|
||||
|
||||
-- Paciente → plano patient_free
|
||||
INSERT INTO public.subscriptions (
|
||||
user_id, plan_id, plan_key, status, interval,
|
||||
current_period_start, current_period_end,
|
||||
source, started_at, activated_at
|
||||
)
|
||||
SELECT
|
||||
'aaaaaaaa-0001-0001-0001-000000000001',
|
||||
p.id, p.key, 'active', 'month',
|
||||
now(), now() + interval '30 days',
|
||||
'seed', now(), now()
|
||||
FROM public.plans p WHERE p.key = 'patient_free';
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 9. Vincula terapeuta à Clínica 3 (full) como exemplo
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES (
|
||||
'bbbbbbbb-0005-0005-0005-000000000005',
|
||||
'aaaaaaaa-0002-0002-0002-000000000002',
|
||||
'therapist',
|
||||
'active',
|
||||
now()
|
||||
)
|
||||
ON CONFLICT (tenant_id, user_id) DO NOTHING;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 10. Confirma
|
||||
-- ============================================================
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
RAISE NOTICE '✅ Seed aplicado com sucesso.';
|
||||
RAISE NOTICE '';
|
||||
RAISE NOTICE ' Usuários criados:';
|
||||
RAISE NOTICE ' paciente@agenciapsi.com.br → patient';
|
||||
RAISE NOTICE ' terapeuta@agenciapsi.com.br → therapist (tenant próprio + vinculado à Clínica 3)';
|
||||
RAISE NOTICE ' clinica1@agenciapsi.com.br → clinic_coworking';
|
||||
RAISE NOTICE ' clinica2@agenciapsi.com.br → clinic_reception';
|
||||
RAISE NOTICE ' clinica3@agenciapsi.com.br → clinic_full';
|
||||
RAISE NOTICE ' saas@agenciapsi.com.br → saas_admin';
|
||||
RAISE NOTICE ' Senha de todos: Teste@123';
|
||||
END;
|
||||
$$;
|
||||
|
||||
COMMIT;
|
||||
199
DBS/2026-03-11/Novo-DB/seed_002.sql
Normal file
199
DBS/2026-03-11/Novo-DB/seed_002.sql
Normal file
@@ -0,0 +1,199 @@
|
||||
-- =============================================================================
|
||||
-- SEED 002 — Supervisor e Editor
|
||||
-- =============================================================================
|
||||
-- Execute APÓS seed_001.sql
|
||||
-- Requer: pgcrypto (já ativo no Supabase)
|
||||
--
|
||||
-- Cria os seguintes usuários de teste:
|
||||
--
|
||||
-- supervisor@agenciapsi.com.br senha: Teste@123 → supervisor da Clínica 3
|
||||
-- editor@agenciapsi.com.br senha: Teste@123 → editor de conteúdo (plataforma)
|
||||
--
|
||||
-- UUIDs reservados:
|
||||
-- Supervisor → aaaaaaaa-0007-0007-0007-000000000007
|
||||
-- Editor → aaaaaaaa-0008-0008-0008-000000000008
|
||||
--
|
||||
-- =============================================================================
|
||||
|
||||
BEGIN;
|
||||
|
||||
-- ============================================================
|
||||
-- 0. Migration: adiciona platform_roles em profiles (se não existir)
|
||||
-- ============================================================
|
||||
|
||||
ALTER TABLE public.profiles
|
||||
ADD COLUMN IF NOT EXISTS platform_roles text[] NOT NULL DEFAULT '{}';
|
||||
|
||||
COMMENT ON COLUMN public.profiles.platform_roles IS
|
||||
'Papéis globais de plataforma, independentes de tenant. Ex: editor de microlearning. Atribuído pelo saas_admin.';
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 1. Remove seeds anteriores (idempotente)
|
||||
-- ============================================================
|
||||
|
||||
DELETE FROM auth.users
|
||||
WHERE email IN (
|
||||
'supervisor@agenciapsi.com.br',
|
||||
'editor@agenciapsi.com.br'
|
||||
);
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 2. Cria usuários no auth.users
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO auth.users (
|
||||
instance_id,
|
||||
id,
|
||||
email,
|
||||
encrypted_password,
|
||||
email_confirmed_at,
|
||||
created_at,
|
||||
updated_at,
|
||||
raw_user_meta_data,
|
||||
raw_app_meta_data,
|
||||
role,
|
||||
aud,
|
||||
is_sso_user,
|
||||
is_anonymous,
|
||||
confirmation_token,
|
||||
recovery_token,
|
||||
email_change_token_new,
|
||||
email_change_token_current,
|
||||
email_change
|
||||
)
|
||||
VALUES
|
||||
-- Supervisor
|
||||
(
|
||||
'00000000-0000-0000-0000-000000000000',
|
||||
'aaaaaaaa-0007-0007-0007-000000000007',
|
||||
'supervisor@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(),
|
||||
'{"name": "Carlos Supervisor"}'::jsonb,
|
||||
'{"provider": "email", "providers": ["email"]}'::jsonb,
|
||||
'authenticated', 'authenticated', false, false, '', '', '', '', ''
|
||||
),
|
||||
-- Editor de Conteúdo
|
||||
(
|
||||
'00000000-0000-0000-0000-000000000000',
|
||||
'aaaaaaaa-0008-0008-0008-000000000008',
|
||||
'editor@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(),
|
||||
'{"name": "Diana Editora"}'::jsonb,
|
||||
'{"provider": "email", "providers": ["email"]}'::jsonb,
|
||||
'authenticated', 'authenticated', false, false, '', '', '', '', ''
|
||||
);
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 3. auth.identities (obrigatório para GoTrue reconhecer login)
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO auth.identities (id, user_id, provider_id, provider, identity_data, created_at, updated_at, last_sign_in_at)
|
||||
VALUES
|
||||
(
|
||||
gen_random_uuid(),
|
||||
'aaaaaaaa-0007-0007-0007-000000000007',
|
||||
'supervisor@agenciapsi.com.br',
|
||||
'email',
|
||||
'{"sub": "aaaaaaaa-0007-0007-0007-000000000007", "email": "supervisor@agenciapsi.com.br", "email_verified": true}'::jsonb,
|
||||
now(), now(), now()
|
||||
),
|
||||
(
|
||||
gen_random_uuid(),
|
||||
'aaaaaaaa-0008-0008-0008-000000000008',
|
||||
'editor@agenciapsi.com.br',
|
||||
'email',
|
||||
'{"sub": "aaaaaaaa-0008-0008-0008-000000000008", "email": "editor@agenciapsi.com.br", "email_verified": true}'::jsonb,
|
||||
now(), now(), now()
|
||||
)
|
||||
ON CONFLICT (provider, provider_id) DO NOTHING;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 4. Profiles
|
||||
-- Supervisor → tenant_member (papel no tenant via tenant_members.role)
|
||||
-- Editor → tenant_member + platform_roles = '{editor}'
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.profiles (id, role, account_type, full_name, platform_roles)
|
||||
VALUES
|
||||
(
|
||||
'aaaaaaaa-0007-0007-0007-000000000007',
|
||||
'tenant_member',
|
||||
'therapist',
|
||||
'Carlos Supervisor',
|
||||
'{}'
|
||||
),
|
||||
(
|
||||
'aaaaaaaa-0008-0008-0008-000000000008',
|
||||
'tenant_member',
|
||||
'therapist',
|
||||
'Diana Editora',
|
||||
'{editor}' -- permissão de plataforma: acesso à área do editor
|
||||
)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
role = EXCLUDED.role,
|
||||
account_type = EXCLUDED.account_type,
|
||||
full_name = EXCLUDED.full_name,
|
||||
platform_roles = EXCLUDED.platform_roles;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 5. Vincula Supervisor à Clínica 3 (Full) com role 'supervisor'
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES (
|
||||
'bbbbbbbb-0005-0005-0005-000000000005', -- Clínica Bem Estar (Full)
|
||||
'aaaaaaaa-0007-0007-0007-000000000007', -- Carlos Supervisor
|
||||
'supervisor',
|
||||
'active',
|
||||
now()
|
||||
)
|
||||
ON CONFLICT (tenant_id, user_id) DO UPDATE SET
|
||||
role = EXCLUDED.role,
|
||||
status = EXCLUDED.status;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 6. Vincula Editor à Clínica 3 como terapeuta
|
||||
-- (contexto de tenant para o editor poder usar /therapist também,
|
||||
-- se necessário. O papel de editor vem de platform_roles.)
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES (
|
||||
'bbbbbbbb-0005-0005-0005-000000000005', -- Clínica Bem Estar (Full)
|
||||
'aaaaaaaa-0008-0008-0008-000000000008', -- Diana Editora
|
||||
'therapist',
|
||||
'active',
|
||||
now()
|
||||
)
|
||||
ON CONFLICT (tenant_id, user_id) DO UPDATE SET
|
||||
role = EXCLUDED.role,
|
||||
status = EXCLUDED.status;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 7. Confirma
|
||||
-- ============================================================
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
RAISE NOTICE '✅ Seed 002 aplicado com sucesso.';
|
||||
RAISE NOTICE '';
|
||||
RAISE NOTICE ' Migration aplicada:';
|
||||
RAISE NOTICE ' → profiles.platform_roles text[] adicionada (se não existia)';
|
||||
RAISE NOTICE '';
|
||||
RAISE NOTICE ' Usuários criados:';
|
||||
RAISE NOTICE ' supervisor@agenciapsi.com.br → supervisor da Clínica Bem Estar (Full)';
|
||||
RAISE NOTICE ' editor@agenciapsi.com.br → editor de conteúdo (platform_roles = {editor})';
|
||||
RAISE NOTICE ' Senha de todos: Teste@123';
|
||||
END;
|
||||
$$;
|
||||
|
||||
COMMIT;
|
||||
283
DBS/2026-03-11/Novo-DB/seed_003.sql
Normal file
283
DBS/2026-03-11/Novo-DB/seed_003.sql
Normal file
@@ -0,0 +1,283 @@
|
||||
-- =============================================================================
|
||||
-- SEED 003 — Terapeuta 2, Terapeuta 3 e Secretária
|
||||
-- =============================================================================
|
||||
-- Execute APÓS seed_001.sql (e seed_002.sql se quiser todos os seeds)
|
||||
-- Requer: pgcrypto (já ativo no Supabase)
|
||||
--
|
||||
-- Cria os seguintes usuários de teste:
|
||||
--
|
||||
-- therapist2@agenciapsi.com.br senha: Teste@123 → terapeuta 2 (tenant próprio + Clínica 3)
|
||||
-- therapist3@agenciapsi.com.br senha: Teste@123 → terapeuta 3 (tenant próprio + Clínica 3)
|
||||
-- secretary@agenciapsi.com.br senha: Teste@123 → clinic_admin na Clínica 2 (Mente Sã)
|
||||
--
|
||||
-- UUIDs reservados:
|
||||
-- Terapeuta 2 → aaaaaaaa-0009-0009-0009-000000000009
|
||||
-- Terapeuta 3 → aaaaaaaa-0010-0010-0010-000000000010
|
||||
-- Secretária → aaaaaaaa-0011-0011-0011-000000000011
|
||||
-- Tenant Terapeuta 2 → bbbbbbbb-0009-0009-0009-000000000009
|
||||
-- Tenant Terapeuta 3 → bbbbbbbb-0010-0010-0010-000000000010
|
||||
-- =============================================================================
|
||||
|
||||
BEGIN;
|
||||
|
||||
-- ============================================================
|
||||
-- 1. Remove seeds anteriores (idempotente)
|
||||
-- ============================================================
|
||||
|
||||
DELETE FROM auth.users
|
||||
WHERE email IN (
|
||||
'therapist2@agenciapsi.com.br',
|
||||
'therapist3@agenciapsi.com.br',
|
||||
'secretary@agenciapsi.com.br'
|
||||
);
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 2. Cria usuários no auth.users
|
||||
-- ⚠️ confirmed_at é coluna gerada — NÃO incluir na lista
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO auth.users (
|
||||
instance_id,
|
||||
id,
|
||||
email,
|
||||
encrypted_password,
|
||||
email_confirmed_at,
|
||||
created_at,
|
||||
updated_at,
|
||||
raw_user_meta_data,
|
||||
raw_app_meta_data,
|
||||
role,
|
||||
aud,
|
||||
is_sso_user,
|
||||
is_anonymous,
|
||||
confirmation_token,
|
||||
recovery_token,
|
||||
email_change_token_new,
|
||||
email_change_token_current,
|
||||
email_change
|
||||
)
|
||||
VALUES
|
||||
-- Terapeuta 2
|
||||
(
|
||||
'00000000-0000-0000-0000-000000000000',
|
||||
'aaaaaaaa-0009-0009-0009-000000000009',
|
||||
'therapist2@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(),
|
||||
'{"name": "Eva Terapeuta"}'::jsonb,
|
||||
'{"provider": "email", "providers": ["email"]}'::jsonb,
|
||||
'authenticated', 'authenticated', false, false, '', '', '', '', ''
|
||||
),
|
||||
-- Terapeuta 3
|
||||
(
|
||||
'00000000-0000-0000-0000-000000000000',
|
||||
'aaaaaaaa-0010-0010-0010-000000000010',
|
||||
'therapist3@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(),
|
||||
'{"name": "Felipe Terapeuta"}'::jsonb,
|
||||
'{"provider": "email", "providers": ["email"]}'::jsonb,
|
||||
'authenticated', 'authenticated', false, false, '', '', '', '', ''
|
||||
),
|
||||
-- Secretária
|
||||
(
|
||||
'00000000-0000-0000-0000-000000000000',
|
||||
'aaaaaaaa-0011-0011-0011-000000000011',
|
||||
'secretary@agenciapsi.com.br',
|
||||
crypt('Teste@123', gen_salt('bf')),
|
||||
now(), now(), now(),
|
||||
'{"name": "Gabriela Secretária"}'::jsonb,
|
||||
'{"provider": "email", "providers": ["email"]}'::jsonb,
|
||||
'authenticated', 'authenticated', false, false, '', '', '', '', ''
|
||||
);
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 3. auth.identities (obrigatório para GoTrue reconhecer login)
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO auth.identities (id, user_id, provider_id, provider, identity_data, created_at, updated_at, last_sign_in_at)
|
||||
VALUES
|
||||
(
|
||||
gen_random_uuid(),
|
||||
'aaaaaaaa-0009-0009-0009-000000000009',
|
||||
'therapist2@agenciapsi.com.br',
|
||||
'email',
|
||||
'{"sub": "aaaaaaaa-0009-0009-0009-000000000009", "email": "therapist2@agenciapsi.com.br", "email_verified": true}'::jsonb,
|
||||
now(), now(), now()
|
||||
),
|
||||
(
|
||||
gen_random_uuid(),
|
||||
'aaaaaaaa-0010-0010-0010-000000000010',
|
||||
'therapist3@agenciapsi.com.br',
|
||||
'email',
|
||||
'{"sub": "aaaaaaaa-0010-0010-0010-000000000010", "email": "therapist3@agenciapsi.com.br", "email_verified": true}'::jsonb,
|
||||
now(), now(), now()
|
||||
),
|
||||
(
|
||||
gen_random_uuid(),
|
||||
'aaaaaaaa-0011-0011-0011-000000000011',
|
||||
'secretary@agenciapsi.com.br',
|
||||
'email',
|
||||
'{"sub": "aaaaaaaa-0011-0011-0011-000000000011", "email": "secretary@agenciapsi.com.br", "email_verified": true}'::jsonb,
|
||||
now(), now(), now()
|
||||
)
|
||||
ON CONFLICT (provider, provider_id) DO NOTHING;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 4. Profiles
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.profiles (id, role, account_type, full_name)
|
||||
VALUES
|
||||
(
|
||||
'aaaaaaaa-0009-0009-0009-000000000009',
|
||||
'tenant_member',
|
||||
'therapist',
|
||||
'Eva Terapeuta'
|
||||
),
|
||||
(
|
||||
'aaaaaaaa-0010-0010-0010-000000000010',
|
||||
'tenant_member',
|
||||
'therapist',
|
||||
'Felipe Terapeuta'
|
||||
),
|
||||
(
|
||||
'aaaaaaaa-0011-0011-0011-000000000011',
|
||||
'tenant_member',
|
||||
'therapist',
|
||||
'Gabriela Secretária'
|
||||
)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
role = EXCLUDED.role,
|
||||
account_type = EXCLUDED.account_type,
|
||||
full_name = EXCLUDED.full_name;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 5. Tenants pessoais dos Terapeutas 2 e 3
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.tenants (id, name, kind, created_at)
|
||||
VALUES
|
||||
('bbbbbbbb-0009-0009-0009-000000000009', 'Eva Terapeuta', 'therapist', now()),
|
||||
('bbbbbbbb-0010-0010-0010-000000000010', 'Felipe Terapeuta', 'therapist', now())
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
-- Terapeuta 2 → tenant_admin do próprio tenant
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES (
|
||||
'bbbbbbbb-0009-0009-0009-000000000009',
|
||||
'aaaaaaaa-0009-0009-0009-000000000009',
|
||||
'tenant_admin', 'active', now()
|
||||
)
|
||||
ON CONFLICT (tenant_id, user_id) DO NOTHING;
|
||||
|
||||
-- Terapeuta 3 → tenant_admin do próprio tenant
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES (
|
||||
'bbbbbbbb-0010-0010-0010-000000000010',
|
||||
'aaaaaaaa-0010-0010-0010-000000000010',
|
||||
'tenant_admin', 'active', now()
|
||||
)
|
||||
ON CONFLICT (tenant_id, user_id) DO NOTHING;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 6. Vincula Terapeutas 2 e 3 à Clínica 3 — Full
|
||||
-- (mesmo padrão de terapeuta@agenciapsi.com.br no seed_001)
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES
|
||||
(
|
||||
'bbbbbbbb-0005-0005-0005-000000000005', -- Clínica Bem Estar (Full)
|
||||
'aaaaaaaa-0009-0009-0009-000000000009', -- Eva Terapeuta
|
||||
'therapist', 'active', now()
|
||||
),
|
||||
(
|
||||
'bbbbbbbb-0005-0005-0005-000000000005', -- Clínica Bem Estar (Full)
|
||||
'aaaaaaaa-0010-0010-0010-000000000010', -- Felipe Terapeuta
|
||||
'therapist', 'active', now()
|
||||
)
|
||||
ON CONFLICT (tenant_id, user_id) DO NOTHING;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 7. Vincula Secretária à Clínica 2 (Recepção) como clinic_admin
|
||||
-- A secretária gerencia a recepção/agenda da clínica.
|
||||
-- Acessa a área /admin com o mesmo contexto de clinic_admin.
|
||||
-- ============================================================
|
||||
|
||||
INSERT INTO public.tenant_members (tenant_id, user_id, role, status, created_at)
|
||||
VALUES (
|
||||
'bbbbbbbb-0004-0004-0004-000000000004', -- Clínica Mente Sã (Recepção)
|
||||
'aaaaaaaa-0011-0011-0011-000000000011', -- Gabriela Secretária
|
||||
'clinic_admin', 'active', now()
|
||||
)
|
||||
ON CONFLICT (tenant_id, user_id) DO NOTHING;
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 8. Subscriptions
|
||||
-- Terapeutas 2 e 3 → therapist_free (escopo: user_id)
|
||||
-- Secretária → sem assinatura própria (usa o plano da Clínica 2)
|
||||
-- ============================================================
|
||||
|
||||
-- Terapeuta 2 → therapist_free
|
||||
INSERT INTO public.subscriptions (
|
||||
user_id, plan_id, plan_key, status, interval,
|
||||
current_period_start, current_period_end,
|
||||
source, started_at, activated_at
|
||||
)
|
||||
SELECT
|
||||
'aaaaaaaa-0009-0009-0009-000000000009',
|
||||
p.id, p.key, 'active', 'month',
|
||||
now(), now() + interval '30 days',
|
||||
'seed', now(), now()
|
||||
FROM public.plans p WHERE p.key = 'therapist_free'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM public.subscriptions s
|
||||
WHERE s.user_id = 'aaaaaaaa-0009-0009-0009-000000000009' AND s.status = 'active'
|
||||
);
|
||||
|
||||
-- Terapeuta 3 → therapist_free
|
||||
INSERT INTO public.subscriptions (
|
||||
user_id, plan_id, plan_key, status, interval,
|
||||
current_period_start, current_period_end,
|
||||
source, started_at, activated_at
|
||||
)
|
||||
SELECT
|
||||
'aaaaaaaa-0010-0010-0010-000000000010',
|
||||
p.id, p.key, 'active', 'month',
|
||||
now(), now() + interval '30 days',
|
||||
'seed', now(), now()
|
||||
FROM public.plans p WHERE p.key = 'therapist_free'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM public.subscriptions s
|
||||
WHERE s.user_id = 'aaaaaaaa-0010-0010-0010-000000000010' AND s.status = 'active'
|
||||
);
|
||||
|
||||
-- Nota: a Secretária não tem assinatura própria.
|
||||
-- O acesso vem do plano da Clínica 2 (tenant_id = bbbbbbbb-0004-0004-0004-000000000004).
|
||||
|
||||
|
||||
-- ============================================================
|
||||
-- 9. Confirma
|
||||
-- ============================================================
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
RAISE NOTICE '✅ Seed 003 aplicado com sucesso.';
|
||||
RAISE NOTICE '';
|
||||
RAISE NOTICE ' Usuários criados:';
|
||||
RAISE NOTICE ' therapist2@agenciapsi.com.br → tenant próprio (bbbbbbbb-0009) + Clínica 3 como therapist';
|
||||
RAISE NOTICE ' therapist3@agenciapsi.com.br → tenant próprio (bbbbbbbb-0010) + Clínica 3 como therapist';
|
||||
RAISE NOTICE ' secretary@agenciapsi.com.br → clinic_admin na Clínica 2 Mente Sã (bbbbbbbb-0004)';
|
||||
RAISE NOTICE ' Senha de todos: Teste@123';
|
||||
END;
|
||||
$$;
|
||||
|
||||
COMMIT;
|
||||
Reference in New Issue
Block a user