From 99ef9ec73c9ac4617cb08425e51144cd0d198aad Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Sun, 1 Nov 2026 15:21:53 -0500 Subject: [PATCH] fix(mission-ratings): block mission authors from rating their own mission Authors can no longer rate a mission they ran or planned. The rating action, the availability check and the server submit all enforce it, and the UI explains why. The mission-ratings fixtures now use a dedicated author so the rater accounts stay non-authors. --- .../frontend/intelligence/MissionRating.tsx | 7 ++- src/lib/missionRatings/index.ts | 35 +++++++++++++- tests/int/mission-ratings.int.spec.ts | 47 ++++++++++++++++++- 3 files changed, 84 insertions(+), 5 deletions(-) diff --git a/src/components/frontend/intelligence/MissionRating.tsx b/src/components/frontend/intelligence/MissionRating.tsx index 83fb39b2..a3a4966b 100644 --- a/src/components/frontend/intelligence/MissionRating.tsx +++ b/src/components/frontend/intelligence/MissionRating.tsx @@ -340,11 +340,16 @@ export function MissionRating({ The rating window for this mission is closed.

)} - {!showForm && !ratingLocked && !windowClosed && notEligible && ( + {!showForm && !ratingLocked && !windowClosed && notEligible && !state.isAuthor && (

You need a Yes RSVP for this mission to rate it.

)} + {!showForm && !ratingLocked && state.isAuthor && ( +

+ Mission authors cannot rate their own mission. +

+ )} diff --git a/src/lib/missionRatings/index.ts b/src/lib/missionRatings/index.ts index a02abc0a..666942cf 100644 --- a/src/lib/missionRatings/index.ts +++ b/src/lib/missionRatings/index.ts @@ -22,9 +22,23 @@ interface MissionRatingMission { ownershipAndStatus?: { status?: string | null; completedAt?: string | null; + authors?: Array | null; } | null; } +/** + * Whether `userId` is one of the mission's authors. Authors ran or planned the + * op, so they may not rate it. Authors are a relationship array: entries are + * either bare ids or populated docs, depending on depth. + */ +export function isMissionAuthor( + mission: MissionRatingMission, + userId: number, +): boolean { + const authors = mission.ownershipAndStatus?.authors ?? []; + return authors.some((a) => (typeof a === "number" ? a : a.id) === userId); +} + /** * Whether the rating window is open for a mission at `now`. The window opens * when the mission is marked Completed and stays open for RATING_WINDOW_DAYS from @@ -96,6 +110,7 @@ export async function isRatingAvailable( if (!isRatingWindowOpen(mission.ownershipAndStatus?.status, mission.ownershipAndStatus?.completedAt, now)) { return false; } + if (isMissionAuthor(mission, userId)) return false; if (!(await hasYesAttendance(payload, userId, mission.id))) return false; if (await hasRated(payload, mission.id, userId)) return false; return true; @@ -136,10 +151,21 @@ export async function submitMissionRating( overrideAccess: true, }) .catch(() => null)) as unknown as - | { ownershipAndStatus?: { status?: string | null; completedAt?: string | null }; missionRating?: { customFactors?: Array<{ label: string }> | null } } + | { + id: number; + ownershipAndStatus?: { + status?: string | null; + completedAt?: string | null; + authors?: Array | null; + }; + missionRating?: { customFactors?: Array<{ label: string }> | null }; + } | null; if (!mission) throw new Error("Mission not found."); + if (isMissionAuthor(mission, userId)) { + throw new Error("Mission authors cannot rate their own mission."); + } if (!isRatingWindowOpen(mission.ownershipAndStatus?.status, mission.ownershipAndStatus?.completedAt)) { throw new Error("The rating window for this mission is closed."); } @@ -188,6 +214,8 @@ export interface MissionRatingState { windowClosed: boolean; /** The current user has a yes-RSVP for the mission. */ isEligible: boolean; + /** The current user is an author of the mission (authors never rate it). */ + isAuthor: boolean; /** Window open AND eligible AND the user has not already rated. */ canRate: boolean; /** The user's existing ratings, or null when they have not rated yet. */ @@ -213,7 +241,9 @@ export async function getMissionRatingState( now, ); - const isEligible = userId !== null && (await hasYesAttendance(payload, userId, mission.id)); + const isAuthor = userId !== null && isMissionAuthor(mission, userId); + const isEligible = + userId !== null && !isAuthor && (await hasYesAttendance(payload, userId, mission.id)); let myValues: MissionRatingEntry[] | null = null; if (userId !== null) { @@ -238,6 +268,7 @@ export async function getMissionRatingState( windowOpen, windowClosed: !windowOpen, isEligible, + isAuthor, canRate, myValues, }; diff --git a/tests/int/mission-ratings.int.spec.ts b/tests/int/mission-ratings.int.spec.ts index b5e12af8..aed7516d 100644 --- a/tests/int/mission-ratings.int.spec.ts +++ b/tests/int/mission-ratings.int.spec.ts @@ -30,6 +30,7 @@ describe("Mission ratings (post-op survey)", () => { const userIds: number[] = []; // Raters shared across scenarios. + let author: User; let rater1: User; let rater2: User; let rater3: User; @@ -98,7 +99,7 @@ describe("Mission ratings (post-op survey)", () => { status: "Completed", completedAt: past.toISOString(), visibility: "unit", - authors: [rater1.id], + authors: [author.id], }, ...(opts?.customFactors ? { missionRating: { customFactors: opts.customFactors } } : {}), missionRoles: { maxPlayers: 16 }, @@ -130,7 +131,7 @@ describe("Mission ratings (post-op survey)", () => { ownershipAndStatus: { status: "Scheduled", visibility: "unit", - authors: [rater1.id], + authors: [author.id], }, missionRoles: { maxPlayers: 16 }, gameDetails: { serverDetails: { serverIp: "127.0.0.1", serverPort: 2302 } }, @@ -174,6 +175,7 @@ describe("Mission ratings (post-op survey)", () => { })) as unknown as Campaign; campaignId = campaign.id; + author = await makeUser("author"); rater1 = await makeUser("rater1"); rater2 = await makeUser("rater2"); rater3 = await makeUser("rater3"); @@ -296,6 +298,47 @@ describe("Mission ratings (post-op survey)", () => { expect(second).toHaveLength(5); }); + it("blocks a mission author from rating their own mission", async () => { + // author authored aggMission and has a yes-RSVP, so only the author rule can block them. + await rsvp(author.id, aggMissionId); + + expect( + await isRatingAvailable( + payload, + { id: aggMissionId, ownershipAndStatus: { status: "Completed", completedAt: past.toISOString(), authors: [author.id] } }, + author.id, + ), + ).toBe(false); + + const state = await getMissionRatingState( + payload, + { id: aggMissionId, ownershipAndStatus: { status: "Completed", completedAt: past.toISOString(), authors: [author.id] } }, + author.id, + ); + expect(state.isAuthor).toBe(true); + expect(state.isEligible).toBe(false); + expect(state.canRate).toBe(false); + + await expect( + submitMissionRating(payload, { + missionId: aggMissionId, + userId: author.id, + values: { Briefing: 5 }, + }), + ).rejects.toThrow(/cannot rate their own mission/i); + }); + + it("still lets a non-author with a yes-RSVP rate an authored mission", async () => { + // rater2 is not an author of eligMission (authored by author) and has a yes-RSVP. + await rsvp(rater2.id, eligMissionId); + const mission = { id: eligMissionId, ownershipAndStatus: { status: "Completed", completedAt: past.toISOString(), authors: [author.id] } }; + + expect(await isRatingAvailable(payload, mission, rater2.id)).toBe(true); + const state = await getMissionRatingState(payload, mission, rater2.id); + expect(state.isAuthor).toBe(false); + expect(state.isEligible).toBe(true); + }); + it("validates factor values before writing (unknown / out-of-range / closed)", async () => { await rsvp(rater3.id, aggMissionId);