Preficicação, Convenio, Ajustes Agenda, Configurações Excessões
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
// src/features/agenda/composables/useInsurancePlans.js
|
||||
//
|
||||
// CRUD sobre a tabela public.insurance_plans.
|
||||
//
|
||||
// Interface pública:
|
||||
// plans – ref([]) lista de planos ativos do owner
|
||||
// loading – ref(false)
|
||||
// error – ref('')
|
||||
//
|
||||
// load(ownerId) – carrega todos os planos ativos
|
||||
// save(payload) – cria ou atualiza (id presente = update)
|
||||
// remove(id) – soft-delete (active = false)
|
||||
|
||||
import { ref } from 'vue'
|
||||
import { supabase } from '@/lib/supabase/client'
|
||||
|
||||
export function useInsurancePlans () {
|
||||
const plans = ref([])
|
||||
const loading = ref(false)
|
||||
const error = ref('')
|
||||
|
||||
async function load (ownerId) {
|
||||
if (!ownerId) return
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
const { data, error: err } = await supabase
|
||||
.from('insurance_plans')
|
||||
.select('id, name, notes, default_value, active')
|
||||
.eq('owner_id', ownerId)
|
||||
.eq('active', true)
|
||||
.order('name', { ascending: true })
|
||||
|
||||
if (err) throw err
|
||||
plans.value = data || []
|
||||
} catch (e) {
|
||||
error.value = e?.message || 'Falha ao carregar convênios.'
|
||||
plans.value = []
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function save (payload) {
|
||||
error.value = ''
|
||||
try {
|
||||
if (payload.id) {
|
||||
const { id, owner_id, tenant_id, ...fields } = payload
|
||||
const { error: err } = await supabase
|
||||
.from('insurance_plans')
|
||||
.update(fields)
|
||||
.eq('id', id)
|
||||
.eq('owner_id', owner_id)
|
||||
if (err) throw err
|
||||
} else {
|
||||
const { error: err } = await supabase
|
||||
.from('insurance_plans')
|
||||
.insert(payload)
|
||||
if (err) throw err
|
||||
}
|
||||
} catch (e) {
|
||||
error.value = e?.message || 'Falha ao salvar convênio.'
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
async function remove (id) {
|
||||
error.value = ''
|
||||
try {
|
||||
const { error: err } = await supabase
|
||||
.from('insurance_plans')
|
||||
.update({ active: false })
|
||||
.eq('id', id)
|
||||
if (err) throw err
|
||||
plans.value = plans.value.filter(p => p.id !== id)
|
||||
} catch (e) {
|
||||
error.value = e?.message || 'Falha ao remover convênio.'
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
return { plans, loading, error, load, save, remove }
|
||||
}
|
||||
Reference in New Issue
Block a user