Merge pull request #619 from mendableai/nsc/bill-team-async

Bill team async
This commit is contained in:
Nicolas 2024-09-03 21:44:27 -03:00 committed by GitHub
commit f07c2bd23b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 25 additions and 43 deletions

View File

@ -244,14 +244,10 @@ export async function scrapeController(req: Request, res: Response) {
} }
if (creditsToBeBilled > 0) { if (creditsToBeBilled > 0) {
// billing for doc done on queue end, bill only for llm extraction // billing for doc done on queue end, bill only for llm extraction
const billingResult = await billTeam(team_id, creditsToBeBilled); billTeam(team_id, creditsToBeBilled).catch(error => {
if (!billingResult.success) { Logger.error(`Failed to bill team ${team_id} for ${creditsToBeBilled} credits: ${error}`);
return res.status(402).json({ // Optionally, you could notify an admin or add to a retry queue here
success: false, });
error:
"Failed to bill team. Insufficient credits or subscription not found.",
});
}
} }
} }

View File

@ -54,18 +54,10 @@ export async function searchHelper(
if (justSearch) { if (justSearch) {
const billingResult = await billTeam( billTeam(team_id, res.length).catch(error => {
team_id, Logger.error(`Failed to bill team ${team_id} for ${res.length} credits: ${error}`);
res.length // Optionally, you could notify an admin or add to a retry queue here
); });
if (!billingResult.success) {
return {
success: false,
error:
"Failed to bill team. Insufficient credits or subscription not found.",
returnCode: 402,
};
}
return { success: true, data: res, returnCode: 200 }; return { success: true, data: res, returnCode: 200 };
} }

View File

@ -18,6 +18,7 @@ import { fireEngineMap } from "../../search/fireEngine";
import { billTeam } from "../../services/billing/credit_billing"; import { billTeam } from "../../services/billing/credit_billing";
import { logJob } from "../../services/logging/log_job"; import { logJob } from "../../services/logging/log_job";
import { performCosineSimilarity } from "../../lib/map-cosine"; import { performCosineSimilarity } from "../../lib/map-cosine";
import { Logger } from "../../lib/logger";
configDotenv(); configDotenv();
@ -100,7 +101,10 @@ export async function mapController(
// remove duplicates that could be due to http/https or www // remove duplicates that could be due to http/https or www
links = removeDuplicateUrls(links); links = removeDuplicateUrls(links);
await billTeam(req.auth.team_id, 1); billTeam(req.auth.team_id, 1).catch(error => {
Logger.error(`Failed to bill team ${req.auth.team_id} for 1 credit: ${error}`);
// Optionally, you could notify an admin or add to a retry queue here
});
const endTime = new Date().getTime(); const endTime = new Date().getTime();
const timeTakenInSeconds = (endTime - startTime) / 1000; const timeTakenInSeconds = (endTime - startTime) / 1000;

View File

@ -106,14 +106,10 @@ export async function scrapeController(
creditsToBeBilled = 50; creditsToBeBilled = 50;
} }
const billingResult = await billTeam(req.auth.team_id, creditsToBeBilled); billTeam(req.auth.team_id, creditsToBeBilled).catch(error => {
if (!billingResult.success) { Logger.error(`Failed to bill team ${req.auth.team_id} for ${creditsToBeBilled} credits: ${error}`);
return res.status(402).json({ // Optionally, you could notify an admin or add to a retry queue here
success: false, });
error:
"Failed to bill team. Insufficient credits or subscription not found.",
});
}
if (!pageOptions || !pageOptions.includeRawHtml) { if (!pageOptions || !pageOptions.includeRawHtml) {
if (doc && doc.rawHtml) { if (doc && doc.rawHtml) {

View File

@ -118,15 +118,10 @@ export async function runWebScraper({
: docs; : docs;
if(is_scrape === false) { if(is_scrape === false) {
const billingResult = await billTeam(team_id, filteredDocs.length); billTeam(team_id, filteredDocs.length).catch(error => {
if (!billingResult.success) { Logger.error(`Failed to bill team ${team_id} for ${filteredDocs.length} credits: ${error}`);
// throw new Error("Failed to bill team, no subscription was found"); // Optionally, you could notify an admin or add to a retry queue here
return { });
success: false,
message: "Failed to bill team, no subscription was found",
docs: [],
};
}
} }

View File

@ -465,8 +465,8 @@ async function createCreditUsage({
subscription_id?: string; subscription_id?: string;
credits: number; credits: number;
}) { }) {
const { data: credit_usage } = await supabase_service await supabase_service
.from("credit_usage") .from("credit_usage")
.insert([ .insert([
{ {
team_id, team_id,
@ -474,8 +474,7 @@ async function createCreditUsage({
subscription_id: subscription_id || null, subscription_id: subscription_id || null,
created_at: new Date(), created_at: new Date(),
}, },
]) ]);
.select();
return { success: true, credit_usage }; return { success: true };
} }