@@ -165,6 +165,48 @@ export async function shareVersionWithClient(versionId: string) {
|
||||
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");
|
||||
|
||||
Reference in New Issue
Block a user