Complete remaining product gaps: credit-based shop purchases, guild thread replies, help ticket detail/reply/close, offline message compose, sign-out-everywhere via JWT version, ads delete confirm, soft-fail feedback, rate limits, loading states, and single auth() in site layout. Co-authored-by: Cursor <[email protected]>
147 lines
3.7 KiB
TypeScript
147 lines
3.7 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { redirect } from "next/navigation";
|
|
import { auth } from "@/lib/auth";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { clientIp, rateLimit } from "@/lib/rate-limit";
|
|
|
|
// AtomCMS validates the application body with `min:10`. Mirror that floor and
|
|
// cap the write defensively (column is TEXT, but we keep applications sane).
|
|
const CONTENT_MIN = 10;
|
|
const CONTENT_MAX = 5000;
|
|
|
|
type ApplyOutcome =
|
|
| "submitted"
|
|
| "empty"
|
|
| "invalid"
|
|
| "duplicate"
|
|
| "ratelimit"
|
|
| "error";
|
|
|
|
function staffRedirect(outcome: ApplyOutcome): never {
|
|
if (outcome === "submitted") redirect("/apply/staff?submitted=1");
|
|
redirect(`/apply/staff?error=${outcome}`);
|
|
}
|
|
|
|
function teamRedirect(outcome: ApplyOutcome): never {
|
|
if (outcome === "submitted") redirect("/apply/team?submitted=1");
|
|
redirect(`/apply/team?error=${outcome}`);
|
|
}
|
|
|
|
function isNextRedirect(e: unknown): boolean {
|
|
return (
|
|
!!e &&
|
|
typeof e === "object" &&
|
|
"digest" in e &&
|
|
typeof (e as { digest?: unknown }).digest === "string" &&
|
|
(e as { digest: string }).digest.startsWith("NEXT_REDIRECT")
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Submit a STAFF application for an open position.
|
|
*/
|
|
export async function applyStaff(formData: FormData): Promise<void> {
|
|
let outcome: ApplyOutcome = "error";
|
|
|
|
try {
|
|
const session = await auth();
|
|
const userId = Number(session?.user?.id);
|
|
if (!Number.isInteger(userId) || userId <= 0) {
|
|
redirect("/login");
|
|
}
|
|
|
|
await clientIp();
|
|
if (!(await rateLimit(`apply-staff:${userId}`, 3, 60_000)).ok) {
|
|
outcome = "ratelimit";
|
|
} else {
|
|
const rankId = Number(formData.get("rankId"));
|
|
if (!Number.isInteger(rankId) || rankId <= 0) {
|
|
outcome = "invalid";
|
|
} else {
|
|
const content = String(formData.get("content") ?? "")
|
|
.normalize("NFC")
|
|
.trim()
|
|
.slice(0, CONTENT_MAX);
|
|
if (content.length < CONTENT_MIN) {
|
|
outcome = "empty";
|
|
} else {
|
|
const existing = await prisma.websiteStaffApplications.findFirst({
|
|
where: { userId, rankId },
|
|
select: { id: true },
|
|
});
|
|
if (existing) {
|
|
outcome = "duplicate";
|
|
} else {
|
|
const now = new Date();
|
|
await prisma.websiteStaffApplications.create({
|
|
data: { userId, rankId, content, createdAt: now, updatedAt: now },
|
|
});
|
|
outcome = "submitted";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (e) {
|
|
if (isNextRedirect(e)) throw e;
|
|
outcome = "error";
|
|
}
|
|
|
|
revalidatePath("/apply/staff");
|
|
staffRedirect(outcome);
|
|
}
|
|
|
|
/**
|
|
* Submit a TEAM application.
|
|
*/
|
|
export async function applyTeam(formData: FormData): Promise<void> {
|
|
let outcome: ApplyOutcome = "error";
|
|
|
|
try {
|
|
const session = await auth();
|
|
const userId = Number(session?.user?.id);
|
|
if (!Number.isInteger(userId) || userId <= 0) {
|
|
redirect("/login");
|
|
}
|
|
|
|
await clientIp();
|
|
if (!(await rateLimit(`apply-team:${userId}`, 3, 60_000)).ok) {
|
|
outcome = "ratelimit";
|
|
} else {
|
|
const rankId = Number(formData.get("teamId"));
|
|
if (!Number.isInteger(rankId) || rankId <= 0) {
|
|
outcome = "invalid";
|
|
} else {
|
|
const content = String(formData.get("content") ?? "")
|
|
.normalize("NFC")
|
|
.trim()
|
|
.slice(0, CONTENT_MAX);
|
|
if (content.length < CONTENT_MIN) {
|
|
outcome = "empty";
|
|
} else {
|
|
const existing = await prisma.websiteStaffApplications.findFirst({
|
|
where: { userId, rankId },
|
|
select: { id: true },
|
|
});
|
|
if (existing) {
|
|
outcome = "duplicate";
|
|
} else {
|
|
const now = new Date();
|
|
await prisma.websiteStaffApplications.create({
|
|
data: { userId, rankId, content, createdAt: now, updatedAt: now },
|
|
});
|
|
outcome = "submitted";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (e) {
|
|
if (isNextRedirect(e)) throw e;
|
|
outcome = "error";
|
|
}
|
|
|
|
revalidatePath("/apply/team");
|
|
teamRedirect(outcome);
|
|
}
|