Files
kickkingsadmin/src/app/actions/update-user-cc-rc.ts
T
2026-07-12 17:55:44 +00:00

128 lines
3.6 KiB
TypeScript

"use server";
import { redirect } from "next/navigation";
import { requireWriteAccess } from "@/lib/auth/require-session";
import {
buildDashboardHref,
type AdminDashboardTab,
} from "@/lib/dashboard-search-url";
import { createAdminSupabase } from "@/lib/supabase/admin";
import { normalizeLedgerPageSize } from "@/lib/ledger-table-view";
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) {
await requireWriteAccess();
const userId = Number(formData.get("userId"));
const tabRaw = String(formData.get("tab") ?? "");
const tab: AdminDashboardTab =
tabRaw === "matches"
? "matches"
: tabRaw === "players"
? "players"
: tabRaw === "matchmaker"
? "matchmaker"
: tabRaw === "ledger"
? "ledger"
: tabRaw === "analysis"
? "analysis"
: "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 ledgerFromRaw = String(formData.get("ledgerFrom") ?? "").trim();
const ledgerToRaw = String(formData.get("ledgerTo") ?? "").trim();
const ledgerPageRaw = String(formData.get("ledgerPage") ?? "").trim();
const ledgerPageSizeRaw = String(
formData.get("ledgerPageSize") ?? "",
).trim();
const ledgerSortRaw = String(formData.get("ledgerSort") ?? "").trim();
const ledgerOrderRaw = String(formData.get("ledgerOrder") ?? "").trim();
const ledgerPageNum =
ledgerPageRaw === "" ? NaN : Number.parseInt(ledgerPageRaw, 10);
const base = {
tab,
highlightId,
participantRaw: participant,
...(tab === "ledger"
? {
ledgerFrom: ledgerFromRaw || null,
ledgerTo: ledgerToRaw || null,
ledgerPage:
Number.isInteger(ledgerPageNum) && ledgerPageNum > 1
? ledgerPageNum
: null,
ledgerSort: ledgerSortRaw || null,
ledgerOrder:
ledgerOrderRaw === "desc"
? ("desc" as const)
: ledgerOrderRaw === "asc"
? ("asc" as const)
: null,
ledgerPageSize: normalizeLedgerPageSize(
ledgerPageSizeRaw === "" ? null : ledgerPageSizeRaw,
),
}
: {}),
};
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));
}