fix(mentor): primary-role mentors are selectable/assignable

getCandidates, getMentorPool and bulkAssign matched MENTOR via roles[] only, so
a user with role=MENTOR but an empty roles[] array (legacy/seeded records, e.g.
Arnaud Blandin on prod) was excluded from the mentor picker and rejected by
bulk assign. Match MENTOR as primary role OR in roles[], mirroring userHasRole.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matt
2026-06-01 19:34:40 +02:00
parent fe7f133879
commit 828c09df6d
2 changed files with 109 additions and 4 deletions

View File

@@ -315,7 +315,10 @@ export const mentorRouter = router({
const mentors = await ctx.prisma.user.findMany({
where: {
roles: { has: 'MENTOR' },
// MENTOR as primary role OR in the secondary roles[] array. Some legacy
// users have role=MENTOR with an empty roles[], so checking roles only
// would wrongly exclude them (mirrors userHasRole's fallback).
OR: [{ role: 'MENTOR' }, { roles: { has: 'MENTOR' } }],
status: { not: 'SUSPENDED' },
},
select: {
@@ -728,9 +731,11 @@ export const mentorRouter = router({
.mutation(async ({ ctx, input }) => {
const mentors = await ctx.prisma.user.findMany({
where: { id: { in: input.mentorIds } },
select: { id: true, name: true, email: true, roles: true },
select: { id: true, name: true, email: true, role: true, roles: true },
})
const validMentors = mentors.filter((m) => m.roles.includes('MENTOR'))
// Accept MENTOR as primary role OR in roles[] (legacy users may have
// role=MENTOR with an empty roles[] array).
const validMentors = mentors.filter((m) => m.roles.includes('MENTOR') || m.role === 'MENTOR')
if (validMentors.length === 0) {
throw new TRPCError({
code: 'BAD_REQUEST',
@@ -1574,7 +1579,9 @@ export const mentorRouter = router({
.query(async ({ ctx, input }) => {
const mentors = await ctx.prisma.user.findMany({
where: {
roles: { has: 'MENTOR' },
// MENTOR as primary role OR in the secondary roles[] array (see
// getCandidates: legacy users may have role=MENTOR with empty roles[]).
OR: [{ role: 'MENTOR' }, { roles: { has: 'MENTOR' } }],
status: { not: 'SUSPENDED' },
},
select: {