Initial commit
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
"use server";
|
||||
|
||||
import { auth } from "@/auth";
|
||||
import { db } from "@/lib/db";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { z } from "zod";
|
||||
|
||||
function requireScheduleAccess(role: string) {
|
||||
if (!["ADMIN", "PRODUCER", "SUPERVISOR"].includes(role)) {
|
||||
throw new Error("Insufficient permissions");
|
||||
}
|
||||
}
|
||||
|
||||
const scheduleTaskSchema = z.object({
|
||||
taskId: z.string().cuid(),
|
||||
scheduledStartDate: z.string().nullable(),
|
||||
scheduledEndDate: z.string().nullable(),
|
||||
assignedArtistId: z.string().cuid().nullable().optional(),
|
||||
scheduleNotes: z.string().nullable().optional(),
|
||||
estimatedHours: z.number().positive().optional(),
|
||||
});
|
||||
|
||||
export async function scheduleTask(data: z.infer<typeof scheduleTaskSchema>) {
|
||||
const session = await auth();
|
||||
if (!session?.user) throw new Error("Unauthorized");
|
||||
requireScheduleAccess(session.user.role);
|
||||
|
||||
const parsed = scheduleTaskSchema.parse(data);
|
||||
|
||||
await db.task.update({
|
||||
where: { id: parsed.taskId },
|
||||
data: {
|
||||
scheduledStartDate: parsed.scheduledStartDate
|
||||
? new Date(parsed.scheduledStartDate)
|
||||
: null,
|
||||
scheduledEndDate: parsed.scheduledEndDate
|
||||
? new Date(parsed.scheduledEndDate)
|
||||
: null,
|
||||
...(parsed.assignedArtistId !== undefined && {
|
||||
assignedArtistId: parsed.assignedArtistId,
|
||||
}),
|
||||
...(parsed.scheduleNotes !== undefined && {
|
||||
scheduleNotes: parsed.scheduleNotes,
|
||||
}),
|
||||
...(parsed.estimatedHours !== undefined && {
|
||||
estimatedHours: parsed.estimatedHours,
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
revalidatePath("/schedule");
|
||||
revalidatePath("/dashboard");
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export async function unscheduleTask(taskId: string) {
|
||||
const session = await auth();
|
||||
if (!session?.user) throw new Error("Unauthorized");
|
||||
requireScheduleAccess(session.user.role);
|
||||
|
||||
await db.task.update({
|
||||
where: { id: taskId },
|
||||
data: {
|
||||
scheduledStartDate: null,
|
||||
scheduledEndDate: null,
|
||||
},
|
||||
});
|
||||
|
||||
revalidatePath("/schedule");
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export async function bulkSchedule(
|
||||
tasks: {
|
||||
taskId: string;
|
||||
scheduledStartDate: string;
|
||||
scheduledEndDate: string;
|
||||
assignedArtistId?: string;
|
||||
}[]
|
||||
) {
|
||||
const session = await auth();
|
||||
if (!session?.user) throw new Error("Unauthorized");
|
||||
requireScheduleAccess(session.user.role);
|
||||
|
||||
await db.$transaction(
|
||||
tasks.map((t) =>
|
||||
db.task.update({
|
||||
where: { id: t.taskId },
|
||||
data: {
|
||||
scheduledStartDate: new Date(t.scheduledStartDate),
|
||||
scheduledEndDate: new Date(t.scheduledEndDate),
|
||||
...(t.assignedArtistId ? { assignedArtistId: t.assignedArtistId } : {}),
|
||||
},
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
revalidatePath("/schedule");
|
||||
return { success: true };
|
||||
}
|
||||
Reference in New Issue
Block a user