feat(finale): vote comments, by-round session lookup, my-finale-inputs query

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matt
2026-06-10 18:10:32 +02:00
parent 6d2fa3369f
commit 45b007334e
2 changed files with 240 additions and 37 deletions

View File

@@ -55,6 +55,62 @@ async function getOrderedFinaleProjects(
return order.map((id) => byId.get(id)).filter((p): p is NonNullable<typeof p> => !!p)
}
/** Shared jury-voting payload for getSessionForVoting / getSessionForVotingByRound. */
async function buildVotingPayload(
prisma: PrismaClient,
session: {
id: string
status: string
currentProjectId: string | null
votingStartedAt: Date | null
votingEndsAt: Date | null
votingMode: string
criteriaJson: unknown
round: unknown
},
userId: string
) {
let currentProject = null
if (session.currentProjectId && session.status === 'IN_PROGRESS') {
currentProject = await prisma.project.findUnique({
where: { id: session.currentProjectId },
select: { id: true, title: true, teamName: true, description: true },
})
}
let userVote = null
if (session.currentProjectId) {
userVote = await prisma.liveVote.findFirst({
where: {
sessionId: session.id,
projectId: session.currentProjectId,
userId,
},
})
}
let timeRemaining = null
if (session.votingEndsAt && session.status === 'IN_PROGRESS') {
const remaining = new Date(session.votingEndsAt).getTime() - Date.now()
timeRemaining = Math.max(0, Math.floor(remaining / 1000))
}
return {
session: {
id: session.id,
status: session.status,
votingStartedAt: session.votingStartedAt,
votingEndsAt: session.votingEndsAt,
votingMode: session.votingMode,
criteriaJson: session.criteriaJson,
},
round: session.round,
currentProject,
userVote,
timeRemaining,
}
}
export const liveVotingRouter = router({
/**
* Get or create a live voting session for a round
@@ -141,46 +197,63 @@ export const liveVotingRouter = router({
},
},
})
return buildVotingPayload(ctx.prisma, session, ctx.user.id)
}),
let currentProject = null
if (session.currentProjectId && session.status === 'IN_PROGRESS') {
currentProject = await ctx.prisma.project.findUnique({
where: { id: session.currentProjectId },
select: { id: true, title: true, teamName: true, description: true },
})
}
let userVote = null
if (session.currentProjectId) {
userVote = await ctx.prisma.liveVote.findFirst({
where: {
sessionId: session.id,
projectId: session.currentProjectId,
userId: ctx.user.id,
/**
* Same payload, resolved from a roundId (the jury live page only knows the
* round). Returns null — and creates nothing — when no session exists yet.
*/
getSessionForVotingByRound: protectedProcedure
.input(z.object({ roundId: z.string() }))
.query(async ({ ctx, input }) => {
const session = await ctx.prisma.liveVotingSession.findUnique({
where: { roundId: input.roundId },
include: {
round: {
include: {
competition: {
include: {
program: { select: { name: true, year: true } },
},
},
},
},
})
}
let timeRemaining = null
if (session.votingEndsAt && session.status === 'IN_PROGRESS') {
const remaining = new Date(session.votingEndsAt).getTime() - Date.now()
timeRemaining = Math.max(0, Math.floor(remaining / 1000))
}
return {
session: {
id: session.id,
status: session.status,
votingStartedAt: session.votingStartedAt,
votingEndsAt: session.votingEndsAt,
votingMode: session.votingMode,
criteriaJson: session.criteriaJson,
},
round: session.round,
currentProject,
userVote,
timeRemaining,
}
})
if (!session) return null
return buildVotingPayload(ctx.prisma, session, ctx.user.id)
}),
/**
* A juror's own finale inputs (votes incl. comments + ceremony notes) for a
* round — resurfaced during deliberation so they can review or revise.
*/
getMyFinaleInputs: protectedProcedure
.input(z.object({ roundId: z.string() }))
.query(async ({ ctx, input }) => {
const session = await ctx.prisma.liveVotingSession.findUnique({
where: { roundId: input.roundId },
select: { id: true, status: true, votingMode: true, criteriaJson: true },
})
const [votes, notes] = await Promise.all([
session
? ctx.prisma.liveVote.findMany({
where: { sessionId: session.id, userId: ctx.user.id },
select: {
projectId: true,
score: true,
criterionScoresJson: true,
comment: true,
votedAt: true,
},
})
: Promise.resolve([]),
ctx.prisma.liveNote.findMany({
where: { roundId: input.roundId, userId: ctx.user.id },
}),
])
return { session: session ?? null, votes, notes }
}),
/**
@@ -491,6 +564,7 @@ export const liveVotingRouter = router({
criterionScores: z
.record(z.string(), z.number())
.optional(),
comment: z.string().max(5000).optional(),
})
)
.mutation(async ({ ctx, input }) => {
@@ -612,11 +686,14 @@ export const liveVotingRouter = router({
score: finalScore,
isAudienceVote: false,
criterionScoresJson: criterionScoresJson ?? undefined,
comment: input.comment ?? undefined,
},
update: {
score: finalScore,
isAudienceVote: false,
criterionScoresJson: criterionScoresJson ?? undefined,
// An omitted comment leaves the existing one untouched
...(input.comment !== undefined ? { comment: input.comment } : {}),
votedAt: new Date(),
},
})