This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
"use server";
|
||||
|
||||
import { auth } from "@/auth";
|
||||
import { db } from "@/lib/db";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { z } from "zod";
|
||||
|
||||
const setEpisodeDueDateSchema = z.object({
|
||||
projectId: z.string().cuid(),
|
||||
episode: z.string().min(1).max(50),
|
||||
dueDate: z.string().nullable(),
|
||||
});
|
||||
|
||||
export async function setEpisodeDueDate(data: z.infer<typeof setEpisodeDueDateSchema>) {
|
||||
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 parsed = setEpisodeDueDateSchema.parse(data);
|
||||
|
||||
if (!parsed.dueDate) {
|
||||
// Remove the due date
|
||||
await db.episodeDueDate.deleteMany({
|
||||
where: { projectId: parsed.projectId, episode: parsed.episode },
|
||||
});
|
||||
} else {
|
||||
await db.episodeDueDate.upsert({
|
||||
where: {
|
||||
projectId_episode: { projectId: parsed.projectId, episode: parsed.episode },
|
||||
},
|
||||
create: {
|
||||
projectId: parsed.projectId,
|
||||
episode: parsed.episode,
|
||||
dueDate: new Date(parsed.dueDate),
|
||||
},
|
||||
update: {
|
||||
dueDate: new Date(parsed.dueDate),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
revalidatePath(`/projects/${parsed.projectId}`);
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export async function getEpisodeDueDates(projectId: string) {
|
||||
const rows = await db.episodeDueDate.findMany({
|
||||
where: { projectId },
|
||||
orderBy: { episode: "asc" },
|
||||
});
|
||||
return rows;
|
||||
}
|
||||
Reference in New Issue
Block a user