Correcao Sidebar Classico e Rail, Correcao Layout, Ajuste de Breakpoint para Tailwind, Ajuste AppTopbar, Ajuste Menu PopOver, Recriado Paleta de Cores, Inserido algumas animações leves, Reajuste Cor items NOVOS da tabela, Drawer Ajuda Corrigido no Logout, Whatsapp, sms, email, recursos extras

This commit is contained in:
Leonardo
2026-03-24 21:26:58 -03:00
parent a89d1f5560
commit 53a4980396
453 changed files with 121427 additions and 174407 deletions

View File

@@ -0,0 +1,472 @@
-- =============================================================================
-- AgenciaPsi — Tables — Agenda + Recorrências + Agendador Online
-- =============================================================================
-- agenda_bloqueios, agenda_configuracoes, agenda_eventos, agenda_excecoes,
-- agenda_online_slots, agenda_regras_semanais, agenda_slots_bloqueados_semanais,
-- agenda_slots_regras, recurrence_rules, recurrence_exceptions,
-- recurrence_rule_services, agendador_configuracoes, agendador_solicitacoes
-- =============================================================================
CREATE TABLE public.agenda_bloqueios (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
tenant_id uuid,
tipo text NOT NULL,
titulo text NOT NULL,
data_inicio date NOT NULL,
data_fim date,
hora_inicio time without time zone,
hora_fim time without time zone,
recorrente boolean DEFAULT false NOT NULL,
dia_semana smallint,
observacao text,
origem text DEFAULT 'manual'::text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT agenda_bloqueios_tipo_check CHECK ((tipo = ANY (ARRAY['feriado_nacional'::text, 'feriado_municipal'::text, 'bloqueio'::text])))
);
ALTER TABLE public.agenda_bloqueios OWNER TO supabase_admin;
--
-- Name: agenda_configuracoes; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.agenda_configuracoes (
owner_id uuid NOT NULL,
duracao_padrao_minutos integer DEFAULT 50 NOT NULL,
intervalo_padrao_minutos integer DEFAULT 0 NOT NULL,
timezone text DEFAULT 'America/Sao_Paulo'::text NOT NULL,
usar_horario_admin_custom boolean DEFAULT false NOT NULL,
admin_inicio_visualizacao time without time zone,
admin_fim_visualizacao time without time zone,
admin_slot_visual_minutos integer DEFAULT 30 NOT NULL,
online_ativo boolean DEFAULT false NOT NULL,
online_min_antecedencia_horas integer DEFAULT 24 NOT NULL,
online_max_dias_futuro integer DEFAULT 60 NOT NULL,
online_cancelar_ate_horas integer DEFAULT 12 NOT NULL,
online_reagendar_ate_horas integer DEFAULT 12 NOT NULL,
online_limite_agendamentos_futuros integer DEFAULT 1 NOT NULL,
online_modo text DEFAULT 'automatico'::text NOT NULL,
online_buffer_antes_min integer DEFAULT 0 NOT NULL,
online_buffer_depois_min integer DEFAULT 0 NOT NULL,
online_modalidade text DEFAULT 'ambos'::text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
usar_granularidade_custom boolean DEFAULT false NOT NULL,
granularidade_min integer,
setup_concluido boolean DEFAULT false NOT NULL,
setup_concluido_em timestamp with time zone,
agenda_view_mode text DEFAULT 'full_24h'::text NOT NULL,
agenda_custom_start time without time zone,
agenda_custom_end time without time zone,
session_duration_min integer DEFAULT 50 NOT NULL,
session_break_min integer DEFAULT 10 NOT NULL,
pausas_semanais jsonb DEFAULT '[]'::jsonb NOT NULL,
setup_clinica_concluido boolean DEFAULT false NOT NULL,
setup_clinica_concluido_em timestamp with time zone,
tenant_id uuid,
jornada_igual_todos boolean DEFAULT true,
slot_mode text DEFAULT 'fixed'::text NOT NULL,
CONSTRAINT agenda_configuracoes_admin_slot_visual_minutos_check CHECK ((admin_slot_visual_minutos = ANY (ARRAY[5, 10, 15, 20, 30, 60]))),
CONSTRAINT agenda_configuracoes_check CHECK (((usar_horario_admin_custom = false) OR ((admin_inicio_visualizacao IS NOT NULL) AND (admin_fim_visualizacao IS NOT NULL) AND (admin_fim_visualizacao > admin_inicio_visualizacao)))),
CONSTRAINT agenda_configuracoes_duracao_padrao_minutos_check CHECK (((duracao_padrao_minutos >= 10) AND (duracao_padrao_minutos <= 240))),
CONSTRAINT agenda_configuracoes_granularidade_min_check CHECK (((granularidade_min IS NULL) OR (granularidade_min = ANY (ARRAY[5, 10, 15, 20, 30, 45, 50, 60])))),
CONSTRAINT agenda_configuracoes_intervalo_padrao_minutos_check CHECK (((intervalo_padrao_minutos >= 0) AND (intervalo_padrao_minutos <= 120))),
CONSTRAINT agenda_configuracoes_online_buffer_antes_min_check CHECK (((online_buffer_antes_min >= 0) AND (online_buffer_antes_min <= 120))),
CONSTRAINT agenda_configuracoes_online_buffer_depois_min_check CHECK (((online_buffer_depois_min >= 0) AND (online_buffer_depois_min <= 120))),
CONSTRAINT agenda_configuracoes_online_cancelar_ate_horas_check CHECK (((online_cancelar_ate_horas >= 0) AND (online_cancelar_ate_horas <= 720))),
CONSTRAINT agenda_configuracoes_online_limite_agendamentos_futuros_check CHECK (((online_limite_agendamentos_futuros >= 1) AND (online_limite_agendamentos_futuros <= 10))),
CONSTRAINT agenda_configuracoes_online_max_dias_futuro_check CHECK (((online_max_dias_futuro >= 1) AND (online_max_dias_futuro <= 365))),
CONSTRAINT agenda_configuracoes_online_min_antecedencia_horas_check CHECK (((online_min_antecedencia_horas >= 0) AND (online_min_antecedencia_horas <= 720))),
CONSTRAINT agenda_configuracoes_online_modalidade_check CHECK ((online_modalidade = ANY (ARRAY['online'::text, 'presencial'::text, 'ambos'::text]))),
CONSTRAINT agenda_configuracoes_online_modo_check CHECK ((online_modo = ANY (ARRAY['automatico'::text, 'aprovacao'::text]))),
CONSTRAINT agenda_configuracoes_online_reagendar_ate_horas_check CHECK (((online_reagendar_ate_horas >= 0) AND (online_reagendar_ate_horas <= 720))),
CONSTRAINT agenda_configuracoes_slot_mode_chk CHECK ((slot_mode = ANY (ARRAY['fixed'::text, 'dynamic'::text]))),
CONSTRAINT session_break_min_chk CHECK (((session_break_min >= 0) AND (session_break_min <= 60))),
CONSTRAINT session_duration_min_chk CHECK (((session_duration_min >= 10) AND (session_duration_min <= 240)))
);
ALTER TABLE public.agenda_configuracoes OWNER TO supabase_admin;
--
-- Name: agenda_eventos; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.agenda_eventos (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
tipo public.tipo_evento_agenda DEFAULT 'sessao'::public.tipo_evento_agenda NOT NULL,
status public.status_evento_agenda DEFAULT 'agendado'::public.status_evento_agenda NOT NULL,
titulo text,
observacoes text,
inicio_em timestamp with time zone NOT NULL,
fim_em timestamp with time zone NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
terapeuta_id uuid,
tenant_id uuid NOT NULL,
visibility_scope text DEFAULT 'public'::text NOT NULL,
mirror_of_event_id uuid,
mirror_source text,
patient_id uuid,
determined_commitment_id uuid,
link_online text,
titulo_custom text,
extra_fields jsonb,
recurrence_id uuid,
recurrence_date date,
modalidade text DEFAULT 'presencial'::text,
price numeric(10,2),
billing_contract_id uuid,
billed boolean DEFAULT false NOT NULL,
services_customized boolean DEFAULT false NOT NULL,
insurance_plan_id uuid,
insurance_guide_number text,
insurance_value numeric(10,2),
insurance_plan_service_id uuid,
CONSTRAINT agenda_eventos_check CHECK ((fim_em > inicio_em))
);
ALTER TABLE public.agenda_eventos OWNER TO supabase_admin;
--
-- Name: COLUMN agenda_eventos.price; Type: COMMENT; Schema: public; Owner: supabase_admin
--
COMMENT ON COLUMN public.agenda_eventos.price IS 'Valor da sessão em BRL. Preenchido automaticamente pela tabela professional_pricing do profissional.';
--
-- Name: agenda_excecoes; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.agenda_excecoes (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
data date NOT NULL,
hora_inicio time without time zone,
hora_fim time without time zone,
tipo public.tipo_excecao_agenda NOT NULL,
motivo text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
status public.status_excecao_agenda DEFAULT 'ativo'::public.status_excecao_agenda NOT NULL,
fonte text DEFAULT 'manual'::text NOT NULL,
aplicavel_online boolean DEFAULT true NOT NULL,
tenant_id uuid NOT NULL,
CONSTRAINT agenda_excecoes_check CHECK ((((hora_inicio IS NULL) AND (hora_fim IS NULL)) OR ((hora_inicio IS NOT NULL) AND (hora_fim IS NOT NULL) AND (hora_fim > hora_inicio)))),
CONSTRAINT agenda_excecoes_fonte_check CHECK ((fonte = ANY (ARRAY['manual'::text, 'feriado_google'::text, 'sistema'::text])))
);
ALTER TABLE public.agenda_excecoes OWNER TO supabase_admin;
--
-- Name: agenda_online_slots; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.agenda_online_slots (
id bigint NOT NULL,
owner_id uuid NOT NULL,
weekday integer NOT NULL,
"time" time without time zone NOT NULL,
enabled boolean DEFAULT true NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
tenant_id uuid NOT NULL,
CONSTRAINT agenda_online_slots_weekday_check CHECK ((weekday = ANY (ARRAY[0, 1, 2, 3, 4, 5, 6])))
);
ALTER TABLE public.agenda_online_slots OWNER TO supabase_admin;
--
-- Name: agenda_online_slots_id_seq; Type: SEQUENCE; Schema: public; Owner: supabase_admin
--
CREATE SEQUENCE public.agenda_online_slots_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.agenda_online_slots_id_seq OWNER TO supabase_admin;
--
-- Name: agenda_online_slots_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: supabase_admin
--
ALTER SEQUENCE public.agenda_online_slots_id_seq OWNED BY public.agenda_online_slots.id;
--
-- Name: agenda_regras_semanais; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.agenda_regras_semanais (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
dia_semana smallint NOT NULL,
hora_inicio time without time zone NOT NULL,
hora_fim time without time zone NOT NULL,
modalidade text DEFAULT 'ambos'::text NOT NULL,
ativo boolean DEFAULT true NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
tenant_id uuid NOT NULL,
CONSTRAINT agenda_regras_semanais_check CHECK ((hora_fim > hora_inicio)),
CONSTRAINT agenda_regras_semanais_dia_semana_check CHECK (((dia_semana >= 0) AND (dia_semana <= 6))),
CONSTRAINT agenda_regras_semanais_modalidade_check CHECK (((modalidade = ANY (ARRAY['online'::text, 'presencial'::text, 'ambos'::text])) OR (modalidade IS NULL)))
);
ALTER TABLE public.agenda_regras_semanais OWNER TO supabase_admin;
--
-- Name: agenda_slots_bloqueados_semanais; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.agenda_slots_bloqueados_semanais (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
dia_semana smallint NOT NULL,
hora_inicio time without time zone NOT NULL,
motivo text,
ativo boolean DEFAULT true NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
tenant_id uuid NOT NULL,
CONSTRAINT agenda_slots_bloqueados_semanais_dia_semana_check CHECK (((dia_semana >= 0) AND (dia_semana <= 6)))
);
ALTER TABLE public.agenda_slots_bloqueados_semanais OWNER TO supabase_admin;
--
-- Name: agenda_slots_regras; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.agenda_slots_regras (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
dia_semana smallint NOT NULL,
passo_minutos integer NOT NULL,
offset_minutos integer DEFAULT 0 NOT NULL,
buffer_antes_min integer DEFAULT 0 NOT NULL,
buffer_depois_min integer DEFAULT 0 NOT NULL,
min_antecedencia_horas integer DEFAULT 0 NOT NULL,
ativo boolean DEFAULT true NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
tenant_id uuid NOT NULL,
CONSTRAINT agenda_slots_regras_buffer_antes_min_check CHECK (((buffer_antes_min >= 0) AND (buffer_antes_min <= 240))),
CONSTRAINT agenda_slots_regras_buffer_depois_min_check CHECK (((buffer_depois_min >= 0) AND (buffer_depois_min <= 240))),
CONSTRAINT agenda_slots_regras_dia_semana_check CHECK (((dia_semana >= 0) AND (dia_semana <= 6))),
CONSTRAINT agenda_slots_regras_min_antecedencia_horas_check CHECK (((min_antecedencia_horas >= 0) AND (min_antecedencia_horas <= 720))),
CONSTRAINT agenda_slots_regras_offset_minutos_check CHECK (((offset_minutos >= 0) AND (offset_minutos <= 55))),
CONSTRAINT agenda_slots_regras_passo_minutos_check CHECK (((passo_minutos >= 5) AND (passo_minutos <= 240)))
);
ALTER TABLE public.agenda_slots_regras OWNER TO supabase_admin;
--
-- Name: agendador_configuracoes; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.agendador_configuracoes (
owner_id uuid NOT NULL,
tenant_id uuid,
ativo boolean DEFAULT false NOT NULL,
link_slug text,
imagem_fundo_url text,
imagem_header_url text,
logomarca_url text,
cor_primaria text DEFAULT '#4b6bff'::text,
nome_exibicao text,
endereco text,
botao_como_chegar_ativo boolean DEFAULT true NOT NULL,
maps_url text,
modo_aprovacao text DEFAULT 'aprovacao'::text NOT NULL,
modalidade text DEFAULT 'presencial'::text NOT NULL,
tipos_habilitados jsonb DEFAULT '["primeira", "retorno"]'::jsonb NOT NULL,
duracao_sessao_min integer DEFAULT 50 NOT NULL,
antecedencia_minima_horas integer DEFAULT 24 NOT NULL,
prazo_resposta_horas integer DEFAULT 2 NOT NULL,
reserva_horas integer DEFAULT 2 NOT NULL,
pagamento_obrigatorio boolean DEFAULT false NOT NULL,
pix_chave text,
pix_countdown_minutos integer DEFAULT 20 NOT NULL,
triagem_motivo boolean DEFAULT true NOT NULL,
triagem_como_conheceu boolean DEFAULT false NOT NULL,
verificacao_email boolean DEFAULT false NOT NULL,
exigir_aceite_lgpd boolean DEFAULT true NOT NULL,
mensagem_boas_vindas text,
texto_como_se_preparar text,
texto_termos_lgpd text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
pagamento_modo text DEFAULT 'sem_pagamento'::text NOT NULL,
pagamento_metodos_visiveis text[] DEFAULT '{}'::text[] NOT NULL,
CONSTRAINT agendador_configuracoes_antecedencia_check CHECK (((antecedencia_minima_horas >= 0) AND (antecedencia_minima_horas <= 720))),
CONSTRAINT agendador_configuracoes_duracao_check CHECK (((duracao_sessao_min >= 10) AND (duracao_sessao_min <= 240))),
CONSTRAINT agendador_configuracoes_modalidade_check CHECK ((modalidade = ANY (ARRAY['presencial'::text, 'online'::text, 'ambos'::text]))),
CONSTRAINT agendador_configuracoes_modo_check CHECK ((modo_aprovacao = ANY (ARRAY['automatico'::text, 'aprovacao'::text]))),
CONSTRAINT agendador_configuracoes_pix_countdown_check CHECK (((pix_countdown_minutos >= 5) AND (pix_countdown_minutos <= 120))),
CONSTRAINT agendador_configuracoes_prazo_check CHECK (((prazo_resposta_horas >= 1) AND (prazo_resposta_horas <= 72))),
CONSTRAINT agendador_configuracoes_reserva_check CHECK (((reserva_horas >= 1) AND (reserva_horas <= 48)))
);
ALTER TABLE public.agendador_configuracoes OWNER TO supabase_admin;
--
-- Name: COLUMN agendador_configuracoes.pagamento_modo; Type: COMMENT; Schema: public; Owner: supabase_admin
--
COMMENT ON COLUMN public.agendador_configuracoes.pagamento_modo IS 'sem_pagamento | pagar_na_hora | pix_antecipado';
--
-- Name: COLUMN agendador_configuracoes.pagamento_metodos_visiveis; Type: COMMENT; Schema: public; Owner: supabase_admin
--
COMMENT ON COLUMN public.agendador_configuracoes.pagamento_metodos_visiveis IS 'Métodos exibidos ao paciente quando pagamento_modo = pagar_na_hora. Ex: {pix, deposito, dinheiro, cartao, convenio}';
--
-- Name: agendador_solicitacoes; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.agendador_solicitacoes (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
tenant_id uuid,
paciente_nome text NOT NULL,
paciente_sobrenome text,
paciente_email text NOT NULL,
paciente_celular text,
paciente_cpf text,
tipo text NOT NULL,
modalidade text NOT NULL,
data_solicitada date NOT NULL,
hora_solicitada time without time zone NOT NULL,
reservado_ate timestamp with time zone,
motivo text,
como_conheceu text,
pix_status text DEFAULT 'pendente'::text,
pix_pago_em timestamp with time zone,
status text DEFAULT 'pendente'::text NOT NULL,
recusado_motivo text,
autorizado_em timestamp with time zone,
autorizado_por uuid,
user_id uuid,
patient_id uuid,
evento_id uuid,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT agendador_sol_modalidade_check CHECK ((modalidade = ANY (ARRAY['presencial'::text, 'online'::text]))),
CONSTRAINT agendador_sol_pix_check CHECK (((pix_status IS NULL) OR (pix_status = ANY (ARRAY['pendente'::text, 'pago'::text, 'expirado'::text])))),
CONSTRAINT agendador_sol_status_check CHECK ((status = ANY (ARRAY['pendente'::text, 'autorizado'::text, 'recusado'::text, 'expirado'::text, 'convertido'::text]))),
CONSTRAINT agendador_sol_tipo_check CHECK ((tipo = ANY (ARRAY['primeira'::text, 'retorno'::text, 'reagendar'::text])))
);
ALTER TABLE public.agendador_solicitacoes OWNER TO supabase_admin;
--
-- Name: billing_contracts; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.recurrence_exceptions (
id uuid DEFAULT gen_random_uuid() NOT NULL,
recurrence_id uuid NOT NULL,
tenant_id uuid NOT NULL,
original_date date NOT NULL,
type public.recurrence_exception_type NOT NULL,
new_date date,
new_start_time time without time zone,
new_end_time time without time zone,
modalidade text,
observacoes text,
titulo_custom text,
extra_fields jsonb,
reason text,
agenda_evento_id uuid,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE public.recurrence_exceptions OWNER TO supabase_admin;
--
-- Name: recurrence_rule_services; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.recurrence_rule_services (
id uuid DEFAULT gen_random_uuid() NOT NULL,
rule_id uuid NOT NULL,
service_id uuid NOT NULL,
quantity integer DEFAULT 1 NOT NULL,
unit_price numeric(10,2) NOT NULL,
discount_pct numeric(5,2) DEFAULT 0,
discount_flat numeric(10,2) DEFAULT 0,
final_price numeric(10,2) NOT NULL,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT recurrence_rule_services_disc_flat_chk CHECK ((discount_flat >= (0)::numeric)),
CONSTRAINT recurrence_rule_services_disc_pct_chk CHECK (((discount_pct >= (0)::numeric) AND (discount_pct <= (100)::numeric))),
CONSTRAINT recurrence_rule_services_final_price_chk CHECK ((final_price >= (0)::numeric)),
CONSTRAINT recurrence_rule_services_quantity_chk CHECK ((quantity > 0))
);
ALTER TABLE public.recurrence_rule_services OWNER TO supabase_admin;
--
-- Name: recurrence_rules; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.recurrence_rules (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
owner_id uuid NOT NULL,
therapist_id uuid,
patient_id uuid,
determined_commitment_id uuid,
type public.recurrence_type DEFAULT 'weekly'::public.recurrence_type NOT NULL,
"interval" smallint DEFAULT 1 NOT NULL,
weekdays smallint[] DEFAULT '{}'::smallint[] NOT NULL,
start_time time without time zone NOT NULL,
end_time time without time zone NOT NULL,
timezone text DEFAULT 'America/Sao_Paulo'::text NOT NULL,
duration_min smallint DEFAULT 50 NOT NULL,
start_date date NOT NULL,
end_date date,
max_occurrences integer,
open_ended boolean DEFAULT true NOT NULL,
modalidade text DEFAULT 'presencial'::text,
titulo_custom text,
observacoes text,
extra_fields jsonb,
status text DEFAULT 'ativo'::text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
price numeric(10,2),
insurance_plan_id uuid,
insurance_guide_number text,
insurance_value numeric(10,2),
insurance_plan_service_id uuid,
CONSTRAINT recurrence_rules_dates_chk CHECK (((end_date IS NULL) OR (end_date >= start_date))),
CONSTRAINT recurrence_rules_interval_chk CHECK (("interval" >= 1)),
CONSTRAINT recurrence_rules_status_check CHECK ((status = ANY (ARRAY['ativo'::text, 'pausado'::text, 'cancelado'::text]))),
CONSTRAINT recurrence_rules_times_chk CHECK ((end_time > start_time))
);

View File

@@ -0,0 +1,608 @@
-- =============================================================================
-- AgenciaPsi — Tables — auth schema (Supabase GoTrue)
-- =============================================================================
-- auth.users, auth.identities, auth.sessions, auth.refresh_tokens,
-- auth.mfa_*, auth.saml_*, auth.sso_*, auth.flow_state, etc.
-- =============================================================================
CREATE TABLE auth.audit_log_entries (
instance_id uuid,
id uuid NOT NULL,
payload json,
created_at timestamp with time zone,
ip_address character varying(64) DEFAULT ''::character varying NOT NULL
);
ALTER TABLE auth.audit_log_entries OWNER TO supabase_auth_admin;
--
-- Name: TABLE audit_log_entries; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.audit_log_entries IS 'Auth: Audit trail for user actions.';
--
-- Name: flow_state; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.flow_state (
id uuid NOT NULL,
user_id uuid,
auth_code text,
code_challenge_method auth.code_challenge_method,
code_challenge text,
provider_type text NOT NULL,
provider_access_token text,
provider_refresh_token text,
created_at timestamp with time zone,
updated_at timestamp with time zone,
authentication_method text NOT NULL,
auth_code_issued_at timestamp with time zone,
invite_token text,
referrer text,
oauth_client_state_id uuid,
linking_target_id uuid,
email_optional boolean DEFAULT false NOT NULL
);
ALTER TABLE auth.flow_state OWNER TO supabase_auth_admin;
--
-- Name: TABLE flow_state; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.flow_state IS 'Stores metadata for all OAuth/SSO login flows';
--
-- Name: identities; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.identities (
provider_id text NOT NULL,
user_id uuid NOT NULL,
identity_data jsonb NOT NULL,
provider text NOT NULL,
last_sign_in_at timestamp with time zone,
created_at timestamp with time zone,
updated_at timestamp with time zone,
email text GENERATED ALWAYS AS (lower((identity_data ->> 'email'::text))) STORED,
id uuid DEFAULT gen_random_uuid() NOT NULL
);
ALTER TABLE auth.identities OWNER TO supabase_auth_admin;
--
-- Name: TABLE identities; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.identities IS 'Auth: Stores identities associated to a user.';
--
-- Name: COLUMN identities.email; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON COLUMN auth.identities.email IS 'Auth: Email is a generated column that references the optional email property in the identity_data';
--
-- Name: instances; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.instances (
id uuid NOT NULL,
uuid uuid,
raw_base_config text,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
ALTER TABLE auth.instances OWNER TO supabase_auth_admin;
--
-- Name: TABLE instances; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.instances IS 'Auth: Manages users across multiple sites.';
--
-- Name: mfa_amr_claims; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.mfa_amr_claims (
session_id uuid NOT NULL,
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL,
authentication_method text NOT NULL,
id uuid NOT NULL
);
ALTER TABLE auth.mfa_amr_claims OWNER TO supabase_auth_admin;
--
-- Name: TABLE mfa_amr_claims; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.mfa_amr_claims IS 'auth: stores authenticator method reference claims for multi factor authentication';
--
-- Name: mfa_challenges; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.mfa_challenges (
id uuid NOT NULL,
factor_id uuid NOT NULL,
created_at timestamp with time zone NOT NULL,
verified_at timestamp with time zone,
ip_address inet NOT NULL,
otp_code text,
web_authn_session_data jsonb
);
ALTER TABLE auth.mfa_challenges OWNER TO supabase_auth_admin;
--
-- Name: TABLE mfa_challenges; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.mfa_challenges IS 'auth: stores metadata about challenge requests made';
--
-- Name: mfa_factors; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.mfa_factors (
id uuid NOT NULL,
user_id uuid NOT NULL,
friendly_name text,
factor_type auth.factor_type NOT NULL,
status auth.factor_status NOT NULL,
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL,
secret text,
phone text,
last_challenged_at timestamp with time zone,
web_authn_credential jsonb,
web_authn_aaguid uuid,
last_webauthn_challenge_data jsonb
);
ALTER TABLE auth.mfa_factors OWNER TO supabase_auth_admin;
--
-- Name: TABLE mfa_factors; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.mfa_factors IS 'auth: stores metadata about factors';
--
-- Name: COLUMN mfa_factors.last_webauthn_challenge_data; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON COLUMN auth.mfa_factors.last_webauthn_challenge_data IS 'Stores the latest WebAuthn challenge data including attestation/assertion for customer verification';
--
-- Name: oauth_authorizations; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.oauth_authorizations (
id uuid NOT NULL,
authorization_id text NOT NULL,
client_id uuid NOT NULL,
user_id uuid,
redirect_uri text NOT NULL,
scope text NOT NULL,
state text,
resource text,
code_challenge text,
code_challenge_method auth.code_challenge_method,
response_type auth.oauth_response_type DEFAULT 'code'::auth.oauth_response_type NOT NULL,
status auth.oauth_authorization_status DEFAULT 'pending'::auth.oauth_authorization_status NOT NULL,
authorization_code text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
expires_at timestamp with time zone DEFAULT (now() + '00:03:00'::interval) NOT NULL,
approved_at timestamp with time zone,
nonce text,
CONSTRAINT oauth_authorizations_authorization_code_length CHECK ((char_length(authorization_code) <= 255)),
CONSTRAINT oauth_authorizations_code_challenge_length CHECK ((char_length(code_challenge) <= 128)),
CONSTRAINT oauth_authorizations_expires_at_future CHECK ((expires_at > created_at)),
CONSTRAINT oauth_authorizations_nonce_length CHECK ((char_length(nonce) <= 255)),
CONSTRAINT oauth_authorizations_redirect_uri_length CHECK ((char_length(redirect_uri) <= 2048)),
CONSTRAINT oauth_authorizations_resource_length CHECK ((char_length(resource) <= 2048)),
CONSTRAINT oauth_authorizations_scope_length CHECK ((char_length(scope) <= 4096)),
CONSTRAINT oauth_authorizations_state_length CHECK ((char_length(state) <= 4096))
);
ALTER TABLE auth.oauth_authorizations OWNER TO supabase_auth_admin;
--
-- Name: oauth_client_states; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.oauth_client_states (
id uuid NOT NULL,
provider_type text NOT NULL,
code_verifier text,
created_at timestamp with time zone NOT NULL
);
ALTER TABLE auth.oauth_client_states OWNER TO supabase_auth_admin;
--
-- Name: TABLE oauth_client_states; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.oauth_client_states IS 'Stores OAuth states for third-party provider authentication flows where Supabase acts as the OAuth client.';
--
-- Name: oauth_clients; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.oauth_clients (
id uuid NOT NULL,
client_secret_hash text,
registration_type auth.oauth_registration_type NOT NULL,
redirect_uris text NOT NULL,
grant_types text NOT NULL,
client_name text,
client_uri text,
logo_uri text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
deleted_at timestamp with time zone,
client_type auth.oauth_client_type DEFAULT 'confidential'::auth.oauth_client_type NOT NULL,
token_endpoint_auth_method text NOT NULL,
CONSTRAINT oauth_clients_client_name_length CHECK ((char_length(client_name) <= 1024)),
CONSTRAINT oauth_clients_client_uri_length CHECK ((char_length(client_uri) <= 2048)),
CONSTRAINT oauth_clients_logo_uri_length CHECK ((char_length(logo_uri) <= 2048)),
CONSTRAINT oauth_clients_token_endpoint_auth_method_check CHECK ((token_endpoint_auth_method = ANY (ARRAY['client_secret_basic'::text, 'client_secret_post'::text, 'none'::text])))
);
ALTER TABLE auth.oauth_clients OWNER TO supabase_auth_admin;
--
-- Name: oauth_consents; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.oauth_consents (
id uuid NOT NULL,
user_id uuid NOT NULL,
client_id uuid NOT NULL,
scopes text NOT NULL,
granted_at timestamp with time zone DEFAULT now() NOT NULL,
revoked_at timestamp with time zone,
CONSTRAINT oauth_consents_revoked_after_granted CHECK (((revoked_at IS NULL) OR (revoked_at >= granted_at))),
CONSTRAINT oauth_consents_scopes_length CHECK ((char_length(scopes) <= 2048)),
CONSTRAINT oauth_consents_scopes_not_empty CHECK ((char_length(TRIM(BOTH FROM scopes)) > 0))
);
ALTER TABLE auth.oauth_consents OWNER TO supabase_auth_admin;
--
-- Name: one_time_tokens; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.one_time_tokens (
id uuid NOT NULL,
user_id uuid NOT NULL,
token_type auth.one_time_token_type NOT NULL,
token_hash text NOT NULL,
relates_to text NOT NULL,
created_at timestamp without time zone DEFAULT now() NOT NULL,
updated_at timestamp without time zone DEFAULT now() NOT NULL,
CONSTRAINT one_time_tokens_token_hash_check CHECK ((char_length(token_hash) > 0))
);
ALTER TABLE auth.one_time_tokens OWNER TO supabase_auth_admin;
--
-- Name: refresh_tokens; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.refresh_tokens (
instance_id uuid,
id bigint NOT NULL,
token character varying(255),
user_id character varying(255),
revoked boolean,
created_at timestamp with time zone,
updated_at timestamp with time zone,
parent character varying(255),
session_id uuid
);
ALTER TABLE auth.refresh_tokens OWNER TO supabase_auth_admin;
--
-- Name: TABLE refresh_tokens; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.refresh_tokens IS 'Auth: Store of tokens used to refresh JWT tokens once they expire.';
--
-- Name: refresh_tokens_id_seq; Type: SEQUENCE; Schema: auth; Owner: supabase_auth_admin
--
CREATE SEQUENCE auth.refresh_tokens_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE auth.refresh_tokens_id_seq OWNER TO supabase_auth_admin;
--
-- Name: refresh_tokens_id_seq; Type: SEQUENCE OWNED BY; Schema: auth; Owner: supabase_auth_admin
--
ALTER SEQUENCE auth.refresh_tokens_id_seq OWNED BY auth.refresh_tokens.id;
--
-- Name: saml_providers; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.saml_providers (
id uuid NOT NULL,
sso_provider_id uuid NOT NULL,
entity_id text NOT NULL,
metadata_xml text NOT NULL,
metadata_url text,
attribute_mapping jsonb,
created_at timestamp with time zone,
updated_at timestamp with time zone,
name_id_format text,
CONSTRAINT "entity_id not empty" CHECK ((char_length(entity_id) > 0)),
CONSTRAINT "metadata_url not empty" CHECK (((metadata_url = NULL::text) OR (char_length(metadata_url) > 0))),
CONSTRAINT "metadata_xml not empty" CHECK ((char_length(metadata_xml) > 0))
);
ALTER TABLE auth.saml_providers OWNER TO supabase_auth_admin;
--
-- Name: TABLE saml_providers; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.saml_providers IS 'Auth: Manages SAML Identity Provider connections.';
--
-- Name: saml_relay_states; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.saml_relay_states (
id uuid NOT NULL,
sso_provider_id uuid NOT NULL,
request_id text NOT NULL,
for_email text,
redirect_to text,
created_at timestamp with time zone,
updated_at timestamp with time zone,
flow_state_id uuid,
CONSTRAINT "request_id not empty" CHECK ((char_length(request_id) > 0))
);
ALTER TABLE auth.saml_relay_states OWNER TO supabase_auth_admin;
--
-- Name: TABLE saml_relay_states; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.saml_relay_states IS 'Auth: Contains SAML Relay State information for each Service Provider initiated login.';
--
-- Name: schema_migrations; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.schema_migrations (
version character varying(255) NOT NULL
);
ALTER TABLE auth.schema_migrations OWNER TO supabase_auth_admin;
--
-- Name: TABLE schema_migrations; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.schema_migrations IS 'Auth: Manages updates to the auth system.';
--
-- Name: sessions; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.sessions (
id uuid NOT NULL,
user_id uuid NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone,
factor_id uuid,
aal auth.aal_level,
not_after timestamp with time zone,
refreshed_at timestamp without time zone,
user_agent text,
ip inet,
tag text,
oauth_client_id uuid,
refresh_token_hmac_key text,
refresh_token_counter bigint,
scopes text,
CONSTRAINT sessions_scopes_length CHECK ((char_length(scopes) <= 4096))
);
ALTER TABLE auth.sessions OWNER TO supabase_auth_admin;
--
-- Name: TABLE sessions; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.sessions IS 'Auth: Stores session data associated to a user.';
--
-- Name: COLUMN sessions.not_after; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON COLUMN auth.sessions.not_after IS 'Auth: Not after is a nullable column that contains a timestamp after which the session should be regarded as expired.';
--
-- Name: COLUMN sessions.refresh_token_hmac_key; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON COLUMN auth.sessions.refresh_token_hmac_key IS 'Holds a HMAC-SHA256 key used to sign refresh tokens for this session.';
--
-- Name: COLUMN sessions.refresh_token_counter; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON COLUMN auth.sessions.refresh_token_counter IS 'Holds the ID (counter) of the last issued refresh token.';
--
-- Name: sso_domains; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.sso_domains (
id uuid NOT NULL,
sso_provider_id uuid NOT NULL,
domain text NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone,
CONSTRAINT "domain not empty" CHECK ((char_length(domain) > 0))
);
ALTER TABLE auth.sso_domains OWNER TO supabase_auth_admin;
--
-- Name: TABLE sso_domains; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.sso_domains IS 'Auth: Manages SSO email address domain mapping to an SSO Identity Provider.';
--
-- Name: sso_providers; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.sso_providers (
id uuid NOT NULL,
resource_id text,
created_at timestamp with time zone,
updated_at timestamp with time zone,
disabled boolean,
CONSTRAINT "resource_id not empty" CHECK (((resource_id = NULL::text) OR (char_length(resource_id) > 0)))
);
ALTER TABLE auth.sso_providers OWNER TO supabase_auth_admin;
--
-- Name: TABLE sso_providers; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.sso_providers IS 'Auth: Manages SSO identity provider information; see saml_providers for SAML.';
--
-- Name: COLUMN sso_providers.resource_id; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON COLUMN auth.sso_providers.resource_id IS 'Auth: Uniquely identifies a SSO provider according to a user-chosen resource ID (case insensitive), useful in infrastructure as code.';
--
-- Name: users; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE auth.users (
instance_id uuid,
id uuid NOT NULL,
aud character varying(255),
role character varying(255),
email character varying(255),
encrypted_password character varying(255),
email_confirmed_at timestamp with time zone,
invited_at timestamp with time zone,
confirmation_token character varying(255),
confirmation_sent_at timestamp with time zone,
recovery_token character varying(255),
recovery_sent_at timestamp with time zone,
email_change_token_new character varying(255),
email_change character varying(255),
email_change_sent_at timestamp with time zone,
last_sign_in_at timestamp with time zone,
raw_app_meta_data jsonb,
raw_user_meta_data jsonb,
is_super_admin boolean,
created_at timestamp with time zone,
updated_at timestamp with time zone,
phone text DEFAULT NULL::character varying,
phone_confirmed_at timestamp with time zone,
phone_change text DEFAULT ''::character varying,
phone_change_token character varying(255) DEFAULT ''::character varying,
phone_change_sent_at timestamp with time zone,
confirmed_at timestamp with time zone GENERATED ALWAYS AS (LEAST(email_confirmed_at, phone_confirmed_at)) STORED,
email_change_token_current character varying(255) DEFAULT ''::character varying,
email_change_confirm_status smallint DEFAULT 0,
banned_until timestamp with time zone,
reauthentication_token character varying(255) DEFAULT ''::character varying,
reauthentication_sent_at timestamp with time zone,
is_sso_user boolean DEFAULT false NOT NULL,
deleted_at timestamp with time zone,
is_anonymous boolean DEFAULT false NOT NULL,
CONSTRAINT users_email_change_confirm_status_check CHECK (((email_change_confirm_status >= 0) AND (email_change_confirm_status <= 2)))
);
ALTER TABLE auth.users OWNER TO supabase_auth_admin;
--
-- Name: TABLE users; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON TABLE auth.users IS 'Auth: Stores user login data within a secure schema.';
--
-- Name: COLUMN users.is_sso_user; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--
COMMENT ON COLUMN auth.users.is_sso_user IS 'Auth: Set this column to true when the account comes from SSO. These accounts can have duplicate emails.';
--
-- Name: addon_credits; Type: TABLE; Schema: public; Owner: supabase_admin
--

View File

@@ -0,0 +1,77 @@
-- =============================================================================
-- AgenciaPsi — Tables — Compromissos Determinados
-- =============================================================================
-- determined_commitments, determined_commitment_fields,
-- commitment_services, commitment_time_logs
-- =============================================================================
CREATE TABLE public.determined_commitments (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
created_by uuid,
is_native boolean DEFAULT false NOT NULL,
native_key text,
is_locked boolean DEFAULT false NOT NULL,
active boolean DEFAULT true NOT NULL,
name text NOT NULL,
description text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
bg_color text,
text_color text
);
CREATE TABLE public.determined_commitment_fields (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
commitment_id uuid NOT NULL,
key text NOT NULL,
label text NOT NULL,
field_type public.determined_field_type DEFAULT 'text'::public.determined_field_type NOT NULL,
required boolean DEFAULT false NOT NULL,
sort_order integer DEFAULT 0 NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.commitment_services (
id uuid DEFAULT gen_random_uuid() NOT NULL,
commitment_id uuid NOT NULL,
service_id uuid NOT NULL,
quantity integer DEFAULT 1 NOT NULL,
unit_price numeric(10,2) NOT NULL,
discount_pct numeric(5,2) DEFAULT 0,
discount_flat numeric(10,2) DEFAULT 0,
final_price numeric(10,2) NOT NULL,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT commitment_services_disc_flat_chk CHECK ((discount_flat >= (0)::numeric)),
CONSTRAINT commitment_services_disc_pct_chk CHECK (((discount_pct >= (0)::numeric) AND (discount_pct <= (100)::numeric))),
CONSTRAINT commitment_services_final_price_chk CHECK ((final_price >= (0)::numeric)),
CONSTRAINT commitment_services_quantity_chk CHECK ((quantity > 0))
);
ALTER TABLE public.commitment_services OWNER TO supabase_admin;
--
-- Name: commitment_time_logs; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.commitment_time_logs (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
commitment_id uuid NOT NULL,
calendar_event_id uuid,
source public.commitment_log_source DEFAULT 'manual'::public.commitment_log_source NOT NULL,
started_at timestamp with time zone NOT NULL,
ended_at timestamp with time zone NOT NULL,
minutes integer NOT NULL,
created_by uuid,
created_at timestamp with time zone DEFAULT now() NOT NULL
);

View File

@@ -0,0 +1,171 @@
-- =============================================================================
-- AgenciaPsi — Tables — Core
-- =============================================================================
-- profiles, tenants, tenant_members, tenant_invites, tenant_features,
-- tenant_feature_exceptions_log, saas_admins, owner_users, user_settings,
-- company_profiles, dev_user_credentials
-- =============================================================================
CREATE TABLE public.profiles (
id uuid NOT NULL,
role text DEFAULT 'tenant_member'::text NOT NULL,
full_name text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
avatar_url text,
phone text,
bio text,
language text DEFAULT 'pt-BR'::text NOT NULL,
timezone text DEFAULT 'America/Sao_Paulo'::text NOT NULL,
notify_system_email boolean DEFAULT true NOT NULL,
notify_reminders boolean DEFAULT true NOT NULL,
notify_news boolean DEFAULT false NOT NULL,
account_type text DEFAULT 'free'::text NOT NULL,
platform_roles text[] DEFAULT '{}'::text[] NOT NULL,
nickname text,
work_description text,
work_description_other text,
site_url text,
social_instagram text,
social_youtube text,
social_facebook text,
social_x text,
social_custom jsonb DEFAULT '[]'::jsonb NOT NULL,
CONSTRAINT profiles_account_type_check CHECK ((account_type = ANY (ARRAY['free'::text, 'patient'::text, 'therapist'::text, 'clinic'::text]))),
CONSTRAINT profiles_role_check CHECK ((role = ANY (ARRAY['saas_admin'::text, 'tenant_member'::text, 'portal_user'::text, 'patient'::text])))
);
CREATE TABLE public.tenants (
id uuid DEFAULT gen_random_uuid() NOT NULL,
name text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
kind text DEFAULT 'saas'::text NOT NULL,
CONSTRAINT tenants_kind_check CHECK ((kind = ANY (ARRAY['therapist'::text, 'clinic_coworking'::text, 'clinic_reception'::text, 'clinic_full'::text, 'clinic'::text, 'saas'::text, 'supervisor'::text])))
);
CREATE TABLE public.tenant_members (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
user_id uuid NOT NULL,
role text NOT NULL,
status text DEFAULT 'active'::text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.tenant_invites (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
email text NOT NULL,
role text NOT NULL,
token uuid DEFAULT gen_random_uuid() NOT NULL,
invited_by uuid,
created_at timestamp with time zone DEFAULT now() NOT NULL,
expires_at timestamp with time zone DEFAULT (now() + '7 days'::interval) NOT NULL,
accepted_at timestamp with time zone,
accepted_by uuid,
revoked_at timestamp with time zone,
revoked_by uuid,
CONSTRAINT tenant_invites_role_check CHECK ((role = ANY (ARRAY['therapist'::text, 'secretary'::text])))
);
CREATE TABLE public.tenant_features (
tenant_id uuid NOT NULL,
feature_key text NOT NULL,
enabled boolean DEFAULT false NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.tenant_feature_exceptions_log (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
feature_key text NOT NULL,
enabled boolean NOT NULL,
reason text,
created_by uuid,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.saas_admins (
user_id uuid NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.owner_users (
owner_id uuid NOT NULL,
user_id uuid NOT NULL,
role text DEFAULT 'admin'::text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.user_settings (
user_id uuid NOT NULL,
theme_mode text DEFAULT 'dark'::text NOT NULL,
preset text DEFAULT 'Aura'::text NOT NULL,
primary_color text DEFAULT 'noir'::text NOT NULL,
surface_color text DEFAULT 'slate'::text NOT NULL,
menu_mode text DEFAULT 'static'::text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
layout_variant text DEFAULT 'classic'::text NOT NULL,
CONSTRAINT user_settings_layout_variant_check CHECK ((layout_variant = ANY (ARRAY['classic'::text, 'rail'::text]))),
CONSTRAINT user_settings_menu_mode_check CHECK ((menu_mode = ANY (ARRAY['static'::text, 'overlay'::text]))),
CONSTRAINT user_settings_theme_mode_check CHECK ((theme_mode = ANY (ARRAY['light'::text, 'dark'::text])))
);
CREATE TABLE public.company_profiles (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
nome_fantasia text,
razao_social text,
tipo_empresa text,
cnpj text,
ie text,
im text,
cep text,
logradouro text,
numero text,
complemento text,
bairro text,
cidade text,
estado text,
email text,
telefone text,
site text,
logo_url text,
redes_sociais jsonb DEFAULT '[]'::jsonb NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.dev_user_credentials (
id uuid DEFAULT gen_random_uuid() NOT NULL,
user_id uuid,
email text NOT NULL,
password_dev text NOT NULL,
kind text DEFAULT 'custom'::text NOT NULL,
note text,
created_at timestamp with time zone DEFAULT now() NOT NULL
);

View File

@@ -0,0 +1,199 @@
-- =============================================================================
-- AgenciaPsi — Tables — Financeiro
-- =============================================================================
-- financial_records, financial_categories, financial_exceptions,
-- payment_settings, professional_pricing, therapist_payouts,
-- therapist_payout_records, services, insurance_plans, insurance_plan_services
-- =============================================================================
CREATE TABLE public.financial_records (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
tenant_id uuid,
type public.financial_record_type DEFAULT 'receita'::public.financial_record_type NOT NULL,
amount numeric(10,2) NOT NULL,
description text,
category text,
payment_method text,
paid_at timestamp with time zone,
due_date date,
installments smallint DEFAULT 1,
installment_number smallint DEFAULT 1,
installment_group uuid,
agenda_evento_id uuid,
patient_id uuid,
clinic_fee_pct numeric(5,2) DEFAULT 0,
clinic_fee_amount numeric(10,2) DEFAULT 0,
net_amount numeric(10,2) GENERATED ALWAYS AS ((amount - clinic_fee_amount)) STORED,
insurance_plan_id uuid,
notes text,
tags text[],
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
deleted_at timestamp with time zone,
discount_amount numeric(10,2) DEFAULT 0 NOT NULL,
final_amount numeric(10,2) DEFAULT 0 NOT NULL,
status text DEFAULT 'pending'::text NOT NULL,
category_id uuid,
CONSTRAINT financial_records_amount_check CHECK ((amount >= (0)::numeric)),
CONSTRAINT financial_records_clinic_fee_amount_check CHECK ((clinic_fee_amount >= (0)::numeric)),
CONSTRAINT financial_records_clinic_fee_pct_check CHECK (((clinic_fee_pct >= (0)::numeric) AND (clinic_fee_pct <= (100)::numeric))),
CONSTRAINT financial_records_discount_amount_check CHECK ((discount_amount >= (0)::numeric)),
CONSTRAINT financial_records_final_amount_check CHECK ((final_amount >= (0)::numeric)),
CONSTRAINT financial_records_installments_check CHECK ((installments >= 1)),
CONSTRAINT financial_records_status_check CHECK ((status = ANY (ARRAY['pending'::text, 'paid'::text, 'partial'::text, 'overdue'::text, 'cancelled'::text, 'refunded'::text])))
);
CREATE TABLE public.financial_categories (
id uuid DEFAULT gen_random_uuid() NOT NULL,
user_id uuid NOT NULL,
name text NOT NULL,
type public.financial_record_type DEFAULT 'receita'::public.financial_record_type NOT NULL,
color text DEFAULT '#6366f1'::text,
icon text DEFAULT 'pi pi-tag'::text,
sort_order integer DEFAULT 0,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE public.financial_categories OWNER TO supabase_admin;
--
-- Name: financial_exceptions; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.financial_exceptions (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid,
tenant_id uuid NOT NULL,
exception_type text NOT NULL,
charge_mode text NOT NULL,
charge_value numeric(10,2),
charge_pct numeric(5,2),
min_hours_notice integer,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
CONSTRAINT financial_exceptions_charge_chk CHECK ((charge_mode = ANY (ARRAY['none'::text, 'full'::text, 'fixed_fee'::text, 'percentage'::text]))),
CONSTRAINT financial_exceptions_type_chk CHECK ((exception_type = ANY (ARRAY['patient_no_show'::text, 'patient_cancellation'::text, 'professional_cancellation'::text])))
);
CREATE TABLE public.payment_settings (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
tenant_id uuid,
pix_ativo boolean DEFAULT false NOT NULL,
pix_tipo text DEFAULT 'cpf'::text NOT NULL,
pix_chave text DEFAULT ''::text NOT NULL,
pix_nome_titular text DEFAULT ''::text NOT NULL,
deposito_ativo boolean DEFAULT false NOT NULL,
deposito_banco text DEFAULT ''::text NOT NULL,
deposito_agencia text DEFAULT ''::text NOT NULL,
deposito_conta text DEFAULT ''::text NOT NULL,
deposito_tipo_conta text DEFAULT 'corrente'::text NOT NULL,
deposito_titular text DEFAULT ''::text NOT NULL,
deposito_cpf_cnpj text DEFAULT ''::text NOT NULL,
dinheiro_ativo boolean DEFAULT false NOT NULL,
cartao_ativo boolean DEFAULT false NOT NULL,
cartao_instrucao text DEFAULT ''::text NOT NULL,
convenio_ativo boolean DEFAULT false NOT NULL,
convenio_lista text DEFAULT ''::text NOT NULL,
observacoes_pagamento text DEFAULT ''::text NOT NULL,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now()
);
CREATE TABLE public.professional_pricing (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
tenant_id uuid NOT NULL,
determined_commitment_id uuid,
price numeric(10,2) NOT NULL,
notes text,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now()
);
CREATE TABLE public.therapist_payouts (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
tenant_id uuid NOT NULL,
period_start date NOT NULL,
period_end date NOT NULL,
total_sessions integer DEFAULT 0 NOT NULL,
gross_amount numeric(10,2) DEFAULT 0 NOT NULL,
clinic_fee_total numeric(10,2) DEFAULT 0 NOT NULL,
net_amount numeric(10,2) DEFAULT 0 NOT NULL,
status text DEFAULT 'pending'::text NOT NULL,
paid_at timestamp with time zone,
notes text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT therapist_payouts_clinic_fee_total_check CHECK ((clinic_fee_total >= (0)::numeric)),
CONSTRAINT therapist_payouts_gross_amount_check CHECK ((gross_amount >= (0)::numeric)),
CONSTRAINT therapist_payouts_net_amount_check CHECK ((net_amount >= (0)::numeric)),
CONSTRAINT therapist_payouts_period_chk CHECK ((period_end >= period_start)),
CONSTRAINT therapist_payouts_status_check CHECK ((status = ANY (ARRAY['pending'::text, 'paid'::text, 'cancelled'::text])))
);
CREATE TABLE public.therapist_payout_records (
payout_id uuid NOT NULL,
financial_record_id uuid NOT NULL
);
CREATE TABLE public.services (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
tenant_id uuid NOT NULL,
name text NOT NULL,
description text,
price numeric(10,2) NOT NULL,
duration_min integer,
active boolean DEFAULT true NOT NULL,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now()
);
CREATE TABLE public.insurance_plan_services (
id uuid DEFAULT gen_random_uuid() NOT NULL,
insurance_plan_id uuid NOT NULL,
name text NOT NULL,
value numeric(10,2) NOT NULL,
active boolean DEFAULT true NOT NULL,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now()
);
ALTER TABLE public.insurance_plan_services OWNER TO supabase_admin;
--
-- Name: insurance_plans; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.insurance_plans (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
tenant_id uuid NOT NULL,
name text NOT NULL,
notes text,
default_value numeric(10,2),
active boolean DEFAULT true NOT NULL,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now()
);

View File

@@ -0,0 +1,500 @@
-- =============================================================================
-- AgenciaPsi — Tables — Infraestrutura (realtime, storage, supabase_functions)
-- =============================================================================
CREATE TABLE _realtime.extensions (
id uuid NOT NULL,
type text,
settings jsonb,
tenant_external_id text,
inserted_at timestamp(0) without time zone NOT NULL,
updated_at timestamp(0) without time zone NOT NULL
);
ALTER TABLE _realtime.extensions OWNER TO supabase_admin;
--
-- Name: schema_migrations; Type: TABLE; Schema: _realtime; Owner: supabase_admin
--
CREATE TABLE _realtime.schema_migrations (
version bigint NOT NULL,
inserted_at timestamp(0) without time zone
);
ALTER TABLE _realtime.schema_migrations OWNER TO supabase_admin;
--
-- Name: tenants; Type: TABLE; Schema: _realtime; Owner: supabase_admin
--
CREATE TABLE _realtime.tenants (
id uuid NOT NULL,
name text,
external_id text,
jwt_secret text,
max_concurrent_users integer DEFAULT 200 NOT NULL,
inserted_at timestamp(0) without time zone NOT NULL,
updated_at timestamp(0) without time zone NOT NULL,
max_events_per_second integer DEFAULT 100 NOT NULL,
postgres_cdc_default text DEFAULT 'postgres_cdc_rls'::text,
max_bytes_per_second integer DEFAULT 100000 NOT NULL,
max_channels_per_client integer DEFAULT 100 NOT NULL,
max_joins_per_second integer DEFAULT 500 NOT NULL,
suspend boolean DEFAULT false,
jwt_jwks jsonb,
notify_private_alpha boolean DEFAULT false,
private_only boolean DEFAULT false NOT NULL,
migrations_ran integer DEFAULT 0,
broadcast_adapter character varying(255) DEFAULT 'gen_rpc'::character varying,
max_presence_events_per_second integer DEFAULT 1000,
max_payload_size_in_kb integer DEFAULT 3000,
CONSTRAINT jwt_secret_or_jwt_jwks_required CHECK (((jwt_secret IS NOT NULL) OR (jwt_jwks IS NOT NULL)))
);
ALTER TABLE _realtime.tenants OWNER TO supabase_admin;
--
-- Name: audit_log_entries; Type: TABLE; Schema: auth; Owner: supabase_auth_admin
--
CREATE TABLE realtime.messages (
topic text NOT NULL,
extension text NOT NULL,
payload jsonb,
event text,
private boolean DEFAULT false,
updated_at timestamp without time zone DEFAULT now() NOT NULL,
inserted_at timestamp without time zone DEFAULT now() NOT NULL,
id uuid DEFAULT gen_random_uuid() NOT NULL
)
PARTITION BY RANGE (inserted_at);
ALTER TABLE realtime.messages OWNER TO supabase_realtime_admin;
--
-- Name: messages_2026_03_20; Type: TABLE; Schema: realtime; Owner: supabase_admin
--
CREATE TABLE realtime.messages_2026_03_20 (
topic text NOT NULL,
extension text NOT NULL,
payload jsonb,
event text,
private boolean DEFAULT false,
updated_at timestamp without time zone DEFAULT now() NOT NULL,
inserted_at timestamp without time zone DEFAULT now() NOT NULL,
id uuid DEFAULT gen_random_uuid() NOT NULL
);
ALTER TABLE realtime.messages_2026_03_20 OWNER TO supabase_admin;
--
-- Name: messages_2026_03_21; Type: TABLE; Schema: realtime; Owner: supabase_admin
--
CREATE TABLE realtime.messages_2026_03_21 (
topic text NOT NULL,
extension text NOT NULL,
payload jsonb,
event text,
private boolean DEFAULT false,
updated_at timestamp without time zone DEFAULT now() NOT NULL,
inserted_at timestamp without time zone DEFAULT now() NOT NULL,
id uuid DEFAULT gen_random_uuid() NOT NULL
);
ALTER TABLE realtime.messages_2026_03_21 OWNER TO supabase_admin;
--
-- Name: messages_2026_03_22; Type: TABLE; Schema: realtime; Owner: supabase_admin
--
CREATE TABLE realtime.messages_2026_03_22 (
topic text NOT NULL,
extension text NOT NULL,
payload jsonb,
event text,
private boolean DEFAULT false,
updated_at timestamp without time zone DEFAULT now() NOT NULL,
inserted_at timestamp without time zone DEFAULT now() NOT NULL,
id uuid DEFAULT gen_random_uuid() NOT NULL
);
ALTER TABLE realtime.messages_2026_03_22 OWNER TO supabase_admin;
--
-- Name: messages_2026_03_23; Type: TABLE; Schema: realtime; Owner: supabase_admin
--
CREATE TABLE realtime.messages_2026_03_23 (
topic text NOT NULL,
extension text NOT NULL,
payload jsonb,
event text,
private boolean DEFAULT false,
updated_at timestamp without time zone DEFAULT now() NOT NULL,
inserted_at timestamp without time zone DEFAULT now() NOT NULL,
id uuid DEFAULT gen_random_uuid() NOT NULL
);
ALTER TABLE realtime.messages_2026_03_23 OWNER TO supabase_admin;
--
-- Name: messages_2026_03_24; Type: TABLE; Schema: realtime; Owner: supabase_admin
--
CREATE TABLE realtime.messages_2026_03_24 (
topic text NOT NULL,
extension text NOT NULL,
payload jsonb,
event text,
private boolean DEFAULT false,
updated_at timestamp without time zone DEFAULT now() NOT NULL,
inserted_at timestamp without time zone DEFAULT now() NOT NULL,
id uuid DEFAULT gen_random_uuid() NOT NULL
);
ALTER TABLE realtime.messages_2026_03_24 OWNER TO supabase_admin;
--
-- Name: messages_2026_03_25; Type: TABLE; Schema: realtime; Owner: supabase_admin
--
CREATE TABLE realtime.messages_2026_03_25 (
topic text NOT NULL,
extension text NOT NULL,
payload jsonb,
event text,
private boolean DEFAULT false,
updated_at timestamp without time zone DEFAULT now() NOT NULL,
inserted_at timestamp without time zone DEFAULT now() NOT NULL,
id uuid DEFAULT gen_random_uuid() NOT NULL
);
ALTER TABLE realtime.messages_2026_03_25 OWNER TO supabase_admin;
--
-- Name: messages_2026_03_26; Type: TABLE; Schema: realtime; Owner: supabase_admin
--
CREATE TABLE realtime.messages_2026_03_26 (
topic text NOT NULL,
extension text NOT NULL,
payload jsonb,
event text,
private boolean DEFAULT false,
updated_at timestamp without time zone DEFAULT now() NOT NULL,
inserted_at timestamp without time zone DEFAULT now() NOT NULL,
id uuid DEFAULT gen_random_uuid() NOT NULL
);
ALTER TABLE realtime.messages_2026_03_26 OWNER TO supabase_admin;
--
-- Name: schema_migrations; Type: TABLE; Schema: realtime; Owner: supabase_admin
--
CREATE TABLE realtime.schema_migrations (
version bigint NOT NULL,
inserted_at timestamp(0) without time zone
);
ALTER TABLE realtime.schema_migrations OWNER TO supabase_admin;
--
-- Name: subscription; Type: TABLE; Schema: realtime; Owner: supabase_admin
--
CREATE TABLE realtime.subscription (
id bigint NOT NULL,
subscription_id uuid NOT NULL,
entity regclass NOT NULL,
filters realtime.user_defined_filter[] DEFAULT '{}'::realtime.user_defined_filter[] NOT NULL,
claims jsonb NOT NULL,
claims_role regrole GENERATED ALWAYS AS (realtime.to_regrole((claims ->> 'role'::text))) STORED NOT NULL,
created_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL
);
ALTER TABLE realtime.subscription OWNER TO supabase_admin;
--
-- Name: subscription_id_seq; Type: SEQUENCE; Schema: realtime; Owner: supabase_admin
--
ALTER TABLE realtime.subscription ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME realtime.subscription_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
--
-- Name: buckets; Type: TABLE; Schema: storage; Owner: supabase_storage_admin
--
CREATE TABLE storage.buckets (
id text NOT NULL,
name text NOT NULL,
owner uuid,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
public boolean DEFAULT false,
avif_autodetection boolean DEFAULT false,
file_size_limit bigint,
allowed_mime_types text[],
owner_id text,
type storage.buckettype DEFAULT 'STANDARD'::storage.buckettype NOT NULL
);
ALTER TABLE storage.buckets OWNER TO supabase_storage_admin;
--
-- Name: COLUMN buckets.owner; Type: COMMENT; Schema: storage; Owner: supabase_storage_admin
--
COMMENT ON COLUMN storage.buckets.owner IS 'Field is deprecated, use owner_id instead';
--
-- Name: buckets_analytics; Type: TABLE; Schema: storage; Owner: supabase_storage_admin
--
CREATE TABLE storage.buckets_analytics (
name text NOT NULL,
type storage.buckettype DEFAULT 'ANALYTICS'::storage.buckettype NOT NULL,
format text DEFAULT 'ICEBERG'::text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
id uuid DEFAULT gen_random_uuid() NOT NULL,
deleted_at timestamp with time zone
);
ALTER TABLE storage.buckets_analytics OWNER TO supabase_storage_admin;
--
-- Name: buckets_vectors; Type: TABLE; Schema: storage; Owner: supabase_storage_admin
--
CREATE TABLE storage.buckets_vectors (
id text NOT NULL,
type storage.buckettype DEFAULT 'VECTOR'::storage.buckettype NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE storage.buckets_vectors OWNER TO supabase_storage_admin;
--
-- Name: iceberg_namespaces; Type: TABLE; Schema: storage; Owner: supabase_storage_admin
--
CREATE TABLE storage.iceberg_namespaces (
id uuid DEFAULT gen_random_uuid() NOT NULL,
bucket_name text NOT NULL,
name text NOT NULL COLLATE pg_catalog."C",
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
catalog_id uuid NOT NULL
);
ALTER TABLE storage.iceberg_namespaces OWNER TO supabase_storage_admin;
--
-- Name: iceberg_tables; Type: TABLE; Schema: storage; Owner: supabase_storage_admin
--
CREATE TABLE storage.iceberg_tables (
id uuid DEFAULT gen_random_uuid() NOT NULL,
namespace_id uuid NOT NULL,
bucket_name text NOT NULL,
name text NOT NULL COLLATE pg_catalog."C",
location text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
remote_table_id text,
shard_key text,
shard_id text,
catalog_id uuid NOT NULL
);
ALTER TABLE storage.iceberg_tables OWNER TO supabase_storage_admin;
--
-- Name: migrations; Type: TABLE; Schema: storage; Owner: supabase_storage_admin
--
CREATE TABLE storage.migrations (
id integer NOT NULL,
name character varying(100) NOT NULL,
hash character varying(40) NOT NULL,
executed_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);
ALTER TABLE storage.migrations OWNER TO supabase_storage_admin;
--
-- Name: objects; Type: TABLE; Schema: storage; Owner: supabase_storage_admin
--
CREATE TABLE storage.objects (
id uuid DEFAULT gen_random_uuid() NOT NULL,
bucket_id text,
name text,
owner uuid,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
last_accessed_at timestamp with time zone DEFAULT now(),
metadata jsonb,
path_tokens text[] GENERATED ALWAYS AS (string_to_array(name, '/'::text)) STORED,
version text,
owner_id text,
user_metadata jsonb
);
ALTER TABLE storage.objects OWNER TO supabase_storage_admin;
--
-- Name: COLUMN objects.owner; Type: COMMENT; Schema: storage; Owner: supabase_storage_admin
--
COMMENT ON COLUMN storage.objects.owner IS 'Field is deprecated, use owner_id instead';
--
-- Name: s3_multipart_uploads; Type: TABLE; Schema: storage; Owner: supabase_storage_admin
--
CREATE TABLE storage.s3_multipart_uploads (
id text NOT NULL,
in_progress_size bigint DEFAULT 0 NOT NULL,
upload_signature text NOT NULL,
bucket_id text NOT NULL,
key text NOT NULL COLLATE pg_catalog."C",
version text NOT NULL,
owner_id text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
user_metadata jsonb
);
ALTER TABLE storage.s3_multipart_uploads OWNER TO supabase_storage_admin;
--
-- Name: s3_multipart_uploads_parts; Type: TABLE; Schema: storage; Owner: supabase_storage_admin
--
CREATE TABLE storage.s3_multipart_uploads_parts (
id uuid DEFAULT gen_random_uuid() NOT NULL,
upload_id text NOT NULL,
size bigint DEFAULT 0 NOT NULL,
part_number integer NOT NULL,
bucket_id text NOT NULL,
key text NOT NULL COLLATE pg_catalog."C",
etag text NOT NULL,
owner_id text,
version text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE storage.s3_multipart_uploads_parts OWNER TO supabase_storage_admin;
--
-- Name: vector_indexes; Type: TABLE; Schema: storage; Owner: supabase_storage_admin
--
CREATE TABLE storage.vector_indexes (
id text DEFAULT gen_random_uuid() NOT NULL,
name text NOT NULL COLLATE pg_catalog."C",
bucket_id text NOT NULL,
data_type text NOT NULL,
dimension integer NOT NULL,
distance_metric text NOT NULL,
metadata_configuration jsonb,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE supabase_functions.hooks (
id bigint NOT NULL,
hook_table_id integer NOT NULL,
hook_name text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
request_id bigint
);
ALTER TABLE supabase_functions.hooks OWNER TO supabase_functions_admin;
--
-- Name: TABLE hooks; Type: COMMENT; Schema: supabase_functions; Owner: supabase_functions_admin
--
COMMENT ON TABLE supabase_functions.hooks IS 'Supabase Functions Hooks: Audit trail for triggered hooks.';
--
-- Name: hooks_id_seq; Type: SEQUENCE; Schema: supabase_functions; Owner: supabase_functions_admin
--
CREATE SEQUENCE supabase_functions.hooks_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE supabase_functions.hooks_id_seq OWNER TO supabase_functions_admin;
--
-- Name: hooks_id_seq; Type: SEQUENCE OWNED BY; Schema: supabase_functions; Owner: supabase_functions_admin
--
ALTER SEQUENCE supabase_functions.hooks_id_seq OWNED BY supabase_functions.hooks.id;
--
-- Name: migrations; Type: TABLE; Schema: supabase_functions; Owner: supabase_functions_admin
--
CREATE TABLE supabase_functions.migrations (
version text NOT NULL,
inserted_at timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE supabase_functions.migrations OWNER TO supabase_functions_admin;
--
-- Name: messages_2026_03_20; Type: TABLE ATTACH; Schema: realtime; Owner: supabase_admin
--

View File

@@ -0,0 +1,267 @@
-- =============================================================================
-- AgenciaPsi — Tables — Notificações + Email Templates
-- =============================================================================
-- notification_channels, notification_logs, notification_preferences,
-- notification_queue, notification_schedules, notification_templates,
-- notifications, email_templates_global, email_templates_tenant,
-- email_layout_config
-- =============================================================================
CREATE TABLE public.notification_channels (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
owner_id uuid NOT NULL,
channel text NOT NULL,
provider text NOT NULL,
is_active boolean DEFAULT false NOT NULL,
display_name text,
sender_address text,
credentials jsonb DEFAULT '{}'::jsonb NOT NULL,
connection_status text DEFAULT 'disconnected'::text,
last_health_check timestamp with time zone,
metadata jsonb DEFAULT '{}'::jsonb,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
deleted_at timestamp with time zone,
CONSTRAINT notification_channels_channel_check CHECK ((channel = ANY (ARRAY['whatsapp'::text, 'email'::text, 'sms'::text]))),
CONSTRAINT notification_channels_connection_status_check CHECK ((connection_status = ANY (ARRAY['connected'::text, 'disconnected'::text, 'connecting'::text, 'qr_pending'::text, 'error'::text]))),
CONSTRAINT notification_channels_provider_check CHECK ((provider = ANY (ARRAY['evolution_api'::text, 'meta_official'::text, 'twilio'::text, 'zenvia'::text, 'sendgrid'::text, 'resend'::text, 'smtp'::text, 'zapi'::text])))
);
ALTER TABLE public.notification_channels OWNER TO supabase_admin;
--
-- Name: notification_logs; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.notification_logs (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
owner_id uuid NOT NULL,
queue_id uuid,
agenda_evento_id uuid,
patient_id uuid NOT NULL,
channel text NOT NULL,
template_key text NOT NULL,
schedule_key text,
recipient_address text NOT NULL,
resolved_message text,
resolved_vars jsonb,
status text NOT NULL,
provider text,
provider_message_id text,
provider_status text,
provider_response jsonb,
sent_at timestamp with time zone,
delivered_at timestamp with time zone,
read_at timestamp with time zone,
failed_at timestamp with time zone,
failure_reason text,
estimated_cost_brl numeric(8,4) DEFAULT 0,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT notification_logs_status_check CHECK ((status = ANY (ARRAY['sent'::text, 'delivered'::text, 'read'::text, 'failed'::text, 'bounced'::text, 'opted_out'::text])))
);
ALTER TABLE public.notification_logs OWNER TO supabase_admin;
--
-- Name: notification_preferences; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.notification_preferences (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
owner_id uuid NOT NULL,
patient_id uuid NOT NULL,
whatsapp_opt_in boolean DEFAULT true NOT NULL,
email_opt_in boolean DEFAULT true NOT NULL,
sms_opt_in boolean DEFAULT false NOT NULL,
preferred_time_start time without time zone DEFAULT '08:00:00'::time without time zone,
preferred_time_end time without time zone DEFAULT '20:00:00'::time without time zone,
lgpd_consent_given boolean DEFAULT false NOT NULL,
lgpd_consent_date timestamp with time zone,
lgpd_consent_version text,
lgpd_consent_ip inet,
lgpd_opt_out_date timestamp with time zone,
lgpd_opt_out_reason text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
deleted_at timestamp with time zone
);
ALTER TABLE public.notification_preferences OWNER TO supabase_admin;
--
-- Name: notification_queue; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.notification_queue (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
owner_id uuid NOT NULL,
agenda_evento_id uuid,
patient_id uuid NOT NULL,
channel text NOT NULL,
template_key text NOT NULL,
schedule_key text NOT NULL,
resolved_vars jsonb DEFAULT '{}'::jsonb NOT NULL,
recipient_address text NOT NULL,
status text DEFAULT 'pendente'::text NOT NULL,
scheduled_at timestamp with time zone NOT NULL,
sent_at timestamp with time zone,
next_retry_at timestamp with time zone,
attempts integer DEFAULT 0 NOT NULL,
max_attempts integer DEFAULT 5 NOT NULL,
last_error text,
idempotency_key text NOT NULL,
provider_message_id text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT notification_queue_channel_check CHECK ((channel = ANY (ARRAY['whatsapp'::text, 'email'::text, 'sms'::text]))),
CONSTRAINT notification_queue_status_check CHECK ((status = ANY (ARRAY['pendente'::text, 'processando'::text, 'enviado'::text, 'falhou'::text, 'cancelado'::text, 'ignorado'::text])))
);
ALTER TABLE public.notification_queue OWNER TO supabase_admin;
--
-- Name: notification_schedules; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.notification_schedules (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
owner_id uuid NOT NULL,
schedule_key text NOT NULL,
event_type text NOT NULL,
trigger_type text NOT NULL,
offset_minutes integer DEFAULT 0,
whatsapp_enabled boolean DEFAULT true NOT NULL,
email_enabled boolean DEFAULT true NOT NULL,
sms_enabled boolean DEFAULT false NOT NULL,
allowed_time_start time without time zone DEFAULT '08:00:00'::time without time zone,
allowed_time_end time without time zone DEFAULT '20:00:00'::time without time zone,
skip_weekends boolean DEFAULT false,
skip_holidays boolean DEFAULT false,
is_active boolean DEFAULT true NOT NULL,
sort_order integer DEFAULT 0,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
deleted_at timestamp with time zone,
CONSTRAINT notification_schedules_event_type_check CHECK ((event_type = ANY (ARRAY['lembrete_sessao'::text, 'confirmacao_sessao'::text, 'cancelamento_sessao'::text, 'reagendamento'::text, 'cobranca_pendente'::text, 'boas_vindas_paciente'::text]))),
CONSTRAINT notification_schedules_trigger_type_check CHECK ((trigger_type = ANY (ARRAY['before_event'::text, 'after_event'::text, 'immediate'::text])))
);
ALTER TABLE public.notification_schedules OWNER TO supabase_admin;
--
-- Name: notification_templates; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.notification_templates (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid,
owner_id uuid,
key text NOT NULL,
domain text NOT NULL,
channel text NOT NULL,
event_type text NOT NULL,
body_text text NOT NULL,
meta_template_name text,
meta_template_namespace text,
meta_components jsonb,
meta_status text DEFAULT 'draft'::text,
variables jsonb DEFAULT '[]'::jsonb,
version integer DEFAULT 1 NOT NULL,
is_active boolean DEFAULT true NOT NULL,
is_default boolean DEFAULT false NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
deleted_at timestamp with time zone,
CONSTRAINT notification_templates_channel_check CHECK ((channel = ANY (ARRAY['whatsapp'::text, 'sms'::text]))),
CONSTRAINT notification_templates_domain_check CHECK ((domain = ANY (ARRAY['session'::text, 'intake'::text, 'billing'::text, 'system'::text]))),
CONSTRAINT notification_templates_event_type_check CHECK ((event_type = ANY (ARRAY['lembrete_sessao'::text, 'confirmacao_sessao'::text, 'cancelamento_sessao'::text, 'reagendamento'::text, 'cobranca_pendente'::text, 'boas_vindas_paciente'::text, 'intake_recebido'::text, 'intake_aprovado'::text, 'intake_rejeitado'::text]))),
CONSTRAINT notification_templates_meta_status_check CHECK ((meta_status = ANY (ARRAY['draft'::text, 'pending_approval'::text, 'approved'::text, 'rejected'::text])))
);
ALTER TABLE public.notification_templates OWNER TO supabase_admin;
--
-- Name: notifications; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.notifications (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
tenant_id uuid,
type text NOT NULL,
ref_id uuid,
ref_table text,
payload jsonb DEFAULT '{}'::jsonb NOT NULL,
read_at timestamp with time zone,
archived boolean DEFAULT false NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT notifications_type_check CHECK ((type = ANY (ARRAY['new_scheduling'::text, 'new_patient'::text, 'recurrence_alert'::text, 'session_status'::text])))
);
CREATE TABLE public.email_layout_config (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
header_config jsonb DEFAULT '{"layout": null, "content": "", "enabled": false}'::jsonb NOT NULL,
footer_config jsonb DEFAULT '{"layout": null, "content": "", "enabled": false}'::jsonb NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE public.email_layout_config OWNER TO supabase_admin;
--
-- Name: email_templates_global; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.email_templates_global (
id uuid DEFAULT gen_random_uuid() NOT NULL,
key text NOT NULL,
domain text NOT NULL,
channel text DEFAULT 'email'::text NOT NULL,
subject text NOT NULL,
body_html text NOT NULL,
body_text text,
version integer DEFAULT 1 NOT NULL,
is_active boolean DEFAULT true NOT NULL,
variables jsonb,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE public.email_templates_global OWNER TO supabase_admin;
--
-- Name: email_templates_tenant; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.email_templates_tenant (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
owner_id uuid,
template_key text NOT NULL,
subject text,
body_html text,
body_text text,
enabled boolean DEFAULT true NOT NULL,
synced_version integer,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);

View File

@@ -0,0 +1,185 @@
-- =============================================================================
-- AgenciaPsi — Tables — Pacientes
-- =============================================================================
-- patients, patient_groups, patient_group_patient, patient_tags,
-- patient_patient_tag, patient_intake_requests, patient_invites,
-- patient_discounts
-- =============================================================================
CREATE TABLE public.patients (
id uuid DEFAULT gen_random_uuid() NOT NULL,
nome_completo text NOT NULL,
email_principal text,
telefone text,
created_at timestamp with time zone DEFAULT now(),
owner_id uuid,
avatar_url text,
status text DEFAULT 'Ativo'::text,
last_attended_at timestamp with time zone,
is_native boolean DEFAULT false,
naturalidade text,
data_nascimento date,
rg text,
cpf text,
identification_color text,
genero text,
estado_civil text,
email_alternativo text,
pais text DEFAULT 'Brasil'::text,
cep text,
cidade text,
estado text,
endereco text,
numero text,
bairro text,
complemento text,
escolaridade text,
profissao text,
nome_parente text,
grau_parentesco text,
telefone_alternativo text,
onde_nos_conheceu text,
encaminhado_por text,
nome_responsavel text,
telefone_responsavel text,
cpf_responsavel text,
observacao_responsavel text,
cobranca_no_responsavel boolean DEFAULT false,
observacoes text,
notas_internas text,
updated_at timestamp with time zone DEFAULT now(),
telefone_parente text,
tenant_id uuid NOT NULL,
responsible_member_id uuid NOT NULL,
user_id uuid,
patient_scope text DEFAULT 'clinic'::text NOT NULL,
therapist_member_id uuid,
CONSTRAINT cpf_responsavel_format_check CHECK (((cpf_responsavel IS NULL) OR (cpf_responsavel ~ '^\d{11}$'::text))),
CONSTRAINT patients_cpf_format_check CHECK (((cpf IS NULL) OR (cpf ~ '^\d{11}$'::text))),
CONSTRAINT patients_patient_scope_check CHECK ((patient_scope = ANY (ARRAY['clinic'::text, 'therapist'::text]))),
CONSTRAINT patients_status_check CHECK ((status = ANY (ARRAY['Ativo'::text, 'Inativo'::text, 'Alta'::text, 'Encaminhado'::text, 'Arquivado'::text]))),
CONSTRAINT patients_therapist_scope_consistency CHECK ((((patient_scope = 'clinic'::text) AND (therapist_member_id IS NULL)) OR ((patient_scope = 'therapist'::text) AND (therapist_member_id IS NOT NULL))))
);
CREATE TABLE public.patient_groups (
id uuid DEFAULT gen_random_uuid() NOT NULL,
nome text NOT NULL,
descricao text,
cor text,
is_active boolean DEFAULT true NOT NULL,
is_system boolean DEFAULT false NOT NULL,
owner_id uuid DEFAULT auth.uid() NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
therapist_id uuid,
tenant_id uuid NOT NULL
);
CREATE TABLE public.patient_group_patient (
patient_group_id uuid NOT NULL,
patient_id uuid NOT NULL,
created_at timestamp with time zone DEFAULT now(),
tenant_id uuid NOT NULL
);
CREATE TABLE public.patient_tags (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
nome text NOT NULL,
cor text,
is_padrao boolean DEFAULT false NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone,
tenant_id uuid NOT NULL
);
CREATE TABLE public.patient_patient_tag (
owner_id uuid NOT NULL,
patient_id uuid NOT NULL,
tag_id uuid NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
tenant_id uuid NOT NULL
);
CREATE TABLE public.patient_intake_requests (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
token text NOT NULL,
consent boolean DEFAULT false NOT NULL,
status text DEFAULT 'new'::text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
converted_patient_id uuid,
rejected_reason text,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
cpf text,
rg text,
cep text,
nome_completo text,
email_principal text,
telefone text,
pais text,
cidade text,
estado text,
endereco text,
numero text,
bairro text,
complemento text,
data_nascimento date,
naturalidade text,
genero text,
estado_civil text,
onde_nos_conheceu text,
encaminhado_por text,
observacoes text,
notas_internas text,
email_alternativo text,
telefone_alternativo text,
profissao text,
escolaridade text,
nacionalidade text,
avatar_url text,
tenant_id uuid,
CONSTRAINT chk_intakes_status CHECK ((status = ANY (ARRAY['new'::text, 'converted'::text, 'rejected'::text])))
);
CREATE TABLE public.patient_invites (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
token text NOT NULL,
active boolean DEFAULT true NOT NULL,
expires_at timestamp with time zone,
max_uses integer,
uses integer DEFAULT 0 NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
tenant_id uuid
);
CREATE TABLE public.patient_discounts (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
tenant_id uuid NOT NULL,
patient_id uuid NOT NULL,
discount_pct numeric(5,2) DEFAULT 0,
discount_flat numeric(10,2) DEFAULT 0,
reason text,
active boolean DEFAULT true NOT NULL,
active_from timestamp with time zone DEFAULT now(),
active_to timestamp with time zone,
created_at timestamp with time zone DEFAULT now()
);

View File

@@ -0,0 +1,371 @@
-- =============================================================================
-- AgenciaPsi — Tables — Plans, Billing, Subscriptions
-- =============================================================================
-- plans, plan_prices, plan_features, plan_public, plan_public_bullets,
-- features, entitlements_invalidation, subscriptions, subscription_events,
-- subscription_intents_personal, subscription_intents_tenant,
-- subscription_intents_legacy, billing_contracts,
-- addon_credits, addon_products, addon_transactions,
-- modules, module_features, tenant_modules
-- =============================================================================
CREATE TABLE public.plans (
id uuid DEFAULT gen_random_uuid() NOT NULL,
key text NOT NULL,
name text NOT NULL,
description text,
is_active boolean DEFAULT true NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
price_cents integer DEFAULT 0 NOT NULL,
currency text DEFAULT 'BRL'::text NOT NULL,
billing_interval text DEFAULT 'month'::text NOT NULL,
target text,
max_supervisees integer,
CONSTRAINT plans_target_check CHECK ((target = ANY (ARRAY['patient'::text, 'therapist'::text, 'clinic'::text, 'supervisor'::text])))
);
CREATE TABLE public.plan_prices (
id uuid DEFAULT gen_random_uuid() NOT NULL,
plan_id uuid NOT NULL,
currency text DEFAULT 'BRL'::text NOT NULL,
"interval" text NOT NULL,
amount_cents integer NOT NULL,
is_active boolean DEFAULT true NOT NULL,
active_from timestamp with time zone DEFAULT now() NOT NULL,
active_to timestamp with time zone,
source text DEFAULT 'manual'::text NOT NULL,
provider text,
provider_price_id text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT plan_prices_amount_cents_check CHECK ((amount_cents >= 0)),
CONSTRAINT plan_prices_interval_check CHECK (("interval" = ANY (ARRAY['month'::text, 'year'::text])))
);
CREATE TABLE public.plan_features (
plan_id uuid NOT NULL,
feature_id uuid NOT NULL,
enabled boolean DEFAULT true NOT NULL,
limits jsonb,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.plan_public (
plan_id uuid NOT NULL,
public_name text DEFAULT ''::text NOT NULL,
public_description text DEFAULT ''::text NOT NULL,
badge text,
is_featured boolean DEFAULT false NOT NULL,
is_visible boolean DEFAULT true NOT NULL,
sort_order integer DEFAULT 0 NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.plan_public_bullets (
id uuid DEFAULT gen_random_uuid() NOT NULL,
plan_id uuid NOT NULL,
text text NOT NULL,
sort_order integer DEFAULT 0 NOT NULL,
highlight boolean DEFAULT false NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.features (
id uuid DEFAULT gen_random_uuid() NOT NULL,
key text NOT NULL,
description text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
descricao text DEFAULT ''::text NOT NULL,
name text DEFAULT ''::text NOT NULL
);
ALTER TABLE public.features OWNER TO supabase_admin;
--
-- Name: COLUMN features.descricao; Type: COMMENT; Schema: public; Owner: supabase_admin
--
COMMENT ON COLUMN public.features.descricao IS 'Descrição humana da feature (exibição no admin e documentação).';
--
-- Name: feriados; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.entitlements_invalidation (
owner_id uuid NOT NULL,
changed_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.subscriptions (
id uuid DEFAULT gen_random_uuid() NOT NULL,
user_id uuid,
plan_id uuid NOT NULL,
status text DEFAULT 'active'::text NOT NULL,
current_period_start timestamp with time zone,
current_period_end timestamp with time zone,
cancel_at_period_end boolean DEFAULT false NOT NULL,
provider text DEFAULT 'manual'::text NOT NULL,
provider_customer_id text,
provider_subscription_id text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
tenant_id uuid,
plan_key text,
"interval" text,
source text DEFAULT 'manual'::text NOT NULL,
started_at timestamp with time zone DEFAULT now() NOT NULL,
canceled_at timestamp with time zone,
activated_at timestamp with time zone,
past_due_since timestamp with time zone,
suspended_at timestamp with time zone,
suspended_reason text,
cancelled_at timestamp with time zone,
cancel_reason text,
expired_at timestamp with time zone,
CONSTRAINT subscriptions_interval_check CHECK (("interval" = ANY (ARRAY['month'::text, 'year'::text]))),
CONSTRAINT subscriptions_owner_xor CHECK ((((tenant_id IS NOT NULL) AND (user_id IS NULL)) OR ((tenant_id IS NULL) AND (user_id IS NOT NULL)))),
CONSTRAINT subscriptions_status_check CHECK ((status = ANY (ARRAY['pending'::text, 'active'::text, 'past_due'::text, 'suspended'::text, 'cancelled'::text, 'expired'::text])))
);
CREATE TABLE public.subscription_events (
id uuid DEFAULT gen_random_uuid() NOT NULL,
subscription_id uuid NOT NULL,
owner_id uuid NOT NULL,
event_type text NOT NULL,
old_plan_id uuid,
new_plan_id uuid,
created_at timestamp with time zone DEFAULT now() NOT NULL,
created_by uuid,
source text DEFAULT 'admin_ui'::text,
reason text,
metadata jsonb,
owner_type text NOT NULL,
owner_ref uuid NOT NULL,
CONSTRAINT subscription_events_owner_ref_consistency_chk CHECK ((owner_id = owner_ref)),
CONSTRAINT subscription_events_owner_type_chk CHECK (((owner_type IS NULL) OR (owner_type = ANY (ARRAY['clinic'::text, 'therapist'::text]))))
);
CREATE TABLE public.subscription_intents_personal (
id uuid DEFAULT gen_random_uuid() NOT NULL,
user_id uuid NOT NULL,
created_by_user_id uuid,
email text NOT NULL,
plan_id uuid NOT NULL,
plan_key text,
"interval" text,
amount_cents integer,
currency text,
status text DEFAULT 'new'::text NOT NULL,
source text DEFAULT 'manual'::text NOT NULL,
notes text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
paid_at timestamp with time zone,
subscription_id uuid,
CONSTRAINT sint_personal_interval_check CHECK ((("interval" IS NULL) OR ("interval" = ANY (ARRAY['month'::text, 'year'::text])))),
CONSTRAINT sint_personal_status_check CHECK ((status = ANY (ARRAY['new'::text, 'waiting_payment'::text, 'paid'::text, 'canceled'::text])))
);
CREATE TABLE public.subscription_intents_tenant (
id uuid DEFAULT gen_random_uuid() NOT NULL,
user_id uuid NOT NULL,
created_by_user_id uuid,
email text NOT NULL,
plan_id uuid NOT NULL,
plan_key text,
"interval" text,
amount_cents integer,
currency text,
status text DEFAULT 'new'::text NOT NULL,
source text DEFAULT 'manual'::text NOT NULL,
notes text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
paid_at timestamp with time zone,
tenant_id uuid NOT NULL,
subscription_id uuid,
CONSTRAINT sint_tenant_interval_check CHECK ((("interval" IS NULL) OR ("interval" = ANY (ARRAY['month'::text, 'year'::text])))),
CONSTRAINT sint_tenant_status_check CHECK ((status = ANY (ARRAY['new'::text, 'waiting_payment'::text, 'paid'::text, 'canceled'::text])))
);
CREATE TABLE public.subscription_intents_legacy (
id uuid DEFAULT gen_random_uuid() NOT NULL,
user_id uuid,
email text,
plan_key text NOT NULL,
"interval" text NOT NULL,
amount_cents integer NOT NULL,
currency text DEFAULT 'BRL'::text NOT NULL,
status text DEFAULT 'new'::text NOT NULL,
source text DEFAULT 'landing'::text NOT NULL,
notes text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
paid_at timestamp with time zone,
tenant_id uuid NOT NULL,
created_by_user_id uuid,
CONSTRAINT subscription_intents_interval_check CHECK (("interval" = ANY (ARRAY['month'::text, 'year'::text]))),
CONSTRAINT subscription_intents_status_check CHECK ((status = ANY (ARRAY['new'::text, 'waiting_payment'::text, 'paid'::text, 'canceled'::text])))
);
CREATE TABLE public.billing_contracts (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
tenant_id uuid NOT NULL,
patient_id uuid NOT NULL,
type text NOT NULL,
total_sessions integer,
sessions_used integer DEFAULT 0,
package_price numeric(10,2),
amount numeric(10,2),
billing_interval text,
active_from timestamp with time zone DEFAULT now(),
active_to timestamp with time zone,
status text DEFAULT 'active'::text NOT NULL,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT billing_contracts_interval_chk CHECK (((billing_interval IS NULL) OR (billing_interval = ANY (ARRAY['monthly'::text, 'weekly'::text])))),
CONSTRAINT billing_contracts_sess_used_chk CHECK (((sessions_used IS NULL) OR (sessions_used >= 0))),
CONSTRAINT billing_contracts_status_chk CHECK ((status = ANY (ARRAY['active'::text, 'completed'::text, 'cancelled'::text]))),
CONSTRAINT billing_contracts_total_sess_chk CHECK (((total_sessions IS NULL) OR (total_sessions > 0))),
CONSTRAINT billing_contracts_type_chk CHECK ((type = ANY (ARRAY['per_session'::text, 'package'::text, 'subscription'::text])))
);
CREATE TABLE public.addon_credits (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
owner_id uuid,
addon_type text NOT NULL,
balance integer DEFAULT 0 NOT NULL,
total_purchased integer DEFAULT 0 NOT NULL,
total_consumed integer DEFAULT 0 NOT NULL,
low_balance_threshold integer DEFAULT 10,
low_balance_notified boolean DEFAULT false,
daily_limit integer,
hourly_limit integer,
daily_used integer DEFAULT 0,
hourly_used integer DEFAULT 0,
daily_reset_at timestamp with time zone,
hourly_reset_at timestamp with time zone,
from_number_override text,
expires_at timestamp with time zone,
is_active boolean DEFAULT true,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now()
);
CREATE TABLE public.addon_products (
id uuid DEFAULT gen_random_uuid() NOT NULL,
slug text NOT NULL,
name text NOT NULL,
description text,
addon_type text NOT NULL,
icon text DEFAULT 'pi pi-box'::text,
credits_amount integer DEFAULT 0,
price_cents integer DEFAULT 0 NOT NULL,
currency text DEFAULT 'BRL'::text,
is_active boolean DEFAULT true,
is_visible boolean DEFAULT true,
sort_order integer DEFAULT 0,
metadata jsonb DEFAULT '{}'::jsonb,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
deleted_at timestamp with time zone
);
CREATE TABLE public.addon_transactions (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
owner_id uuid,
addon_type text NOT NULL,
type text NOT NULL,
amount integer NOT NULL,
balance_before integer DEFAULT 0 NOT NULL,
balance_after integer DEFAULT 0 NOT NULL,
product_id uuid,
queue_id uuid,
description text,
admin_user_id uuid,
payment_method text,
payment_reference text,
price_cents integer,
currency text DEFAULT 'BRL'::text,
created_at timestamp with time zone DEFAULT now(),
metadata jsonb DEFAULT '{}'::jsonb
);
ALTER TABLE public.addon_transactions OWNER TO supabase_admin;
--
-- Name: TABLE addon_transactions; Type: COMMENT; Schema: public; Owner: supabase_admin
--
COMMENT ON TABLE public.addon_transactions IS 'Histórico de todas as transações de créditos: compras, consumo, ajustes, reembolsos.';
--
-- Name: agenda_bloqueios; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.modules (
id uuid DEFAULT gen_random_uuid() NOT NULL,
key text NOT NULL,
name text NOT NULL,
description text,
is_active boolean DEFAULT true NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.module_features (
module_id uuid NOT NULL,
feature_id uuid NOT NULL,
enabled boolean DEFAULT true NOT NULL,
limits jsonb,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.tenant_modules (
id uuid DEFAULT gen_random_uuid() NOT NULL,
owner_id uuid NOT NULL,
module_id uuid NOT NULL,
status text DEFAULT 'active'::text NOT NULL,
settings jsonb,
provider text DEFAULT 'manual'::text NOT NULL,
provider_item_id text,
installed_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE public.tenant_modules OWNER TO supabase_admin;

View File

@@ -0,0 +1,204 @@
-- =============================================================================
-- AgenciaPsi — Tables — SaaS Admin, FAQ, Docs, UI
-- =============================================================================
-- saas_docs, saas_doc_votos, saas_faq, saas_faq_itens,
-- feriados, global_notices, login_carousel_slides, notice_dismissals,
-- support_sessions
-- =============================================================================
CREATE TABLE public.saas_doc_votos (
id uuid DEFAULT gen_random_uuid() NOT NULL,
doc_id uuid NOT NULL,
user_id uuid NOT NULL,
util boolean NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE public.saas_doc_votos OWNER TO supabase_admin;
--
-- Name: saas_docs; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.saas_docs (
id uuid DEFAULT gen_random_uuid() NOT NULL,
titulo text NOT NULL,
conteudo text DEFAULT ''::text NOT NULL,
medias jsonb DEFAULT '[]'::jsonb NOT NULL,
tipo_acesso text DEFAULT 'usuario'::text NOT NULL,
pagina_path text NOT NULL,
docs_relacionados uuid[] DEFAULT '{}'::uuid[] NOT NULL,
ativo boolean DEFAULT true NOT NULL,
ordem integer DEFAULT 0 NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
categoria text,
exibir_no_faq boolean DEFAULT false NOT NULL,
votos_util integer DEFAULT 0 NOT NULL,
votos_nao_util integer DEFAULT 0 NOT NULL,
CONSTRAINT saas_docs_tipo_acesso_check CHECK ((tipo_acesso = ANY (ARRAY['admin'::text, 'usuario'::text])))
);
ALTER TABLE public.saas_docs OWNER TO supabase_admin;
--
-- Name: COLUMN saas_docs.categoria; Type: COMMENT; Schema: public; Owner: supabase_admin
--
COMMENT ON COLUMN public.saas_docs.categoria IS 'Agrupa docs no portal FAQ (ex: Conta, Agenda, Pagamentos)';
--
-- Name: COLUMN saas_docs.exibir_no_faq; Type: COMMENT; Schema: public; Owner: supabase_admin
--
COMMENT ON COLUMN public.saas_docs.exibir_no_faq IS 'Se true, a doc e seus itens FAQ aparecem no portal de FAQ';
--
-- Name: saas_faq; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.saas_faq (
id uuid DEFAULT gen_random_uuid() NOT NULL,
pergunta text NOT NULL,
categoria text,
publico boolean DEFAULT false NOT NULL,
votos integer DEFAULT 0 NOT NULL,
titulo text,
conteudo text,
tipo_acesso text DEFAULT 'usuario'::text NOT NULL,
pagina_path text NOT NULL,
pagina_label text,
medias jsonb DEFAULT '[]'::jsonb NOT NULL,
faqs_relacionados uuid[] DEFAULT '{}'::uuid[] NOT NULL,
ativo boolean DEFAULT true NOT NULL,
ordem integer DEFAULT 0 NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE public.saas_faq OWNER TO supabase_admin;
--
-- Name: saas_faq_itens; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.saas_faq_itens (
id uuid DEFAULT gen_random_uuid() NOT NULL,
doc_id uuid NOT NULL,
pergunta text NOT NULL,
resposta text,
ordem integer DEFAULT 0 NOT NULL,
ativo boolean DEFAULT true NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE public.saas_faq_itens OWNER TO supabase_admin;
--
-- Name: TABLE saas_faq_itens; Type: COMMENT; Schema: public; Owner: supabase_admin
--
COMMENT ON TABLE public.saas_faq_itens IS 'Pares pergunta/resposta vinculados a um documento de ajuda';
--
-- Name: services; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.feriados (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid,
owner_id uuid,
tipo text DEFAULT 'municipal'::text NOT NULL,
nome text NOT NULL,
data date NOT NULL,
cidade text,
estado text,
observacao text,
bloqueia_sessoes boolean DEFAULT false NOT NULL,
criado_em timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT feriados_tipo_check CHECK ((tipo = ANY (ARRAY['municipal'::text, 'personalizado'::text])))
);
CREATE TABLE public.global_notices (
id uuid DEFAULT gen_random_uuid() NOT NULL,
title text,
message text DEFAULT ''::text NOT NULL,
variant text DEFAULT 'info'::text NOT NULL,
roles text[] DEFAULT '{}'::text[] NOT NULL,
contexts text[] DEFAULT '{}'::text[] NOT NULL,
starts_at timestamp with time zone,
ends_at timestamp with time zone,
is_active boolean DEFAULT true NOT NULL,
priority integer DEFAULT 0 NOT NULL,
dismissible boolean DEFAULT true NOT NULL,
persist_dismiss boolean DEFAULT true NOT NULL,
dismiss_scope text DEFAULT 'device'::text NOT NULL,
show_once boolean DEFAULT false NOT NULL,
max_views integer,
cooldown_minutes integer,
version integer DEFAULT 1 NOT NULL,
action_type text DEFAULT 'none'::text NOT NULL,
action_label text,
action_url text,
action_route text,
views_count integer DEFAULT 0 NOT NULL,
clicks_count integer DEFAULT 0 NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
created_by uuid,
content_align text DEFAULT 'left'::text NOT NULL,
link_target text DEFAULT '_blank'::text NOT NULL,
CONSTRAINT global_notices_action_type_check CHECK ((action_type = ANY (ARRAY['none'::text, 'internal'::text, 'external'::text]))),
CONSTRAINT global_notices_content_align_check CHECK ((content_align = ANY (ARRAY['left'::text, 'center'::text, 'right'::text, 'justify'::text]))),
CONSTRAINT global_notices_dismiss_scope_check CHECK ((dismiss_scope = ANY (ARRAY['session'::text, 'device'::text, 'user'::text]))),
CONSTRAINT global_notices_link_target_check CHECK ((link_target = ANY (ARRAY['_blank'::text, '_self'::text, '_parent'::text, '_top'::text]))),
CONSTRAINT global_notices_variant_check CHECK ((variant = ANY (ARRAY['info'::text, 'success'::text, 'warning'::text, 'error'::text])))
);
CREATE TABLE public.login_carousel_slides (
id uuid DEFAULT gen_random_uuid() NOT NULL,
title text NOT NULL,
body text NOT NULL,
icon text DEFAULT 'pi-star'::text NOT NULL,
ordem integer DEFAULT 0 NOT NULL,
ativo boolean DEFAULT true NOT NULL,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now()
);
CREATE TABLE public.notice_dismissals (
id uuid DEFAULT gen_random_uuid() NOT NULL,
notice_id uuid NOT NULL,
user_id uuid NOT NULL,
version integer DEFAULT 1 NOT NULL,
dismissed_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.support_sessions (
id uuid DEFAULT gen_random_uuid() NOT NULL,
tenant_id uuid NOT NULL,
admin_id uuid NOT NULL,
token text DEFAULT encode(extensions.gen_random_bytes(32), 'hex'::text) NOT NULL,
expires_at timestamp with time zone DEFAULT (now() + '01:00:00'::interval) NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL
);