- 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)
150 lines
4.2 KiB
TypeScript
150 lines
4.2 KiB
TypeScript
/**
|
|
* Seeds the years-of-service advancement series and its automation rules.
|
|
*
|
|
* - Wires the existing "First/Two/Three/Five Years of Service" ribbons into
|
|
* one advancement series (seriesKey "years-of-service", tier = years).
|
|
* - Creates one active award-rule per ribbon: yearsOfService >= <years>,
|
|
* auto-retiring the lower tier so anniversaries upgrade the member's ribbon
|
|
* in place instead of stacking.
|
|
*
|
|
* Idempotent: re-running updates nothing that already exists. Pass
|
|
* --create-missing to create the ribbons first (local dev databases only;
|
|
* production already has them).
|
|
*
|
|
* Usage: bun src/tools/seed/seedYearsOfServiceRules.ts [--create-missing]
|
|
*/
|
|
import { getPayload } from "payload";
|
|
import config from "@payload-config";
|
|
import type { Award } from "@/payload-types";
|
|
|
|
const SERIES_KEY = "years-of-service";
|
|
|
|
const PLAN: ReadonlyArray<{ name: string; tier: number; threshold: number }> = [
|
|
{ name: "First Year of Service", tier: 1, threshold: 1 },
|
|
{ name: "Two Years of Service", tier: 2, threshold: 2 },
|
|
{ name: "Three Years of Service", tier: 3, threshold: 3 },
|
|
{ name: "Five Years of Service", tier: 5, threshold: 5 },
|
|
];
|
|
|
|
function richText(text: string) {
|
|
return {
|
|
root: {
|
|
type: "root" as const,
|
|
format: "" as const,
|
|
indent: 0,
|
|
version: 1,
|
|
children: [
|
|
{
|
|
type: "paragraph" as const,
|
|
format: "" as const,
|
|
indent: 0,
|
|
version: 1,
|
|
direction: "ltr" as const,
|
|
children: [
|
|
{
|
|
type: "text" as const,
|
|
format: 0,
|
|
style: "",
|
|
mode: "normal" as const,
|
|
text,
|
|
version: 1,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
direction: "ltr" as const,
|
|
},
|
|
};
|
|
}
|
|
|
|
async function main() {
|
|
const payload = await getPayload({ config });
|
|
const createMissing = process.argv.includes("--create-missing");
|
|
|
|
for (const entry of PLAN) {
|
|
const found = await payload.find({
|
|
collection: "awards",
|
|
where: { name: { equals: entry.name } },
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
|
|
let award = found.docs[0] as Award | undefined;
|
|
|
|
if (!award && createMissing) {
|
|
award = (await payload.create({
|
|
collection: "awards",
|
|
data: {
|
|
name: entry.name,
|
|
description: richText(`Awarded for ${entry.threshold} full years of service.`),
|
|
type: "ribbon",
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
})) as unknown as Award;
|
|
payload.logger.info(`Created ribbon "${entry.name}"`);
|
|
}
|
|
|
|
if (!award) {
|
|
payload.logger.warn(`Ribbon "${entry.name}" not found; skipping.`);
|
|
continue;
|
|
}
|
|
|
|
if (award.advancement?.seriesKey !== SERIES_KEY || award.advancement?.tier !== entry.tier) {
|
|
await payload.update({
|
|
collection: "awards",
|
|
id: award.id,
|
|
data: { advancement: { seriesKey: SERIES_KEY, tier: entry.tier } },
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
payload.logger.info(
|
|
`Wired "${entry.name}" into series ${SERIES_KEY} at tier ${entry.tier}`,
|
|
);
|
|
}
|
|
|
|
const label = `Auto: ${entry.name.toLowerCase()}`;
|
|
const existingRule = await payload.find({
|
|
collection: "award-rules",
|
|
where: { label: { equals: label } },
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
if (existingRule.docs.length > 0) {
|
|
payload.logger.info(`Rule "${label}" already exists; skipping.`);
|
|
continue;
|
|
}
|
|
|
|
await payload.create({
|
|
collection: "award-rules",
|
|
data: {
|
|
label,
|
|
award: award.id,
|
|
metric: "yearsOfService",
|
|
operator: "gte",
|
|
value: entry.threshold,
|
|
autoRetireLowerTiers: entry.tier > 1,
|
|
oncePerSeries: false,
|
|
active: true,
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
payload.logger.info(
|
|
`Created rule "${label}" (yearsOfService >= ${entry.threshold}${
|
|
entry.tier > 1 ? ", auto-retire lower tiers" : ""
|
|
})`,
|
|
);
|
|
}
|
|
|
|
payload.logger.info("Years-of-service rules seed complete.");
|
|
}
|
|
|
|
main()
|
|
.then(() => process.exit(0))
|
|
.catch((error) => {
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|