F3 schema-per-tenant: frontend usa tenantDb() pra tabelas tenant
- useTenantDb composable + lib/supabase/tenantClient (tenantDb/tenantSchemaName)
- tenantStore: getters activeTenantSlug/activeTenantSchema; my_tenants() RPC
passa a devolver slug+nome (migration 07)
- codemod scripts/codemod-tenant-db.py: supabase.from('<84 tabelas + 6 views
tenant>') -> tenantDb().from(...) em 139 arquivos (777 chamadas), remove
.eq('tenant_id') das cadeias tenant (173)
- passada manual (4 agentes): remove tenant_id de payloads insert/upsert/update,
selects, .or/.is de defaults; onConflict ajustado pros uniques sem tenant_id
(singletons usam 'singleton'); realtime de tabelas tenant aponta pro schema
do tenant ativo; repos dropam tenant_id defensivamente de payloads externos
- agendaSelects: tenant_id fora do AGENDA_EVENT_SELECT (quebraria PostgREST)
- zero embeds cross-schema (todos FK embeds sao tenant->tenant ou global->global)
- build de producao passa; 67 .js checados
Pendente (fora do escopo F3, sao cross-tenant/anon -> F4/F6):
- AgendadorPublicoPage (anon, resolve tenant por link_slug)
- Saas{Feriados,NotificationTemplates,DocumentTemplates,Whatsapp}Page
(gerenciam defaults do sistema / views cross-tenant)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -20,6 +20,7 @@ import { useRouter, useRoute } from 'vue-router';
|
||||
import { useTenantStore } from '@/stores/tenantStore';
|
||||
import { supabase } from '@/lib/supabase/client';
|
||||
|
||||
import { tenantDb } from '@/lib/supabase/tenantClient';
|
||||
import { useToast } from 'primevue/usetoast';
|
||||
import { useConfirm } from 'primevue/useconfirm';
|
||||
|
||||
@@ -606,8 +607,7 @@ async function loadMonthSearchRows() {
|
||||
try {
|
||||
// 1. Eventos reais do banco — inclui recurrence_id/recurrence_date para
|
||||
// mergeWithStoredSessions deduplicar sessões materializadas de séries.
|
||||
const { data, error } = await supabase
|
||||
.from('agenda_eventos')
|
||||
const { data, error } = await tenantDb().from('agenda_eventos')
|
||||
.select(
|
||||
'id, owner_id, tipo, status, titulo, inicio_em, fim_em, observacoes, modalidade, determined_commitment_id, insurance_plan_id, insurance_guide_number, insurance_value, insurance_plan_service_id, recurrence_id, recurrence_date, patients!agenda_eventos_patient_id_fkey(nome_completo, status)'
|
||||
)
|
||||
@@ -981,7 +981,7 @@ async function loadAgendadorSlug() {
|
||||
const uid = ownerId.value;
|
||||
if (!uid) return;
|
||||
try {
|
||||
const { data } = await supabase.from('agendador_configuracoes').select('link_slug').eq('owner_id', uid).eq('ativo', true).maybeSingle();
|
||||
const { data } = await tenantDb().from('agendador_configuracoes').select('link_slug').eq('owner_id', uid).eq('ativo', true).maybeSingle();
|
||||
agendadorSlug.value = data?.link_slug || '';
|
||||
} catch {
|
||||
agendadorSlug.value = '';
|
||||
@@ -993,7 +993,7 @@ async function loadCadastroToken() {
|
||||
const { data: authData } = await supabase.auth.getUser();
|
||||
const uid = authData?.user?.id;
|
||||
if (!uid) return;
|
||||
const { data } = await supabase.from('patient_invites').select('token').eq('owner_id', uid).eq('active', true).order('created_at', { ascending: false }).limit(1);
|
||||
const { data } = await tenantDb().from('patient_invites').select('token').eq('owner_id', uid).eq('active', true).order('created_at', { ascending: false }).limit(1);
|
||||
cadastroToken.value = data?.[0]?.token || '';
|
||||
} catch {
|
||||
cadastroToken.value = '';
|
||||
@@ -1031,7 +1031,7 @@ const desativadoFcRef = ref(null);
|
||||
async function loadDesativados() {
|
||||
if (!ownerId.value) return;
|
||||
try {
|
||||
const { data: pats, error: pErr } = await supabase.from('patients').select('id, nome_completo, status').eq('owner_id', ownerId.value).in('status', ['Inativo', 'Arquivado']);
|
||||
const { data: pats, error: pErr } = await tenantDb().from('patients').select('id, nome_completo, status').eq('owner_id', ownerId.value).in('status', ['Inativo', 'Arquivado']);
|
||||
|
||||
if (pErr) {
|
||||
console.warn('[loadDesativados] patients error:', pErr);
|
||||
@@ -1044,9 +1044,8 @@ async function loadDesativados() {
|
||||
}
|
||||
|
||||
const patIds = pats.map((p) => p.id);
|
||||
const sessQ = supabase.from('agenda_eventos').select('id, patient_id, inicio_em, fim_em, status, titulo, modalidade, determined_commitment_id').in('patient_id', patIds).order('inicio_em', { ascending: true });
|
||||
const sessQ = tenantDb().from('agenda_eventos').select('id, patient_id, inicio_em, fim_em, status, titulo, modalidade, determined_commitment_id').in('patient_id', patIds).order('inicio_em', { ascending: true });
|
||||
if (ownerId.value) sessQ.eq('owner_id', ownerId.value);
|
||||
if (clinicTenantId.value) sessQ.eq('tenant_id', clinicTenantId.value);
|
||||
const { data: sessions, error: sErr } = await sessQ;
|
||||
|
||||
if (sErr) {
|
||||
@@ -1278,7 +1277,7 @@ async function loadMiniMonthEvents(refDate) {
|
||||
|
||||
try {
|
||||
// 1. Eventos reais (agenda_eventos)
|
||||
const { data: evData } = await supabase.from('agenda_eventos').select('inicio_em').eq('owner_id', ownerId.value).gte('inicio_em', start.toISOString()).lte('inicio_em', end.toISOString());
|
||||
const { data: evData } = await tenantDb().from('agenda_eventos').select('inicio_em').eq('owner_id', ownerId.value).gte('inicio_em', start.toISOString()).lte('inicio_em', end.toISOString());
|
||||
|
||||
const evSet = new Set();
|
||||
for (const r of evData || []) {
|
||||
@@ -1303,8 +1302,7 @@ async function loadMiniMonthEvents(refDate) {
|
||||
const isoStart = `${year}-${pad(month + 1)}-01`;
|
||||
const lastDay = new Date(year, month + 1, 0).getDate();
|
||||
const isoEnd = `${year}-${pad(month + 1)}-${pad(lastDay)}`;
|
||||
const { data: blkData } = await supabase
|
||||
.from('agenda_bloqueios')
|
||||
const { data: blkData } = await tenantDb().from('agenda_bloqueios')
|
||||
.select('data_inicio')
|
||||
.eq('owner_id', ownerId.value || '')
|
||||
.is('hora_inicio', null)
|
||||
@@ -1398,10 +1396,9 @@ async function bloquearFeriadoDoAlerta(feriado) {
|
||||
if (!ownerId.value || !clinicTenantId.value) return;
|
||||
feriadosAlertaSalvando.value = feriado.data;
|
||||
try {
|
||||
const { error } = await supabase.from('agenda_bloqueios').insert([
|
||||
const { error } = await tenantDb().from('agenda_bloqueios').insert([
|
||||
{
|
||||
owner_id: ownerId.value,
|
||||
tenant_id: clinicTenantId.value,
|
||||
tipo: 'bloqueio',
|
||||
recorrente: false,
|
||||
titulo: `Feriado: ${feriado.nome}`,
|
||||
@@ -1413,7 +1410,7 @@ async function bloquearFeriadoDoAlerta(feriado) {
|
||||
}
|
||||
]);
|
||||
if (error) throw error;
|
||||
await supabase.from('agenda_eventos').update({ status: 'remarcado' }).eq('owner_id', ownerId.value).eq('tipo', 'sessao').gte('inicio_em', `${feriado.data}T00:00:00`).lte('inicio_em', `${feriado.data}T23:59:59`);
|
||||
await tenantDb().from('agenda_eventos').update({ status: 'remarcado' }).eq('owner_id', ownerId.value).eq('tipo', 'sessao').gte('inicio_em', `${feriado.data}T00:00:00`).lte('inicio_em', `${feriado.data}T23:59:59`);
|
||||
|
||||
feriadosAlertaBloqueados.value = new Set([...feriadosAlertaBloqueados.value, feriado.data]);
|
||||
miniBlockedDaySet.value = new Set([...miniBlockedDaySet.value, feriado.data]);
|
||||
@@ -1438,7 +1435,7 @@ async function desbloquearFeriadoDoAlerta(feriado) {
|
||||
if (!ownerId.value) return;
|
||||
feriadosAlertaSalvando.value = `unblock_${feriado.data}`;
|
||||
try {
|
||||
const { error } = await supabase.from('agenda_bloqueios').delete().eq('owner_id', ownerId.value).eq('data_inicio', feriado.data).in('origem', ['agenda_feriado', 'agenda_dia']);
|
||||
const { error } = await tenantDb().from('agenda_bloqueios').delete().eq('owner_id', ownerId.value).eq('data_inicio', feriado.data).in('origem', ['agenda_feriado', 'agenda_dia']);
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
@@ -1736,8 +1733,7 @@ async function onUpdateSeriesEvent({ id, status, recurrence_date, inicio_em, fim
|
||||
if (!is_virtual || !inicio_em) return;
|
||||
const rid = row.recurrence_id ?? row.serie_id ?? null;
|
||||
const rDate = recurrence_date || inicio_em?.slice(0, 10);
|
||||
const { data: existing } = await supabase
|
||||
.from('agenda_eventos')
|
||||
const { data: existing } = await tenantDb().from('agenda_eventos')
|
||||
.select('id')
|
||||
.eq('recurrence_id', rid)
|
||||
.eq('recurrence_date', rDate)
|
||||
@@ -1807,9 +1803,8 @@ async function _offerBillingContract(normalized, recorrencia, tenantId) {
|
||||
rejectLabel: 'Agora não',
|
||||
accept: async () => {
|
||||
try {
|
||||
const { error } = await supabase.from('billing_contracts').insert({
|
||||
const { error } = await tenantDb().from('billing_contracts').insert({
|
||||
owner_id: normalized.owner_id,
|
||||
tenant_id: tenantId,
|
||||
patient_id: normalized.paciente_id,
|
||||
type: 'package',
|
||||
total_sessions: n,
|
||||
@@ -1933,12 +1928,11 @@ async function onDialogSave(arg) {
|
||||
if (recorrencia?.conflitos?.length && createdRule?.id) {
|
||||
const exceptions = recorrencia.conflitos.map((c) => ({
|
||||
recurrence_id: createdRule.id,
|
||||
tenant_id: clinicId,
|
||||
original_date: c.date,
|
||||
type: c.conflict.type === 'feriado' ? 'holiday_block' : c.conflict.type === 'bloqueado' ? 'cancel_session' : c.conflict.type === 'folga' ? 'cancel_session' : 'cancel_session',
|
||||
reason: c.conflict.label
|
||||
}));
|
||||
const { error: exErr } = await supabase.from('recurrence_exceptions').insert(exceptions);
|
||||
const { error: exErr } = await tenantDb().from('recurrence_exceptions').insert(exceptions);
|
||||
if (exErr) logError('AgendaTerapeutaPage', 'onDialogSave: erro ao inserir exceptions', exErr);
|
||||
}
|
||||
|
||||
@@ -1998,7 +1992,7 @@ async function onDialogSave(arg) {
|
||||
extra_fields: normalized.extra_fields ?? null
|
||||
});
|
||||
if (arg.onSaved) {
|
||||
const { data: existing } = await supabase.from('agenda_eventos').select('id').eq('recurrence_id', recurrenceId).eq('recurrence_date', originalDate).maybeSingle();
|
||||
const { data: existing } = await tenantDb().from('agenda_eventos').select('id').eq('recurrence_id', recurrenceId).eq('recurrence_date', originalDate).maybeSingle();
|
||||
if (existing?.id) {
|
||||
eventId = existing.id;
|
||||
} else {
|
||||
@@ -2091,8 +2085,7 @@ async function onDialogSave(arg) {
|
||||
});
|
||||
|
||||
// Propaga campos não-serviço para sessões já materializadas da série
|
||||
await supabase
|
||||
.from('agenda_eventos')
|
||||
await tenantDb().from('agenda_eventos')
|
||||
.update({
|
||||
modalidade: normalized.modalidade ?? 'presencial',
|
||||
titulo_custom: normalized.titulo_custom ?? null,
|
||||
@@ -2140,8 +2133,7 @@ async function onDialogSave(arg) {
|
||||
});
|
||||
|
||||
// Propaga TODOS os campos para TODAS as sessões materializadas (sem exceção)
|
||||
await supabase
|
||||
.from('agenda_eventos')
|
||||
await tenantDb().from('agenda_eventos')
|
||||
.update({
|
||||
modalidade: normalized.modalidade ?? 'presencial',
|
||||
titulo_custom: normalized.titulo_custom ?? null,
|
||||
@@ -2205,8 +2197,7 @@ async function onDialogSave(arg) {
|
||||
let detail = 'Já existe um compromisso nesse horário. Verifique a agenda e escolha outro horário.';
|
||||
try {
|
||||
if (normalized?.inicio_em && normalized?.fim_em && normalized?.owner_id) {
|
||||
const { data: conflicting } = await supabase
|
||||
.from('agenda_eventos')
|
||||
const { data: conflicting } = await tenantDb().from('agenda_eventos')
|
||||
.select('titulo, inicio_em, fim_em')
|
||||
.eq('owner_id', normalized.owner_id)
|
||||
.lt('inicio_em', normalized.fim_em)
|
||||
@@ -2276,7 +2267,7 @@ async function onDialogDelete(arg) {
|
||||
if (isVirtual) {
|
||||
// Ocorrência virtual: materializa como evento avulso (sem recurrence_id)
|
||||
const rDate = row.original_date || row.inicio_em?.slice(0, 10);
|
||||
const existing = await supabase.from('agenda_eventos').select('id').eq('recurrence_id', recurrenceId).eq('recurrence_date', rDate).maybeSingle();
|
||||
const existing = await tenantDb().from('agenda_eventos').select('id').eq('recurrence_id', recurrenceId).eq('recurrence_date', rDate).maybeSingle();
|
||||
|
||||
if (existing.data?.id) {
|
||||
await update(existing.data.id, { recurrence_id: null, recurrence_date: null });
|
||||
|
||||
Reference in New Issue
Block a user