Ajuste Convenios e Particular
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
// 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('')
|
||||
// plans – ref([]) todos os planos do owner (ativos e inativos)
|
||||
// loading – ref(false)
|
||||
// error – ref(null)
|
||||
//
|
||||
// load(ownerId) – carrega todos os planos ativos
|
||||
// save(payload) – cria ou atualiza (id presente = update)
|
||||
// remove(id) – soft-delete (active = false)
|
||||
// load(ownerId) – carrega planos com seus procedimentos
|
||||
// save(payload) – insert ou update do plano (name, notes)
|
||||
// toggle(id, active) – alterna active do plano
|
||||
// remove(id) – soft-delete do plano
|
||||
// savePlanService(payload) – insert ou update de procedimento { id?, insurance_plan_id, name, value }
|
||||
// togglePlanService(id, active) – alterna active do procedimento
|
||||
// removePlanService(id) – DELETE definitivo do procedimento
|
||||
|
||||
import { ref } from 'vue'
|
||||
import { supabase } from '@/lib/supabase/client'
|
||||
@@ -17,24 +19,27 @@ import { supabase } from '@/lib/supabase/client'
|
||||
export function useInsurancePlans () {
|
||||
const plans = ref([])
|
||||
const loading = ref(false)
|
||||
const error = ref('')
|
||||
const error = ref(null)
|
||||
|
||||
async function load (ownerId) {
|
||||
if (!ownerId) return
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
error.value = null
|
||||
try {
|
||||
const { data, error: err } = await supabase
|
||||
.from('insurance_plans')
|
||||
.select('id, name, notes, default_value, active')
|
||||
.select(`
|
||||
*,
|
||||
insurance_plan_services (
|
||||
id, name, value, active
|
||||
)
|
||||
`)
|
||||
.eq('owner_id', ownerId)
|
||||
.eq('active', true)
|
||||
.order('name', { ascending: true })
|
||||
|
||||
.order('name')
|
||||
if (err) throw err
|
||||
plans.value = data || []
|
||||
} catch (e) {
|
||||
error.value = e?.message || 'Falha ao carregar convênios.'
|
||||
error.value = e?.message || 'Erro ao carregar convênios'
|
||||
plans.value = []
|
||||
} finally {
|
||||
loading.value = false
|
||||
@@ -42,42 +47,141 @@ export function useInsurancePlans () {
|
||||
}
|
||||
|
||||
async function save (payload) {
|
||||
error.value = ''
|
||||
error.value = null
|
||||
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)
|
||||
.update({
|
||||
name: payload.name,
|
||||
notes: payload.notes || null,
|
||||
updated_at: new Date().toISOString(),
|
||||
})
|
||||
.eq('id', payload.id)
|
||||
if (err) throw err
|
||||
} else {
|
||||
const { error: err } = await supabase
|
||||
.from('insurance_plans')
|
||||
.insert(payload)
|
||||
.insert({
|
||||
owner_id: payload.owner_id,
|
||||
tenant_id: payload.tenant_id,
|
||||
name: payload.name,
|
||||
notes: payload.notes || null,
|
||||
})
|
||||
if (err) throw err
|
||||
}
|
||||
} catch (e) {
|
||||
error.value = e?.message || 'Falha ao salvar convênio.'
|
||||
error.value = e?.message || 'Erro ao salvar convênio'
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
async function toggle (id, active) {
|
||||
error.value = null
|
||||
try {
|
||||
const { error: err } = await supabase
|
||||
.from('insurance_plans')
|
||||
.update({ active })
|
||||
.eq('id', id)
|
||||
if (err) throw err
|
||||
const plan = plans.value.find(p => p.id === id)
|
||||
if (plan) plan.active = active
|
||||
} catch (e) {
|
||||
error.value = e?.message || 'Erro ao atualizar convênio'
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
async function remove (id) {
|
||||
error.value = ''
|
||||
error.value = null
|
||||
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)
|
||||
const plan = plans.value.find(p => p.id === id)
|
||||
if (plan) plan.active = false
|
||||
} catch (e) {
|
||||
error.value = e?.message || 'Falha ao remover convênio.'
|
||||
error.value = e?.message || 'Erro ao remover convênio'
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
return { plans, loading, error, load, save, remove }
|
||||
async function savePlanService (payload) {
|
||||
error.value = null
|
||||
try {
|
||||
if (payload.id) {
|
||||
const { error: err } = await supabase
|
||||
.from('insurance_plan_services')
|
||||
.update({
|
||||
name: payload.name,
|
||||
value: payload.value,
|
||||
})
|
||||
.eq('id', payload.id)
|
||||
if (err) throw err
|
||||
} else {
|
||||
const { error: err } = await supabase
|
||||
.from('insurance_plan_services')
|
||||
.insert({
|
||||
insurance_plan_id: payload.insurance_plan_id,
|
||||
name: payload.name,
|
||||
value: payload.value,
|
||||
})
|
||||
if (err) throw err
|
||||
}
|
||||
} catch (e) {
|
||||
error.value = e?.message || 'Erro ao salvar procedimento'
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
async function togglePlanService (id, active) {
|
||||
error.value = null
|
||||
try {
|
||||
const { error: err } = await supabase
|
||||
.from('insurance_plan_services')
|
||||
.update({ active })
|
||||
.eq('id', id)
|
||||
if (err) throw err
|
||||
} catch (e) {
|
||||
error.value = e?.message || 'Erro ao atualizar procedimento'
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
async function removeDefinitivo (id) {
|
||||
error.value = null
|
||||
try {
|
||||
const { error: err } = await supabase
|
||||
.from('insurance_plans')
|
||||
.delete()
|
||||
.eq('id', id)
|
||||
if (err) throw err
|
||||
plans.value = plans.value.filter(p => p.id !== id)
|
||||
} catch (e) {
|
||||
error.value = e?.message || 'Erro ao remover convênio'
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
async function removePlanService (id) {
|
||||
error.value = null
|
||||
try {
|
||||
const { error: err } = await supabase
|
||||
.from('insurance_plan_services')
|
||||
.delete()
|
||||
.eq('id', id)
|
||||
if (err) throw err
|
||||
} catch (e) {
|
||||
error.value = e?.message || 'Erro ao remover procedimento'
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
plans, loading, error,
|
||||
load, save, toggle, remove, removeDefinitivo,
|
||||
savePlanService, togglePlanService, removePlanService,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user