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) {
// billing for doc done on queue end, bill only for llm extraction
const billingResult = await billTeam(team_id, creditsToBeBilled);
if (!billingResult.success) {
return res.status(402).json({
success: false,
error:
"Failed to bill team. Insufficient credits or subscription not found.",
});
}
billTeam(team_id, creditsToBeBilled).catch(error => {
Logger.error(`Failed to bill team ${team_id} for ${creditsToBeBilled} credits: ${error}`);
// Optionally, you could notify an admin or add to a retry queue here
});
}
}

View File

@ -54,18 +54,10 @@ export async function searchHelper(
if (justSearch) {
const billingResult = await billTeam(
team_id,
res.length
);
if (!billingResult.success) {
return {
success: false,
error:
"Failed to bill team. Insufficient credits or subscription not found.",
returnCode: 402,
};
}
billTeam(team_id, res.length).catch(error => {
Logger.error(`Failed to bill team ${team_id} for ${res.length} credits: ${error}`);
// Optionally, you could notify an admin or add to a retry queue here
});
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 { logJob } from "../../services/logging/log_job";
import { performCosineSimilarity } from "../../lib/map-cosine";
import { Logger } from "../../lib/logger";
configDotenv();
@ -100,7 +101,10 @@ export async function mapController(
// remove duplicates that could be due to http/https or www
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 timeTakenInSeconds = (endTime - startTime) / 1000;

View File

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

View File

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

View File

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