Round system redesign: Phases 1-7 complete
Full pipeline/track/stage architecture replacing the legacy round system. Schema: 11 new models (Pipeline, Track, Stage, StageTransition, ProjectStageState, RoutingRule, Cohort, CohortProject, LiveProgressCursor, OverrideAction, AudienceVoter) + 8 new enums. Backend: 9 new routers (pipeline, stage, routing, stageFiltering, stageAssignment, cohort, live, decision, award) + 6 new services (stage-engine, routing-engine, stage-filtering, stage-assignment, stage-notifications, live-control). Frontend: Pipeline wizard (17 components), jury stage pages (7), applicant pipeline pages (3), public stage pages (2), admin pipeline pages (5), shared stage components (3), SSE route, live hook. Phase 6 refit: 23 routers/services migrated from roundId to stageId, all frontend components refitted. Deleted round.ts (985 lines), roundTemplate.ts, round-helpers.ts, round-settings.ts, round-type-settings.tsx, 10 legacy admin pages, 7 legacy jury pages, 3 legacy dialogs. Phase 7 validation: 36 tests (10 unit + 8 integration files) all passing, TypeScript 0 errors, Next.js build succeeds, 13 integrity checks, legacy symbol sweep clean, auto-seed on first Docker startup. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -16,7 +16,6 @@ export const evaluationRouter = router({
|
||||
// Verify ownership or admin
|
||||
const assignment = await ctx.prisma.assignment.findUniqueOrThrow({
|
||||
where: { id: input.assignmentId },
|
||||
include: { round: true },
|
||||
})
|
||||
|
||||
if (
|
||||
@@ -47,25 +46,20 @@ export const evaluationRouter = router({
|
||||
// Verify assignment ownership
|
||||
const assignment = await ctx.prisma.assignment.findUniqueOrThrow({
|
||||
where: { id: input.assignmentId },
|
||||
include: {
|
||||
round: {
|
||||
include: {
|
||||
evaluationForms: { where: { isActive: true }, take: 1 },
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (assignment.userId !== ctx.user.id) {
|
||||
throw new TRPCError({ code: 'FORBIDDEN' })
|
||||
}
|
||||
|
||||
// Get active form
|
||||
const form = assignment.round.evaluationForms[0]
|
||||
// Get active form for this stage
|
||||
const form = await ctx.prisma.evaluationForm.findFirst({
|
||||
where: { stageId: assignment.stageId, isActive: true },
|
||||
})
|
||||
if (!form) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'No active evaluation form for this round',
|
||||
message: 'No active evaluation form for this stage',
|
||||
})
|
||||
}
|
||||
|
||||
@@ -150,9 +144,7 @@ export const evaluationRouter = router({
|
||||
const evaluation = await ctx.prisma.evaluation.findUniqueOrThrow({
|
||||
where: { id },
|
||||
include: {
|
||||
assignment: {
|
||||
include: { round: true },
|
||||
},
|
||||
assignment: true,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -160,21 +152,23 @@ export const evaluationRouter = router({
|
||||
throw new TRPCError({ code: 'FORBIDDEN' })
|
||||
}
|
||||
|
||||
// Check voting window
|
||||
const round = evaluation.assignment.round
|
||||
// Check voting window via stage
|
||||
const stage = await ctx.prisma.stage.findUniqueOrThrow({
|
||||
where: { id: evaluation.assignment.stageId },
|
||||
})
|
||||
const now = new Date()
|
||||
|
||||
if (round.status !== 'ACTIVE') {
|
||||
if (stage.status !== 'STAGE_ACTIVE') {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Round is not active',
|
||||
message: 'Stage is not active',
|
||||
})
|
||||
}
|
||||
|
||||
// Check for grace period
|
||||
const gracePeriod = await ctx.prisma.gracePeriod.findFirst({
|
||||
where: {
|
||||
roundId: round.id,
|
||||
stageId: stage.id,
|
||||
userId: ctx.user.id,
|
||||
OR: [
|
||||
{ projectId: null },
|
||||
@@ -184,9 +178,9 @@ export const evaluationRouter = router({
|
||||
},
|
||||
})
|
||||
|
||||
const effectiveEndDate = gracePeriod?.extendedUntil ?? round.votingEndAt
|
||||
const effectiveEndDate = gracePeriod?.extendedUntil ?? stage.windowCloseAt
|
||||
|
||||
if (round.votingStartAt && now < round.votingStartAt) {
|
||||
if (stage.windowOpenAt && now < stage.windowOpenAt) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Voting has not started yet',
|
||||
@@ -225,7 +219,7 @@ export const evaluationRouter = router({
|
||||
entityId: id,
|
||||
detailsJson: {
|
||||
projectId: evaluation.assignment.projectId,
|
||||
roundId: evaluation.assignment.roundId,
|
||||
stageId: evaluation.assignment.stageId,
|
||||
globalScore: data.globalScore,
|
||||
binaryDecision: data.binaryDecision,
|
||||
},
|
||||
@@ -276,19 +270,19 @@ export const evaluationRouter = router({
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get all evaluations for a round (admin only)
|
||||
* Get all evaluations for a stage (admin only)
|
||||
*/
|
||||
listByRound: adminProcedure
|
||||
listByStage: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
roundId: z.string(),
|
||||
stageId: z.string(),
|
||||
status: z.enum(['NOT_STARTED', 'DRAFT', 'SUBMITTED', 'LOCKED']).optional(),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
return ctx.prisma.evaluation.findMany({
|
||||
where: {
|
||||
assignment: { roundId: input.roundId },
|
||||
assignment: { stageId: input.stageId },
|
||||
...(input.status && { status: input.status }),
|
||||
},
|
||||
include: {
|
||||
@@ -307,13 +301,13 @@ export const evaluationRouter = router({
|
||||
* Get my past evaluations (read-only for jury)
|
||||
*/
|
||||
myPastEvaluations: protectedProcedure
|
||||
.input(z.object({ roundId: z.string().optional() }))
|
||||
.input(z.object({ stageId: z.string().optional() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
return ctx.prisma.evaluation.findMany({
|
||||
where: {
|
||||
assignment: {
|
||||
userId: ctx.user.id,
|
||||
...(input.roundId && { roundId: input.roundId }),
|
||||
...(input.stageId && { stageId: input.stageId }),
|
||||
},
|
||||
status: 'SUBMITTED',
|
||||
},
|
||||
@@ -321,7 +315,7 @@ export const evaluationRouter = router({
|
||||
assignment: {
|
||||
include: {
|
||||
project: { select: { id: true, title: true } },
|
||||
round: { select: { id: true, name: true } },
|
||||
stage: { select: { id: true, name: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -346,12 +340,12 @@ export const evaluationRouter = router({
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Look up the assignment to get projectId, roundId, userId
|
||||
// Look up the assignment to get projectId, stageId, userId
|
||||
const assignment = await ctx.prisma.assignment.findUniqueOrThrow({
|
||||
where: { id: input.assignmentId },
|
||||
include: {
|
||||
project: { select: { title: true } },
|
||||
round: { select: { name: true } },
|
||||
stage: { select: { id: true, name: true } },
|
||||
},
|
||||
})
|
||||
|
||||
@@ -367,7 +361,6 @@ export const evaluationRouter = router({
|
||||
assignmentId: input.assignmentId,
|
||||
userId: ctx.user.id,
|
||||
projectId: assignment.projectId,
|
||||
roundId: assignment.roundId,
|
||||
hasConflict: input.hasConflict,
|
||||
conflictType: input.hasConflict ? input.conflictType : null,
|
||||
description: input.hasConflict ? input.description : null,
|
||||
@@ -385,15 +378,15 @@ export const evaluationRouter = router({
|
||||
await notifyAdmins({
|
||||
type: NotificationTypes.JURY_INACTIVE,
|
||||
title: 'Conflict of Interest Declared',
|
||||
message: `${ctx.user.name || ctx.user.email} declared a conflict of interest (${input.conflictType || 'unspecified'}) for project "${assignment.project.title}" in ${assignment.round.name}.`,
|
||||
linkUrl: `/admin/rounds/${assignment.roundId}/coi`,
|
||||
message: `${ctx.user.name || ctx.user.email} declared a conflict of interest (${input.conflictType || 'unspecified'}) for project "${assignment.project.title}" in ${assignment.stage.name}.`,
|
||||
linkUrl: `/admin/stages/${assignment.stageId}/coi`,
|
||||
linkLabel: 'Review COI',
|
||||
priority: 'high',
|
||||
metadata: {
|
||||
assignmentId: input.assignmentId,
|
||||
userId: ctx.user.id,
|
||||
projectId: assignment.projectId,
|
||||
roundId: assignment.roundId,
|
||||
stageId: assignment.stageId,
|
||||
conflictType: input.conflictType,
|
||||
},
|
||||
})
|
||||
@@ -409,7 +402,7 @@ export const evaluationRouter = router({
|
||||
detailsJson: {
|
||||
assignmentId: input.assignmentId,
|
||||
projectId: assignment.projectId,
|
||||
roundId: assignment.roundId,
|
||||
stageId: assignment.stageId,
|
||||
hasConflict: input.hasConflict,
|
||||
conflictType: input.conflictType,
|
||||
},
|
||||
@@ -432,19 +425,19 @@ export const evaluationRouter = router({
|
||||
}),
|
||||
|
||||
/**
|
||||
* List COI declarations for a round (admin only)
|
||||
* List COI declarations for a stage (admin only)
|
||||
*/
|
||||
listCOIByRound: adminProcedure
|
||||
listCOIByStage: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
roundId: z.string(),
|
||||
stageId: z.string(),
|
||||
hasConflictOnly: z.boolean().optional(),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
return ctx.prisma.conflictOfInterest.findMany({
|
||||
where: {
|
||||
roundId: input.roundId,
|
||||
assignment: { stageId: input.stageId },
|
||||
...(input.hasConflictOnly && { hasConflict: true }),
|
||||
},
|
||||
include: {
|
||||
@@ -505,19 +498,19 @@ export const evaluationRouter = router({
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Manually trigger reminder check for a specific round (admin only)
|
||||
* Manually trigger reminder check for a specific stage (admin only)
|
||||
*/
|
||||
triggerReminders: adminProcedure
|
||||
.input(z.object({ roundId: z.string() }))
|
||||
.input(z.object({ stageId: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const result = await processEvaluationReminders(input.roundId)
|
||||
const result = await processEvaluationReminders(input.stageId)
|
||||
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'REMINDERS_TRIGGERED',
|
||||
entityType: 'Round',
|
||||
entityId: input.roundId,
|
||||
entityType: 'Stage',
|
||||
entityId: input.stageId,
|
||||
detailsJson: {
|
||||
sent: result.sent,
|
||||
errors: result.errors,
|
||||
@@ -540,13 +533,13 @@ export const evaluationRouter = router({
|
||||
.input(
|
||||
z.object({
|
||||
projectId: z.string(),
|
||||
roundId: z.string(),
|
||||
stageId: z.string(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
return generateSummary({
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
stageId: input.stageId,
|
||||
userId: ctx.user.id,
|
||||
prisma: ctx.prisma,
|
||||
})
|
||||
@@ -559,64 +552,63 @@ export const evaluationRouter = router({
|
||||
.input(
|
||||
z.object({
|
||||
projectId: z.string(),
|
||||
roundId: z.string(),
|
||||
stageId: z.string(),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
return ctx.prisma.evaluationSummary.findUnique({
|
||||
where: {
|
||||
projectId_roundId: {
|
||||
projectId_stageId: {
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
stageId: input.stageId,
|
||||
},
|
||||
},
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* Generate summaries for all projects in a round with submitted evaluations (admin only)
|
||||
* Generate summaries for all projects in a stage with submitted evaluations (admin only)
|
||||
*/
|
||||
generateBulkSummaries: adminProcedure
|
||||
.input(z.object({ roundId: z.string() }))
|
||||
.input(z.object({ stageId: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Find all projects in the round with at least 1 submitted evaluation
|
||||
const projects = await ctx.prisma.project.findMany({
|
||||
// Find all projects with at least 1 submitted evaluation in this stage
|
||||
const assignments = await ctx.prisma.assignment.findMany({
|
||||
where: {
|
||||
roundId: input.roundId,
|
||||
assignments: {
|
||||
some: {
|
||||
evaluation: {
|
||||
status: 'SUBMITTED',
|
||||
},
|
||||
},
|
||||
stageId: input.stageId,
|
||||
evaluation: {
|
||||
status: 'SUBMITTED',
|
||||
},
|
||||
},
|
||||
select: { id: true },
|
||||
select: { projectId: true },
|
||||
distinct: ['projectId'],
|
||||
})
|
||||
|
||||
const projectIds = assignments.map((a) => a.projectId)
|
||||
|
||||
let generated = 0
|
||||
const errors: Array<{ projectId: string; error: string }> = []
|
||||
|
||||
// Generate summaries sequentially to avoid rate limits
|
||||
for (const project of projects) {
|
||||
for (const projectId of projectIds) {
|
||||
try {
|
||||
await generateSummary({
|
||||
projectId: project.id,
|
||||
roundId: input.roundId,
|
||||
projectId,
|
||||
stageId: input.stageId,
|
||||
userId: ctx.user.id,
|
||||
prisma: ctx.prisma,
|
||||
})
|
||||
generated++
|
||||
} catch (error) {
|
||||
errors.push({
|
||||
projectId: project.id,
|
||||
projectId,
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
total: projects.length,
|
||||
total: projectIds.length,
|
||||
generated,
|
||||
errors,
|
||||
}
|
||||
@@ -633,15 +625,15 @@ export const evaluationRouter = router({
|
||||
.input(
|
||||
z.object({
|
||||
projectIds: z.array(z.string()).min(2).max(3),
|
||||
roundId: z.string(),
|
||||
stageId: z.string(),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
// Verify all projects are assigned to current user in this round
|
||||
// Verify all projects are assigned to current user in this stage
|
||||
const assignments = await ctx.prisma.assignment.findMany({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
roundId: input.roundId,
|
||||
stageId: input.stageId,
|
||||
projectId: { in: input.projectIds },
|
||||
},
|
||||
include: {
|
||||
@@ -670,13 +662,13 @@ export const evaluationRouter = router({
|
||||
if (assignments.length !== input.projectIds.length) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You are not assigned to all requested projects in this round',
|
||||
message: 'You are not assigned to all requested projects in this stage',
|
||||
})
|
||||
}
|
||||
|
||||
// Fetch the active evaluation form for this round to get criteria labels
|
||||
// Fetch the active evaluation form for this stage to get criteria labels
|
||||
const evaluationForm = await ctx.prisma.evaluationForm.findFirst({
|
||||
where: { roundId: input.roundId, isActive: true },
|
||||
where: { stageId: input.stageId, isActive: true },
|
||||
select: { criteriaJson: true, scalesJson: true },
|
||||
})
|
||||
|
||||
@@ -704,7 +696,7 @@ export const evaluationRouter = router({
|
||||
.input(
|
||||
z.object({
|
||||
projectId: z.string(),
|
||||
roundId: z.string(),
|
||||
stageId: z.string(),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
@@ -713,7 +705,7 @@ export const evaluationRouter = router({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
stageId: input.stageId,
|
||||
},
|
||||
include: { evaluation: true },
|
||||
})
|
||||
@@ -725,16 +717,16 @@ export const evaluationRouter = router({
|
||||
})
|
||||
}
|
||||
|
||||
// Check round settings for peer review
|
||||
const round = await ctx.prisma.round.findUniqueOrThrow({
|
||||
where: { id: input.roundId },
|
||||
// Check stage settings for peer review
|
||||
const stage = await ctx.prisma.stage.findUniqueOrThrow({
|
||||
where: { id: input.stageId },
|
||||
})
|
||||
|
||||
const settings = (round.settingsJson as Record<string, unknown>) || {}
|
||||
const settings = (stage.configJson as Record<string, unknown>) || {}
|
||||
if (!settings.peer_review_enabled) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Peer review is not enabled for this round',
|
||||
message: 'Peer review is not enabled for this stage',
|
||||
})
|
||||
}
|
||||
|
||||
@@ -744,7 +736,7 @@ export const evaluationRouter = router({
|
||||
status: 'SUBMITTED',
|
||||
assignment: {
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
stageId: input.stageId,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
@@ -829,16 +821,16 @@ export const evaluationRouter = router({
|
||||
.input(
|
||||
z.object({
|
||||
projectId: z.string(),
|
||||
roundId: z.string(),
|
||||
stageId: z.string(),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
// Get or create discussion
|
||||
let discussion = await ctx.prisma.evaluationDiscussion.findUnique({
|
||||
where: {
|
||||
projectId_roundId: {
|
||||
projectId_stageId: {
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
stageId: input.stageId,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
@@ -855,7 +847,7 @@ export const evaluationRouter = router({
|
||||
discussion = await ctx.prisma.evaluationDiscussion.create({
|
||||
data: {
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
stageId: input.stageId,
|
||||
},
|
||||
include: {
|
||||
comments: {
|
||||
@@ -868,11 +860,11 @@ export const evaluationRouter = router({
|
||||
})
|
||||
}
|
||||
|
||||
// Anonymize comments based on round settings
|
||||
const round = await ctx.prisma.round.findUniqueOrThrow({
|
||||
where: { id: input.roundId },
|
||||
// Anonymize comments based on stage settings
|
||||
const stage = await ctx.prisma.stage.findUniqueOrThrow({
|
||||
where: { id: input.stageId },
|
||||
})
|
||||
const settings = (round.settingsJson as Record<string, unknown>) || {}
|
||||
const settings = (stage.configJson as Record<string, unknown>) || {}
|
||||
const anonymizationLevel = (settings.anonymization_level as string) || 'fully_anonymous'
|
||||
|
||||
const anonymizedComments = discussion.comments.map((c: { id: string; userId: string; user: { name: string | null }; content: string; createdAt: Date }, idx: number) => {
|
||||
@@ -915,16 +907,16 @@ export const evaluationRouter = router({
|
||||
.input(
|
||||
z.object({
|
||||
projectId: z.string(),
|
||||
roundId: z.string(),
|
||||
stageId: z.string(),
|
||||
content: z.string().min(1).max(2000),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Check max comment length from round settings
|
||||
const round = await ctx.prisma.round.findUniqueOrThrow({
|
||||
where: { id: input.roundId },
|
||||
// Check max comment length from stage settings
|
||||
const stage = await ctx.prisma.stage.findUniqueOrThrow({
|
||||
where: { id: input.stageId },
|
||||
})
|
||||
const settings = (round.settingsJson as Record<string, unknown>) || {}
|
||||
const settings = (stage.configJson as Record<string, unknown>) || {}
|
||||
const maxLength = (settings.max_comment_length as number) || 2000
|
||||
if (input.content.length > maxLength) {
|
||||
throw new TRPCError({
|
||||
@@ -936,9 +928,9 @@ export const evaluationRouter = router({
|
||||
// Get or create discussion
|
||||
let discussion = await ctx.prisma.evaluationDiscussion.findUnique({
|
||||
where: {
|
||||
projectId_roundId: {
|
||||
projectId_stageId: {
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
stageId: input.stageId,
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -947,7 +939,7 @@ export const evaluationRouter = router({
|
||||
discussion = await ctx.prisma.evaluationDiscussion.create({
|
||||
data: {
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
stageId: input.stageId,
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -978,7 +970,7 @@ export const evaluationRouter = router({
|
||||
detailsJson: {
|
||||
discussionId: discussion.id,
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
stageId: input.stageId,
|
||||
},
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
@@ -1015,7 +1007,7 @@ export const evaluationRouter = router({
|
||||
entityId: input.discussionId,
|
||||
detailsJson: {
|
||||
projectId: discussion.projectId,
|
||||
roundId: discussion.roundId,
|
||||
stageId: discussion.stageId,
|
||||
},
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
@@ -1026,4 +1018,228 @@ export const evaluationRouter = router({
|
||||
|
||||
return discussion
|
||||
}),
|
||||
|
||||
// =========================================================================
|
||||
// Phase 4: Stage-scoped evaluation procedures
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Start a stage-scoped evaluation (create or return existing draft)
|
||||
*/
|
||||
startStage: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
assignmentId: z.string(),
|
||||
stageId: z.string(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Verify assignment ownership and stageId match
|
||||
const assignment = await ctx.prisma.assignment.findUniqueOrThrow({
|
||||
where: { id: input.assignmentId },
|
||||
})
|
||||
|
||||
if (assignment.userId !== ctx.user.id) {
|
||||
throw new TRPCError({ code: 'FORBIDDEN' })
|
||||
}
|
||||
|
||||
if (assignment.stageId !== input.stageId) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Assignment does not belong to this stage',
|
||||
})
|
||||
}
|
||||
|
||||
// Check stage window
|
||||
const stage = await ctx.prisma.stage.findUniqueOrThrow({
|
||||
where: { id: input.stageId },
|
||||
})
|
||||
|
||||
const now = new Date()
|
||||
if (stage.status !== 'STAGE_ACTIVE') {
|
||||
throw new TRPCError({
|
||||
code: 'PRECONDITION_FAILED',
|
||||
message: 'Stage is not active',
|
||||
})
|
||||
}
|
||||
|
||||
// Check grace period
|
||||
const gracePeriod = await ctx.prisma.gracePeriod.findFirst({
|
||||
where: {
|
||||
stageId: input.stageId,
|
||||
userId: ctx.user.id,
|
||||
OR: [
|
||||
{ projectId: null },
|
||||
{ projectId: assignment.projectId },
|
||||
],
|
||||
extendedUntil: { gte: now },
|
||||
},
|
||||
})
|
||||
|
||||
const effectiveClose = gracePeriod?.extendedUntil ?? stage.windowCloseAt
|
||||
if (stage.windowOpenAt && now < stage.windowOpenAt) {
|
||||
throw new TRPCError({
|
||||
code: 'PRECONDITION_FAILED',
|
||||
message: 'Evaluation window has not opened yet',
|
||||
})
|
||||
}
|
||||
if (effectiveClose && now > effectiveClose) {
|
||||
throw new TRPCError({
|
||||
code: 'PRECONDITION_FAILED',
|
||||
message: 'Evaluation window has closed',
|
||||
})
|
||||
}
|
||||
|
||||
// Check for existing evaluation
|
||||
const existing = await ctx.prisma.evaluation.findUnique({
|
||||
where: { assignmentId: input.assignmentId },
|
||||
})
|
||||
if (existing) return existing
|
||||
|
||||
// Get active evaluation form for this stage
|
||||
const form = await ctx.prisma.evaluationForm.findFirst({
|
||||
where: { stageId: input.stageId, isActive: true },
|
||||
})
|
||||
if (!form) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'No active evaluation form for this stage',
|
||||
})
|
||||
}
|
||||
|
||||
return ctx.prisma.evaluation.create({
|
||||
data: {
|
||||
assignmentId: input.assignmentId,
|
||||
formId: form.id,
|
||||
status: 'DRAFT',
|
||||
},
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get the active evaluation form for a stage
|
||||
*/
|
||||
getStageForm: protectedProcedure
|
||||
.input(z.object({ stageId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const form = await ctx.prisma.evaluationForm.findFirst({
|
||||
where: { stageId: input.stageId, isActive: true },
|
||||
})
|
||||
|
||||
if (!form) {
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
id: form.id,
|
||||
criteriaJson: form.criteriaJson as Array<{
|
||||
id: string
|
||||
label: string
|
||||
description?: string
|
||||
scale?: string
|
||||
weight?: number
|
||||
type?: string
|
||||
required?: boolean
|
||||
}>,
|
||||
scalesJson: form.scalesJson as Record<string, { min: number; max: number; labels?: Record<string, string> }> | null,
|
||||
version: form.version,
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Check the evaluation window status for a stage
|
||||
*/
|
||||
checkStageWindow: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
stageId: z.string(),
|
||||
userId: z.string().optional(),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const stage = await ctx.prisma.stage.findUniqueOrThrow({
|
||||
where: { id: input.stageId },
|
||||
select: {
|
||||
id: true,
|
||||
status: true,
|
||||
windowOpenAt: true,
|
||||
windowCloseAt: true,
|
||||
},
|
||||
})
|
||||
|
||||
const userId = input.userId ?? ctx.user.id
|
||||
const now = new Date()
|
||||
|
||||
// Check for grace period
|
||||
const gracePeriod = await ctx.prisma.gracePeriod.findFirst({
|
||||
where: {
|
||||
stageId: input.stageId,
|
||||
userId,
|
||||
extendedUntil: { gte: now },
|
||||
},
|
||||
orderBy: { extendedUntil: 'desc' },
|
||||
})
|
||||
|
||||
const effectiveClose = gracePeriod?.extendedUntil ?? stage.windowCloseAt
|
||||
|
||||
const isOpen =
|
||||
stage.status === 'STAGE_ACTIVE' &&
|
||||
(!stage.windowOpenAt || now >= stage.windowOpenAt) &&
|
||||
(!effectiveClose || now <= effectiveClose)
|
||||
|
||||
let reason = ''
|
||||
if (!isOpen) {
|
||||
if (stage.status !== 'STAGE_ACTIVE') {
|
||||
reason = 'Stage is not active'
|
||||
} else if (stage.windowOpenAt && now < stage.windowOpenAt) {
|
||||
reason = 'Window has not opened yet'
|
||||
} else {
|
||||
reason = 'Window has closed'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
isOpen,
|
||||
opensAt: stage.windowOpenAt,
|
||||
closesAt: stage.windowCloseAt,
|
||||
hasGracePeriod: !!gracePeriod,
|
||||
graceExpiresAt: gracePeriod?.extendedUntil ?? null,
|
||||
reason,
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* List evaluations for the current user in a specific stage
|
||||
*/
|
||||
listStageEvaluations: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
stageId: z.string(),
|
||||
projectId: z.string().optional(),
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const where: Record<string, unknown> = {
|
||||
assignment: {
|
||||
userId: ctx.user.id,
|
||||
stageId: input.stageId,
|
||||
...(input.projectId ? { projectId: input.projectId } : {}),
|
||||
},
|
||||
}
|
||||
|
||||
return ctx.prisma.evaluation.findMany({
|
||||
where,
|
||||
include: {
|
||||
assignment: {
|
||||
include: {
|
||||
project: { select: { id: true, title: true, teamName: true } },
|
||||
},
|
||||
},
|
||||
form: {
|
||||
select: { criteriaJson: true, scalesJson: true },
|
||||
},
|
||||
},
|
||||
orderBy: { updatedAt: 'desc' },
|
||||
})
|
||||
}),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user