mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-06-26 18:26:38 +00:00
Keep track of API usage in user accounts, improve approval mechanism (#95)
This commit is contained in:
37
app/lib/supabase/peanuts.ts
Normal file
37
app/lib/supabase/peanuts.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { getSupabase } from './client';
|
||||
|
||||
export async function supabaseAddRefund(peanuts: number) {
|
||||
const supabase = getSupabase();
|
||||
|
||||
// Get the current user ID if available
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser();
|
||||
const userId = user?.id || null;
|
||||
|
||||
const { data, error } = await supabase.from('profiles').select('peanuts_refunded').eq('id', userId).single();
|
||||
|
||||
if (error) {
|
||||
console.error('AddPeanutsRefund:ErrorFetchingData', { error });
|
||||
return;
|
||||
}
|
||||
|
||||
const currentPeanutsRefunded = data.peanuts_refunded;
|
||||
if (typeof currentPeanutsRefunded !== 'number') {
|
||||
console.error('AddPeanutsRefund:InvalidPeanutsRefunded', { currentPeanutsRefunded });
|
||||
return;
|
||||
}
|
||||
|
||||
const newPeanutsRefunded = Math.round(currentPeanutsRefunded + peanuts);
|
||||
|
||||
// Note: this is not atomic.
|
||||
// https://linear.app/replay/issue/PRO-1122/update-api-usage-atomically
|
||||
const { error: updateError } = await supabase
|
||||
.from('profiles')
|
||||
.update({ peanuts_refunded: newPeanutsRefunded })
|
||||
.eq('id', userId);
|
||||
|
||||
if (updateError) {
|
||||
console.error('AddPeanutsRefund:ErrorUpdatingData', { updateError });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user