From ee3e5dc69cbd85ab687e19fb96fe0a6e02fabfd1 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Fri, 30 Aug 2024 12:34:45 -0300 Subject: [PATCH] Nick: --- apps/api/src/controllers/v1/types.ts | 10 +++--- .../src/services/billing/credit_billing.ts | 33 ++++++++++++++----- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/apps/api/src/controllers/v1/types.ts b/apps/api/src/controllers/v1/types.ts index 7774823a..c8981024 100644 --- a/apps/api/src/controllers/v1/types.ts +++ b/apps/api/src/controllers/v1/types.ts @@ -312,12 +312,12 @@ export function legacyScrapeOptions(x: ScrapeOptions): PageOptions { }; } -export function legacyExtractorOptions(x: ExtractOptions): ExtractorOptions { +export function legacyExtractorOptions(x?: ExtractOptions): ExtractorOptions { return { - mode: x.mode ? "llm-extraction" : "markdown", - extractionPrompt: x.prompt ?? "Based on the information on the page, extract the information from the schema.", - extractionSchema: x.schema, - userPrompt: x.prompt ?? "", + mode: x?.mode ? "llm-extraction" : "markdown", + extractionPrompt: x?.prompt ?? "Based on the information on the page, extract the information from the schema.", + extractionSchema: x?.schema, + userPrompt: x?.prompt ?? "", }; } diff --git a/apps/api/src/services/billing/credit_billing.ts b/apps/api/src/services/billing/credit_billing.ts index 8e4e21d3..22dc72df 100644 --- a/apps/api/src/services/billing/credit_billing.ts +++ b/apps/api/src/services/billing/credit_billing.ts @@ -199,27 +199,44 @@ export async function supaCheckTeamCredits(team_id: string, credits: number) { ); } - if(subscriptionError) { - Logger.error(`Subscription error: ${subscriptionError}`); - throw new Error(`Subscription error: ${subscriptionError}`); - } + // Free credits, no coupons - if (!subscription) { + if (!subscription || subscriptionError) { + // If there is no active subscription but there are available coupons if (couponCredits >= credits) { return { success: true, message: "Sufficient credits available", remainingCredits: couponCredits }; } - const { data: creditUsages, error: creditUsageError } = - await supabase_service + let creditUsages; + let creditUsageError; + let retries = 0; + const maxRetries = 3; + const retryInterval = 2000; // 2 seconds + + while (retries < maxRetries) { + const result = await supabase_service .from("credit_usage") .select("credits_used") .is("subscription_id", null) .eq("team_id", team_id); + creditUsages = result.data; + creditUsageError = result.error; + + if (!creditUsageError) { + break; + } + + retries++; + if (retries < maxRetries) { + await new Promise(resolve => setTimeout(resolve, retryInterval)); + } + } + if (creditUsageError) { - Logger.error(`Credit usage error: ${creditUsageError}`); + Logger.error(`Credit usage error after ${maxRetries} attempts: ${creditUsageError}`); throw new Error( `Failed to retrieve credit usage for team_id: ${team_id}` );