"use client"; import * as React from "react"; import { Button } from "@payloadcms/ui"; import { runAwardEvaluation, applyAwardGrants } from "@/lib/awards/automationActions"; import type { EvaluationResult } from "@/lib/awards/automation"; const panelStyle: React.CSSProperties = { border: "1px solid var(--theme-elevation-150)", borderRadius: "3px", background: "var(--theme-elevation-50)", }; /** * Admin view (award-rules collection): preview which members currently * qualify under the active rules, then apply the grants. Both actions are * idempotent; the sweeper skips members who already hold the award. */ export default function AwardAutomationView() { const [result, setResult] = React.useState(null); const [busy, setBusy] = React.useState<"preview" | "apply" | null>(null); const [error, setError] = React.useState(null); const runPreview = async () => { setBusy("preview"); setError(null); const res = await runAwardEvaluation(); setBusy(null); if (!res.success) { setError(res.error); return; } setResult(res.result); }; const runApply = async () => { setBusy("apply"); setError(null); const res = await applyAwardGrants(); setBusy(null); if (!res.success) { setError(res.error); return; } setResult(res.result); }; const grantCount = result ? result.plans.filter((p) => p.kind === "grant").length : 0; const upgradeCount = result ? result.plans.filter((p) => p.kind === "upgrade").length : 0; return (

Award Automation

Evaluate every active award rule against all members. Preview first: nothing is written until you apply. Applying is safe to repeat, members who already hold an award are skipped.

{error && (

{error}

)} {result && (
{[ { label: "Mode", value: result.dryRun ? "Dry run" : "Applied" }, { label: "Rules evaluated", value: String(result.rulesEvaluated) }, { label: "Members checked", value: String(result.membersChecked) }, { label: result.dryRun ? "Would grant" : "Granted", value: String(result.dryRun ? grantCount : result.granted), }, { label: result.dryRun ? "Would upgrade" : "Upgraded", value: String(result.dryRun ? upgradeCount : result.upgraded), }, { label: "Skipped", value: String(result.skips.length) }, ].map((stat) => (
{stat.label} {stat.value}
))}
{result.plans.length > 0 ? (
{["Member", "Rule", "Award", "Metric value", "Action"].map((col) => ( ))} {result.plans.map((plan, i) => ( {[ plan.username, plan.ruleLabel, plan.awardName, plan.metricValue.toFixed(2), plan.kind === "upgrade" ? `Upgrade (retires award #${plan.replacesAwardId})` : "Grant", ].map((cell, j) => ( ))} ))}
{col}
{cell}
) : (

No members currently qualify for anything new.

)}
)}
); }