mirror of
https://git.mirrors.martin98.com/https://github.com/mendableai/firecrawl
synced 2025-08-05 17:00:44 +08:00
Merge pull request #619 from mendableai/nsc/bill-team-async
Bill team async
This commit is contained in:
commit
f07c2bd23b
@ -244,16 +244,12 @@ 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.",
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let doc = result.data;
|
let doc = result.data;
|
||||||
if (!pageOptions || !pageOptions.includeRawHtml) {
|
if (!pageOptions || !pageOptions.includeRawHtml) {
|
||||||
|
@ -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 };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
@ -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) {
|
||||||
|
@ -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: [],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -465,7 +465,7 @@ 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([
|
||||||
{
|
{
|
||||||
@ -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 };
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user