Initial commit
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { db } from "@/lib/db";
|
||||
import { ApprovalStatus } from "@prisma/client";
|
||||
import { recalcShotStatus } from "@/lib/shot-status";
|
||||
|
||||
async function getOrCreateClientUser(email: string, label?: string | null) {
|
||||
const existing = await db.user.findUnique({ where: { email } });
|
||||
if (existing) return existing;
|
||||
return db.user.create({
|
||||
data: {
|
||||
email,
|
||||
name: label ?? email.split("@")[0],
|
||||
role: "CLIENT",
|
||||
isActive: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function validateToken(token: string) {
|
||||
const session = await db.reviewSession.findUnique({ where: { token } });
|
||||
if (!session || !session.isActive) return null;
|
||||
if (session.expiresAt && session.expiresAt < new Date()) return null;
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ token: string }> }
|
||||
) {
|
||||
const { token } = await params;
|
||||
const session = await validateToken(token);
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "Invalid or expired review link" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await req.json();
|
||||
const { versionId, status, notes } = body;
|
||||
|
||||
const validStatuses: ApprovalStatus[] = ["APPROVED", "REJECTED", "NEEDS_CHANGES"];
|
||||
if (!versionId || !validStatuses.includes(status)) {
|
||||
return NextResponse.json({ error: "versionId and valid status required" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Ensure the version belongs to this project via its task
|
||||
const version = await db.version.findUnique({
|
||||
where: { id: versionId },
|
||||
include: {
|
||||
task: {
|
||||
include: { shot: true, project: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const projectId = version?.task?.projectId;
|
||||
if (!version || projectId !== session.projectId) {
|
||||
return NextResponse.json({ error: "Version not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const email = session.email ?? `client+${token.slice(0, 8)}@review.external`;
|
||||
const user = await getOrCreateClientUser(email, session.label);
|
||||
|
||||
// Record approval
|
||||
await db.approval.create({
|
||||
data: { versionId, userId: user.id, status, notes },
|
||||
});
|
||||
|
||||
// Update version approval status
|
||||
await db.version.update({
|
||||
where: { id: versionId },
|
||||
data: { approvalStatus: status },
|
||||
});
|
||||
|
||||
// Update task status based on approval decision
|
||||
if (version.task) {
|
||||
if (status === "APPROVED") {
|
||||
await db.task.update({ where: { id: version.task.id }, data: { status: "DONE" } });
|
||||
} else {
|
||||
await db.task.update({ where: { id: version.task.id }, data: { status: "CHANGES" } });
|
||||
}
|
||||
|
||||
// Recalculate derived shot status
|
||||
if (version.task.shot) {
|
||||
await recalcShotStatus(version.task.shot.id).catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
// Slack notification
|
||||
if (version.task?.project?.slackWebhook) {
|
||||
const { slackNotifyApproval } = await import("@/lib/slack");
|
||||
const appUrl = process.env.NEXT_PUBLIC_APP_URL ?? "";
|
||||
const contextCode = version.task.shot?.shotCode ?? version.task.title;
|
||||
await slackNotifyApproval(version.task.project.slackWebhook, {
|
||||
shotCode: contextCode,
|
||||
versionLabel: `v${String(version.versionNumber).padStart(3, "0")}`,
|
||||
reviewerName: user.name ?? "Client",
|
||||
status,
|
||||
projectName: version.task.project.name,
|
||||
reviewUrl: `${appUrl}/client/${token}/review/${versionId}`,
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user