/** * 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 >= , * 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); });