94792279c5
Requesting changes on a shot and then re-rendering it through the render queue left every layer of the app still showing the old verdict. Three independent causes, all on the pipeline path. finalizePreview inherited the superseded version's approvalStatus unconditionally. The carry-over exists so a re-export of a signed-off shot does not reappear as awaiting review, but it applied equally to NEEDS_CHANGES and REJECTED, so the render queued to address a note arrived already wearing that note and nothing registered that the work had been redone. carryApproval now carries only APPROVED. The pipeline never moved the task. One left on CHANGES by a review or a QC fail stayed there through every re-render, with no way out but a manual kanban drag. A genuinely new version now moves it to INTERNAL_REVIEW, as actions/versions.ts and batch-upload already did. A replaced version is excluded: regeneratePreview re-delivers the same version after a slate fix and is not a new submission. The pipeline never called recalcShotStatus at all, so shot boards went stale the moment a render changed a task. finalizePreview and the QC fail path both recalculate now; the latter already documented the REVISIONS derivation it was relying on and never triggering. Shot.status is derived from tasks and approval flags, but six call sites wrote it directly with the status they expected: internallyApproveShot, shareWithClient, unshareFromClient, clientApproveShot, unapproveShot and the client approve route. Rule 1 of the derivation is that any CHANGES task outranks an approval, so a shot could be pushed past open rework and then snap back to REVISIONS on the next unrelated recalc, which is close to unreportable as a bug. They now set the approval inputs and let recalcShotStatus derive. unshareFromClient additionally hardcoded READY_FOR_CLIENT even for a client-approved shot. updateShotStatus keeps its direct write as the deliberate manual override, now documented as transient for the same reason. markShotForRevisions replaces the duplicated change-request logic. Both copies marked all non-DONE tasks CHANGES, which matches nothing on the shot this is most often called for: one out for client review precisely because all its tasks are finished. The request then derived to INTERNAL_REVIEW and had no visible effect anywhere. When nothing is picked up it now reopens the task behind the shot's latest version, which is the deliverable the reviewer was looking at. A client's shot-level change request also marks the versions and writes Approval rows now. The dashboard counters, the version badges and the portal's own filters all read Version.approvalStatus, so a shot-level verdict was invisible everywhere those look. clearStaleShotApproval covers the reverse case: uploading a new version onto an approved shot moved the task to INTERNAL_REVIEW and changed nothing visible, because the derivation short-circuits on shotApprovalStatus. The shot kept reading COMPLETE while carrying work nobody had looked at. This is the one user-visible behaviour change here, so watch the boards on a live show: an approved shot now drops back to INTERNAL_REVIEW on a new upload. sharedWithClient is deliberately left alone, since revoking client access stays a separate act. submitApproval and unapproveVersion are transactional now. The verdict, the version, the task and the derived shot status are one decision, and a failure partway through left the three layers disagreeing with no way to tell which one was right. KanbanCard compared approvalStatus against "CHANGES_REQUESTED", which is not a member of the enum, so the changes-requested icon never rendered at all. Thirteen tests cover carryApproval, markShotForRevisions including the all-DONE regression, and clearStaleShotApproval. What is not covered is the pipeline transaction itself, which needs a database: queue a render against a shot in REVISIONS and confirm the task and shot both move before trusting this on a show. Version.reviewStatus is still written nowhere while being published through ext/shots/lookup and EXT_API_REFERENCE.md as a permanent "PENDING". Left alone deliberately: removing a field from that contract could break the connector. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
262 lines
8.1 KiB
TypeScript
262 lines
8.1 KiB
TypeScript
"use server";
|
|
|
|
import { auth } from "@/auth";
|
|
import { db } from "@/lib/db";
|
|
import { revalidatePath } from "next/cache";
|
|
import { z } from "zod";
|
|
import { recalcShotStatus, clearStaleShotApproval } from "@/lib/shot-status";
|
|
import { notifyNewVersionUploaded } from "@/lib/notifications";
|
|
import { slackNotifyVersionUploaded } from "@/lib/slack";
|
|
|
|
const createVersionSchema = z.object({
|
|
taskId: z.string().cuid(),
|
|
fileUrl: z.string().min(1),
|
|
fileName: z.string(),
|
|
fileSize: z.number().optional(),
|
|
mimeType: z.string().optional(),
|
|
thumbnailUrl: z.string().min(1).optional().or(z.literal("")),
|
|
fps: z.number().default(24),
|
|
duration: z.number().optional(),
|
|
frameCount: z.number().int().optional(),
|
|
width: z.number().int().optional(),
|
|
height: z.number().int().optional(),
|
|
notes: z.string().optional(),
|
|
});
|
|
|
|
export async function createVersion(data: z.infer<typeof createVersionSchema>) {
|
|
const session = await auth();
|
|
if (!session?.user) throw new Error("Unauthorized");
|
|
|
|
const parsed = createVersionSchema.parse(data);
|
|
|
|
// All versions must belong to a task
|
|
const task = await db.task.findUnique({
|
|
where: { id: parsed.taskId },
|
|
include: { project: true, shot: { select: { id: true, shotCode: true } } },
|
|
});
|
|
if (!task) throw new Error("Task not found");
|
|
|
|
const projectId = task.projectId;
|
|
const slackWebhook = task.project.slackWebhook ?? undefined;
|
|
const projectName = task.project.name;
|
|
const shotCode = task.shot?.shotCode;
|
|
|
|
// Mark all existing versions for this task as not latest
|
|
await db.version.updateMany({ where: { taskId: parsed.taskId }, data: { isLatest: false } });
|
|
|
|
// Move task to INTERNAL_REVIEW on upload
|
|
await db.task.update({
|
|
where: { id: parsed.taskId },
|
|
data: { status: "INTERNAL_REVIEW" },
|
|
});
|
|
|
|
// Recalculate shot status — clearing any stale sign-off first, so a new
|
|
// version does not land under a shot still reading COMPLETE.
|
|
if (task.shot) {
|
|
const shotId = task.shot.id;
|
|
await db
|
|
.$transaction(async (tx) => {
|
|
await clearStaleShotApproval(tx, shotId);
|
|
await recalcShotStatus(shotId, tx);
|
|
})
|
|
.catch(() => {});
|
|
}
|
|
|
|
// Get the next version number for this task
|
|
const lastVersion = await db.version.findFirst({
|
|
where: { taskId: parsed.taskId },
|
|
orderBy: { versionNumber: "desc" },
|
|
});
|
|
const versionNumber = (lastVersion?.versionNumber ?? 0) + 1;
|
|
|
|
// Create the new version
|
|
const version = await db.version.create({
|
|
data: {
|
|
versionNumber,
|
|
taskId: parsed.taskId,
|
|
artistId: session.user.id,
|
|
fileUrl: parsed.fileUrl,
|
|
fileName: parsed.fileName,
|
|
fileSize: parsed.fileSize ? BigInt(parsed.fileSize) : undefined,
|
|
mimeType: parsed.mimeType,
|
|
thumbnailUrl: parsed.thumbnailUrl || undefined,
|
|
fps: parsed.fps,
|
|
duration: parsed.duration,
|
|
frameCount: parsed.frameCount,
|
|
width: parsed.width,
|
|
height: parsed.height,
|
|
notes: parsed.notes,
|
|
isLatest: true,
|
|
},
|
|
});
|
|
|
|
// Send notifications
|
|
const user = await db.user.findUnique({ where: { id: session.user.id } });
|
|
const versionLabelStr = `v${String(versionNumber).padStart(3, "0")}`;
|
|
|
|
if (shotCode) {
|
|
await notifyNewVersionUploaded({
|
|
shotCode,
|
|
versionNumber,
|
|
projectId,
|
|
versionId: version.id,
|
|
artistName: user?.name ?? session.user.email ?? "Artist",
|
|
});
|
|
}
|
|
|
|
// Slack notification
|
|
if (slackWebhook && shotCode) {
|
|
const appUrl = process.env.NEXT_PUBLIC_APP_URL ?? "http://localhost:3000";
|
|
await slackNotifyVersionUploaded(slackWebhook, {
|
|
shotCode,
|
|
versionLabel: versionLabelStr,
|
|
artistName: user?.name ?? "Artist",
|
|
projectName,
|
|
reviewUrl: `${appUrl}/review/${version.id}`,
|
|
});
|
|
}
|
|
|
|
revalidatePath(`/projects/${projectId}`);
|
|
if (task.shot) revalidatePath(`/projects/${projectId}/shots/${task.shot.id}`);
|
|
revalidatePath(`/tasks/${parsed.taskId}`);
|
|
|
|
return { success: true, version, versionNumber };
|
|
}
|
|
|
|
/**
|
|
* Share a version with the client — marks it as client-visible,
|
|
* records who shared it, and moves the associated task to CLIENT_REVIEW.
|
|
*/
|
|
export async function shareVersionWithClient(versionId: string) {
|
|
const session = await auth();
|
|
if (!session?.user) throw new Error("Unauthorized");
|
|
if (!["ADMIN", "PRODUCER", "SUPERVISOR"].includes(session.user.role)) {
|
|
throw new Error("Insufficient permissions");
|
|
}
|
|
|
|
const version = await db.version.findUnique({
|
|
where: { id: versionId },
|
|
select: { id: true, taskId: true, isClientVisible: true },
|
|
});
|
|
if (!version) throw new Error("Version not found");
|
|
|
|
await db.version.update({
|
|
where: { id: versionId },
|
|
data: {
|
|
isClientVisible: true,
|
|
sharedAt: new Date(),
|
|
sharedById: session.user.id,
|
|
},
|
|
});
|
|
|
|
// Move task to CLIENT_REVIEW if it is in INTERNAL_REVIEW; recalc shot status
|
|
if (version.taskId) {
|
|
const task = await db.task.findUnique({
|
|
where: { id: version.taskId },
|
|
select: { status: true, projectId: true, shotId: true },
|
|
});
|
|
if (task && task.status === "INTERNAL_REVIEW") {
|
|
await db.task.update({
|
|
where: { id: version.taskId },
|
|
data: { status: "CLIENT_REVIEW" },
|
|
});
|
|
if (task.shotId) {
|
|
await recalcShotStatus(task.shotId).catch(() => {});
|
|
}
|
|
revalidatePath(`/tasks/${version.taskId}`);
|
|
revalidatePath(`/projects/${task.projectId}`);
|
|
}
|
|
}
|
|
|
|
revalidatePath(`/review/${versionId}`);
|
|
return { success: true };
|
|
}
|
|
|
|
export async function deleteVersion(versionId: string) {
|
|
const session = await auth();
|
|
if (!session?.user) throw new Error("Unauthorized");
|
|
if (!["ADMIN", "PRODUCER", "SUPERVISOR"].includes(session.user.role)) {
|
|
throw new Error("Insufficient permissions");
|
|
}
|
|
|
|
const version = await db.version.findUnique({
|
|
where: { id: versionId },
|
|
select: {
|
|
id: true,
|
|
isLatest: true,
|
|
taskId: true,
|
|
task: { select: { projectId: true, shotId: true } },
|
|
},
|
|
});
|
|
if (!version) throw new Error("Version not found");
|
|
|
|
// If this was the latest version, promote the next most recent one
|
|
if (version.isLatest && version.taskId) {
|
|
const next = await db.version.findFirst({
|
|
where: { taskId: version.taskId, id: { not: versionId } },
|
|
orderBy: { versionNumber: "desc" },
|
|
select: { id: true },
|
|
});
|
|
if (next) {
|
|
await db.version.update({ where: { id: next.id }, data: { isLatest: true } });
|
|
}
|
|
}
|
|
|
|
await db.version.delete({ where: { id: versionId } });
|
|
|
|
const projectId = version.task?.projectId;
|
|
const shotId = version.task?.shotId;
|
|
if (shotId) await recalcShotStatus(shotId).catch(() => {});
|
|
if (projectId) revalidatePath(`/projects/${projectId}`);
|
|
if (version.taskId) revalidatePath(`/tasks/${version.taskId}`);
|
|
if (shotId && projectId) revalidatePath(`/projects/${projectId}/shots/${shotId}`);
|
|
|
|
return { success: true };
|
|
}
|
|
|
|
export async function getVersionById(versionId: string) {
|
|
const session = await auth();
|
|
if (!session?.user) throw new Error("Unauthorized");
|
|
|
|
return db.version.findUnique({
|
|
where: { id: versionId },
|
|
include: {
|
|
task: {
|
|
include: {
|
|
project: { include: { client: true } },
|
|
shot: true,
|
|
asset: true,
|
|
versions: {
|
|
orderBy: { versionNumber: "desc" },
|
|
select: { id: true, versionNumber: true, approvalStatus: true, createdAt: true },
|
|
},
|
|
},
|
|
},
|
|
artist: { select: { id: true, name: true, email: true, image: true } },
|
|
approvals: {
|
|
include: { user: { select: { id: true, name: true, role: true } } },
|
|
orderBy: { createdAt: "desc" },
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getVersionComments(versionId: string) {
|
|
return db.comment.findMany({
|
|
where: { versionId },
|
|
include: {
|
|
author: { select: { id: true, name: true, email: true, image: true, role: true } },
|
|
replies: {
|
|
include: {
|
|
author: { select: { id: true, name: true, email: true, image: true, role: true } },
|
|
},
|
|
orderBy: { createdAt: "asc" },
|
|
},
|
|
annotations: {
|
|
select: { id: true, frameNumber: true, drawingData: true, color: true, isVisible: true, authorId: true },
|
|
},
|
|
},
|
|
orderBy: { frameNumber: "asc" },
|
|
});
|
|
}
|