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 ` `, 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.", }, }, ], };