98 lines
2.3 KiB
JavaScript
98 lines
2.3 KiB
JavaScript
// src/features/agenda/services/agendaRepository.js
|
|
import { supabase } from '@/lib/supabase/client'
|
|
|
|
export async function getMyAgendaSettings () {
|
|
const { data: userRes, error: userErr } = await supabase.auth.getUser()
|
|
if (userErr) throw userErr
|
|
|
|
const uid = userRes?.user?.id
|
|
if (!uid) throw new Error('Usuário não autenticado.')
|
|
|
|
const { data, error } = await supabase
|
|
.from('agenda_configuracoes')
|
|
.select('*')
|
|
.eq('owner_id', uid)
|
|
.single()
|
|
|
|
if (error) throw error
|
|
return data
|
|
}
|
|
|
|
export async function listMyAgendaEvents ({ startISO, endISO }) {
|
|
const { data: userRes, error: userErr } = await supabase.auth.getUser()
|
|
if (userErr) throw userErr
|
|
|
|
const uid = userRes?.user?.id
|
|
if (!uid) throw new Error('Usuário não autenticado.')
|
|
|
|
const { data, error } = await supabase
|
|
.from('agenda_eventos')
|
|
.select('*')
|
|
.eq('owner_id', uid)
|
|
.gte('inicio_em', startISO)
|
|
.lt('inicio_em', endISO)
|
|
.order('inicio_em', { ascending: true })
|
|
|
|
if (error) throw error
|
|
return data || []
|
|
}
|
|
|
|
export async function listClinicEvents ({ ownerIds, startISO, endISO }) {
|
|
if (!ownerIds?.length) return []
|
|
|
|
const { data, error } = await supabase
|
|
.from('agenda_eventos')
|
|
.select('*')
|
|
.in('owner_id', ownerIds)
|
|
.gte('inicio_em', startISO)
|
|
.lt('inicio_em', endISO)
|
|
.order('inicio_em', { ascending: true })
|
|
|
|
if (error) throw error
|
|
return data || []
|
|
}
|
|
|
|
export async function listTenantStaff (tenantId) {
|
|
if (!tenantId || tenantId === 'null' || tenantId === 'undefined') return []
|
|
|
|
const { data, error } = await supabase
|
|
.from('v_tenant_staff')
|
|
.select('*')
|
|
.eq('tenant_id', tenantId)
|
|
|
|
if (error) throw error
|
|
return data || []
|
|
}
|
|
|
|
export async function createAgendaEvento (payload) {
|
|
const { data, error } = await supabase
|
|
.from('agenda_eventos')
|
|
.insert(payload)
|
|
.select('*')
|
|
.single()
|
|
|
|
if (error) throw error
|
|
return data
|
|
}
|
|
|
|
export async function updateAgendaEvento (id, patch) {
|
|
const { data, error } = await supabase
|
|
.from('agenda_eventos')
|
|
.update(patch)
|
|
.eq('id', id)
|
|
.select('*')
|
|
.single()
|
|
|
|
if (error) throw error
|
|
return data
|
|
}
|
|
|
|
export async function deleteAgendaEvento (id) {
|
|
const { error } = await supabase
|
|
.from('agenda_eventos')
|
|
.delete()
|
|
.eq('id', id)
|
|
|
|
if (error) throw error
|
|
return true
|
|
} |