diff --git a/src/collections/intelligence/MissionRatings.ts b/src/collections/intelligence/MissionRatings.ts new file mode 100644 index 00000000..9d192ce5 --- /dev/null +++ b/src/collections/intelligence/MissionRatings.ts @@ -0,0 +1,98 @@ +import { CollectionConfig } from "payload"; +import { requirePermission } from "@/utils/access-control/hasPermission"; +import { RATING_VALUE_MAX, RATING_VALUE_MIN } from "@/lib/missionRatings/factors"; + +/** + * Mission ratings (a.k.a. post-op surveys). + * + * One row per (rater, mission) - the uniqueness is enforced by the service layer + * (`submitMissionRating` in `src/lib/missionRatings`), not by a DB constraint, + * exactly like `src/collections/users/Evaluations.ts`. + * + * Structure: + * - `ratings` is a flexible array of { factor, value } pairs. `factor` is the + * aggregation key. Core factors (Briefing, Pacing, Map, Host, Balance) are a + * fixed constant; mission authors add custom factor labels that are stored on + * the mission (`missionRating.customFactors`). Both are rated on a 1-5 scale. + * - `comment` is optional free text, shown anonymized in the public overview. + * + * Privacy model: raw documents are only readable/writable by users holding the + * `mission-ratings:*` permissions (admin via role docs). Normal users never + * receive raw rows over REST - the mission page consumes pre-aggregated, + * rater-free output produced by the service with explicit RBAC gating. `user` + * and `comment` are private fields and must never be surfaced to normal users + * by any read path. + */ +export const MissionRatings: CollectionConfig = { + slug: "mission-ratings", + admin: { + group: "Intelligence", + defaultColumns: ["mission", "user", "createdAt"], + }, + access: { + read: requirePermission("mission-ratings:read"), + create: requirePermission("mission-ratings:create"), + update: requirePermission("mission-ratings:update"), + delete: requirePermission("mission-ratings:delete"), + }, + fields: [ + { + name: "mission", + type: "relationship", + relationTo: "missions", + required: true, + index: true, + admin: { + description: "The mission this rating is for.", + }, + }, + { + name: "user", + type: "relationship", + relationTo: "users", + required: true, + admin: { + description: "Who submitted this rating. Private, only visible to staff.", + }, + }, + { + name: "ratings", + type: "array", + labels: { + singular: "Rating", + plural: "Ratings", + }, + admin: { + description: + "One entry per factor rated (core + any custom). factor is the label, value is 1-5 stars.", + }, + fields: [ + { + name: "factor", + type: "text", + required: true, + admin: { + description: "The factor label (core factor or authored custom factor).", + }, + }, + { + name: "value", + type: "select", + required: true, + options: [1, 2, 3, 4, 5].map((n) => ({ label: `${n} stars`, value: String(n) })), + admin: { + description: `Rating on a 1-${RATING_VALUE_MAX} star scale.`, + }, + }, + ], + }, + { + name: "comment", + type: "textarea", + admin: { + description: + "Optional free-text note. Shown anonymized in the public overview; raw attribution is staff-only.", + }, + }, + ], +}; diff --git a/src/collections/intelligence/Missions.ts b/src/collections/intelligence/Missions.ts index dacda6f6..0ff375c3 100644 --- a/src/collections/intelligence/Missions.ts +++ b/src/collections/intelligence/Missions.ts @@ -339,6 +339,15 @@ export const Missions: CollectionConfig = { hidden: true, }, }, + { + name: "discordRatingSentAt", + type: "date", + admin: { + description: + "When the post-op survey prompt was first posted to Discord (managed by the bot, one-shot).", + hidden: true, + }, + }, { name: "discordCancelledAt", type: "date", @@ -575,6 +584,36 @@ export const Missions: CollectionConfig = { }, ], }, + { + name: "missionRating", + type: "group", + label: "Post-Op Survey", + admin: { + description: + "Custom rating factors for the post-op survey. The core factors (Briefing, Pacing, Map, Host, Balance) are fixed; add extra labels below. Both core and authored factors are rated 1-5 stars.", + }, + fields: [ + { + name: "customFactors", + type: "array", + labels: { + singular: "Custom Factor", + plural: "Custom Factors", + }, + admin: { + description: + "Extra factor labels (e.g. 'Gameplay', 'Server Lag') shown on the survey. Web-authoring only; Discord shows core factors. A factor appears in the aggregate only once it has a rating.", + }, + fields: [ + { + name: "label", + type: "text", + required: true, + }, + ], + }, + ], + }, { name: "missionRoles", type: "group", diff --git a/src/lib/missionRatings/factors.ts b/src/lib/missionRatings/factors.ts new file mode 100644 index 00000000..4d07970b --- /dev/null +++ b/src/lib/missionRatings/factors.ts @@ -0,0 +1,62 @@ +/** + * Pure mission-rating definitions for the post-op survey. + * + * This module has no Payload imports - it is safe to use from server actions, + * integration tests, and frontend components. The factor labels drive the UI + * star selectors and the aggregation key; the value range drives validation. + */ + +/** + * The immutable core factor set. These labels are the same on every mission and + * are the only factors shown in Discord. Mission authors may add custom labels + * (stored on the mission as `missionRating.customFactors`); both core and custom + * factors aggregate by their shared `factor` label. + */ +export const MISSION_RATING_CORE_FACTORS = [ + "Briefing", + "Pacing", + "Map", + "Host", + "Balance", +] as const; + +export type MissionRatingCoreFactor = (typeof MISSION_RATING_CORE_FACTORS)[number]; + +/** + * The 1-5 star scale every factor is rated on. `RATING_VALUE_MIN`/`RATING_VALUE_MAX` + * are the inclusive bounds; `RATING_VALUE_OPTIONS` is the ordered list for select + * widgets and distribution bars. + */ +export const RATING_VALUE_MIN = 1; +export const RATING_VALUE_MAX = 5; +export const RATING_VALUE_OPTIONS = [1, 2, 3, 4, 5] as const; + +/** + * How long the rating window stays open after a mission is marked Completed, + * in days. Anchored on the mission's `completedAt` timestamp. + */ +export const RATING_WINDOW_DAYS = 14; + +/** + * Minimum number of distinct raters before the factor breakdown and comments are + * surfaced publicly. Below this threshold only the overall average and rater + * count show, so a small group of raters cannot be identified from the + * distribution. + */ +export const RATING_ANONYMITY_THRESHOLD = 3; + +/** Distinct core factor labels, in display order. */ +export const MISSION_RATING_CORE_FACTORS_DISPLAY: readonly string[] = [ + ...MISSION_RATING_CORE_FACTORS, +]; + +/** True when `value` is an integer within the rating scale. */ +export function isRatingValue(value: unknown): boolean { + return typeof value === "number" && RATING_VALUE_MIN <= value && value <= RATING_VALUE_MAX; +} + +/** Resolve a human label for a factor. Core factors use their label verbatim; + * unknown/custom factors fall back to the raw label. */ +export function getFactorLabel(factor: string): string { + return factor; +} diff --git a/src/lib/missionRatings/xp.ts b/src/lib/missionRatings/xp.ts new file mode 100644 index 00000000..e9c3da54 --- /dev/null +++ b/src/lib/missionRatings/xp.ts @@ -0,0 +1,17 @@ +/** + * Tunable contribution configuration for the mission-rating survey. + * + * The contribution type and XP amount live here (not inline in the server action) + * so they are a single source of truth and easy to rebalance. The type string + * ("mission-rating-completion") must also be registered in + * `src/lib/contributions/rules.ts` so `isContributionType` accepts it. + */ + +/** The contribution type recorded when a user submits a mission rating. */ +export const CONTRIBUTION_TYPE_MISSION_RATING = "mission-rating-completion" as const; + +/** + * Flat XP awarded for submitting a mission rating. Small and intentional: the + * survey is a post-op courtesy, not a primary XP earner. + */ +export const MISSION_RATING_XP = 50; diff --git a/src/payload-types.ts b/src/payload-types.ts index 5a3b2997..fc40bd24 100644 --- a/src/payload-types.ts +++ b/src/payload-types.ts @@ -103,6 +103,7 @@ export interface Config { 'mission-attendances': MissionAttendance; 'mission-role-signups': MissionRoleSignup; 'mission-map-markers': MissionMapMarker; + 'mission-ratings': MissionRating; campaigns: Campaign; factions: Faction; technologies: Technology; @@ -217,6 +218,7 @@ export interface Config { 'mission-attendances': MissionAttendancesSelect | MissionAttendancesSelect; 'mission-role-signups': MissionRoleSignupsSelect | MissionRoleSignupsSelect; 'mission-map-markers': MissionMapMarkersSelect | MissionMapMarkersSelect; + 'mission-ratings': MissionRatingsSelect | MissionRatingsSelect; campaigns: CampaignsSelect | CampaignsSelect; factions: FactionsSelect | FactionsSelect; technologies: TechnologiesSelect | TechnologiesSelect; @@ -725,6 +727,11 @@ export interface Role { | 'mission-map-markers:update' | 'mission-map-markers:delete' | 'admin:mission-map-markers:manage' + | 'mission-ratings:create' + | 'mission-ratings:read' + | 'mission-ratings:update' + | 'mission-ratings:delete' + | 'admin:mission-ratings:manage' | 'campaigns:create' | 'campaigns:read' | 'campaigns:update' @@ -3252,6 +3259,11 @@ export interface GameEventLog { | 'mission-map-markers:update' | 'mission-map-markers:delete' | 'admin:mission-map-markers:manage' + | 'mission-ratings:create' + | 'mission-ratings:read' + | 'mission-ratings:update' + | 'mission-ratings:delete' + | 'admin:mission-ratings:manage' | 'campaigns:create' | 'campaigns:read' | 'campaigns:update' @@ -4315,11 +4327,12 @@ export interface ContributionEntry { | 'wiki-page-edit' | 'mission-map-marker' | 'survey-completion' + | 'mission-rating-completion' | 'journal-publish' | 'manual'; status: 'queued' | 'granted' | 'revoked' | 'expired'; points: number; - sourceCollection: 'wiki-pages' | 'mission-map-markers' | 'form-submissions' | 'journals' | 'manual'; + sourceCollection: 'wiki-pages' | 'mission-map-markers' | 'form-submissions' | 'journals' | 'missions' | 'manual'; /** * Id of the source document (polymorphic). */ @@ -4374,6 +4387,10 @@ export interface Mission { * Fingerprint of the attached modlist last announced to Discord (managed by the bot). */ discordModlistHash?: string | null; + /** + * When the post-op survey prompt was first posted to Discord (managed by the bot, one-shot). + */ + discordRatingSentAt?: string | null; /** * When the roll-call was marked cancelled in Discord (managed by the bot; cleared when a cancelled op is rescheduled). */ @@ -4438,6 +4455,20 @@ export interface Mission { */ completedAt?: string | null; }; + /** + * Custom rating factors for the post-op survey. The core factors (Briefing, Pacing, Map, Host, Balance) are fixed; add extra labels below. Both core and authored factors are rated 1-5 stars. + */ + missionRating?: { + /** + * Extra factor labels (e.g. 'Gameplay', 'Server Lag') shown on the survey. Web-authoring only; Discord shows core factors. A factor appears in the aggregate only once it has a rating. + */ + customFactors?: + | { + label: string; + id?: string | null; + }[] + | null; + }; missionRoles: { maxPlayers: number; providedRoles?: @@ -4941,6 +4972,43 @@ export interface MissionMapMarker { updatedAt: string; createdAt: string; } +/** + * This interface was referenced by `Config`'s JSON-Schema + * via the `definition` "mission-ratings". + */ +export interface MissionRating { + id: number; + /** + * The mission this rating is for. + */ + mission: number | Mission; + /** + * Who submitted this rating. Private, only visible to staff. + */ + user: number | User; + /** + * One entry per factor rated (core + any custom). factor is the label, value is 1-5 stars. + */ + ratings?: + | { + /** + * The factor label (core factor or authored custom factor). + */ + factor: string; + /** + * Rating on a 1-5 star scale. + */ + value: '1' | '2' | '3' | '4' | '5'; + id?: string | null; + }[] + | null; + /** + * Optional free-text note. Shown anonymized in the public overview; raw attribution is staff-only. + */ + comment?: string | null; + updatedAt: string; + createdAt: string; +} /** * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "tech-researches". @@ -7933,6 +8001,24 @@ export interface PayloadMcpApiKey { */ delete?: boolean | null; }; + missionRatings?: { + /** + * Allow clients to find mission-ratings. + */ + find?: boolean | null; + /** + * Allow clients to create mission-ratings. + */ + create?: boolean | null; + /** + * Allow clients to update mission-ratings. + */ + update?: boolean | null; + /** + * Allow clients to delete mission-ratings. + */ + delete?: boolean | null; + }; campaigns?: { /** * Allow clients to find campaigns. @@ -9202,6 +9288,10 @@ export interface PayloadLockedDocument { relationTo: 'mission-map-markers'; value: number | MissionMapMarker; } | null) + | ({ + relationTo: 'mission-ratings'; + value: number | MissionRating; + } | null) | ({ relationTo: 'campaigns'; value: number | Campaign; @@ -10489,6 +10579,7 @@ export interface MissionsSelect { discordAttendanceHash?: T; discordAttendanceSentAt?: T; discordModlistHash?: T; + discordRatingSentAt?: T; discordCancelledAt?: T; evaluationRemindersSentAt?: T; readyReminderWebSentAt?: T; @@ -10518,6 +10609,16 @@ export interface MissionsSelect { visibility?: T; completedAt?: T; }; + missionRating?: + | T + | { + customFactors?: + | T + | { + label?: T; + id?: T; + }; + }; missionRoles?: | T | { @@ -10664,6 +10765,24 @@ export interface MissionMapMarkersSelect { updatedAt?: T; createdAt?: T; } +/** + * This interface was referenced by `Config`'s JSON-Schema + * via the `definition` "mission-ratings_select". + */ +export interface MissionRatingsSelect { + mission?: T; + user?: T; + ratings?: + | T + | { + factor?: T; + value?: T; + id?: T; + }; + comment?: T; + updatedAt?: T; + createdAt?: T; +} /** * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "campaigns_select". @@ -12798,6 +12917,14 @@ export interface PayloadMcpApiKeysSelect { update?: T; delete?: T; }; + missionRatings?: + | T + | { + find?: T; + create?: T; + update?: T; + delete?: T; + }; campaigns?: | T | { diff --git a/src/payload.config.ts b/src/payload.config.ts index e311e034..f5a42202 100644 --- a/src/payload.config.ts +++ b/src/payload.config.ts @@ -19,6 +19,7 @@ import { Missions } from "@/collections/intelligence/Missions"; import { MissionAttendances } from "@/collections/intelligence/MissionAttendances"; import { MissionRoleSignups } from "@/collections/intelligence/MissionRoleSignups"; import { MissionMapMarkers } from "@/collections/intelligence/MissionMapMarkers"; +import { MissionRatings } from "@/collections/intelligence/MissionRatings"; import { Maps } from "@/collections/world/Maps"; import { MapRoads } from "@/collections/world/MapRoads"; import { MapZones } from "@/collections/world/MapZones"; @@ -347,6 +348,7 @@ const collections = [ MissionAttendances, MissionRoleSignups, MissionMapMarkers, + MissionRatings, Campaigns, Factions, Technologies, @@ -738,6 +740,8 @@ export default buildConfig({ // Intel [Missions.slug]: true, [MissionAttendances.slug]: true, + [MissionRoleSignups.slug]: true, + [MissionRatings.slug]: true, [Campaigns.slug]: true, [Factions.slug]: true, [Technologies.slug]: true, diff --git a/src/permissions/index.ts b/src/permissions/index.ts index 59656b88..19614d84 100644 --- a/src/permissions/index.ts +++ b/src/permissions/index.ts @@ -230,6 +230,16 @@ export const PERMISSION_GROUPS: PermissionGroup[] = [ { value: "admin:mission-map-markers:manage", label: "Manage Admin Page" }, ], }, + { + group: "Mission Ratings", + permissions: [ + { value: "mission-ratings:create", label: "Create" }, + { value: "mission-ratings:read", label: "Read (staff: raw per-user ratings)" }, + { value: "mission-ratings:update", label: "Update" }, + { value: "mission-ratings:delete", label: "Delete" }, + { value: "admin:mission-ratings:manage", label: "Manage Admin Page" }, + ], + }, { group: "Campaigns", permissions: [ @@ -981,6 +991,11 @@ export const ALL_PERMISSION_VALUES = PERMISSION_GROUPS.flatMap((g) => "mission-map-markers:update", "mission-map-markers:delete", "admin:mission-map-markers:manage", + "mission-ratings:create", + "mission-ratings:read", + "mission-ratings:update", + "mission-ratings:delete", + "admin:mission-ratings:manage", "campaigns:create", "campaigns:read", "campaigns:update", diff --git a/src/tools/seed/seedRoles.ts b/src/tools/seed/seedRoles.ts index 0cff202e..dea35da2 100644 --- a/src/tools/seed/seedRoles.ts +++ b/src/tools/seed/seedRoles.ts @@ -57,6 +57,10 @@ const ADMIN_PERMISSIONS: Permission[] = [ "mission-attendances:read", "mission-attendances:update", "mission-attendances:delete", + "mission-ratings:create", + "mission-ratings:read", + "mission-ratings:update", + "mission-ratings:delete", "missions:read", "market-negotiations:read", "market-negotiations:update",