carousel, agenda arquivados, agenda cor, agenda arquivados, grupos pacientes, pacientes arquivados - desativados, sessoes verificadas, ajuste notificações, Prontuario, Agenda Animation, Menu Profile, bagdes Profile, Offline
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
// src/composables/usePatientLifecycle.js
|
||||
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),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user