Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
Update credit_billing.ts
Nicolasfce17e6beb4c
diff --git a/apps/api/src/services/billing/credit_billing.ts b/apps/api/src/services/billing/credit_billing.tsindex 37db664ed..892530c93 100644--- a/apps/api/src/services/billing/credit_billing.ts+++ b/apps/api/src/services/billing/credit_billing.ts@@ -212,20 +212,26 @@ export async function supaCheckTeamCredits(team_id: string, credits: number) {return { success: true, message: "Sufficient credits available" };}- // Calculate the total credits used by the team within the current billing period- const { data: creditUsages, error: creditUsageError } = await supabase_service- .from("credit_usage")- .select("credits_used")- .eq("subscription_id", subscription.id)- .gte("created_at", subscription.current_period_start)- .lte("created_at", subscription.current_period_end);-- if (creditUsageError) {- throw new Error(`Failed to retrieve credit usage for subscription_id: ${subscription.id}`);- }+ let totalCreditsUsed = 0;+ try {+ const { data: creditUsages, error: creditUsageError } = await supabase_service+ .rpc("get_credit_usage_2", {+ sub_id: subscription.id,+ start_time: subscription.current_period_start,+ end_time: subscription.current_period_end+ });- const totalCreditsUsed = creditUsages.reduce((acc, usage) => acc + usage.credits_used, 0);+ if (creditUsageError) {+ console.error("Error calculating credit usage:", creditUsageError);+ }+ if (creditUsages && creditUsages.length > 0) {+ totalCreditsUsed = creditUsages[0].total_credits_used;+ console.log("Total Credits Used:", totalCreditsUsed);+ }+ } catch (error) {+ console.error("Error calculating credit usage:", error);+ }// Adjust total credits used by subtracting coupon valueconst adjustedCreditsUsed = Math.max(0, totalCreditsUsed - couponCredits);