2026-01-30 13:41:32 +01:00
|
|
|
import { z } from 'zod'
|
|
|
|
|
import { TRPCError } from '@trpc/server'
|
|
|
|
|
import { Prisma } from '@prisma/client'
|
|
|
|
|
import { router, protectedProcedure, adminProcedure } from '../trpc'
|
|
|
|
|
|
|
|
|
|
export const roundRouter = router({
|
|
|
|
|
/**
|
|
|
|
|
* List rounds for a program
|
|
|
|
|
*/
|
|
|
|
|
list: protectedProcedure
|
|
|
|
|
.input(z.object({ programId: z.string() }))
|
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
|
return ctx.prisma.round.findMany({
|
|
|
|
|
where: { programId: input.programId },
|
2026-02-02 22:33:55 +01:00
|
|
|
orderBy: { sortOrder: 'asc' },
|
2026-01-30 13:41:32 +01:00
|
|
|
include: {
|
|
|
|
|
_count: {
|
2026-02-02 22:33:55 +01:00
|
|
|
select: { roundProjects: true, assignments: true },
|
2026-01-30 13:41:32 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a single round with stats
|
|
|
|
|
*/
|
|
|
|
|
get: protectedProcedure
|
|
|
|
|
.input(z.object({ id: z.string() }))
|
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
|
const round = await ctx.prisma.round.findUniqueOrThrow({
|
|
|
|
|
where: { id: input.id },
|
|
|
|
|
include: {
|
|
|
|
|
program: true,
|
|
|
|
|
_count: {
|
2026-02-02 22:33:55 +01:00
|
|
|
select: { roundProjects: true, assignments: true },
|
2026-01-30 13:41:32 +01:00
|
|
|
},
|
|
|
|
|
evaluationForms: {
|
|
|
|
|
where: { isActive: true },
|
|
|
|
|
take: 1,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Get evaluation stats
|
|
|
|
|
const evaluationStats = await ctx.prisma.evaluation.groupBy({
|
|
|
|
|
by: ['status'],
|
|
|
|
|
where: {
|
|
|
|
|
assignment: { roundId: input.id },
|
|
|
|
|
},
|
|
|
|
|
_count: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...round,
|
|
|
|
|
evaluationStats,
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new round (admin only)
|
|
|
|
|
*/
|
|
|
|
|
create: adminProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
programId: z.string(),
|
|
|
|
|
name: z.string().min(1).max(255),
|
2026-02-02 22:33:55 +01:00
|
|
|
roundType: z.enum(['FILTERING', 'EVALUATION', 'LIVE_EVENT']).default('EVALUATION'),
|
2026-01-30 13:41:32 +01:00
|
|
|
requiredReviews: z.number().int().min(1).max(10).default(3),
|
2026-02-02 22:33:55 +01:00
|
|
|
sortOrder: z.number().int().optional(),
|
|
|
|
|
settingsJson: z.record(z.unknown()).optional(),
|
2026-01-30 13:41:32 +01:00
|
|
|
votingStartAt: z.date().optional(),
|
|
|
|
|
votingEndAt: z.date().optional(),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
|
|
|
|
// Validate dates
|
|
|
|
|
if (input.votingStartAt && input.votingEndAt) {
|
|
|
|
|
if (input.votingEndAt <= input.votingStartAt) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'BAD_REQUEST',
|
|
|
|
|
message: 'End date must be after start date',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-02 22:33:55 +01:00
|
|
|
// Auto-set sortOrder if not provided (append to end)
|
|
|
|
|
let sortOrder = input.sortOrder
|
|
|
|
|
if (sortOrder === undefined) {
|
|
|
|
|
const maxOrder = await ctx.prisma.round.aggregate({
|
|
|
|
|
where: { programId: input.programId },
|
|
|
|
|
_max: { sortOrder: true },
|
|
|
|
|
})
|
|
|
|
|
sortOrder = (maxOrder._max.sortOrder ?? -1) + 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { settingsJson, sortOrder: _so, ...rest } = input
|
2026-02-03 23:19:45 +01:00
|
|
|
|
|
|
|
|
// Auto-activate if voting start date is in the past
|
|
|
|
|
const now = new Date()
|
|
|
|
|
const shouldAutoActivate = input.votingStartAt && input.votingStartAt <= now
|
|
|
|
|
|
2026-01-30 13:41:32 +01:00
|
|
|
const round = await ctx.prisma.round.create({
|
2026-02-02 22:33:55 +01:00
|
|
|
data: {
|
|
|
|
|
...rest,
|
|
|
|
|
sortOrder,
|
2026-02-03 23:19:45 +01:00
|
|
|
status: shouldAutoActivate ? 'ACTIVE' : 'DRAFT',
|
2026-02-02 22:33:55 +01:00
|
|
|
settingsJson: settingsJson as Prisma.InputJsonValue ?? undefined,
|
|
|
|
|
},
|
2026-01-30 13:41:32 +01:00
|
|
|
})
|
|
|
|
|
|
2026-02-03 23:19:45 +01:00
|
|
|
// For FILTERING rounds, automatically add all projects from the program
|
|
|
|
|
if (input.roundType === 'FILTERING') {
|
|
|
|
|
const projects = await ctx.prisma.project.findMany({
|
|
|
|
|
where: { programId: input.programId },
|
|
|
|
|
select: { id: true },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (projects.length > 0) {
|
|
|
|
|
await ctx.prisma.roundProject.createMany({
|
|
|
|
|
data: projects.map((p) => ({
|
|
|
|
|
roundId: round.id,
|
|
|
|
|
projectId: p.id,
|
|
|
|
|
status: 'SUBMITTED',
|
|
|
|
|
})),
|
|
|
|
|
skipDuplicates: true,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 13:41:32 +01:00
|
|
|
// Audit log
|
|
|
|
|
await ctx.prisma.auditLog.create({
|
|
|
|
|
data: {
|
|
|
|
|
userId: ctx.user.id,
|
|
|
|
|
action: 'CREATE',
|
|
|
|
|
entityType: 'Round',
|
|
|
|
|
entityId: round.id,
|
2026-02-02 22:33:55 +01:00
|
|
|
detailsJson: { ...rest, settingsJson } as Prisma.InputJsonValue,
|
2026-01-30 13:41:32 +01:00
|
|
|
ipAddress: ctx.ip,
|
|
|
|
|
userAgent: ctx.userAgent,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return round
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update round details (admin only)
|
|
|
|
|
*/
|
|
|
|
|
update: adminProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
id: z.string(),
|
|
|
|
|
name: z.string().min(1).max(255).optional(),
|
|
|
|
|
slug: z.string().min(1).max(100).regex(/^[a-z0-9-]+$/).optional().nullable(),
|
|
|
|
|
roundType: z.enum(['FILTERING', 'EVALUATION', 'LIVE_EVENT']).optional(),
|
|
|
|
|
requiredReviews: z.number().int().min(1).max(10).optional(),
|
|
|
|
|
submissionDeadline: z.date().optional().nullable(),
|
|
|
|
|
votingStartAt: z.date().optional().nullable(),
|
|
|
|
|
votingEndAt: z.date().optional().nullable(),
|
|
|
|
|
settingsJson: z.record(z.unknown()).optional(),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
|
|
|
|
const { id, settingsJson, ...data } = input
|
|
|
|
|
|
|
|
|
|
// Validate dates if both provided
|
|
|
|
|
if (data.votingStartAt && data.votingEndAt) {
|
|
|
|
|
if (data.votingEndAt <= data.votingStartAt) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'BAD_REQUEST',
|
|
|
|
|
message: 'End date must be after start date',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-03 23:19:45 +01:00
|
|
|
// Check if we should auto-activate (if voting start is in the past and round is DRAFT)
|
|
|
|
|
const now = new Date()
|
|
|
|
|
let autoActivate = false
|
|
|
|
|
if (data.votingStartAt && data.votingStartAt <= now) {
|
|
|
|
|
const existingRound = await ctx.prisma.round.findUnique({
|
|
|
|
|
where: { id },
|
|
|
|
|
select: { status: true },
|
|
|
|
|
})
|
|
|
|
|
if (existingRound?.status === 'DRAFT') {
|
|
|
|
|
autoActivate = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 13:41:32 +01:00
|
|
|
const round = await ctx.prisma.round.update({
|
|
|
|
|
where: { id },
|
|
|
|
|
data: {
|
|
|
|
|
...data,
|
2026-02-03 23:19:45 +01:00
|
|
|
...(autoActivate && { status: 'ACTIVE' }),
|
2026-01-30 13:41:32 +01:00
|
|
|
settingsJson: settingsJson as Prisma.InputJsonValue ?? undefined,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Audit log
|
|
|
|
|
await ctx.prisma.auditLog.create({
|
|
|
|
|
data: {
|
|
|
|
|
userId: ctx.user.id,
|
|
|
|
|
action: 'UPDATE',
|
|
|
|
|
entityType: 'Round',
|
|
|
|
|
entityId: id,
|
|
|
|
|
detailsJson: { ...data, settingsJson } as Prisma.InputJsonValue,
|
|
|
|
|
ipAddress: ctx.ip,
|
|
|
|
|
userAgent: ctx.userAgent,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return round
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update round status (admin only)
|
|
|
|
|
*/
|
|
|
|
|
updateStatus: adminProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
id: z.string(),
|
|
|
|
|
status: z.enum(['DRAFT', 'ACTIVE', 'CLOSED', 'ARCHIVED']),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
Implement Prototype 1 improvements: unified members, project filters, audit expansion, filtering rounds, special awards
- Unified Member Management: merge /admin/users and /admin/mentors into /admin/members with role tabs, search, pagination
- Project List Filters: add search, multi-status filter, round/category/country selects, boolean toggles, URL persistence
- Audit Log Expansion: track logins, round state changes, evaluation submissions, file access, role changes via shared logAudit utility
- Founding Date Field: add foundedAt to Project model with CSV import support
- Filtering Round System: configurable rules (field-based, document check, AI screening), execution engine, results review with override/reinstate
- Special Awards System: named awards with eligibility criteria, dedicated jury, PICK_WINNER/RANKED/SCORED voting modes, AI eligibility
- Dashboard resilience: wrap heavy queries in try-catch to prevent error boundary on transient DB failures
- Reusable pagination component extracted to src/components/shared/pagination.tsx
- Old /admin/users and /admin/mentors routes redirect to /admin/members
- Prisma migration for all schema additions (additive, no data loss)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 16:58:29 +01:00
|
|
|
// Get previous status for audit
|
|
|
|
|
const previousRound = await ctx.prisma.round.findUniqueOrThrow({
|
|
|
|
|
where: { id: input.id },
|
|
|
|
|
select: { status: true },
|
|
|
|
|
})
|
|
|
|
|
|
2026-01-30 13:41:32 +01:00
|
|
|
const round = await ctx.prisma.round.update({
|
|
|
|
|
where: { id: input.id },
|
|
|
|
|
data: { status: input.status },
|
|
|
|
|
})
|
|
|
|
|
|
Implement Prototype 1 improvements: unified members, project filters, audit expansion, filtering rounds, special awards
- Unified Member Management: merge /admin/users and /admin/mentors into /admin/members with role tabs, search, pagination
- Project List Filters: add search, multi-status filter, round/category/country selects, boolean toggles, URL persistence
- Audit Log Expansion: track logins, round state changes, evaluation submissions, file access, role changes via shared logAudit utility
- Founding Date Field: add foundedAt to Project model with CSV import support
- Filtering Round System: configurable rules (field-based, document check, AI screening), execution engine, results review with override/reinstate
- Special Awards System: named awards with eligibility criteria, dedicated jury, PICK_WINNER/RANKED/SCORED voting modes, AI eligibility
- Dashboard resilience: wrap heavy queries in try-catch to prevent error boundary on transient DB failures
- Reusable pagination component extracted to src/components/shared/pagination.tsx
- Old /admin/users and /admin/mentors routes redirect to /admin/members
- Prisma migration for all schema additions (additive, no data loss)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 16:58:29 +01:00
|
|
|
// Map status to specific action name
|
|
|
|
|
const statusActionMap: Record<string, string> = {
|
|
|
|
|
ACTIVE: 'ROUND_ACTIVATED',
|
|
|
|
|
CLOSED: 'ROUND_CLOSED',
|
|
|
|
|
ARCHIVED: 'ROUND_ARCHIVED',
|
|
|
|
|
}
|
|
|
|
|
const action = statusActionMap[input.status] || 'UPDATE_STATUS'
|
|
|
|
|
|
2026-01-30 13:41:32 +01:00
|
|
|
// Audit log
|
|
|
|
|
await ctx.prisma.auditLog.create({
|
|
|
|
|
data: {
|
|
|
|
|
userId: ctx.user.id,
|
Implement Prototype 1 improvements: unified members, project filters, audit expansion, filtering rounds, special awards
- Unified Member Management: merge /admin/users and /admin/mentors into /admin/members with role tabs, search, pagination
- Project List Filters: add search, multi-status filter, round/category/country selects, boolean toggles, URL persistence
- Audit Log Expansion: track logins, round state changes, evaluation submissions, file access, role changes via shared logAudit utility
- Founding Date Field: add foundedAt to Project model with CSV import support
- Filtering Round System: configurable rules (field-based, document check, AI screening), execution engine, results review with override/reinstate
- Special Awards System: named awards with eligibility criteria, dedicated jury, PICK_WINNER/RANKED/SCORED voting modes, AI eligibility
- Dashboard resilience: wrap heavy queries in try-catch to prevent error boundary on transient DB failures
- Reusable pagination component extracted to src/components/shared/pagination.tsx
- Old /admin/users and /admin/mentors routes redirect to /admin/members
- Prisma migration for all schema additions (additive, no data loss)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 16:58:29 +01:00
|
|
|
action,
|
2026-01-30 13:41:32 +01:00
|
|
|
entityType: 'Round',
|
|
|
|
|
entityId: input.id,
|
Implement Prototype 1 improvements: unified members, project filters, audit expansion, filtering rounds, special awards
- Unified Member Management: merge /admin/users and /admin/mentors into /admin/members with role tabs, search, pagination
- Project List Filters: add search, multi-status filter, round/category/country selects, boolean toggles, URL persistence
- Audit Log Expansion: track logins, round state changes, evaluation submissions, file access, role changes via shared logAudit utility
- Founding Date Field: add foundedAt to Project model with CSV import support
- Filtering Round System: configurable rules (field-based, document check, AI screening), execution engine, results review with override/reinstate
- Special Awards System: named awards with eligibility criteria, dedicated jury, PICK_WINNER/RANKED/SCORED voting modes, AI eligibility
- Dashboard resilience: wrap heavy queries in try-catch to prevent error boundary on transient DB failures
- Reusable pagination component extracted to src/components/shared/pagination.tsx
- Old /admin/users and /admin/mentors routes redirect to /admin/members
- Prisma migration for all schema additions (additive, no data loss)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 16:58:29 +01:00
|
|
|
detailsJson: { status: input.status, previousStatus: previousRound.status },
|
2026-01-30 13:41:32 +01:00
|
|
|
ipAddress: ctx.ip,
|
|
|
|
|
userAgent: ctx.userAgent,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return round
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if voting is currently open for a round
|
|
|
|
|
*/
|
|
|
|
|
isVotingOpen: protectedProcedure
|
|
|
|
|
.input(z.object({ id: z.string() }))
|
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
|
const round = await ctx.prisma.round.findUniqueOrThrow({
|
|
|
|
|
where: { id: input.id },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const now = new Date()
|
|
|
|
|
const isOpen =
|
|
|
|
|
round.status === 'ACTIVE' &&
|
|
|
|
|
round.votingStartAt !== null &&
|
|
|
|
|
round.votingEndAt !== null &&
|
|
|
|
|
now >= round.votingStartAt &&
|
|
|
|
|
now <= round.votingEndAt
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
isOpen,
|
|
|
|
|
startsAt: round.votingStartAt,
|
|
|
|
|
endsAt: round.votingEndAt,
|
|
|
|
|
status: round.status,
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get round progress statistics
|
|
|
|
|
*/
|
|
|
|
|
getProgress: protectedProcedure
|
|
|
|
|
.input(z.object({ id: z.string() }))
|
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
|
const [totalProjects, totalAssignments, completedAssignments] =
|
|
|
|
|
await Promise.all([
|
2026-02-02 22:33:55 +01:00
|
|
|
ctx.prisma.roundProject.count({ where: { roundId: input.id } }),
|
2026-01-30 13:41:32 +01:00
|
|
|
ctx.prisma.assignment.count({ where: { roundId: input.id } }),
|
|
|
|
|
ctx.prisma.assignment.count({
|
|
|
|
|
where: { roundId: input.id, isCompleted: true },
|
|
|
|
|
}),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const evaluationsByStatus = await ctx.prisma.evaluation.groupBy({
|
|
|
|
|
by: ['status'],
|
|
|
|
|
where: {
|
|
|
|
|
assignment: { roundId: input.id },
|
|
|
|
|
},
|
|
|
|
|
_count: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
totalProjects,
|
|
|
|
|
totalAssignments,
|
|
|
|
|
completedAssignments,
|
|
|
|
|
completionPercentage:
|
|
|
|
|
totalAssignments > 0
|
|
|
|
|
? Math.round((completedAssignments / totalAssignments) * 100)
|
|
|
|
|
: 0,
|
|
|
|
|
evaluationsByStatus: evaluationsByStatus.reduce(
|
|
|
|
|
(acc, curr) => {
|
|
|
|
|
acc[curr.status] = curr._count
|
|
|
|
|
return acc
|
|
|
|
|
},
|
|
|
|
|
{} as Record<string, number>
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update or create evaluation form for a round (admin only)
|
|
|
|
|
*/
|
|
|
|
|
updateEvaluationForm: adminProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
roundId: z.string(),
|
|
|
|
|
criteria: z.array(
|
|
|
|
|
z.object({
|
|
|
|
|
id: z.string(),
|
|
|
|
|
label: z.string().min(1),
|
|
|
|
|
description: z.string().optional(),
|
|
|
|
|
scale: z.number().int().min(1).max(10),
|
|
|
|
|
weight: z.number().optional(),
|
|
|
|
|
required: z.boolean(),
|
|
|
|
|
})
|
|
|
|
|
),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
|
|
|
|
const { roundId, criteria } = input
|
|
|
|
|
|
|
|
|
|
// Check if there are existing evaluations
|
|
|
|
|
const existingEvaluations = await ctx.prisma.evaluation.count({
|
|
|
|
|
where: {
|
|
|
|
|
assignment: { roundId },
|
|
|
|
|
status: { in: ['SUBMITTED', 'LOCKED'] },
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (existingEvaluations > 0) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'BAD_REQUEST',
|
|
|
|
|
message: 'Cannot modify criteria after evaluations have been submitted',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get or create the active evaluation form
|
|
|
|
|
const existingForm = await ctx.prisma.evaluationForm.findFirst({
|
|
|
|
|
where: { roundId, isActive: true },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
let form
|
|
|
|
|
|
|
|
|
|
if (existingForm) {
|
|
|
|
|
// Update existing form
|
|
|
|
|
form = await ctx.prisma.evaluationForm.update({
|
|
|
|
|
where: { id: existingForm.id },
|
|
|
|
|
data: { criteriaJson: criteria },
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
// Create new form
|
|
|
|
|
form = await ctx.prisma.evaluationForm.create({
|
|
|
|
|
data: {
|
|
|
|
|
roundId,
|
|
|
|
|
criteriaJson: criteria,
|
|
|
|
|
isActive: true,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Audit log
|
|
|
|
|
await ctx.prisma.auditLog.create({
|
|
|
|
|
data: {
|
|
|
|
|
userId: ctx.user.id,
|
|
|
|
|
action: 'UPDATE_EVALUATION_FORM',
|
|
|
|
|
entityType: 'EvaluationForm',
|
|
|
|
|
entityId: form.id,
|
|
|
|
|
detailsJson: { roundId, criteriaCount: criteria.length },
|
|
|
|
|
ipAddress: ctx.ip,
|
|
|
|
|
userAgent: ctx.userAgent,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return form
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get evaluation form for a round
|
|
|
|
|
*/
|
|
|
|
|
getEvaluationForm: protectedProcedure
|
|
|
|
|
.input(z.object({ roundId: z.string() }))
|
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
|
return ctx.prisma.evaluationForm.findFirst({
|
|
|
|
|
where: { roundId: input.roundId, isActive: true },
|
|
|
|
|
})
|
|
|
|
|
}),
|
|
|
|
|
|
2026-01-30 19:28:57 +01:00
|
|
|
/**
|
|
|
|
|
* Delete a round (admin only)
|
|
|
|
|
* Cascades to projects, assignments, evaluations, etc.
|
|
|
|
|
*/
|
|
|
|
|
delete: adminProcedure
|
|
|
|
|
.input(z.object({ id: z.string() }))
|
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
|
|
|
|
const round = await ctx.prisma.round.findUniqueOrThrow({
|
|
|
|
|
where: { id: input.id },
|
|
|
|
|
include: {
|
2026-02-02 22:33:55 +01:00
|
|
|
_count: { select: { roundProjects: true, assignments: true } },
|
2026-01-30 19:28:57 +01:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await ctx.prisma.round.delete({
|
|
|
|
|
where: { id: input.id },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Audit log
|
|
|
|
|
await ctx.prisma.auditLog.create({
|
|
|
|
|
data: {
|
|
|
|
|
userId: ctx.user.id,
|
|
|
|
|
action: 'DELETE',
|
|
|
|
|
entityType: 'Round',
|
|
|
|
|
entityId: input.id,
|
|
|
|
|
detailsJson: {
|
|
|
|
|
name: round.name,
|
|
|
|
|
status: round.status,
|
2026-02-02 22:33:55 +01:00
|
|
|
projectsDeleted: round._count.roundProjects,
|
2026-01-30 19:28:57 +01:00
|
|
|
assignmentsDeleted: round._count.assignments,
|
|
|
|
|
},
|
|
|
|
|
ipAddress: ctx.ip,
|
|
|
|
|
userAgent: ctx.userAgent,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return round
|
|
|
|
|
}),
|
|
|
|
|
|
2026-01-30 13:41:32 +01:00
|
|
|
/**
|
|
|
|
|
* Check if a round has any submitted evaluations
|
|
|
|
|
*/
|
|
|
|
|
hasEvaluations: adminProcedure
|
|
|
|
|
.input(z.object({ roundId: z.string() }))
|
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
|
const count = await ctx.prisma.evaluation.count({
|
|
|
|
|
where: {
|
|
|
|
|
assignment: { roundId: input.roundId },
|
|
|
|
|
status: { in: ['SUBMITTED', 'LOCKED'] },
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return count > 0
|
|
|
|
|
}),
|
2026-02-02 22:33:55 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assign projects from the program pool to a round
|
|
|
|
|
*/
|
|
|
|
|
assignProjects: adminProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
roundId: z.string(),
|
|
|
|
|
projectIds: z.array(z.string()).min(1),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
|
|
|
|
// Verify round exists and get programId
|
|
|
|
|
const round = await ctx.prisma.round.findUniqueOrThrow({
|
|
|
|
|
where: { id: input.roundId },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Verify all projects belong to the same program
|
|
|
|
|
const projects = await ctx.prisma.project.findMany({
|
|
|
|
|
where: { id: { in: input.projectIds }, programId: round.programId },
|
|
|
|
|
select: { id: true },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (projects.length !== input.projectIds.length) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'BAD_REQUEST',
|
|
|
|
|
message: 'Some projects do not belong to this program',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create RoundProject entries (skip duplicates)
|
|
|
|
|
const created = await ctx.prisma.roundProject.createMany({
|
|
|
|
|
data: input.projectIds.map((projectId) => ({
|
|
|
|
|
roundId: input.roundId,
|
|
|
|
|
projectId,
|
|
|
|
|
status: 'SUBMITTED' as const,
|
|
|
|
|
})),
|
|
|
|
|
skipDuplicates: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Audit log
|
|
|
|
|
await ctx.prisma.auditLog.create({
|
|
|
|
|
data: {
|
|
|
|
|
userId: ctx.user.id,
|
|
|
|
|
action: 'ASSIGN_PROJECTS_TO_ROUND',
|
|
|
|
|
entityType: 'Round',
|
|
|
|
|
entityId: input.roundId,
|
|
|
|
|
detailsJson: { projectCount: created.count },
|
|
|
|
|
ipAddress: ctx.ip,
|
|
|
|
|
userAgent: ctx.userAgent,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return { assigned: created.count }
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove projects from a round
|
|
|
|
|
*/
|
|
|
|
|
removeProjects: adminProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
roundId: z.string(),
|
|
|
|
|
projectIds: z.array(z.string()).min(1),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
|
|
|
|
const deleted = await ctx.prisma.roundProject.deleteMany({
|
|
|
|
|
where: {
|
|
|
|
|
roundId: input.roundId,
|
|
|
|
|
projectId: { in: input.projectIds },
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Audit log
|
|
|
|
|
await ctx.prisma.auditLog.create({
|
|
|
|
|
data: {
|
|
|
|
|
userId: ctx.user.id,
|
|
|
|
|
action: 'REMOVE_PROJECTS_FROM_ROUND',
|
|
|
|
|
entityType: 'Round',
|
|
|
|
|
entityId: input.roundId,
|
|
|
|
|
detailsJson: { projectCount: deleted.count },
|
|
|
|
|
ipAddress: ctx.ip,
|
|
|
|
|
userAgent: ctx.userAgent,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return { removed: deleted.count }
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Advance projects from one round to the next
|
|
|
|
|
* Creates new RoundProject entries in the target round (keeps them in source round too)
|
|
|
|
|
*/
|
|
|
|
|
advanceProjects: adminProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
fromRoundId: z.string(),
|
|
|
|
|
toRoundId: z.string(),
|
|
|
|
|
projectIds: z.array(z.string()).min(1),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
|
|
|
|
// Verify both rounds exist and belong to the same program
|
|
|
|
|
const [fromRound, toRound] = await Promise.all([
|
|
|
|
|
ctx.prisma.round.findUniqueOrThrow({ where: { id: input.fromRoundId } }),
|
|
|
|
|
ctx.prisma.round.findUniqueOrThrow({ where: { id: input.toRoundId } }),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
if (fromRound.programId !== toRound.programId) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'BAD_REQUEST',
|
|
|
|
|
message: 'Rounds must belong to the same program',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify all projects are in the source round
|
|
|
|
|
const sourceProjects = await ctx.prisma.roundProject.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
roundId: input.fromRoundId,
|
|
|
|
|
projectId: { in: input.projectIds },
|
|
|
|
|
},
|
|
|
|
|
select: { projectId: true },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (sourceProjects.length !== input.projectIds.length) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'BAD_REQUEST',
|
|
|
|
|
message: 'Some projects are not in the source round',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create entries in target round (skip duplicates)
|
|
|
|
|
const created = await ctx.prisma.roundProject.createMany({
|
|
|
|
|
data: input.projectIds.map((projectId) => ({
|
|
|
|
|
roundId: input.toRoundId,
|
|
|
|
|
projectId,
|
|
|
|
|
status: 'SUBMITTED' as const,
|
|
|
|
|
})),
|
|
|
|
|
skipDuplicates: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Audit log
|
|
|
|
|
await ctx.prisma.auditLog.create({
|
|
|
|
|
data: {
|
|
|
|
|
userId: ctx.user.id,
|
|
|
|
|
action: 'ADVANCE_PROJECTS',
|
|
|
|
|
entityType: 'Round',
|
|
|
|
|
entityId: input.toRoundId,
|
|
|
|
|
detailsJson: {
|
|
|
|
|
fromRoundId: input.fromRoundId,
|
|
|
|
|
toRoundId: input.toRoundId,
|
|
|
|
|
projectCount: created.count,
|
|
|
|
|
},
|
|
|
|
|
ipAddress: ctx.ip,
|
|
|
|
|
userAgent: ctx.userAgent,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return { advanced: created.count }
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reorder rounds within a program
|
|
|
|
|
*/
|
|
|
|
|
reorder: adminProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
programId: z.string(),
|
|
|
|
|
roundIds: z.array(z.string()).min(1),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
|
|
|
|
// Update sortOrder for each round based on array position
|
|
|
|
|
await ctx.prisma.$transaction(
|
|
|
|
|
input.roundIds.map((roundId, index) =>
|
|
|
|
|
ctx.prisma.round.update({
|
|
|
|
|
where: { id: roundId },
|
|
|
|
|
data: { sortOrder: index },
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Audit log
|
|
|
|
|
await ctx.prisma.auditLog.create({
|
|
|
|
|
data: {
|
|
|
|
|
userId: ctx.user.id,
|
|
|
|
|
action: 'REORDER_ROUNDS',
|
|
|
|
|
entityType: 'Program',
|
|
|
|
|
entityId: input.programId,
|
|
|
|
|
detailsJson: { roundIds: input.roundIds },
|
|
|
|
|
ipAddress: ctx.ip,
|
|
|
|
|
userAgent: ctx.userAgent,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return { success: true }
|
|
|
|
|
}),
|
2026-01-30 13:41:32 +01:00
|
|
|
})
|