35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { cancelJob } from "@/lib/jobs/cancel";
|
|
import { prisma } from "@/lib/db";
|
|
import { requireSessionUser } from "@/lib/auth/session";
|
|
import { authErrorResponse } from "@/lib/auth/api";
|
|
|
|
export async function POST(
|
|
_request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const user = await requireSessionUser();
|
|
const { id } = await params;
|
|
const job = await prisma.backgroundJob.findUnique({ where: { id } });
|
|
const ownedAccount = job?.accountId
|
|
? await prisma.account.findFirst({
|
|
where: { id: job.accountId, userId: user.id },
|
|
select: { id: true },
|
|
})
|
|
: null;
|
|
if (!job || !ownedAccount) {
|
|
return NextResponse.json({ error: "找不到任務" }, { status: 404 });
|
|
}
|
|
const result = await cancelJob(id);
|
|
if (!result.ok) {
|
|
return NextResponse.json({ error: result.error ?? "停止失敗" }, { status: 400 });
|
|
}
|
|
return NextResponse.json({ ok: true, message: "已停止背景任務" });
|
|
} catch (error) {
|
|
const authRes = authErrorResponse(error);
|
|
if (authRes) return authRes;
|
|
return NextResponse.json({ error: "停止任務失敗" }, { status: 500 });
|
|
}
|
|
}
|