117 lines
3.5 KiB
TypeScript
117 lines
3.5 KiB
TypeScript
"use server";
|
|
|
|
import { Prisma } from "@prisma/client";
|
|
import { revalidatePath } from "next/cache";
|
|
import { redirect } from "next/navigation";
|
|
import { auth } from "@/lib/auth";
|
|
import { prisma } from "@/lib/db";
|
|
import { melodyIdSchema, melodySchema } from "@/lib/validations/melody";
|
|
|
|
export type MelodyActionResult = {
|
|
success?: true;
|
|
error?: string;
|
|
};
|
|
|
|
async function isAdminAuthenticated() {
|
|
const session = await auth();
|
|
return Boolean(session?.user);
|
|
}
|
|
|
|
function getActionError(error: unknown) {
|
|
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002") {
|
|
return "A melody with this slug already exists.";
|
|
}
|
|
|
|
return "Unable to save the melody right now.";
|
|
}
|
|
|
|
async function hasMelodyCategory(categoryId: string) {
|
|
const category = await prisma.category.findFirst({
|
|
where: { id: categoryId, kind: "MELODY" },
|
|
select: { id: true },
|
|
});
|
|
return Boolean(category);
|
|
}
|
|
|
|
function normalizeMelodyInput(input: unknown) {
|
|
const parsed = melodySchema.safeParse(input);
|
|
if (!parsed.success) {
|
|
return { error: parsed.error.issues[0]?.message ?? "Invalid melody data." } as const;
|
|
}
|
|
|
|
return {
|
|
data: {
|
|
...parsed.data,
|
|
descAr: parsed.data.descAr || null,
|
|
descEn: parsed.data.descEn || null,
|
|
coverImage: parsed.data.coverImage || null,
|
|
},
|
|
} as const;
|
|
}
|
|
|
|
export async function createMelody(input: unknown): Promise<MelodyActionResult> {
|
|
if (!(await isAdminAuthenticated())) {
|
|
return { error: "Your session has expired. Please sign in again." };
|
|
}
|
|
|
|
const normalized = normalizeMelodyInput(input);
|
|
if ("error" in normalized) return normalized;
|
|
if (!(await hasMelodyCategory(normalized.data.categoryId))) {
|
|
return { error: "Choose a valid melody category." };
|
|
}
|
|
|
|
try {
|
|
await prisma.melody.create({ data: normalized.data });
|
|
revalidatePath("/admin/melodies");
|
|
revalidatePath("/en/melodies");
|
|
return { success: true };
|
|
} catch (error) {
|
|
return { error: getActionError(error) };
|
|
}
|
|
}
|
|
|
|
export async function updateMelody(melodyId: string, input: unknown): Promise<MelodyActionResult> {
|
|
if (!(await isAdminAuthenticated())) {
|
|
return { error: "Your session has expired. Please sign in again." };
|
|
}
|
|
|
|
const validId = melodyIdSchema.safeParse(melodyId);
|
|
if (!validId.success) return { error: "Invalid melody id." };
|
|
|
|
const normalized = normalizeMelodyInput(input);
|
|
if ("error" in normalized) return normalized;
|
|
if (!(await hasMelodyCategory(normalized.data.categoryId))) {
|
|
return { error: "Choose a valid melody category." };
|
|
}
|
|
|
|
try {
|
|
await prisma.melody.update({ where: { id: validId.data }, data: normalized.data });
|
|
revalidatePath("/admin/melodies");
|
|
revalidatePath(`/admin/melodies/${validId.data}/edit`);
|
|
revalidatePath("/en/melodies");
|
|
return { success: true };
|
|
} catch (error) {
|
|
return { error: getActionError(error) };
|
|
}
|
|
}
|
|
|
|
export async function deleteMelody(formData: FormData) {
|
|
if (!(await isAdminAuthenticated())) redirect("/admin/login");
|
|
|
|
const validId = melodyIdSchema.safeParse(formData.get("melodyId"));
|
|
if (!validId.success) redirect("/admin/melodies?error=invalid-id");
|
|
|
|
try {
|
|
await prisma.melody.delete({ where: { id: validId.data } });
|
|
} catch (error) {
|
|
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2025") {
|
|
redirect("/admin/melodies?error=not-found");
|
|
}
|
|
redirect("/admin/melodies?error=delete-failed");
|
|
}
|
|
|
|
revalidatePath("/admin/melodies");
|
|
revalidatePath("/en/melodies");
|
|
redirect("/admin/melodies");
|
|
}
|