This commit is contained in:
+13
-6
@@ -826,10 +826,9 @@ authRoutes.post('/instructor/students', authRequired, async (c) => {
|
||||
|
||||
const email = body.data.email.toLowerCase()
|
||||
const existingUser = await findUserByEmail(email)
|
||||
if (existingUser) return c.json({ error: 'email_already_registered' }, 409)
|
||||
|
||||
try {
|
||||
const user = await createManagedUser({
|
||||
const user = existingUser ?? await createManagedUser({
|
||||
email,
|
||||
password: body.data.password,
|
||||
name: body.data.name,
|
||||
@@ -839,17 +838,25 @@ authRoutes.post('/instructor/students', authRequired, async (c) => {
|
||||
licenseCode: body.data.licenseCode,
|
||||
})
|
||||
|
||||
await prisma.userAppRole.create({
|
||||
data: {
|
||||
await prisma.userAppRole.upsert({
|
||||
where: {
|
||||
userId_appKey_roleCode: {
|
||||
userId: user.id,
|
||||
appKey: 'LMS',
|
||||
roleCode: 'STUDENT',
|
||||
},
|
||||
},
|
||||
create: {
|
||||
userId: user.id,
|
||||
appKey: 'LMS',
|
||||
roleCode: 'STUDENT',
|
||||
assignedById: instructor.id,
|
||||
},
|
||||
update: {},
|
||||
})
|
||||
|
||||
const createdUser = await findUserById(user.id)
|
||||
return c.json({ user: userResponse(createdUser ?? user) }, 201)
|
||||
const resolvedUser = await findUserById(user.id)
|
||||
return c.json({ user: userResponse(resolvedUser ?? user) }, existingUser ? 200 : 201)
|
||||
} catch {
|
||||
return c.json({ error: 'user_create_failed' }, 400)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user