71 lines
3.1 KiB
JavaScript
71 lines
3.1 KiB
JavaScript
/*
|
|
|--------------------------------------------------------------------------
|
|
| Agência PSI
|
|
|--------------------------------------------------------------------------
|
|
| Criado e desenvolvido por Leonardo Nohama
|
|
|
|
|
| Tecnologia aplicada à escuta.
|
|
| Estrutura para o cuidado.
|
|
|
|
|
| Arquivo: src/composables/usePatientLifecycle.js
|
|
| Data: 2026
|
|
| Local: São Carlos/SP — Brasil
|
|
|--------------------------------------------------------------------------
|
|
| © 2026 — Todos os direitos reservados
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
import { supabase } from '@/lib/supabase/client';
|
|
|
|
export function usePatientLifecycle() {
|
|
async function canDelete(patientId) {
|
|
const { data, error } = await supabase.rpc('can_delete_patient', { p_patient_id: patientId });
|
|
if (error) return false;
|
|
return !!data;
|
|
}
|
|
|
|
async function deletePatient(patientId) {
|
|
const { data, error } = await supabase.rpc('safe_delete_patient', { p_patient_id: patientId });
|
|
if (error) return { ok: false, error: 'rpc_error', message: error.message };
|
|
return data; // { ok, error?, message? }
|
|
}
|
|
|
|
async function checkActiveSchedule(patientId) {
|
|
const now = new Date().toISOString();
|
|
const [evts, recs] = await Promise.all([
|
|
supabase.from('agenda_eventos').select('id', { count: 'exact', head: true }).eq('patient_id', patientId).eq('status', 'agendado').gt('inicio_em', now),
|
|
supabase.from('recurrence_rules').select('id', { count: 'exact', head: true }).eq('patient_id', patientId).eq('status', 'ativo')
|
|
]);
|
|
return {
|
|
hasFutureSessions: (evts.count ?? 0) > 0,
|
|
hasActiveRecurrence: (recs.count ?? 0) > 0
|
|
};
|
|
}
|
|
|
|
async function deactivatePatient(patientId) {
|
|
const { error } = await supabase.from('patients').update({ status: 'Inativo', updated_at: new Date().toISOString() }).eq('id', patientId);
|
|
return error ? { ok: false, error } : { ok: true };
|
|
}
|
|
|
|
async function archivePatient(patientId) {
|
|
const { error } = await supabase.from('patients').update({ status: 'Arquivado', updated_at: new Date().toISOString() }).eq('id', patientId);
|
|
return error ? { ok: false, error } : { ok: true };
|
|
}
|
|
|
|
async function reactivatePatient(patientId) {
|
|
const { error } = await supabase.from('patients').update({ status: 'Ativo', updated_at: new Date().toISOString() }).eq('id', patientId);
|
|
return error ? { ok: false, error } : { ok: true };
|
|
}
|
|
|
|
return { canDelete, deletePatient, checkActiveSchedule, deactivatePatient, archivePatient, reactivatePatient };
|
|
}
|
|
|
|
// ─── Helper puro — não precisa de instância do composable ───────────────────
|
|
export function getPatientAgendaPermissions(status) {
|
|
return {
|
|
canCreateSession: !['Inativo', 'Arquivado'].includes(status),
|
|
canReschedule: !['Inativo'].includes(status),
|
|
canEditPastSession: !['Arquivado'].includes(status),
|
|
canCreateRecurrence: !['Inativo', 'Arquivado'].includes(status)
|
|
};
|
|
}
|