feat: full site build — Project/Melody schema (Option A), admin CRUD, public sections, uploads, email+SMTP, internal analytics, legal pages, docs
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
"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 { projectIdSchema, projectSchema } from "@/lib/validations/project";
|
||||
|
||||
export type ProjectActionResult = {
|
||||
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 project with this slug already exists.";
|
||||
}
|
||||
|
||||
return "Unable to save the project right now.";
|
||||
}
|
||||
|
||||
async function validateProjectCategory(categoryId: string | undefined) {
|
||||
if (!categoryId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const category = await prisma.category.findFirst({
|
||||
where: { id: categoryId, kind: "PROJECT" },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
return category?.id ?? false;
|
||||
}
|
||||
|
||||
function normalizeProjectInput(input: unknown) {
|
||||
const parsed = projectSchema.safeParse(input);
|
||||
if (!parsed.success) {
|
||||
return { error: parsed.error.issues[0]?.message ?? "Invalid project data." } as const;
|
||||
}
|
||||
|
||||
return {
|
||||
data: {
|
||||
...parsed.data,
|
||||
categoryId: parsed.data.categoryId || null,
|
||||
coverImage: parsed.data.coverImage || null,
|
||||
summaryAr: parsed.data.summaryAr || null,
|
||||
summaryEn: parsed.data.summaryEn || null,
|
||||
descAr: parsed.data.descAr || null,
|
||||
descEn: parsed.data.descEn || null,
|
||||
externalUrl: parsed.data.externalUrl || null,
|
||||
repoUrl: parsed.data.repoUrl || null,
|
||||
platform: parsed.data.platform || null,
|
||||
appStoreUrl: parsed.data.appStoreUrl || null,
|
||||
testflightUrl: parsed.data.testflightUrl || null,
|
||||
appVersion: parsed.data.appVersion || null,
|
||||
supportUrl: parsed.data.supportUrl || null,
|
||||
appPrivacyUrl: parsed.data.appPrivacyUrl || null,
|
||||
},
|
||||
} as const;
|
||||
}
|
||||
|
||||
export async function createProject(input: unknown): Promise<ProjectActionResult> {
|
||||
if (!(await isAdminAuthenticated())) {
|
||||
return { error: "Your session has expired. Please sign in again." };
|
||||
}
|
||||
|
||||
const normalized = normalizeProjectInput(input);
|
||||
if ("error" in normalized) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
if ((await validateProjectCategory(normalized.data.categoryId ?? undefined)) === false) {
|
||||
return { error: "Choose a valid project category." };
|
||||
}
|
||||
|
||||
try {
|
||||
await prisma.project.create({ data: normalized.data });
|
||||
revalidatePath("/admin/projects");
|
||||
revalidatePath("/en/work");
|
||||
revalidatePath("/en/apps");
|
||||
revalidatePath("/en/websites");
|
||||
revalidatePath("/en/designs");
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return { error: getActionError(error) };
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateProject(projectId: string, input: unknown): Promise<ProjectActionResult> {
|
||||
if (!(await isAdminAuthenticated())) {
|
||||
return { error: "Your session has expired. Please sign in again." };
|
||||
}
|
||||
|
||||
const validId = projectIdSchema.safeParse(projectId);
|
||||
if (!validId.success) {
|
||||
return { error: "Invalid project id." };
|
||||
}
|
||||
|
||||
const normalized = normalizeProjectInput(input);
|
||||
if ("error" in normalized) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
if ((await validateProjectCategory(normalized.data.categoryId ?? undefined)) === false) {
|
||||
return { error: "Choose a valid project category." };
|
||||
}
|
||||
|
||||
try {
|
||||
await prisma.project.update({ where: { id: validId.data }, data: normalized.data });
|
||||
revalidatePath("/admin/projects");
|
||||
revalidatePath(`/admin/projects/${validId.data}/edit`);
|
||||
revalidatePath("/en/work");
|
||||
revalidatePath("/en/apps");
|
||||
revalidatePath("/en/websites");
|
||||
revalidatePath("/en/designs");
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return { error: getActionError(error) };
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteProject(formData: FormData) {
|
||||
if (!(await isAdminAuthenticated())) {
|
||||
redirect("/admin/login");
|
||||
}
|
||||
|
||||
const validId = projectIdSchema.safeParse(formData.get("projectId"));
|
||||
if (!validId.success) {
|
||||
redirect("/admin/projects?error=invalid-id");
|
||||
}
|
||||
|
||||
try {
|
||||
await prisma.project.delete({ where: { id: validId.data } });
|
||||
} catch (error) {
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2025") {
|
||||
redirect("/admin/projects?error=not-found");
|
||||
}
|
||||
|
||||
redirect("/admin/projects?error=delete-failed");
|
||||
}
|
||||
|
||||
revalidatePath("/admin/projects");
|
||||
redirect("/admin/projects");
|
||||
}
|
||||
Reference in New Issue
Block a user