@@ -164,14 +164,9 @@ courseRouter.get('/:id/enrolments', async (req: express.Request, res: express.Re
164
164
165
165
//checks if user is a admin or is responsible for the course, returns forbidden error if not
166
166
const enforceUserHasFullAccess = async ( user , chatInstance ) => {
167
- const hasFullAccess =
168
- user . isAdmin ||
169
- chatInstance . responsibilities
170
- ?. map ( ( r ) => r . userId )
171
- . filter ( Boolean )
172
- . includes ( user . id )
173
-
174
- if ( ! hasFullAccess ) {
167
+ const isResponsibleForCourse = userAssignedAsResponsible ( user . id , chatInstance )
168
+ const hasFullAccess = user . isAdmin || isResponsibleForCourse
169
+ if ( ! hasFullAccess ) {
175
170
throw ApplicationError . Forbidden ( 'Unauthorized' )
176
171
}
177
172
return hasFullAccess
@@ -315,9 +310,9 @@ courseRouter.put('/:id', async (req, res) => {
315
310
} )
316
311
317
312
const userAssignedAsResponsible = ( userId , chatInstance ) => {
318
-
313
+ console . log ( "looking for: " + userId )
319
314
const isResponsible :boolean = chatInstance . responsibilities
320
- ?. map ( ( r ) => r . userId )
315
+ ?. map ( ( r ) => { console . log ( r ) ; return r . user ?. id } )
321
316
. filter ( Boolean )
322
317
. includes ( userId )
323
318
return isResponsible
@@ -334,28 +329,48 @@ courseRouter.put('/:id', async (req, res) => {
334
329
return user
335
330
}
336
331
337
- courseRouter . put ( '/:id/responsibilities/assign' , async ( req , res ) => {
332
+
333
+ const getUserByUsername = async ( username : string ) => {
334
+
335
+ const user = await User . findOne ( {
336
+ where : {
337
+ username : username
338
+ } ,
339
+ raw : true
340
+ } )
341
+ return user
342
+ }
343
+ courseRouter . post ( '/:id/responsibilities/assign' , async ( req , res ) => {
338
344
const chatInstanceId = req . params . id
339
345
const body = req . body as {
340
- assignedUserId : string
346
+ username : string
341
347
}
342
- const assignedUserId :string = body . assignedUserId
348
+ const assignedUserUsername :string = body . username
343
349
344
350
const chatInstanceIdClean = cleanIdStringSchema . safeParse ( chatInstanceId )
345
351
if ( ! chatInstanceIdClean . success ) {
346
352
res . status ( 400 ) . send ( 'Malformed chat instance id' )
353
+ return
347
354
}
348
- const assignedUserIdClean = cleanIdStringSchema . safeParse ( assignedUserId )
349
- if ( ! assignedUserIdClean . success ) {
355
+ //username also must be of similar format as the id only letters and numbers
356
+ const assignedUserUsernameClean = cleanIdStringSchema . safeParse ( assignedUserUsername )
357
+ if ( ! assignedUserUsernameClean . success ) {
350
358
res . status ( 400 ) . send ( 'Malformed assigned user id' )
359
+ return
351
360
}
352
361
353
362
const request = req as unknown as RequestWithUser
354
- const { user} = request
363
+ const { user} = request
355
364
const chatInstance = await getChatInstance ( chatInstanceId )
356
365
const hasPermission = await enforceUserHasFullAccess ( user , chatInstanceId )
357
366
358
- const userToAssign = await getUser ( assignedUserId )
367
+ const userToAssign = await getUserByUsername ( assignedUserUsername )
368
+ if ( ! userToAssign ) {
369
+ res . status ( 400 ) . send ( 'User not found with username' )
370
+ return
371
+ }
372
+
373
+ const assignedUserId = userToAssign . id
359
374
const userAssignedAlready = await userAssignedAsResponsible ( assignedUserId , chatInstance )
360
375
if ( userAssignedAlready ) {
361
376
res . status ( 400 ) . send ( 'User is already responsible for the course' )
@@ -372,8 +387,6 @@ courseRouter.put('/:id/responsibilities/assign', async (req, res) => {
372
387
res . json ( createdResponsibility )
373
388
return
374
389
}
375
-
376
-
377
390
res . status ( 500 ) . send ( 'Unknown error occurred' )
378
391
} )
379
392
0 commit comments