+52
-8
@@ -613,6 +613,8 @@ export async function internallyApproveShot(shotId: string) {
|
||||
/**
|
||||
* Producer/Supervisor/Admin: share an internally-approved shot with the client.
|
||||
* Sets sharedWithClient = true → status becomes CLIENT_REVIEW.
|
||||
* Also marks the latest version of every task on this shot as isClientVisible = true
|
||||
* so the client portal can display them.
|
||||
*/
|
||||
export async function shareWithClient(shotId: string) {
|
||||
const session = await auth();
|
||||
@@ -623,16 +625,43 @@ export async function shareWithClient(shotId: string) {
|
||||
|
||||
const shot = await db.shot.findUnique({
|
||||
where: { id: shotId },
|
||||
select: { projectId: true, shotApprovalStatus: true },
|
||||
select: {
|
||||
projectId: true,
|
||||
shotApprovalStatus: true,
|
||||
tasks: { select: { id: true } },
|
||||
},
|
||||
});
|
||||
if (!shot) throw new Error("Shot not found");
|
||||
if (shot.shotApprovalStatus !== "INTERNALLY_APPROVED") {
|
||||
throw new Error("Shot must be internally approved before sharing with client");
|
||||
}
|
||||
|
||||
await db.shot.update({
|
||||
where: { id: shotId },
|
||||
data: { sharedWithClient: true, status: "CLIENT_REVIEW" },
|
||||
const now = new Date();
|
||||
|
||||
await db.$transaction(async (tx) => {
|
||||
// Mark shot as shared
|
||||
await tx.shot.update({
|
||||
where: { id: shotId },
|
||||
data: { sharedWithClient: true, status: "CLIENT_REVIEW" },
|
||||
});
|
||||
|
||||
// For each task, make the latest version client-visible
|
||||
for (const task of shot.tasks) {
|
||||
const latest = await tx.version.findFirst({
|
||||
where: { taskId: task.id, isLatest: true },
|
||||
select: { id: true, isClientVisible: true },
|
||||
});
|
||||
if (latest && !latest.isClientVisible) {
|
||||
await tx.version.update({
|
||||
where: { id: latest.id },
|
||||
data: {
|
||||
isClientVisible: true,
|
||||
sharedAt: now,
|
||||
sharedById: session.user.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
revalidatePath(`/projects/${shot.projectId}`);
|
||||
@@ -644,6 +673,7 @@ export async function shareWithClient(shotId: string) {
|
||||
/**
|
||||
* Producer/Supervisor/Admin: remove a shot from client review.
|
||||
* Sets sharedWithClient = false → status becomes READY_FOR_CLIENT.
|
||||
* Also hides all task versions that were made visible by shareWithClient.
|
||||
*/
|
||||
export async function unshareFromClient(shotId: string) {
|
||||
const session = await auth();
|
||||
@@ -654,13 +684,27 @@ export async function unshareFromClient(shotId: string) {
|
||||
|
||||
const shot = await db.shot.findUnique({
|
||||
where: { id: shotId },
|
||||
select: { projectId: true },
|
||||
select: {
|
||||
projectId: true,
|
||||
tasks: { select: { id: true } },
|
||||
},
|
||||
});
|
||||
if (!shot) throw new Error("Shot not found");
|
||||
|
||||
await db.shot.update({
|
||||
where: { id: shotId },
|
||||
data: { sharedWithClient: false, status: "READY_FOR_CLIENT" },
|
||||
await db.$transaction(async (tx) => {
|
||||
await tx.shot.update({
|
||||
where: { id: shotId },
|
||||
data: { sharedWithClient: false, status: "READY_FOR_CLIENT" },
|
||||
});
|
||||
|
||||
// Hide all client-visible versions on this shot's tasks
|
||||
const taskIds = shot.tasks.map((t) => t.id);
|
||||
if (taskIds.length > 0) {
|
||||
await tx.version.updateMany({
|
||||
where: { taskId: { in: taskIds }, isClientVisible: true },
|
||||
data: { isClientVisible: false },
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
revalidatePath(`/projects/${shot.projectId}`);
|
||||
|
||||
Reference in New Issue
Block a user