Files
firecrawl/apps/api/src/controllers/v1/crawl-cancel.ts
T

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-09-24 18:28:46 +02:00
import { Response } from "express";
2024-08-27 09:42:55 -03:00
import { supabase_service } from "../../services/supabase";
import { Logger } from "../../lib/logger";
import { getCrawl, saveCrawl } from "../../lib/crawl-redis";
import * as Sentry from "@sentry/node";
2024-09-04 15:57:57 -03:00
import { configDotenv } from "dotenv";
2024-09-24 18:28:46 +02:00
import { RequestWithAuth } from "./types";
2024-09-04 15:57:57 -03:00
configDotenv();
2024-08-06 15:24:45 -03:00
2024-09-24 18:28:46 +02:00
export async function crawlCancelController(req: RequestWithAuth<{ jobId: string }>, res: Response) {
2024-08-06 15:24:45 -03:00
try {
const useDbAuthentication = process.env.USE_DB_AUTHENTICATION === 'true';
2024-08-27 09:42:55 -03:00
const sc = await getCrawl(req.params.jobId);
if (!sc) {
2024-08-06 15:24:45 -03:00
return res.status(404).json({ error: "Job not found" });
}
// check if the job belongs to the team
if (useDbAuthentication) {
const { data, error: supaError } = await supabase_service
.from("bulljobs_teams")
.select("*")
.eq("job_id", req.params.jobId)
2024-09-24 18:28:46 +02:00
.eq("team_id", req.auth.team_id);
2024-08-06 15:24:45 -03:00
if (supaError) {
return res.status(500).json({ error: supaError.message });
}
if (data.length === 0) {
return res.status(403).json({ error: "Unauthorized" });
}
}
try {
2024-08-27 09:42:55 -03:00
sc.cancelled = true;
await saveCrawl(req.params.jobId, sc);
2024-08-06 15:24:45 -03:00
} catch (error) {
Logger.error(error);
}
res.json({
status: "cancelled"
});
} catch (error) {
2024-08-27 09:42:55 -03:00
Sentry.captureException(error);
2024-08-06 15:24:45 -03:00
Logger.error(error);
return res.status(500).json({ error: error.message });
}
}