@@ -164,17 +164,16 @@ 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 isResponsibleForCourse = userAssignedAsResponsible ( user . id , chatInstance )
168
- const hasFullAccess = user . isAdmin || isResponsibleForCourse
169
- if ( ! hasFullAccess ) {
167
+ const isResponsibleForCourse = userAssignedAsResponsible ( user . id , chatInstance )
168
+ const hasFullAccess = user . isAdmin || isResponsibleForCourse
169
+ if ( ! hasFullAccess ) {
170
170
throw ApplicationError . Forbidden ( 'Unauthorized' )
171
171
}
172
172
return hasFullAccess
173
173
}
174
174
175
175
// returns a chatInstance, throws an chat instacne not found if not found
176
176
const getChatInstance = async ( id ) => {
177
-
178
177
const chatInstance = await ChatInstance . findOne ( {
179
178
where : { courseId : id } ,
180
179
include : [
@@ -309,87 +308,84 @@ courseRouter.put('/:id', async (req, res) => {
309
308
res . send ( chatInstance )
310
309
} )
311
310
312
- const userAssignedAsResponsible = ( userId , chatInstance ) => {
313
- console . log ( "looking for: " + userId )
314
- const isResponsible :boolean = chatInstance . responsibilities
315
- ?. map ( ( r ) => { console . log ( r ) ; return r . user ?. id } )
316
- . filter ( Boolean )
317
- . includes ( userId )
311
+ const userAssignedAsResponsible = ( userId , chatInstance ) => {
312
+ console . log ( 'looking for: ' + userId )
313
+ const isResponsible : boolean = chatInstance . responsibilities
314
+ ?. map ( ( r ) => {
315
+ console . log ( r )
316
+ return r . user ?. id
317
+ } )
318
+ . filter ( Boolean )
319
+ . includes ( userId )
318
320
return isResponsible
319
- }
320
-
321
-
322
- const getUser = async ( id : string ) => {
323
-
324
- const user = await User . findByPk ( id )
325
- if ( ! user ) {
326
- throw ApplicationError . NotFound ( 'User not found' )
327
- return null
328
- }
329
- return user
330
- }
321
+ }
331
322
323
+ const getUser = async ( id : string ) => {
324
+ const user = await User . findByPk ( id )
325
+ if ( ! user ) {
326
+ throw ApplicationError . NotFound ( 'User not found' )
327
+ return null
328
+ }
329
+ return user
330
+ }
332
331
333
332
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
333
+ const user = await User . findOne ( {
334
+ where : {
335
+ username : username ,
336
+ } ,
337
+ raw : true ,
338
+ } )
339
+ return user
342
340
}
343
341
courseRouter . post ( '/:id/responsibilities/assign' , async ( req , res ) => {
344
- const chatInstanceId = req . params . id
345
- const body = req . body as {
346
- username : string
347
- }
348
- const assignedUserUsername :string = body . username
349
-
350
- const chatInstanceIdClean = cleanIdStringSchema . safeParse ( chatInstanceId )
351
- if ( ! chatInstanceIdClean . success ) {
352
- res . status ( 400 ) . send ( 'Malformed chat instance id' )
353
- return
354
- }
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 ) {
358
- res . status ( 400 ) . send ( 'Malformed assigned user id' )
359
- return
360
- }
361
-
362
- const request = req as unknown as RequestWithUser
363
- const { user} = request
364
- const chatInstance = await getChatInstance ( chatInstanceId )
365
- const hasPermission = await enforceUserHasFullAccess ( user , chatInstanceId )
366
-
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
374
- const userAssignedAlready = await userAssignedAsResponsible ( assignedUserId , chatInstance )
375
- if ( userAssignedAlready ) {
376
- res . status ( 400 ) . send ( 'User is already responsible for the course' )
377
- return
378
- }
379
-
380
- if ( hasPermission && userToAssign && ! userAssignedAlready ) {
342
+ const chatInstanceId = req . params . id
343
+ const body = req . body as {
344
+ username : string
345
+ }
346
+ const assignedUserUsername : string = body . username
347
+
348
+ const chatInstanceIdClean = cleanIdStringSchema . safeParse ( chatInstanceId )
349
+ if ( ! chatInstanceIdClean . success ) {
350
+ res . status ( 400 ) . send ( 'Malformed chat instance id' )
351
+ return
352
+ }
353
+ //username also must be of similar format as the id only letters and numbers
354
+ const assignedUserUsernameClean = cleanIdStringSchema . safeParse ( assignedUserUsername )
355
+ if ( ! assignedUserUsernameClean . success ) {
356
+ res . status ( 400 ) . send ( 'Malformed assigned user id' )
357
+ return
358
+ }
359
+
360
+ const request = req as unknown as RequestWithUser
361
+ const { user } = request
362
+ const chatInstance = await getChatInstance ( chatInstanceId )
363
+ const hasPermission = await enforceUserHasFullAccess ( user , chatInstanceId )
364
+
365
+ const userToAssign = await getUserByUsername ( assignedUserUsername )
366
+ if ( ! userToAssign ) {
367
+ res . status ( 400 ) . send ( 'User not found with username' )
368
+ return
369
+ }
370
+
371
+ const assignedUserId = userToAssign . id
372
+ const userAssignedAlready = await userAssignedAsResponsible ( assignedUserId , chatInstance )
373
+ if ( userAssignedAlready ) {
374
+ res . status ( 400 ) . send ( 'User is already responsible for the course' )
375
+ return
376
+ }
377
+
378
+ if ( hasPermission && userToAssign && ! userAssignedAlready ) {
381
379
const createdResponsibility = await Responsibility . create ( {
382
380
userId : assignedUserId ,
383
381
chatInstanceId : chatInstance . id ,
384
- createdByUserId : user . id
382
+ createdByUserId : user . id ,
385
383
} )
386
384
387
385
res . json ( createdResponsibility )
388
386
return
389
387
}
390
- res . status ( 500 ) . send ( 'Unknown error occurred' )
388
+ res . status ( 500 ) . send ( 'Unknown error occurred' )
391
389
} )
392
390
393
-
394
-
395
391
export default courseRouter
0 commit comments