F6.2 Lote G: funcoes SQL puras -> plpgsql + roteamento (completa F6.2)

DB (supabase_admin, manual/f6_2g_sql_to_plpgsql.supabase_admin.sql): SQL puro
nao permite set_config dinamico -> converte 5 pra plpgsql + p_tenant_id +
_tenant_route:
- get_financial_summary, get_financial_report, get_patient_session_counts
  (remove filtro tenant_id IN, schema-scoped)
- list_financial_records: RETURNS SETOF financial_records -> jsonb (array,
  transparente no FE)
- get_entity_primary_phone (interno, 0 callers): herda search_path do chamador
  (sem SET, unqualified)
Smoke: get_financial_summary + list_financial_records (array 5) OK.

Frontend (3 arquivos): p_tenant_id de activeTenantId/resolveTenantId nas
chamadas de get_financial_summary/list_financial_records/get_patient_session_
counts. Build passa.

=== F6.2 COMPLETA: 66 funcoes migradas (triggers A/B/C + RPCs D/E/F/G) ===

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Leonardo
2026-06-13 16:00:16 -03:00
parent 423aa5ac2a
commit ee82985dc3
4 changed files with 138 additions and 5 deletions
@@ -21,8 +21,10 @@ import { useRouter } from 'vue-router';
import { supabase } from '@/lib/supabase/client';
import { tenantDb } from '@/lib/supabase/tenantClient';
import { useTenantStore } from '@/stores/tenantStore';
// ─── helpers ─────────────────────────────────────────────────────────────────
const router = useRouter();
const tenantStore = useTenantStore();
const _brl = new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL' });
function fmtBRL(v) {
@@ -67,6 +69,7 @@ async function loadSummary(uid) {
try {
// Receitas e despesas pagas no mês via RPC
const { data: rpc } = await supabase.rpc('get_financial_summary', {
p_tenant_id: tenantStore.activeTenantId,
p_owner_id: uid,
p_year: year,
p_month: month
@@ -117,7 +120,7 @@ async function loadChart(uid) {
chartLoading.value = true;
const months = getLast6Months();
try {
const results = await Promise.all(months.map((m) => supabase.rpc('get_financial_summary', { p_owner_id: uid, p_year: m.year, p_month: m.month })));
const results = await Promise.all(months.map((m) => supabase.rpc('get_financial_summary', { p_tenant_id: tenantStore.activeTenantId, p_owner_id: uid, p_year: m.year, p_month: m.month })));
const receitas = results.map((r) => Number((Array.isArray(r.data) ? r.data[0] : r.data)?.total_receitas ?? 0));
const despesas = results.map((r) => Number((Array.isArray(r.data) ? r.data[0] : r.data)?.total_despesas ?? 0));
@@ -169,6 +172,7 @@ async function loadRecent(uid) {
recentLoading.value = true;
try {
const { data } = await supabase.rpc('list_financial_records', {
p_tenant_id: tenantStore.activeTenantId,
p_owner_id: uid,
p_limit: 5,
p_offset: 0
@@ -510,7 +510,8 @@ export async function markIntakeConverted(intakeId, patientId, { tenantId } = {}
*/
export async function getSessionCounts(patientIds) {
if (!patientIds?.length) return [];
const { data, error } = await supabase.rpc('get_patient_session_counts', { p_patient_ids: patientIds });
const tid = resolveTenantId();
const { data, error } = await supabase.rpc('get_patient_session_counts', { p_tenant_id: tid, p_patient_ids: patientIds });
if (error) throw error;
return data || [];
}