JB/web/src/app/api/jobs/[id]/interaction/route.ts

54 lines
1.5 KiB
TypeScript

import { NextResponse } from "next/server";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
export async function POST(req: Request, { params }: { params: { id: string } }) {
try {
const session = await getServerSession(authOptions);
const userId = (session?.user as any)?.id;
if (!userId) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const jobId = params.id;
const { status, notes } = await req.json();
if (!status) {
// If status is null or empty, delete interaction
await prisma.userJobInteraction.deleteMany({
where: { userId, jobId },
});
return NextResponse.json({ success: true, status: null });
}
const validStatuses = ["SAVED", "APPLIED", "INTERVIEWING", "REJECTED", "HIDDEN"];
if (!validStatuses.includes(status)) {
return NextResponse.json({ error: "Invalid status" }, { status: 400 });
}
const interaction = await prisma.userJobInteraction.upsert({
where: {
userId_jobId: {
userId,
jobId,
},
},
update: {
status,
notes: notes !== undefined ? notes : undefined,
},
create: {
userId,
jobId,
status,
notes: notes || null,
},
});
return NextResponse.json({ success: true, interaction });
} catch (error: any) {
return NextResponse.json({ error: error.message || "Failed to update interaction" }, { status: 500 });
}
}