58 lines
1.9 KiB
JavaScript
58 lines
1.9 KiB
JavaScript
// src/features/agenda/composables/useProfessionalPricing.js
|
|
//
|
|
// Carrega a tabela professional_pricing do owner logado e expõe
|
|
// getPriceFor(commitmentId) → number | null
|
|
//
|
|
// null = commitment_id
|
|
// Regra: lookup exato → fallback NULL → null se nenhum configurado
|
|
|
|
import { ref } from 'vue'
|
|
import { supabase } from '@/lib/supabase/client'
|
|
|
|
export function useProfessionalPricing () {
|
|
const rows = ref([]) // professional_pricing rows
|
|
const loading = ref(false)
|
|
const error = ref('')
|
|
|
|
// ── Carregar todos os preços do owner ──────────────────────────────
|
|
async function load (ownerId) {
|
|
if (!ownerId) return
|
|
loading.value = true
|
|
error.value = ''
|
|
try {
|
|
const { data, error: err } = await supabase
|
|
.from('professional_pricing')
|
|
.select('id, determined_commitment_id, price, notes')
|
|
.eq('owner_id', ownerId)
|
|
|
|
if (err) throw err
|
|
rows.value = data || []
|
|
} catch (e) {
|
|
error.value = e?.message || 'Falha ao carregar precificação.'
|
|
rows.value = []
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
// ── Consulta: preço para um tipo de compromisso ────────────────────
|
|
// 1. Linha com determined_commitment_id === commitmentId
|
|
// 2. Fallback: linha com determined_commitment_id === null (preço padrão)
|
|
// 3. null se nada configurado
|
|
function getPriceFor (commitmentId) {
|
|
if (!rows.value.length) return null
|
|
|
|
// match exato
|
|
if (commitmentId) {
|
|
const exact = rows.value.find(r => r.determined_commitment_id === commitmentId)
|
|
if (exact && exact.price != null) return Number(exact.price)
|
|
}
|
|
|
|
// fallback padrão (commitment_id IS NULL)
|
|
const def = rows.value.find(r => r.determined_commitment_id === null)
|
|
return def && def.price != null ? Number(def.price) : null
|
|
}
|
|
|
|
return { rows, loading, error, load, getPriceFor }
|
|
}
|