first commit
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
// src/services/subscriptionIntents.js
|
||||
import { supabase } from '@/lib/supabase/client'
|
||||
|
||||
function applyFilters(query, { q, status, planKey, interval }) {
|
||||
if (q) query = query.ilike('email', `%${q}%`)
|
||||
if (status) query = query.eq('status', status)
|
||||
if (planKey) query = query.eq('plan_key', planKey)
|
||||
if (interval) query = query.eq('interval', interval)
|
||||
return query
|
||||
}
|
||||
|
||||
export async function listSubscriptionIntents(filters = {}) {
|
||||
let query = supabase
|
||||
.from('subscription_intents')
|
||||
.select('*')
|
||||
.order('created_at', { ascending: false })
|
||||
|
||||
query = applyFilters(query, filters)
|
||||
|
||||
const { data, error } = await query
|
||||
if (error) throw error
|
||||
return data || []
|
||||
}
|
||||
|
||||
export async function markIntentPaid(intentId, notes = '') {
|
||||
// 1) marca como pago
|
||||
const { data: updated, error: upErr } = await supabase
|
||||
.from('subscription_intents')
|
||||
.update({
|
||||
status: 'paid',
|
||||
paid_at: new Date().toISOString(),
|
||||
notes: notes || null
|
||||
})
|
||||
.eq('id', intentId)
|
||||
.select('*')
|
||||
.maybeSingle()
|
||||
|
||||
if (upErr) throw upErr
|
||||
|
||||
// 2) ativa subscription do tenant (Modelo B)
|
||||
const { data: sub, error: rpcErr } = await supabase.rpc('activate_subscription_from_intent', {
|
||||
p_intent_id: intentId
|
||||
})
|
||||
|
||||
if (rpcErr) throw rpcErr
|
||||
|
||||
return { intent: updated, subscription: sub }
|
||||
}
|
||||
|
||||
export async function cancelIntent(intentId, notes = '') {
|
||||
const { data, error } = await supabase
|
||||
.from('subscription_intents')
|
||||
.update({
|
||||
status: 'canceled',
|
||||
notes: notes || null
|
||||
})
|
||||
.eq('id', intentId)
|
||||
.select('*')
|
||||
.maybeSingle()
|
||||
|
||||
if (error) throw error
|
||||
return data
|
||||
}
|
||||
Reference in New Issue
Block a user