"use server"; import config from "@payload-config"; import { getPayload } from "payload"; import { isPayloadUser } from "@/utils/access-control/isPayloadUser"; import { isSuperuser } from "@/utils/access-control/hasPermission"; import { emitGameEvent } from "@/utils/event-log/emit"; import { EventTypes } from "@/utils/event-log/eventTypes"; import { countGrantsByGrantor, findUserProfile, getAwardsByIds, getGrantorAssignments, heldTierOfSeries, isRecipientEligible, } from "@/lib/awards/grant"; import { cleanupDuplicateAwards, type CleanupResult } from "@/lib/awards/index"; import type { Assignment, Award, Profile } from "@/payload-types"; interface ActionResult { success: boolean; error?: string; data?: T; } async function authenticate() { const payloadConfig = await config; const payload = await getPayload({ config: payloadConfig }); const { headers } = await import("next/headers"); const hdrs = await headers(); const { user } = await payload.auth({ headers: hdrs, canSetHeaders: false, }); if (!isPayloadUser(user)) { throw new Error("Unauthorized"); } return { payload, user }; } export interface GrantResult { recipientUserId: number; success: boolean; error?: string; } export async function grantAward(input: { recipientUserIds: number[]; awardId: number; reason?: string; }): Promise> { try { const { payload, user } = await authenticate(); if (input.recipientUserIds.length === 0) { return { success: false, error: "No recipients selected." }; } let award; try { award = await payload.findByID({ collection: "awards", id: input.awardId, depth: 0, overrideAccess: true, }); } catch { return { success: false, error: "Award not found." }; } let grantorProfile: Profile | null = null; const superuser = await isSuperuser(payload, user); const isMedal = award.type === "medal"; if (isMedal && !superuser) { return { success: false, error: "Only superusers can grant medals." }; } let grantorAssignmentId: number | undefined; let scope: "same_command" | "whitelist" | "anyone" | null | undefined; let whitelist: (number | Assignment)[] | null | undefined; let remaining: number | null = null; if (!superuser && !isMedal) { const allowedTypes = award.grantConfig?.allowedGrantorAssignmentTypes ?? []; if (allowedTypes.length === 0) { return { success: false, error: "This award cannot be granted by leaders." }; } const grantorAssignments = await getGrantorAssignments( payload, user.id as number, allowedTypes, ); if (grantorAssignments.length === 0) { return { success: false, error: "You are not authorized to grant this award." }; } grantorAssignmentId = grantorAssignments[0].id; scope = award.grantConfig?.recipientScope ?? "same_command"; whitelist = award.grantConfig?.eligibleRecipientAssignments; const maxGrants = award.grantConfig?.maxGrantsPerGrantor ?? null; if (maxGrants) { grantorProfile = await findUserProfile(payload, user.id as number); if (!grantorProfile) { return { success: false, error: "You do not have a profile." }; } const issued = await countGrantsByGrantor(payload, award.id, grantorProfile.id); remaining = maxGrants - issued; } } if (!grantorProfile) { grantorProfile = await findUserProfile(payload, user.id as number); } if (!grantorProfile) { return { success: false, error: "You do not have a profile." }; } const results: GrantResult[] = []; let granted = 0; let processed = 0; for (const recipientUserId of input.recipientUserIds) { if (grantorAssignmentId !== undefined) { const eligible = await isRecipientEligible( payload, recipientUserId, scope, grantorAssignmentId, whitelist, ); if (!eligible) { results.push({ recipientUserId, success: false, error: "Recipient is not eligible for this award.", }); processed += 1; continue; } } const recipientProfile = await findUserProfile(payload, recipientUserId); if (!recipientProfile) { results.push({ recipientUserId, success: false, error: "Recipient does not have a profile.", }); processed += 1; continue; } const progression = recipientProfile.progression; const currentAwards = progression?.awards ?? []; // Handle advancement series logic for ribbons with advancement if ( award.advancement?.seriesKey && award.advancement?.tier !== undefined && award.advancement?.tier !== null ) { // Get all the awards held by this recipient const heldAwardIds = currentAwards .map((entry) => { const awardId = typeof entry.award === "number" ? entry.award : entry.award?.id; return awardId; }) .filter((id): id is number => id !== undefined); // Fetch all the held awards in one batch to check their advancement series let heldAwards: Award[] = []; if (heldAwardIds.length > 0) { heldAwards = await getAwardsByIds(payload, heldAwardIds); } // Find the recipient's held entry in this series, if any let heldSeries: { entry: (typeof currentAwards)[number]; heldAward: Award; tier: number; } | null = null; for (const entry of currentAwards) { const awardId = typeof entry.award === "number" ? entry.award : entry.award?.id; const heldAward = heldAwards.find((a) => a.id === awardId); if (!heldAward) continue; const tier = heldTierOfSeries(heldAward, award.advancement.seriesKey); if (tier !== null) { heldSeries = { entry, heldAward, tier }; break; } } // If the recipient holds an entry in this series, enforce tier order // or upgrade it in place (preserving the original award date) if (heldSeries) { const targetTier = award.advancement.tier; if (heldSeries.tier === targetTier) { results.push({ recipientUserId, success: false, error: "Recipient already has this award.", }); processed += 1; continue; } if (heldSeries.tier > targetTier) { results.push({ recipientUserId, success: false, error: "Recipient already holds a higher tier of this ribbon.", }); processed += 1; continue; } const upgradedEntry = { award: award.id, awardedBy: grantorProfile.id, awardedAt: heldSeries.entry.awardedAt, awardReason: input.reason?.trim() ? input.reason.trim() : (heldSeries.entry.awardReason ?? null), upgradedFrom: heldSeries.heldAward.id, upgradedAt: new Date().toISOString(), }; const updatedAwards = [...currentAwards]; const heldAwardId = heldSeries.heldAward.id; const entryIndex = updatedAwards.findIndex((e) => { const id = typeof e.award === "number" ? e.award : e.award?.id; return id === heldAwardId; }); if (entryIndex === -1) { throw new Error("Could not locate the recipient's held ribbon entry for the upgrade."); } updatedAwards[entryIndex] = upgradedEntry; await payload.update({ collection: "profiles", id: recipientProfile.id, data: { progression: { ...progression, awards: updatedAwards, }, }, overrideAccess: true, }); const recipientUserDoc = typeof recipientProfile.user === "object" ? recipientProfile.user : await payload.findByID({ collection: "users", id: recipientProfile.user, depth: 0, overrideAccess: true, }); const recipientName = recipientUserDoc.displayName ?? recipientUserDoc.username; await emitGameEvent(payload, { type: EventTypes.AwardUpgrade, message: `${user.displayName ?? user.username} upgraded ${recipientName}'s ribbon to ${award.name}`, actor: user.id, data: { awardId: award.id, grantorProfileId: grantorProfile.id, recipientUserId, fromAwardId: heldAwardId, toAwardId: award.id, seriesKey: award.advancement.seriesKey ?? null, tier: award.advancement.tier ?? null, }, }); results.push({ recipientUserId, success: true }); granted += 1; // Upgrades are exempt from remaining-grant accounting continue; } } else { // No advancement series - check if recipient already has this award const alreadyHasAward = currentAwards.some((e) => { const id = typeof e.award === "number" ? e.award : e.award?.id; return id === award.id; }); if (alreadyHasAward) { results.push({ recipientUserId, success: false, error: "Recipient already has this award.", }); processed += 1; continue; } } // Normal grant (no series advancement) await payload.update({ collection: "profiles", id: recipientProfile.id, data: { progression: { ...progression, awards: [ ...currentAwards, { award: award.id, awardedBy: grantorProfile.id, awardedAt: new Date().toISOString(), awardReason: input.reason ?? null, }, ], }, }, overrideAccess: true, }); await emitGameEvent(payload, { type: EventTypes.AwardGrant, message: `Granted "${award.name}" to recipient profile #${recipientProfile.id}`, actor: user.id, data: { awardId: award.id, grantorProfileId: grantorProfile.id }, }); results.push({ recipientUserId, success: true }); granted += 1; processed += 1; } return { success: true, data: { results, granted, failed: results.length - granted }, }; } catch (e) { return { success: false, error: e instanceof Error ? e.message : "Unknown error", }; } } export async function cleanupDuplicateAwardsAction(): Promise> { try { const { payload, user } = await authenticate(); const superuser = await isSuperuser(payload, user); if (!superuser) { return { success: false, error: "Only superusers can run award cleanup." }; } const result = await cleanupDuplicateAwards(payload); return { success: true, data: result }; } catch (e) { return { success: false, error: e instanceof Error ? e.message : "Unknown error", }; } }