2026-01-30 13:41:32 +01:00
|
|
|
import { z } from 'zod'
|
|
|
|
|
import { router, adminProcedure } from '../trpc'
|
2026-02-05 21:09:06 +01:00
|
|
|
import { logAudit } from '../utils/audit'
|
2026-01-30 13:41:32 +01:00
|
|
|
|
|
|
|
|
export const exportRouter = router({
|
|
|
|
|
/**
|
|
|
|
|
* Export evaluations as CSV data
|
|
|
|
|
*/
|
|
|
|
|
evaluations: adminProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
roundId: z.string(),
|
|
|
|
|
includeDetails: z.boolean().default(true),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
|
const evaluations = await ctx.prisma.evaluation.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
status: 'SUBMITTED',
|
|
|
|
|
assignment: { roundId: input.roundId },
|
|
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
assignment: {
|
|
|
|
|
include: {
|
|
|
|
|
user: { select: { name: true, email: true } },
|
|
|
|
|
project: { select: { title: true, teamName: true, tags: true } },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
form: { select: { criteriaJson: true } },
|
|
|
|
|
},
|
|
|
|
|
orderBy: [
|
|
|
|
|
{ assignment: { project: { title: 'asc' } } },
|
|
|
|
|
{ submittedAt: 'asc' },
|
|
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Get criteria labels from form
|
|
|
|
|
const criteriaLabels: Record<string, string> = {}
|
|
|
|
|
if (evaluations.length > 0) {
|
|
|
|
|
const criteria = evaluations[0].form.criteriaJson as Array<{
|
|
|
|
|
id: string
|
|
|
|
|
label: string
|
|
|
|
|
}>
|
|
|
|
|
criteria.forEach((c) => {
|
|
|
|
|
criteriaLabels[c.id] = c.label
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build export data
|
|
|
|
|
const data = evaluations.map((e) => {
|
|
|
|
|
const scores = e.criterionScoresJson as Record<string, number> | null
|
|
|
|
|
const criteriaScores: Record<string, number | null> = {}
|
|
|
|
|
|
|
|
|
|
Object.keys(criteriaLabels).forEach((id) => {
|
|
|
|
|
criteriaScores[criteriaLabels[id]] = scores?.[id] ?? null
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
projectTitle: e.assignment.project.title,
|
|
|
|
|
teamName: e.assignment.project.teamName,
|
|
|
|
|
tags: e.assignment.project.tags.join(', '),
|
|
|
|
|
jurorName: e.assignment.user.name,
|
|
|
|
|
jurorEmail: e.assignment.user.email,
|
|
|
|
|
...criteriaScores,
|
|
|
|
|
globalScore: e.globalScore,
|
|
|
|
|
decision: e.binaryDecision ? 'Yes' : 'No',
|
|
|
|
|
feedback: input.includeDetails ? e.feedbackText : null,
|
|
|
|
|
submittedAt: e.submittedAt?.toISOString(),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Audit log
|
2026-02-05 21:09:06 +01:00
|
|
|
await logAudit({
|
|
|
|
|
prisma: ctx.prisma,
|
|
|
|
|
userId: ctx.user.id,
|
|
|
|
|
action: 'EXPORT',
|
|
|
|
|
entityType: 'Evaluation',
|
|
|
|
|
detailsJson: { roundId: input.roundId, count: data.length },
|
|
|
|
|
ipAddress: ctx.ip,
|
|
|
|
|
userAgent: ctx.userAgent,
|
2026-01-30 13:41:32 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
data,
|
|
|
|
|
columns: [
|
|
|
|
|
'projectTitle',
|
|
|
|
|
'teamName',
|
|
|
|
|
'tags',
|
|
|
|
|
'jurorName',
|
|
|
|
|
'jurorEmail',
|
|
|
|
|
...Object.values(criteriaLabels),
|
|
|
|
|
'globalScore',
|
|
|
|
|
'decision',
|
|
|
|
|
...(input.includeDetails ? ['feedback'] : []),
|
|
|
|
|
'submittedAt',
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Export project scores summary
|
|
|
|
|
*/
|
|
|
|
|
projectScores: adminProcedure
|
|
|
|
|
.input(z.object({ roundId: z.string() }))
|
|
|
|
|
.query(async ({ ctx, input }) => {
|
2026-02-04 14:15:06 +01:00
|
|
|
const projects = await ctx.prisma.project.findMany({
|
2026-01-30 13:41:32 +01:00
|
|
|
where: { roundId: input.roundId },
|
|
|
|
|
include: {
|
2026-02-04 14:15:06 +01:00
|
|
|
assignments: {
|
2026-01-30 13:41:32 +01:00
|
|
|
include: {
|
2026-02-04 14:15:06 +01:00
|
|
|
evaluation: {
|
|
|
|
|
where: { status: 'SUBMITTED' },
|
2026-01-30 13:41:32 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-02-04 14:15:06 +01:00
|
|
|
orderBy: { title: 'asc' },
|
2026-01-30 13:41:32 +01:00
|
|
|
})
|
|
|
|
|
|
2026-02-04 14:15:06 +01:00
|
|
|
const data = projects.map((p) => {
|
2026-01-30 13:41:32 +01:00
|
|
|
const evaluations = p.assignments
|
|
|
|
|
.map((a) => a.evaluation)
|
|
|
|
|
.filter((e) => e !== null)
|
|
|
|
|
|
|
|
|
|
const globalScores = evaluations
|
|
|
|
|
.map((e) => e?.globalScore)
|
|
|
|
|
.filter((s): s is number => s !== null)
|
|
|
|
|
|
|
|
|
|
const yesVotes = evaluations.filter(
|
|
|
|
|
(e) => e?.binaryDecision === true
|
|
|
|
|
).length
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
title: p.title,
|
|
|
|
|
teamName: p.teamName,
|
2026-02-04 14:15:06 +01:00
|
|
|
status: p.status,
|
2026-01-30 13:41:32 +01:00
|
|
|
tags: p.tags.join(', '),
|
|
|
|
|
totalEvaluations: evaluations.length,
|
|
|
|
|
averageScore:
|
|
|
|
|
globalScores.length > 0
|
|
|
|
|
? (
|
|
|
|
|
globalScores.reduce((a, b) => a + b, 0) / globalScores.length
|
|
|
|
|
).toFixed(2)
|
|
|
|
|
: null,
|
|
|
|
|
minScore: globalScores.length > 0 ? Math.min(...globalScores) : null,
|
|
|
|
|
maxScore: globalScores.length > 0 ? Math.max(...globalScores) : null,
|
|
|
|
|
yesVotes,
|
|
|
|
|
noVotes: evaluations.length - yesVotes,
|
|
|
|
|
yesPercentage:
|
|
|
|
|
evaluations.length > 0
|
|
|
|
|
? ((yesVotes / evaluations.length) * 100).toFixed(1)
|
|
|
|
|
: null,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Audit log
|
2026-02-05 21:09:06 +01:00
|
|
|
await logAudit({
|
|
|
|
|
prisma: ctx.prisma,
|
|
|
|
|
userId: ctx.user.id,
|
|
|
|
|
action: 'EXPORT',
|
|
|
|
|
entityType: 'ProjectScores',
|
|
|
|
|
detailsJson: { roundId: input.roundId, count: data.length },
|
|
|
|
|
ipAddress: ctx.ip,
|
|
|
|
|
userAgent: ctx.userAgent,
|
2026-01-30 13:41:32 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
data,
|
|
|
|
|
columns: [
|
|
|
|
|
'title',
|
|
|
|
|
'teamName',
|
|
|
|
|
'status',
|
|
|
|
|
'tags',
|
|
|
|
|
'totalEvaluations',
|
|
|
|
|
'averageScore',
|
|
|
|
|
'minScore',
|
|
|
|
|
'maxScore',
|
|
|
|
|
'yesVotes',
|
|
|
|
|
'noVotes',
|
|
|
|
|
'yesPercentage',
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Export assignments
|
|
|
|
|
*/
|
|
|
|
|
assignments: adminProcedure
|
|
|
|
|
.input(z.object({ roundId: z.string() }))
|
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
|
const assignments = await ctx.prisma.assignment.findMany({
|
|
|
|
|
where: { roundId: input.roundId },
|
|
|
|
|
include: {
|
|
|
|
|
user: { select: { name: true, email: true } },
|
|
|
|
|
project: { select: { title: true, teamName: true } },
|
|
|
|
|
evaluation: { select: { status: true, submittedAt: true } },
|
|
|
|
|
},
|
|
|
|
|
orderBy: [{ project: { title: 'asc' } }, { user: { name: 'asc' } }],
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const data = assignments.map((a) => ({
|
|
|
|
|
projectTitle: a.project.title,
|
|
|
|
|
teamName: a.project.teamName,
|
|
|
|
|
jurorName: a.user.name,
|
|
|
|
|
jurorEmail: a.user.email,
|
|
|
|
|
method: a.method,
|
|
|
|
|
isRequired: a.isRequired ? 'Yes' : 'No',
|
|
|
|
|
isCompleted: a.isCompleted ? 'Yes' : 'No',
|
|
|
|
|
evaluationStatus: a.evaluation?.status ?? 'NOT_STARTED',
|
|
|
|
|
submittedAt: a.evaluation?.submittedAt?.toISOString() ?? null,
|
|
|
|
|
assignedAt: a.createdAt.toISOString(),
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
data,
|
|
|
|
|
columns: [
|
|
|
|
|
'projectTitle',
|
|
|
|
|
'teamName',
|
|
|
|
|
'jurorName',
|
|
|
|
|
'jurorEmail',
|
|
|
|
|
'method',
|
|
|
|
|
'isRequired',
|
|
|
|
|
'isCompleted',
|
|
|
|
|
'evaluationStatus',
|
|
|
|
|
'submittedAt',
|
|
|
|
|
'assignedAt',
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
|
Implement 10 platform features: evaluation UX, admin tools, AI summaries, applicant portal
Batch 1 - Quick Wins:
- F1: Evaluation progress indicator with touch tracking in sticky status bar
- F2: Export filtering results as CSV with dynamic AI column flattening
- F3: Observer access to analytics dashboards (8 procedures changed to observerProcedure)
Batch 2 - Jury Experience:
- F4: Countdown timer component with urgency colors + email reminder service with cron endpoint
- F5: Conflict of interest declaration system (dialog, admin management, review workflow)
Batch 3 - Admin & AI Enhancements:
- F6: Bulk status update UI with selection checkboxes, floating toolbar, status history recording
- F7: AI-powered evaluation summary with anonymized data, OpenAI integration, scoring patterns
- F8: Smart assignment improvements (geo diversity penalty, round familiarity bonus, COI blocking)
Batch 4 - Form Flexibility & Applicant Portal:
- F9: Evaluation form flexibility (text, boolean, section_header types, conditional visibility)
- F10: Applicant portal (status timeline, per-round documents, mentor messaging)
Schema: 5 new models (ReminderLog, ConflictOfInterest, EvaluationSummary, ProjectStatusHistory, MentorMessage), ProjectFile extended with roundId + isLate.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-05 21:58:27 +01:00
|
|
|
/**
|
|
|
|
|
* Export filtering results as CSV data
|
|
|
|
|
*/
|
|
|
|
|
filteringResults: adminProcedure
|
|
|
|
|
.input(z.object({ roundId: z.string() }))
|
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
|
const results = await ctx.prisma.filteringResult.findMany({
|
|
|
|
|
where: { roundId: input.roundId },
|
|
|
|
|
include: {
|
|
|
|
|
project: {
|
|
|
|
|
select: {
|
|
|
|
|
title: true,
|
|
|
|
|
teamName: true,
|
|
|
|
|
competitionCategory: true,
|
|
|
|
|
country: true,
|
|
|
|
|
oceanIssue: true,
|
|
|
|
|
tags: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
orderBy: { project: { title: 'asc' } },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Collect all unique AI screening keys across all results
|
|
|
|
|
const aiKeys = new Set<string>()
|
|
|
|
|
results.forEach((r) => {
|
|
|
|
|
if (r.aiScreeningJson && typeof r.aiScreeningJson === 'object') {
|
|
|
|
|
const screening = r.aiScreeningJson as Record<string, Record<string, unknown>>
|
|
|
|
|
for (const ruleResult of Object.values(screening)) {
|
|
|
|
|
if (ruleResult && typeof ruleResult === 'object') {
|
|
|
|
|
Object.keys(ruleResult).forEach((k) => aiKeys.add(k))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const sortedAiKeys = Array.from(aiKeys).sort()
|
|
|
|
|
|
|
|
|
|
const data = results.map((r) => {
|
|
|
|
|
// Flatten AI screening - take first rule result's values
|
|
|
|
|
const aiFlat: Record<string, unknown> = {}
|
|
|
|
|
if (r.aiScreeningJson && typeof r.aiScreeningJson === 'object') {
|
|
|
|
|
const screening = r.aiScreeningJson as Record<string, Record<string, unknown>>
|
|
|
|
|
const firstEntry = Object.values(screening)[0]
|
|
|
|
|
if (firstEntry && typeof firstEntry === 'object') {
|
|
|
|
|
for (const key of sortedAiKeys) {
|
|
|
|
|
const val = firstEntry[key]
|
|
|
|
|
aiFlat[`ai_${key}`] = val !== undefined ? String(val) : ''
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
projectTitle: r.project.title,
|
|
|
|
|
teamName: r.project.teamName ?? '',
|
|
|
|
|
category: r.project.competitionCategory ?? '',
|
|
|
|
|
country: r.project.country ?? '',
|
|
|
|
|
oceanIssue: r.project.oceanIssue ?? '',
|
|
|
|
|
tags: r.project.tags.join(', '),
|
|
|
|
|
outcome: r.outcome,
|
|
|
|
|
finalOutcome: r.finalOutcome ?? '',
|
|
|
|
|
overrideReason: r.overrideReason ?? '',
|
|
|
|
|
...aiFlat,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Build columns list
|
|
|
|
|
const baseColumns = [
|
|
|
|
|
'projectTitle',
|
|
|
|
|
'teamName',
|
|
|
|
|
'category',
|
|
|
|
|
'country',
|
|
|
|
|
'oceanIssue',
|
|
|
|
|
'tags',
|
|
|
|
|
'outcome',
|
|
|
|
|
'finalOutcome',
|
|
|
|
|
'overrideReason',
|
|
|
|
|
]
|
|
|
|
|
const aiColumns = sortedAiKeys.map((k) => `ai_${k}`)
|
|
|
|
|
|
|
|
|
|
// Audit log
|
|
|
|
|
await logAudit({
|
|
|
|
|
prisma: ctx.prisma,
|
|
|
|
|
userId: ctx.user.id,
|
|
|
|
|
action: 'EXPORT',
|
|
|
|
|
entityType: 'FilteringResult',
|
|
|
|
|
detailsJson: { roundId: input.roundId, count: data.length },
|
|
|
|
|
ipAddress: ctx.ip,
|
|
|
|
|
userAgent: ctx.userAgent,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
data,
|
|
|
|
|
columns: [...baseColumns, ...aiColumns],
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
|
2026-01-30 13:41:32 +01:00
|
|
|
/**
|
|
|
|
|
* Export audit logs as CSV data
|
|
|
|
|
*/
|
|
|
|
|
auditLogs: adminProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
userId: z.string().optional(),
|
|
|
|
|
action: z.string().optional(),
|
|
|
|
|
entityType: z.string().optional(),
|
|
|
|
|
startDate: z.date().optional(),
|
|
|
|
|
endDate: z.date().optional(),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
|
const { userId, action, entityType, startDate, endDate } = input
|
|
|
|
|
|
|
|
|
|
const where: Record<string, unknown> = {}
|
|
|
|
|
|
|
|
|
|
if (userId) where.userId = userId
|
|
|
|
|
if (action) where.action = { contains: action, mode: 'insensitive' }
|
|
|
|
|
if (entityType) where.entityType = entityType
|
|
|
|
|
if (startDate || endDate) {
|
|
|
|
|
where.timestamp = {}
|
|
|
|
|
if (startDate) (where.timestamp as Record<string, Date>).gte = startDate
|
|
|
|
|
if (endDate) (where.timestamp as Record<string, Date>).lte = endDate
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const logs = await ctx.prisma.auditLog.findMany({
|
|
|
|
|
where,
|
|
|
|
|
orderBy: { timestamp: 'desc' },
|
|
|
|
|
include: {
|
|
|
|
|
user: { select: { name: true, email: true } },
|
|
|
|
|
},
|
|
|
|
|
take: 10000, // Limit export to 10k records
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const data = logs.map((log) => ({
|
|
|
|
|
timestamp: log.timestamp.toISOString(),
|
|
|
|
|
userName: log.user?.name ?? 'System',
|
|
|
|
|
userEmail: log.user?.email ?? 'N/A',
|
|
|
|
|
action: log.action,
|
|
|
|
|
entityType: log.entityType,
|
|
|
|
|
entityId: log.entityId ?? '',
|
|
|
|
|
ipAddress: log.ipAddress ?? '',
|
|
|
|
|
userAgent: log.userAgent ?? '',
|
|
|
|
|
details: log.detailsJson ? JSON.stringify(log.detailsJson) : '',
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
data,
|
|
|
|
|
columns: [
|
|
|
|
|
'timestamp',
|
|
|
|
|
'userName',
|
|
|
|
|
'userEmail',
|
|
|
|
|
'action',
|
|
|
|
|
'entityType',
|
|
|
|
|
'entityId',
|
|
|
|
|
'ipAddress',
|
|
|
|
|
'userAgent',
|
|
|
|
|
'details',
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
})
|