2026-02-14 15:26:42 +01:00
|
|
|
import { z } from 'zod'
|
|
|
|
|
import { TRPCError } from '@trpc/server'
|
|
|
|
|
import { router, adminProcedure } from '../trpc'
|
|
|
|
|
import { logAudit } from '../utils/audit'
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Project Pool Router
|
|
|
|
|
*
|
|
|
|
|
* Manages the pool of unassigned projects (projects not yet assigned to any stage).
|
|
|
|
|
* Provides procedures for listing unassigned projects and bulk assigning them to stages.
|
|
|
|
|
*/
|
|
|
|
|
export const projectPoolRouter = router({
|
|
|
|
|
/**
|
|
|
|
|
* List unassigned projects with filtering and pagination
|
Competition/Round architecture: full platform rewrite (Phases 1-9)
Replace Pipeline/Stage system with Competition/Round architecture.
New schema: Competition, Round (7 types), JuryGroup, AssignmentPolicy,
ProjectRoundState, DeliberationSession, ResultLock, SubmissionWindow.
New services: round-engine, round-assignment, deliberation, result-lock,
submission-manager, competition-context, ai-prompt-guard.
Full admin/jury/applicant/mentor UI rewrite. AI prompt hardening with
structured prompts, retry logic, and injection detection. All legacy
pipeline/stage code removed. 4 new migrations + seed aligned.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 23:04:15 +01:00
|
|
|
* Projects not assigned to any round
|
2026-02-14 15:26:42 +01:00
|
|
|
*/
|
|
|
|
|
listUnassigned: adminProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
programId: z.string(), // Required - must specify which program
|
|
|
|
|
competitionCategory: z
|
|
|
|
|
.enum(['STARTUP', 'BUSINESS_CONCEPT'])
|
|
|
|
|
.optional(),
|
|
|
|
|
search: z.string().optional(), // Search in title, teamName, description
|
|
|
|
|
page: z.number().int().min(1).default(1),
|
|
|
|
|
perPage: z.number().int().min(1).max(200).default(20),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
|
const { programId, competitionCategory, search, page, perPage } = input
|
|
|
|
|
const skip = (page - 1) * perPage
|
|
|
|
|
|
|
|
|
|
// Build where clause
|
|
|
|
|
const where: Record<string, unknown> = {
|
|
|
|
|
programId,
|
Competition/Round architecture: full platform rewrite (Phases 1-9)
Replace Pipeline/Stage system with Competition/Round architecture.
New schema: Competition, Round (7 types), JuryGroup, AssignmentPolicy,
ProjectRoundState, DeliberationSession, ResultLock, SubmissionWindow.
New services: round-engine, round-assignment, deliberation, result-lock,
submission-manager, competition-context, ai-prompt-guard.
Full admin/jury/applicant/mentor UI rewrite. AI prompt hardening with
structured prompts, retry logic, and injection detection. All legacy
pipeline/stage code removed. 4 new migrations + seed aligned.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 23:04:15 +01:00
|
|
|
projectRoundStates: { none: {} }, // Only unassigned projects (not in any round)
|
2026-02-14 15:26:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Filter by competition category
|
|
|
|
|
if (competitionCategory) {
|
|
|
|
|
where.competitionCategory = competitionCategory
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Search in title, teamName, description
|
|
|
|
|
if (search) {
|
|
|
|
|
where.OR = [
|
|
|
|
|
{ title: { contains: search, mode: 'insensitive' } },
|
|
|
|
|
{ teamName: { contains: search, mode: 'insensitive' } },
|
|
|
|
|
{ description: { contains: search, mode: 'insensitive' } },
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Execute queries in parallel
|
|
|
|
|
const [projects, total] = await Promise.all([
|
|
|
|
|
ctx.prisma.project.findMany({
|
|
|
|
|
where,
|
|
|
|
|
skip,
|
|
|
|
|
take: perPage,
|
|
|
|
|
orderBy: { createdAt: 'desc' },
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
title: true,
|
|
|
|
|
teamName: true,
|
|
|
|
|
description: true,
|
|
|
|
|
competitionCategory: true,
|
|
|
|
|
oceanIssue: true,
|
|
|
|
|
country: true,
|
|
|
|
|
status: true,
|
|
|
|
|
submittedAt: true,
|
|
|
|
|
createdAt: true,
|
|
|
|
|
tags: true,
|
|
|
|
|
wantsMentorship: true,
|
|
|
|
|
programId: true,
|
|
|
|
|
_count: {
|
|
|
|
|
select: {
|
|
|
|
|
files: true,
|
|
|
|
|
teamMembers: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
ctx.prisma.project.count({ where }),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
projects,
|
|
|
|
|
total,
|
|
|
|
|
page,
|
|
|
|
|
perPage,
|
|
|
|
|
totalPages: Math.ceil(total / perPage),
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
/**
|
Competition/Round architecture: full platform rewrite (Phases 1-9)
Replace Pipeline/Stage system with Competition/Round architecture.
New schema: Competition, Round (7 types), JuryGroup, AssignmentPolicy,
ProjectRoundState, DeliberationSession, ResultLock, SubmissionWindow.
New services: round-engine, round-assignment, deliberation, result-lock,
submission-manager, competition-context, ai-prompt-guard.
Full admin/jury/applicant/mentor UI rewrite. AI prompt hardening with
structured prompts, retry logic, and injection detection. All legacy
pipeline/stage code removed. 4 new migrations + seed aligned.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 23:04:15 +01:00
|
|
|
* Bulk assign projects to a round
|
2026-02-14 15:26:42 +01:00
|
|
|
*
|
|
|
|
|
* Validates that:
|
|
|
|
|
* - All projects exist
|
Competition/Round architecture: full platform rewrite (Phases 1-9)
Replace Pipeline/Stage system with Competition/Round architecture.
New schema: Competition, Round (7 types), JuryGroup, AssignmentPolicy,
ProjectRoundState, DeliberationSession, ResultLock, SubmissionWindow.
New services: round-engine, round-assignment, deliberation, result-lock,
submission-manager, competition-context, ai-prompt-guard.
Full admin/jury/applicant/mentor UI rewrite. AI prompt hardening with
structured prompts, retry logic, and injection detection. All legacy
pipeline/stage code removed. 4 new migrations + seed aligned.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 23:04:15 +01:00
|
|
|
* - Round exists
|
2026-02-14 15:26:42 +01:00
|
|
|
*
|
|
|
|
|
* Creates:
|
Competition/Round architecture: full platform rewrite (Phases 1-9)
Replace Pipeline/Stage system with Competition/Round architecture.
New schema: Competition, Round (7 types), JuryGroup, AssignmentPolicy,
ProjectRoundState, DeliberationSession, ResultLock, SubmissionWindow.
New services: round-engine, round-assignment, deliberation, result-lock,
submission-manager, competition-context, ai-prompt-guard.
Full admin/jury/applicant/mentor UI rewrite. AI prompt hardening with
structured prompts, retry logic, and injection detection. All legacy
pipeline/stage code removed. 4 new migrations + seed aligned.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 23:04:15 +01:00
|
|
|
* - RoundAssignment entries for each project
|
2026-02-14 15:26:42 +01:00
|
|
|
* - Project.status updated to 'ASSIGNED'
|
|
|
|
|
* - ProjectStatusHistory records for each project
|
|
|
|
|
* - Audit log
|
|
|
|
|
*/
|
Competition/Round architecture: full platform rewrite (Phases 1-9)
Replace Pipeline/Stage system with Competition/Round architecture.
New schema: Competition, Round (7 types), JuryGroup, AssignmentPolicy,
ProjectRoundState, DeliberationSession, ResultLock, SubmissionWindow.
New services: round-engine, round-assignment, deliberation, result-lock,
submission-manager, competition-context, ai-prompt-guard.
Full admin/jury/applicant/mentor UI rewrite. AI prompt hardening with
structured prompts, retry logic, and injection detection. All legacy
pipeline/stage code removed. 4 new migrations + seed aligned.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 23:04:15 +01:00
|
|
|
assignToRound: adminProcedure
|
2026-02-14 15:26:42 +01:00
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
projectIds: z.array(z.string()).min(1).max(200), // Max 200 projects at once
|
Competition/Round architecture: full platform rewrite (Phases 1-9)
Replace Pipeline/Stage system with Competition/Round architecture.
New schema: Competition, Round (7 types), JuryGroup, AssignmentPolicy,
ProjectRoundState, DeliberationSession, ResultLock, SubmissionWindow.
New services: round-engine, round-assignment, deliberation, result-lock,
submission-manager, competition-context, ai-prompt-guard.
Full admin/jury/applicant/mentor UI rewrite. AI prompt hardening with
structured prompts, retry logic, and injection detection. All legacy
pipeline/stage code removed. 4 new migrations + seed aligned.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 23:04:15 +01:00
|
|
|
roundId: z.string(),
|
2026-02-14 15:26:42 +01:00
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
Competition/Round architecture: full platform rewrite (Phases 1-9)
Replace Pipeline/Stage system with Competition/Round architecture.
New schema: Competition, Round (7 types), JuryGroup, AssignmentPolicy,
ProjectRoundState, DeliberationSession, ResultLock, SubmissionWindow.
New services: round-engine, round-assignment, deliberation, result-lock,
submission-manager, competition-context, ai-prompt-guard.
Full admin/jury/applicant/mentor UI rewrite. AI prompt hardening with
structured prompts, retry logic, and injection detection. All legacy
pipeline/stage code removed. 4 new migrations + seed aligned.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 23:04:15 +01:00
|
|
|
const { projectIds, roundId } = input
|
2026-02-14 15:26:42 +01:00
|
|
|
|
|
|
|
|
// Step 1: Fetch all projects to validate
|
|
|
|
|
const projects = await ctx.prisma.project.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
id: { in: projectIds },
|
|
|
|
|
},
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
title: true,
|
|
|
|
|
programId: true,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Validate all projects were found
|
|
|
|
|
if (projects.length !== projectIds.length) {
|
|
|
|
|
const foundIds = new Set(projects.map((p) => p.id))
|
|
|
|
|
const missingIds = projectIds.filter((id) => !foundIds.has(id))
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'BAD_REQUEST',
|
|
|
|
|
message: `Some projects were not found: ${missingIds.join(', ')}`,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
Competition/Round architecture: full platform rewrite (Phases 1-9)
Replace Pipeline/Stage system with Competition/Round architecture.
New schema: Competition, Round (7 types), JuryGroup, AssignmentPolicy,
ProjectRoundState, DeliberationSession, ResultLock, SubmissionWindow.
New services: round-engine, round-assignment, deliberation, result-lock,
submission-manager, competition-context, ai-prompt-guard.
Full admin/jury/applicant/mentor UI rewrite. AI prompt hardening with
structured prompts, retry logic, and injection detection. All legacy
pipeline/stage code removed. 4 new migrations + seed aligned.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 23:04:15 +01:00
|
|
|
// Verify round exists
|
|
|
|
|
const round = await ctx.prisma.round.findUniqueOrThrow({
|
|
|
|
|
where: { id: roundId },
|
|
|
|
|
select: { id: true },
|
2026-02-14 15:26:42 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Step 2: Perform bulk assignment in a transaction
|
|
|
|
|
const result = await ctx.prisma.$transaction(async (tx) => {
|
Competition/Round architecture: full platform rewrite (Phases 1-9)
Replace Pipeline/Stage system with Competition/Round architecture.
New schema: Competition, Round (7 types), JuryGroup, AssignmentPolicy,
ProjectRoundState, DeliberationSession, ResultLock, SubmissionWindow.
New services: round-engine, round-assignment, deliberation, result-lock,
submission-manager, competition-context, ai-prompt-guard.
Full admin/jury/applicant/mentor UI rewrite. AI prompt hardening with
structured prompts, retry logic, and injection detection. All legacy
pipeline/stage code removed. 4 new migrations + seed aligned.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 23:04:15 +01:00
|
|
|
// Create ProjectRoundState entries for each project (skip existing)
|
|
|
|
|
const assignmentData = projectIds.map((projectId) => ({
|
2026-02-14 15:26:42 +01:00
|
|
|
projectId,
|
Competition/Round architecture: full platform rewrite (Phases 1-9)
Replace Pipeline/Stage system with Competition/Round architecture.
New schema: Competition, Round (7 types), JuryGroup, AssignmentPolicy,
ProjectRoundState, DeliberationSession, ResultLock, SubmissionWindow.
New services: round-engine, round-assignment, deliberation, result-lock,
submission-manager, competition-context, ai-prompt-guard.
Full admin/jury/applicant/mentor UI rewrite. AI prompt hardening with
structured prompts, retry logic, and injection detection. All legacy
pipeline/stage code removed. 4 new migrations + seed aligned.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 23:04:15 +01:00
|
|
|
roundId,
|
2026-02-14 15:26:42 +01:00
|
|
|
}))
|
|
|
|
|
|
Competition/Round architecture: full platform rewrite (Phases 1-9)
Replace Pipeline/Stage system with Competition/Round architecture.
New schema: Competition, Round (7 types), JuryGroup, AssignmentPolicy,
ProjectRoundState, DeliberationSession, ResultLock, SubmissionWindow.
New services: round-engine, round-assignment, deliberation, result-lock,
submission-manager, competition-context, ai-prompt-guard.
Full admin/jury/applicant/mentor UI rewrite. AI prompt hardening with
structured prompts, retry logic, and injection detection. All legacy
pipeline/stage code removed. 4 new migrations + seed aligned.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 23:04:15 +01:00
|
|
|
await tx.projectRoundState.createMany({
|
|
|
|
|
data: assignmentData,
|
2026-02-14 15:26:42 +01:00
|
|
|
skipDuplicates: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Update project statuses
|
|
|
|
|
const updatedProjects = await tx.project.updateMany({
|
|
|
|
|
where: {
|
|
|
|
|
id: { in: projectIds },
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
status: 'ASSIGNED',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create status history records for each project
|
|
|
|
|
await tx.projectStatusHistory.createMany({
|
|
|
|
|
data: projectIds.map((projectId) => ({
|
|
|
|
|
projectId,
|
|
|
|
|
status: 'ASSIGNED',
|
|
|
|
|
changedBy: ctx.user?.id,
|
|
|
|
|
})),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return updatedProjects
|
|
|
|
|
})
|
|
|
|
|
|
Round detail overhaul, file requirements, project management, audit log fix
- Redesign round detail page with 6 tabs (overview, projects, filtering, assignments, config, documents)
- Add jury group assignment selector in round stats bar
- Add FileRequirementsEditor component replacing SubmissionWindowManager
- Add FilteringDashboard component for AI-powered project screening
- Add project removal from rounds (single + bulk) with cascading to subsequent rounds
- Add project add/remove UI in ProjectStatesTable with confirmation dialogs
- Fix logAudit inside $transaction pattern across all 12 router files
(PostgreSQL aborted-transaction state caused silent operation failures)
- Fix special awards creation, deletion, status update, and winner assignment
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 07:49:39 +01:00
|
|
|
// Audit outside transaction so failures don't roll back the assignment
|
|
|
|
|
await logAudit({
|
|
|
|
|
prisma: ctx.prisma,
|
|
|
|
|
userId: ctx.user?.id,
|
|
|
|
|
action: 'BULK_ASSIGN_TO_ROUND',
|
|
|
|
|
entityType: 'Project',
|
|
|
|
|
detailsJson: {
|
|
|
|
|
roundId,
|
|
|
|
|
projectCount: projectIds.length,
|
|
|
|
|
projectIds,
|
|
|
|
|
},
|
|
|
|
|
ipAddress: ctx.ip,
|
|
|
|
|
userAgent: ctx.userAgent,
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-14 15:26:42 +01:00
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
assignedCount: result.count,
|
Competition/Round architecture: full platform rewrite (Phases 1-9)
Replace Pipeline/Stage system with Competition/Round architecture.
New schema: Competition, Round (7 types), JuryGroup, AssignmentPolicy,
ProjectRoundState, DeliberationSession, ResultLock, SubmissionWindow.
New services: round-engine, round-assignment, deliberation, result-lock,
submission-manager, competition-context, ai-prompt-guard.
Full admin/jury/applicant/mentor UI rewrite. AI prompt hardening with
structured prompts, retry logic, and injection detection. All legacy
pipeline/stage code removed. 4 new migrations + seed aligned.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 23:04:15 +01:00
|
|
|
roundId,
|
2026-02-14 15:26:42 +01:00
|
|
|
}
|
|
|
|
|
}),
|
2026-02-16 07:06:59 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assign ALL unassigned projects in a program to a round (server-side, no ID limit)
|
|
|
|
|
*/
|
|
|
|
|
assignAllToRound: adminProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
programId: z.string(),
|
|
|
|
|
roundId: z.string(),
|
|
|
|
|
competitionCategory: z.enum(['STARTUP', 'BUSINESS_CONCEPT']).optional(),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
|
|
|
|
const { programId, roundId, competitionCategory } = input
|
|
|
|
|
|
|
|
|
|
// Verify round exists
|
|
|
|
|
await ctx.prisma.round.findUniqueOrThrow({
|
|
|
|
|
where: { id: roundId },
|
|
|
|
|
select: { id: true },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Find all unassigned projects
|
|
|
|
|
const where: Record<string, unknown> = {
|
|
|
|
|
programId,
|
|
|
|
|
projectRoundStates: { none: {} },
|
|
|
|
|
}
|
|
|
|
|
if (competitionCategory) {
|
|
|
|
|
where.competitionCategory = competitionCategory
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const projects = await ctx.prisma.project.findMany({
|
|
|
|
|
where,
|
|
|
|
|
select: { id: true },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (projects.length === 0) {
|
|
|
|
|
return { success: true, assignedCount: 0, roundId }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const projectIds = projects.map((p) => p.id)
|
|
|
|
|
|
|
|
|
|
const result = await ctx.prisma.$transaction(async (tx) => {
|
|
|
|
|
await tx.projectRoundState.createMany({
|
|
|
|
|
data: projectIds.map((projectId) => ({ projectId, roundId })),
|
|
|
|
|
skipDuplicates: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const updated = await tx.project.updateMany({
|
|
|
|
|
where: { id: { in: projectIds } },
|
|
|
|
|
data: { status: 'ASSIGNED' },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await tx.projectStatusHistory.createMany({
|
|
|
|
|
data: projectIds.map((projectId) => ({
|
|
|
|
|
projectId,
|
|
|
|
|
status: 'ASSIGNED',
|
|
|
|
|
changedBy: ctx.user?.id,
|
|
|
|
|
})),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return updated
|
|
|
|
|
})
|
|
|
|
|
|
Round detail overhaul, file requirements, project management, audit log fix
- Redesign round detail page with 6 tabs (overview, projects, filtering, assignments, config, documents)
- Add jury group assignment selector in round stats bar
- Add FileRequirementsEditor component replacing SubmissionWindowManager
- Add FilteringDashboard component for AI-powered project screening
- Add project removal from rounds (single + bulk) with cascading to subsequent rounds
- Add project add/remove UI in ProjectStatesTable with confirmation dialogs
- Fix logAudit inside $transaction pattern across all 12 router files
(PostgreSQL aborted-transaction state caused silent operation failures)
- Fix special awards creation, deletion, status update, and winner assignment
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 07:49:39 +01:00
|
|
|
// Audit outside transaction so failures don't roll back the assignment
|
|
|
|
|
await logAudit({
|
|
|
|
|
prisma: ctx.prisma,
|
|
|
|
|
userId: ctx.user?.id,
|
|
|
|
|
action: 'BULK_ASSIGN_ALL_TO_ROUND',
|
|
|
|
|
entityType: 'Project',
|
|
|
|
|
detailsJson: {
|
|
|
|
|
roundId,
|
|
|
|
|
programId,
|
|
|
|
|
competitionCategory: competitionCategory || 'ALL',
|
|
|
|
|
projectCount: projectIds.length,
|
|
|
|
|
},
|
|
|
|
|
ipAddress: ctx.ip,
|
|
|
|
|
userAgent: ctx.userAgent,
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-16 07:06:59 +01:00
|
|
|
return { success: true, assignedCount: result.count, roundId }
|
|
|
|
|
}),
|
2026-02-14 15:26:42 +01:00
|
|
|
})
|