- award-rules collection: metric/operator/value threshold rules linked to awards, with auto-retire-lower-tiers and once-per-series flags, active toggle, and award-rules:* permissions - src/lib/awards/metrics.ts: pure metric resolvers (years of service from dossier.enlistmentDate with account-age fallback, account age, missions attended, total XP) plus operator comparison, unit tested - evaluateAwardRules() sweeper: idempotent, dry-run capable; grants land as profile rows with awardedBy null so the existing afterChange hook sends award:granted notifications; upgrades replace the lower series tier in place preserving the original date; emits award:rule-granted events - award-tick payload bin for daily evaluation + admin Award Automation view with Evaluate now (dry run) preview and Apply grants - seedYearsOfServiceRules.ts wires the four years-of-service ribbons into one advancement series and creates their rules (idempotent, dev seeded) - migration add_award_rules_automation (table + awards event target enum)
112 lines
3.3 KiB
TypeScript
112 lines
3.3 KiB
TypeScript
import type { CollectionConfig } from "payload";
|
|
import { requirePermission } from "@/utils/access-control/hasPermission";
|
|
import { AWARD_METRICS, METRIC_LABELS, METRIC_OPERATORS, OPERATOR_LABELS } from "@/lib/awards/metrics";
|
|
|
|
/**
|
|
* Declarative grant rules evaluated by the award automation sweeper
|
|
* (src/lib/awards/automation.ts). Each rule says: when a member's metric
|
|
* passes `<operator> <value>`, grant them the linked award automatically.
|
|
*/
|
|
export const AwardRules: CollectionConfig = {
|
|
slug: "award-rules",
|
|
labels: {
|
|
singular: "Award Rule",
|
|
plural: "Award Rules",
|
|
},
|
|
admin: {
|
|
group: "Users",
|
|
useAsTitle: "label",
|
|
defaultColumns: ["label", "award", "metric", "operator", "value", "active"],
|
|
description:
|
|
"Automation rules that grant awards when a member's metric crosses a threshold. Evaluated by the daily award tick and the Evaluate Now view.",
|
|
components: {
|
|
views: {
|
|
awardAutomation: {
|
|
Component: "@/components/admin/awards/AwardAutomationView",
|
|
path: "/award-automation",
|
|
exact: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
access: {
|
|
read: requirePermission("award-rules:read"),
|
|
create: requirePermission("award-rules:create"),
|
|
update: requirePermission("award-rules:update"),
|
|
delete: requirePermission("award-rules:delete"),
|
|
},
|
|
fields: [
|
|
{
|
|
name: "label",
|
|
type: "text",
|
|
required: true,
|
|
admin: {
|
|
description:
|
|
'Short rule name shown in previews and feed events (e.g. "2 years of service").',
|
|
},
|
|
},
|
|
{
|
|
name: "award",
|
|
type: "relationship",
|
|
relationTo: "awards",
|
|
required: true,
|
|
admin: {
|
|
description: "The award granted when the rule fires.",
|
|
},
|
|
},
|
|
{
|
|
name: "metric",
|
|
type: "select",
|
|
required: true,
|
|
options: AWARD_METRICS.map((metric) => ({ label: METRIC_LABELS[metric], value: metric })),
|
|
},
|
|
{
|
|
name: "operator",
|
|
type: "select",
|
|
required: true,
|
|
defaultValue: "gte",
|
|
options: METRIC_OPERATORS.map((operator) => ({
|
|
label: OPERATOR_LABELS[operator],
|
|
value: operator,
|
|
})),
|
|
},
|
|
{
|
|
name: "value",
|
|
type: "number",
|
|
required: true,
|
|
min: 0,
|
|
admin: {
|
|
description:
|
|
"Threshold compared against the metric (years for time-based metrics, counts otherwise).",
|
|
},
|
|
},
|
|
{
|
|
name: "autoRetireLowerTiers",
|
|
type: "checkbox",
|
|
defaultValue: false,
|
|
admin: {
|
|
description:
|
|
"For awards in an advancement series: replace any lower tier the member already holds with this award, preserving their original award date.",
|
|
condition: (_, siblingData) => Boolean(siblingData?.award),
|
|
},
|
|
},
|
|
{
|
|
name: "oncePerSeries",
|
|
type: "checkbox",
|
|
defaultValue: false,
|
|
admin: {
|
|
description:
|
|
"Skip members who already hold any award in this award's advancement series (use instead of auto-retire when tiers must not stack or upgrade).",
|
|
condition: (_, siblingData) => Boolean(siblingData?.award),
|
|
},
|
|
},
|
|
{
|
|
name: "active",
|
|
type: "checkbox",
|
|
defaultValue: true,
|
|
admin: {
|
|
description: "Inactive rules are ignored by the sweeper.",
|
|
},
|
|
},
|
|
],
|
|
};
|