61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
"use server";
|
|
|
|
import { db } from "@/db";
|
|
import { forms } from "@/db/schema";
|
|
import { eq } from "drizzle-orm";
|
|
import { revalidatePath } from "next/cache";
|
|
import { redirect } from "next/navigation";
|
|
import { requireAdmin } from "@/lib/auth";
|
|
import { sanitizeHtml } from "@/lib/sanitize-html";
|
|
|
|
export async function createForm(formData: FormData) {
|
|
await requireAdmin();
|
|
|
|
const title = formData.get("title") as string;
|
|
const description = formData.get("description") as string | null;
|
|
|
|
const [form] = await db
|
|
.insert(forms)
|
|
.values({ title, description: description ? sanitizeHtml(description) : null })
|
|
.returning();
|
|
|
|
revalidatePath("/admin/form");
|
|
redirect(`/admin/form/${form.id}`);
|
|
}
|
|
|
|
export async function createDefaultForm() {
|
|
await requireAdmin();
|
|
|
|
const [form] = await db
|
|
.insert(forms)
|
|
.values({ title: "New Form", description: "" })
|
|
.returning();
|
|
|
|
revalidatePath("/admin/form");
|
|
redirect(`/admin/form/${form.id}`);
|
|
}
|
|
|
|
export async function updateForm(id: string, data: { title?: string; description?: string | null; isOpen?: boolean; discordWebhookUrl?: string | null; discordWebhookTemplate?: string | null; discordWebhookUpdateTemplate?: string | null; allowedRoles?: string[]; deniedRoles?: string[] }) {
|
|
await requireAdmin();
|
|
|
|
const nextData = {
|
|
...data,
|
|
description: data.description === undefined
|
|
? undefined
|
|
: data.description
|
|
? sanitizeHtml(data.description)
|
|
: data.description,
|
|
};
|
|
|
|
await db.update(forms).set(nextData).where(eq(forms.id, id));
|
|
revalidatePath("/form");
|
|
revalidatePath("/admin/form");
|
|
}
|
|
|
|
export async function deleteForm(id: string) {
|
|
await requireAdmin();
|
|
|
|
await db.delete(forms).where(eq(forms.id, id));
|
|
revalidatePath("/admin/form");
|
|
}
|