Skip to content

Commit 9d89d5d

Browse files
user can be added to responsibilities
1 parent 89634d1 commit 9d89d5d

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

src/client/components/Courses/Course/index.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Edit, OpenInNew } from '@mui/icons-material'
2-
import { Alert, Box, Button, Checkbox, Container, FormControlLabel, Input, Modal, Paper, Skeleton, Tab, Tooltip, Typography } from '@mui/material'
2+
import { Alert, Box, Button, Checkbox, Container, FormControlLabel, Input, Modal, Paper, Skeleton, Tab, TextField, Tooltip, Typography } from '@mui/material'
33
import { enqueueSnackbar } from 'notistack'
44
import { useState } from 'react'
55
import { useTranslation } from 'react-i18next'
6-
import { Link, Route, Routes, useParams } from 'react-router-dom'
6+
import { Form, Link, Route, Routes, useParams } from 'react-router-dom'
77

88
import { PUBLIC_URL } from '../../../../config'
99
import useCourse from '../../../hooks/useCourse'
@@ -21,6 +21,7 @@ import Stats from './Stats'
2121
import { RouterTabs } from '../../common/RouterTabs'
2222
import Discussion from './Discussions'
2323
import { ApiErrorView } from '../../common/ApiErrorView'
24+
import apiClient from '../../../util/apiClient'
2425

2526
const Course = () => {
2627
const [showTeachers, setShowTeachers] = useState(false)
@@ -97,7 +98,11 @@ const Course = () => {
9798
boxSizing: 'borderBox',
9899
height: '40px',
99100
}
100-
101+
const handleAddResponsible = async (e) => {
102+
e.preventDefault()
103+
const username = e.target.username.value
104+
apiClient.post(`/courses/${course.id}/responsibilities/assign`, {username: username})
105+
}
101106
return (
102107
<Container sx={{ mt: '4rem', mb: '10rem' }} maxWidth="xl">
103108
<Alert severity={getInfoSeverity()}>
@@ -182,13 +187,22 @@ const Course = () => {
182187
{showTeachers ? t('admin:hideTeachers') : t('admin:showTeachers')}
183188
</Button>
184189
{showTeachers && (
190+
<Box>
191+
<Form onSubmit={handleAddResponsible}>
192+
<TextField name="username" placeholder={"käyttäjänimi: "}></TextField>
193+
<Button type={"submit"}>
194+
Lisää
195+
</Button>
196+
197+
</Form>
185198
<ul>
186199
{course.responsibilities.map((responsibility) => (
187200
<li key={responsibility.id}>
188201
{responsibility.user.last_name} {responsibility.user.first_names}
189202
</li>
190203
))}
191204
</ul>
205+
</Box>
192206
)}
193207
</>
194208
)}

src/server/routes/course.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,9 @@ courseRouter.get('/:id/enrolments', async (req: express.Request, res: express.Re
164164

165165
//checks if user is a admin or is responsible for the course, returns forbidden error if not
166166
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) {
175170
throw ApplicationError.Forbidden('Unauthorized')
176171
}
177172
return hasFullAccess
@@ -315,9 +310,9 @@ courseRouter.put('/:id', async (req, res) => {
315310
})
316311

317312
const userAssignedAsResponsible = (userId, chatInstance) => {
318-
313+
console.log("looking for: " + userId)
319314
const isResponsible:boolean = chatInstance.responsibilities
320-
?.map((r) => r.userId)
315+
?.map((r) => {console.log(r); return r.user?.id})
321316
.filter(Boolean)
322317
.includes(userId)
323318
return isResponsible
@@ -334,28 +329,48 @@ courseRouter.put('/:id', async (req, res) => {
334329
return user
335330
}
336331

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) => {
338344
const chatInstanceId = req.params.id
339345
const body = req.body as {
340-
assignedUserId: string
346+
username : string
341347
}
342-
const assignedUserId:string = body.assignedUserId
348+
const assignedUserUsername:string = body.username
343349

344350
const chatInstanceIdClean = cleanIdStringSchema.safeParse(chatInstanceId)
345351
if(!chatInstanceIdClean.success){
346352
res.status(400).send('Malformed chat instance id')
353+
return
347354
}
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){
350358
res.status(400).send('Malformed assigned user id')
359+
return
351360
}
352361

353362
const request = req as unknown as RequestWithUser
354-
const {user} = request
363+
const {user} = request
355364
const chatInstance = await getChatInstance(chatInstanceId)
356365
const hasPermission = await enforceUserHasFullAccess(user, chatInstanceId)
357366

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
359374
const userAssignedAlready = await userAssignedAsResponsible(assignedUserId, chatInstance)
360375
if(userAssignedAlready){
361376
res.status(400).send('User is already responsible for the course')
@@ -372,8 +387,6 @@ courseRouter.put('/:id/responsibilities/assign', async (req, res) => {
372387
res.json(createdResponsibility)
373388
return
374389
}
375-
376-
377390
res.status(500).send('Unknown error occurred')
378391
})
379392

0 commit comments

Comments
 (0)