|
| 1 | +"use server"; |
| 2 | + |
| 3 | +import { revalidatePath } from "next/cache"; |
| 4 | +import { redirect } from "next/navigation"; |
| 5 | +import { eq } from "drizzle-orm"; |
| 6 | +import { getOperatorSession } from "@/lib/operator"; |
| 7 | +import { |
| 8 | + createProposal, |
| 9 | + updateProposal, |
| 10 | + deleteProposal, |
| 11 | + findProposalBySlug, |
| 12 | +} from "@/lib/proposals"; |
| 13 | +import type { ProposalChoice } from "@/db/schema/proposals"; |
| 14 | +import { getDb } from "@/db/client"; |
| 15 | +import { users } from "@/db/external/auth"; |
| 16 | + |
| 17 | +interface ProposalFormValues { |
| 18 | + slug: string; |
| 19 | + title: string; |
| 20 | + bodyMd: string; |
| 21 | + voteType: "yes_no" | "single_choice"; |
| 22 | + scope: "framework" | "product"; |
| 23 | + productSlug: string | null; |
| 24 | + status: "draft" | "open" | "closed"; |
| 25 | + opensAt: Date | null; |
| 26 | + closesAt: Date | null; |
| 27 | + choices: ProposalChoice[] | null; |
| 28 | +} |
| 29 | + |
| 30 | +function parseValues(form: FormData): ProposalFormValues { |
| 31 | + const strOrNull = (v: FormDataEntryValue | null) => { |
| 32 | + if (v === null) return null; |
| 33 | + const s = String(v).trim(); |
| 34 | + return s.length === 0 ? null : s; |
| 35 | + }; |
| 36 | + const dateOrNull = (v: FormDataEntryValue | null) => { |
| 37 | + const s = strOrNull(v); |
| 38 | + if (!s) return null; |
| 39 | + const d = new Date(s); |
| 40 | + return Number.isNaN(d.getTime()) ? null : d; |
| 41 | + }; |
| 42 | + |
| 43 | + const voteTypeRaw = String(form.get("voteType") ?? "yes_no"); |
| 44 | + const voteType: ProposalFormValues["voteType"] = |
| 45 | + voteTypeRaw === "single_choice" ? "single_choice" : "yes_no"; |
| 46 | + |
| 47 | + const scopeRaw = String(form.get("scope") ?? "product"); |
| 48 | + const scope: ProposalFormValues["scope"] = |
| 49 | + scopeRaw === "framework" ? "framework" : "product"; |
| 50 | + |
| 51 | + const statusRaw = String(form.get("status") ?? "draft"); |
| 52 | + const status: ProposalFormValues["status"] = |
| 53 | + statusRaw === "open" || statusRaw === "closed" ? statusRaw : "draft"; |
| 54 | + |
| 55 | + let choices: ProposalChoice[] | null = null; |
| 56 | + if (voteType === "single_choice") { |
| 57 | + const raw = String(form.get("choicesJson") ?? "[]"); |
| 58 | + try { |
| 59 | + const parsed = JSON.parse(raw); |
| 60 | + if (Array.isArray(parsed)) { |
| 61 | + choices = parsed |
| 62 | + .filter( |
| 63 | + (c): c is ProposalChoice => |
| 64 | + !!c && typeof c.id === "string" && typeof c.label === "string", |
| 65 | + ) |
| 66 | + .slice(0, 20); |
| 67 | + } |
| 68 | + } catch { |
| 69 | + choices = null; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return { |
| 74 | + slug: String(form.get("slug") ?? "").trim(), |
| 75 | + title: String(form.get("title") ?? "").trim(), |
| 76 | + bodyMd: String(form.get("bodyMd") ?? ""), |
| 77 | + voteType, |
| 78 | + scope, |
| 79 | + productSlug: strOrNull(form.get("productSlug")), |
| 80 | + status, |
| 81 | + opensAt: dateOrNull(form.get("opensAt")), |
| 82 | + closesAt: dateOrNull(form.get("closesAt")), |
| 83 | + choices, |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +async function ensureUserId(email: string): Promise<string | null> { |
| 88 | + const db = getDb(); |
| 89 | + const [u] = await db |
| 90 | + .select({ id: users.id }) |
| 91 | + .from(users) |
| 92 | + .where(eq(users.email, email)) |
| 93 | + .limit(1); |
| 94 | + return u?.id ?? null; |
| 95 | +} |
| 96 | + |
| 97 | +export async function createProposalAction(form: FormData) { |
| 98 | + const session = await getOperatorSession(); |
| 99 | + if (!session?.user?.email) throw new Error("Not authorized"); |
| 100 | + |
| 101 | + const v = parseValues(form); |
| 102 | + if (!v.title || !v.slug || !v.bodyMd) { |
| 103 | + throw new Error("Title, slug, and body are required"); |
| 104 | + } |
| 105 | + if (v.voteType === "single_choice" && (!v.choices || v.choices.length < 2)) { |
| 106 | + throw new Error("Single-choice proposals need at least two choices"); |
| 107 | + } |
| 108 | + const existing = await findProposalBySlug(v.slug); |
| 109 | + if (existing) { |
| 110 | + throw new Error(`A proposal with slug '${v.slug}' already exists`); |
| 111 | + } |
| 112 | + |
| 113 | + const authorId = await ensureUserId(session.user.email); |
| 114 | + |
| 115 | + const created = await createProposal({ |
| 116 | + slug: v.slug, |
| 117 | + title: v.title, |
| 118 | + bodyMd: v.bodyMd, |
| 119 | + voteType: v.voteType, |
| 120 | + scope: v.scope, |
| 121 | + productSlug: v.productSlug, |
| 122 | + status: v.status, |
| 123 | + opensAt: v.opensAt, |
| 124 | + closesAt: v.closesAt, |
| 125 | + choices: v.choices, |
| 126 | + authorId, |
| 127 | + }); |
| 128 | + |
| 129 | + revalidatePath("/inside/proposals"); |
| 130 | + revalidatePath("/inside/admin/proposals"); |
| 131 | + redirect(`/inside/admin/proposals/${created.id}/edit`); |
| 132 | +} |
| 133 | + |
| 134 | +export async function updateProposalAction(id: string, form: FormData) { |
| 135 | + const session = await getOperatorSession(); |
| 136 | + if (!session?.user?.email) throw new Error("Not authorized"); |
| 137 | + |
| 138 | + const v = parseValues(form); |
| 139 | + if (!v.title || !v.slug || !v.bodyMd) { |
| 140 | + throw new Error("Title, slug, and body are required"); |
| 141 | + } |
| 142 | + if (v.voteType === "single_choice" && (!v.choices || v.choices.length < 2)) { |
| 143 | + throw new Error("Single-choice proposals need at least two choices"); |
| 144 | + } |
| 145 | + |
| 146 | + await updateProposal(id, { |
| 147 | + slug: v.slug, |
| 148 | + title: v.title, |
| 149 | + bodyMd: v.bodyMd, |
| 150 | + voteType: v.voteType, |
| 151 | + scope: v.scope, |
| 152 | + productSlug: v.productSlug, |
| 153 | + status: v.status, |
| 154 | + opensAt: v.opensAt, |
| 155 | + closesAt: v.closesAt, |
| 156 | + choices: v.choices, |
| 157 | + }); |
| 158 | + |
| 159 | + revalidatePath("/inside/proposals"); |
| 160 | + revalidatePath(`/inside/proposals/${v.slug}`); |
| 161 | + revalidatePath("/inside/admin/proposals"); |
| 162 | + redirect(`/inside/admin/proposals/${id}/edit?saved=1`); |
| 163 | +} |
| 164 | + |
| 165 | +export async function deleteProposalAction(id: string) { |
| 166 | + const session = await getOperatorSession(); |
| 167 | + if (!session?.user?.email) throw new Error("Not authorized"); |
| 168 | + await deleteProposal(id); |
| 169 | + revalidatePath("/inside/proposals"); |
| 170 | + revalidatePath("/inside/admin/proposals"); |
| 171 | + redirect("/inside/admin/proposals"); |
| 172 | +} |
0 commit comments