init
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
"use server";
|
||||
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { ADMIN_SESSION_COOKIE } from "@/lib/auth/session";
|
||||
import {
|
||||
buildDashboardHref,
|
||||
type AdminDashboardTab,
|
||||
} from "@/lib/dashboard-search-url";
|
||||
import { createAdminSupabase } from "@/lib/supabase/admin";
|
||||
|
||||
function parseScoreField(raw: FormDataEntryValue | null): number | null {
|
||||
if (raw == null) return null;
|
||||
const s = String(raw).trim();
|
||||
if (s === "") return null;
|
||||
const n = Number(s);
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
|
||||
export async function updateUserCcRc(formData: FormData) {
|
||||
const cookieStore = await cookies();
|
||||
if (cookieStore.get(ADMIN_SESSION_COOKIE)?.value !== "1") {
|
||||
redirect("/login");
|
||||
}
|
||||
|
||||
const userId = Number(formData.get("userId"));
|
||||
const tabRaw = String(formData.get("tab") ?? "");
|
||||
const tab: AdminDashboardTab =
|
||||
tabRaw === "matches"
|
||||
? "matches"
|
||||
: tabRaw === "players"
|
||||
? "players"
|
||||
: tabRaw === "matchmaker"
|
||||
? "matchmaker"
|
||||
: "dashboard";
|
||||
const highlightRaw = String(formData.get("highlightId") ?? "").trim();
|
||||
const participantRaw = String(formData.get("participantRaw") ?? "").trim();
|
||||
const highlightId = highlightRaw === "" ? null : highlightRaw;
|
||||
const participant =
|
||||
participantRaw === "" ? null : participantRaw;
|
||||
|
||||
const base = { tab, highlightId, participantRaw: participant };
|
||||
|
||||
if (!Number.isInteger(userId) || userId < 1) {
|
||||
redirect(buildDashboardHref(base));
|
||||
}
|
||||
|
||||
const ccRaw = formData.get("cc");
|
||||
const rcRaw = formData.get("rc");
|
||||
const cc = parseScoreField(ccRaw);
|
||||
const rc = parseScoreField(rcRaw);
|
||||
const ccStr = ccRaw == null ? "" : String(ccRaw).trim();
|
||||
const rcStr = rcRaw == null ? "" : String(rcRaw).trim();
|
||||
if ((ccStr !== "" && cc === null) || (rcStr !== "" && rc === null)) {
|
||||
redirect(
|
||||
buildDashboardHref({
|
||||
...base,
|
||||
editId: userId,
|
||||
saveError: true,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const supabase = createAdminSupabase();
|
||||
if (!supabase) {
|
||||
redirect(
|
||||
buildDashboardHref({
|
||||
...base,
|
||||
editId: userId,
|
||||
saveError: true,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const { error } = await supabase
|
||||
.from("users")
|
||||
.update({ cc, rc })
|
||||
.eq("id", userId);
|
||||
|
||||
if (error) {
|
||||
redirect(
|
||||
buildDashboardHref({
|
||||
...base,
|
||||
editId: userId,
|
||||
saveError: true,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
redirect(buildDashboardHref(base));
|
||||
}
|
||||
Reference in New Issue
Block a user