feat: mentor self-drop with required reason

Adds mentor.dropAssignment mutation (mentor-only, ownership-checked, reason
min 10 chars). Filters dropped MentorAssignment rows out of getMyProjects,
getCandidates mentor count, getMentorPool, and getMenteeActivity so they
no longer surface in the mentor or admin UI.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matt
2026-04-28 18:44:45 +02:00
parent 5bdb65181d
commit 3bc1cc14c7
2 changed files with 302 additions and 5 deletions

View File

@@ -178,7 +178,7 @@ export const mentorRouter = router({
country: true,
expertiseTags: true,
maxAssignments: true,
mentorAssignments: { select: { id: true } },
mentorAssignments: { where: { droppedAt: null }, select: { id: true } },
},
})
@@ -1006,7 +1006,10 @@ export const mentorRouter = router({
expertiseTags: true,
maxAssignments: true,
mentorAssignments: {
where: input.programId ? { project: { programId: input.programId } } : undefined,
where: {
droppedAt: null,
...(input.programId ? { project: { programId: input.programId } } : {}),
},
select: {
completionStatus: true,
messages: {
@@ -1099,13 +1102,18 @@ export const mentorRouter = router({
method: true,
assignedAt: true,
completionStatus: true,
droppedAt: true,
mentor: {
select: {
id: true,
name: true,
email: true,
maxAssignments: true,
_count: { select: { mentorAssignments: true } },
_count: {
select: {
mentorAssignments: { where: { droppedAt: null } },
},
},
},
},
messages: {
@@ -1137,7 +1145,8 @@ export const mentorRouter = router({
const totals = { unassigned: 0, assigned: 0, active: 0, stalled: 0 }
const rows = projects.map((p) => {
const ma = p.mentorAssignment
// Treat a dropped mentor assignment as if no mentor is assigned.
const ma = p.mentorAssignment && !p.mentorAssignment.droppedAt ? p.mentorAssignment : null
const lastMessageAt = ma?.messages[0]?.createdAt ?? null
const lastFileAt = ma?.files[0]?.createdAt ?? null
const lastActivityAt = [lastMessageAt, lastFileAt]
@@ -1196,7 +1205,7 @@ export const mentorRouter = router({
*/
getMyProjects: mentorProcedure.query(async ({ ctx }) => {
const assignments = await ctx.prisma.mentorAssignment.findMany({
where: { mentorId: ctx.user.id },
where: { mentorId: ctx.user.id, droppedAt: null },
include: {
project: {
include: {
@@ -2205,4 +2214,91 @@ export const mentorRouter = router({
}
return result
}),
/**
* Mentor self-drops an assignment with a required reason. Notifies all
* program admins so they can re-assign. Audit-logged.
*/
dropAssignment: mentorProcedure
.input(
z.object({
assignmentId: z.string(),
reason: z
.string()
.min(10, 'Reason must be at least 10 characters')
.max(500),
}),
)
.mutation(async ({ ctx, input }) => {
const assignment = await ctx.prisma.mentorAssignment.findUniqueOrThrow({
where: { id: input.assignmentId },
include: {
project: { select: { id: true, title: true } },
},
})
if (assignment.mentorId !== ctx.user.id) {
throw new TRPCError({
code: 'FORBIDDEN',
message: 'This is not your assignment',
})
}
if (assignment.droppedAt) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Assignment is already dropped',
})
}
if (assignment.completionStatus === 'completed') {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Assignment is already completed',
})
}
const dropped = await ctx.prisma.mentorAssignment.update({
where: { id: assignment.id },
data: {
droppedAt: new Date(),
droppedReason: input.reason,
droppedBy: 'mentor',
},
})
// Notify program admins (best-effort, never block the drop)
try {
const admins = await ctx.prisma.user.findMany({
where: {
roles: { hasSome: ['SUPER_ADMIN', 'PROGRAM_ADMIN'] },
status: { not: 'SUSPENDED' },
},
select: { id: true },
})
const mentorName = ctx.user.name ?? ctx.user.email
for (const admin of admins) {
await createNotification({
userId: admin.id,
type: NotificationTypes.MENTOR_DROPPED,
title: 'Mentor dropped a team',
message: `${mentorName} dropped their mentee "${assignment.project.title}". Reason: ${input.reason}`,
linkUrl: `/admin/projects/${assignment.project.id}/mentor`,
priority: 'high',
})
}
} catch (err) {
console.error('[mentor.dropAssignment] notify admins failed:', err)
}
await logAudit({
prisma: ctx.prisma,
userId: ctx.user.id,
action: 'MENTOR_DROP_ASSIGNMENT',
entityType: 'MentorAssignment',
entityId: assignment.id,
detailsJson: {
reason: input.reason,
projectId: assignment.project.id,
},
})
return dropped
}),
})