- Migrate ~41 inline audit log calls to shared logAudit() utility across all routers - Add transaction-aware prisma parameter to logAudit() for atomic operations - Unify jury/mentor/observer navigation into shared RoleNav component - Add composite DB indexes (Evaluation, GracePeriod, AuditLog) for query performance - Fix profile page: consolidate dual save buttons, proper useEffect initialization - Enhance auth error page with MOPC branding and navigation - Improve observer dashboard with prominent read-only badge - Fix DI-3: fetch projects before bulk status update for accurate notifications - Remove unused aiBoost field from smart-assignment scoring - Add shared image-upload utility and structured logger module Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
149 lines
3.5 KiB
TypeScript
149 lines
3.5 KiB
TypeScript
import { z } from 'zod'
|
|
import { router, protectedProcedure, adminProcedure } from '../trpc'
|
|
import { logAudit } from '../utils/audit'
|
|
|
|
export const programRouter = router({
|
|
/**
|
|
* List all programs with optional filtering
|
|
*/
|
|
list: protectedProcedure
|
|
.input(
|
|
z.object({
|
|
status: z.enum(['DRAFT', 'ACTIVE', 'ARCHIVED']).optional(),
|
|
includeRounds: z.boolean().optional(),
|
|
}).optional()
|
|
)
|
|
.query(async ({ ctx, input }) => {
|
|
return ctx.prisma.program.findMany({
|
|
where: input?.status ? { status: input.status } : undefined,
|
|
orderBy: { year: 'desc' },
|
|
include: {
|
|
_count: {
|
|
select: { rounds: true },
|
|
},
|
|
rounds: {
|
|
orderBy: { createdAt: 'asc' },
|
|
include: {
|
|
_count: {
|
|
select: { projects: true, assignments: true },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}),
|
|
|
|
/**
|
|
* Get a single program with its rounds
|
|
*/
|
|
get: protectedProcedure
|
|
.input(z.object({ id: z.string() }))
|
|
.query(async ({ ctx, input }) => {
|
|
return ctx.prisma.program.findUniqueOrThrow({
|
|
where: { id: input.id },
|
|
include: {
|
|
rounds: {
|
|
orderBy: { createdAt: 'asc' },
|
|
include: {
|
|
_count: {
|
|
select: { projects: true, assignments: true },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}),
|
|
|
|
/**
|
|
* Create a new program (admin only)
|
|
*/
|
|
create: adminProcedure
|
|
.input(
|
|
z.object({
|
|
name: z.string().min(1).max(255),
|
|
year: z.number().int().min(2020).max(2100),
|
|
description: z.string().optional(),
|
|
})
|
|
)
|
|
.mutation(async ({ ctx, input }) => {
|
|
const program = await ctx.prisma.program.create({
|
|
data: input,
|
|
})
|
|
|
|
// Audit log
|
|
await logAudit({
|
|
prisma: ctx.prisma,
|
|
userId: ctx.user.id,
|
|
action: 'CREATE',
|
|
entityType: 'Program',
|
|
entityId: program.id,
|
|
detailsJson: input,
|
|
ipAddress: ctx.ip,
|
|
userAgent: ctx.userAgent,
|
|
})
|
|
|
|
return program
|
|
}),
|
|
|
|
/**
|
|
* Update a program (admin only)
|
|
*/
|
|
update: adminProcedure
|
|
.input(
|
|
z.object({
|
|
id: z.string(),
|
|
name: z.string().min(1).max(255).optional(),
|
|
status: z.enum(['DRAFT', 'ACTIVE', 'ARCHIVED']).optional(),
|
|
description: z.string().optional(),
|
|
})
|
|
)
|
|
.mutation(async ({ ctx, input }) => {
|
|
const { id, ...data } = input
|
|
|
|
const program = await ctx.prisma.program.update({
|
|
where: { id },
|
|
data,
|
|
})
|
|
|
|
// Audit log
|
|
await logAudit({
|
|
prisma: ctx.prisma,
|
|
userId: ctx.user.id,
|
|
action: 'UPDATE',
|
|
entityType: 'Program',
|
|
entityId: id,
|
|
detailsJson: data,
|
|
ipAddress: ctx.ip,
|
|
userAgent: ctx.userAgent,
|
|
})
|
|
|
|
return program
|
|
}),
|
|
|
|
/**
|
|
* Delete a program (admin only)
|
|
* Note: This will cascade delete all rounds, projects, etc.
|
|
*/
|
|
delete: adminProcedure
|
|
.input(z.object({ id: z.string() }))
|
|
.mutation(async ({ ctx, input }) => {
|
|
const program = await ctx.prisma.program.delete({
|
|
where: { id: input.id },
|
|
})
|
|
|
|
// Audit log
|
|
await logAudit({
|
|
prisma: ctx.prisma,
|
|
userId: ctx.user.id,
|
|
action: 'DELETE',
|
|
entityType: 'Program',
|
|
entityId: input.id,
|
|
detailsJson: { name: program.name, year: program.year },
|
|
ipAddress: ctx.ip,
|
|
userAgent: ctx.userAgent,
|
|
})
|
|
|
|
return program
|
|
}),
|
|
})
|