From fce17e6beb4c280cef578382c0ef2735b07d77e2 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Thu, 9 May 2024 15:29:58 -0700 Subject: [PATCH] Update credit_billing.ts --- .../src/services/billing/credit_billing.ts | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/apps/api/src/services/billing/credit_billing.ts b/apps/api/src/services/billing/credit_billing.ts index 37db664..892530c 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); + 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 + }); - if (creditUsageError) { - throw new Error(`Failed to retrieve credit usage for subscription_id: ${subscription.id}`); + 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); } - - const totalCreditsUsed = creditUsages.reduce((acc, usage) => acc + usage.credits_used, 0); - // Adjust total credits used by subtracting coupon value const adjustedCreditsUsed = Math.max(0, totalCreditsUsed - couponCredits);