The beforeChange hook compared advancement values against undefined, but the admin form always submits the group, so an award outside a lineup arrived as { seriesKey: null, tier: null }. Null counted as set, which tripped the duplicate tier check for standalone ribbons and the ribbons-only guard for medals and commendations.
Normalize null, empty, and non-finite values to "not set" before validating. Non-ribbon awards clear leftover lineup values on a type change instead of throwing on a hidden field, and partial API updates that omit advancement keep the stored lineup.
244 lines
7.2 KiB
TypeScript
244 lines
7.2 KiB
TypeScript
import { CollectionConfig } from "payload";
|
|
import { requirePermission } from "@/utils/access-control/hasPermission";
|
|
|
|
export const Awards: CollectionConfig = {
|
|
slug: "awards",
|
|
access: {
|
|
create: requirePermission("awards:create"),
|
|
update: requirePermission("awards:update"),
|
|
delete: requirePermission("awards:delete"),
|
|
},
|
|
admin: {
|
|
useAsTitle: "name",
|
|
group: "Users",
|
|
},
|
|
fields: [
|
|
{
|
|
name: "name",
|
|
type: "text",
|
|
required: true,
|
|
unique: true,
|
|
},
|
|
{
|
|
name: "description",
|
|
type: "richText",
|
|
required: true,
|
|
},
|
|
{
|
|
name: "type",
|
|
type: "select",
|
|
options: [
|
|
{
|
|
label: "Commendation",
|
|
value: "commendation",
|
|
},
|
|
{
|
|
label: "Ribbon",
|
|
value: "ribbon",
|
|
},
|
|
{
|
|
label: "Medal",
|
|
value: "medal",
|
|
},
|
|
],
|
|
defaultValue: "commendation",
|
|
},
|
|
{
|
|
name: "commendationColor",
|
|
type: "text",
|
|
defaultValue: "#92400e",
|
|
admin: {
|
|
components: {
|
|
Field: "@/components/admin/awards/CommendationColorField",
|
|
},
|
|
condition: (data) => data?.type === "commendation",
|
|
},
|
|
},
|
|
{
|
|
name: "image",
|
|
type: "upload",
|
|
relationTo: "media",
|
|
},
|
|
{
|
|
name: "experiencePoints",
|
|
type: "number",
|
|
min: 1,
|
|
},
|
|
{
|
|
name: "ribbonDesign",
|
|
type: "json",
|
|
admin: {
|
|
components: {
|
|
Field: "@/components/admin/awards/RibbonDesigner",
|
|
},
|
|
condition: (data) => data?.type === "ribbon",
|
|
},
|
|
},
|
|
{
|
|
name: "backgroundImage",
|
|
type: "upload",
|
|
relationTo: "media",
|
|
admin: {
|
|
description:
|
|
"A background image for the award card. Displayed as a faded backdrop behind the card content.",
|
|
condition: (data) => data?.type === "medal",
|
|
},
|
|
},
|
|
{
|
|
name: "medalDesign",
|
|
type: "json",
|
|
admin: {
|
|
components: {
|
|
Field: "@/components/admin/awards/MedalDesigner",
|
|
},
|
|
condition: (data) => data?.type === "medal",
|
|
},
|
|
},
|
|
{
|
|
name: "advancement",
|
|
type: "group",
|
|
label: "Advancement Series",
|
|
admin: {
|
|
description: "Configure this award as part of an advancement lineup.",
|
|
condition: (data) => data?.type === "ribbon",
|
|
},
|
|
fields: [
|
|
{
|
|
name: "seriesKey",
|
|
type: "text",
|
|
admin: {
|
|
description:
|
|
"Lineup identifier (e.g. years-of-service). Ribbons sharing this key form one advancement family.",
|
|
},
|
|
},
|
|
{
|
|
name: "tier",
|
|
type: "number",
|
|
min: 1,
|
|
admin: {
|
|
description: "Position in the advancement lineup, starting at 1.",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: "grantConfig",
|
|
type: "group",
|
|
label: "Grant Permissions",
|
|
admin: {
|
|
description:
|
|
"Configure who can grant this award and to whom. Only applies to ribbons and commendations.",
|
|
condition: (data) => data?.type !== "medal",
|
|
},
|
|
fields: [
|
|
{
|
|
name: "allowedGrantorAssignmentTypes",
|
|
type: "select",
|
|
hasMany: true,
|
|
label: "Eligible Grantor Assignments",
|
|
options: [
|
|
{ label: "Division Leader", value: "division" },
|
|
{ label: "Squad Leader", value: "squad" },
|
|
{ label: "Team Leader", value: "team" },
|
|
{ label: "Special Assignment Leader", value: "special" },
|
|
],
|
|
admin: {
|
|
description:
|
|
"Leaders of these assignment types can grant this award. Leave empty for admin/developer only.",
|
|
},
|
|
},
|
|
{
|
|
name: "maxGrantsPerGrantor",
|
|
type: "number",
|
|
label: "Max Grants Per Leader",
|
|
min: 1,
|
|
admin: {
|
|
description: "Maximum times each grantor can issue this award. Empty = unlimited.",
|
|
},
|
|
},
|
|
{
|
|
name: "recipientScope",
|
|
type: "select",
|
|
label: "Recipient Eligibility",
|
|
defaultValue: "same_command",
|
|
options: [
|
|
{ label: "Own Command (grantor's assignment tree)", value: "same_command" },
|
|
{ label: "Whitelist (specific assignments)", value: "whitelist" },
|
|
{ label: "Anyone (unit-wide)", value: "anyone" },
|
|
],
|
|
},
|
|
{
|
|
name: "eligibleRecipientAssignments",
|
|
type: "relationship",
|
|
relationTo: "assignments",
|
|
hasMany: true,
|
|
label: "Eligible Recipient Assignments",
|
|
admin: {
|
|
condition: (data) => data?.grantConfig?.recipientScope === "whitelist",
|
|
description: "Only members of these assignments can receive this award.",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
hooks: {
|
|
beforeChange: [
|
|
async ({ req, data, operation, originalDoc }) => {
|
|
const effectiveType = data.type ?? originalDoc?.type;
|
|
const advancementProvided = data.advancement !== undefined;
|
|
const rawAdvancement = data.advancement;
|
|
|
|
// Normalize before validating: the admin form submits the advancement group even
|
|
// when it is hidden or empty, so a standalone award arrives as
|
|
// `{ seriesKey: null, tier: null }` and a cleared text input as "". Treat all of
|
|
// those as "not set" instead of as real values (the old `!== undefined` checks
|
|
// treated null as set, which rejected saving any award without a tier).
|
|
const rawSeriesKey =
|
|
typeof rawAdvancement?.seriesKey === "string" ? rawAdvancement.seriesKey.trim() : "";
|
|
const seriesKey =
|
|
rawSeriesKey === "" ? null : rawSeriesKey.toLowerCase().replace(/\s+/g, "-");
|
|
const rawTier = rawAdvancement?.tier;
|
|
const tier = typeof rawTier === "number" && Number.isFinite(rawTier) ? rawTier : null;
|
|
|
|
// Partial API updates that omit the field leave the stored lineup untouched.
|
|
if (!advancementProvided) {
|
|
return data;
|
|
}
|
|
|
|
if (effectiveType !== "ribbon") {
|
|
// Advancement lineups only exist for ribbons. Clear leftovers when an award is
|
|
// switched to another type, since the field is hidden and cannot be cleared there.
|
|
data.advancement = { seriesKey: null, tier: null };
|
|
return data;
|
|
}
|
|
|
|
if ((seriesKey === null) !== (tier === null)) {
|
|
throw new Error("Advancement requires both a series key and a tier.");
|
|
}
|
|
|
|
if (seriesKey !== null && tier !== null) {
|
|
const result = await req.payload.count({
|
|
collection: "awards",
|
|
where: {
|
|
and: [
|
|
{ "advancement.seriesKey": { equals: seriesKey } },
|
|
{ "advancement.tier": { equals: tier } },
|
|
...(operation === "update" && originalDoc?.id
|
|
? [{ id: { not_equals: originalDoc.id } }]
|
|
: []),
|
|
],
|
|
},
|
|
overrideAccess: true,
|
|
});
|
|
|
|
if (result.totalDocs > 0) {
|
|
throw new Error(`Tier ${tier} of this series already exists.`);
|
|
}
|
|
}
|
|
|
|
data.advancement = { seriesKey, tier };
|
|
return data;
|
|
},
|
|
],
|
|
},
|
|
};
|