@@ -332,27 +332,37 @@ courseRouter.put('/:id', async (req, res) => {
332
332
}
333
333
return user
334
334
}
335
+
335
336
courseRouter . put ( '/:id/responsibilities/assign' , async ( req , res ) => {
336
337
const chatInstanceId = req . params . id
337
338
const body = req . body as {
338
339
assignedUserId : string
339
340
}
340
341
const assignedUserId :string = body . assignedUserId
341
342
342
- const request = req as unknown as RequestWithUser
343
+ const request = req as unknown as RequestWithUser
343
344
const { user} = request
344
345
const chatInstance = await getChatInstance ( chatInstanceId )
345
346
const hasPermission = await enforceUserHasFullAccess ( user , chatInstanceId )
346
347
347
348
const userToAssign = await getUser ( assignedUserId )
348
- const userNotAssignedAlready = await ! userAssignedAsResponsible ( assignedUserId , chatInstance )
349
- if ( hasPermission && userToAssign && userNotAssignedAlready ) {
350
- await Responsibility . create ( {
349
+ const userAssignedAlready = await userAssignedAsResponsible ( assignedUserId , chatInstance )
350
+ if ( userAssignedAlready ) {
351
+ res . status ( 401 ) . send ( 'User is already responsible for the course' )
352
+ }
353
+
354
+ if ( hasPermission && userToAssign && ! userAssignedAlready ) {
355
+ const createdResponsibility = await Responsibility . create ( {
351
356
userId : assignedUserId ,
352
357
chatInstanceId : chatInstance . id ,
353
358
createdByUserId : user . id
354
359
} )
360
+
361
+ return res . json ( createdResponsibility )
355
362
}
363
+
364
+
365
+ res . status ( 500 ) . send ( 'Unknown error occurred' )
356
366
} )
357
367
358
368
0 commit comments