Skip to content

Commit 93871c9

Browse files
format
1 parent 9d89d5d commit 93871c9

File tree

7 files changed

+89
-96
lines changed

7 files changed

+89
-96
lines changed

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const Course = () => {
101101
const handleAddResponsible = async (e) => {
102102
e.preventDefault()
103103
const username = e.target.username.value
104-
apiClient.post(`/courses/${course.id}/responsibilities/assign`, {username: username})
104+
apiClient.post(`/courses/${course.id}/responsibilities/assign`, { username: username })
105105
}
106106
return (
107107
<Container sx={{ mt: '4rem', mb: '10rem' }} maxWidth="xl">
@@ -187,22 +187,19 @@ const Course = () => {
187187
{showTeachers ? t('admin:hideTeachers') : t('admin:showTeachers')}
188188
</Button>
189189
{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-
190+
<Box>
191+
<Form onSubmit={handleAddResponsible}>
192+
<TextField name="username" placeholder={'käyttäjänimi: '}></TextField>
193+
<Button type={'submit'}>Lisää</Button>
197194
</Form>
198-
<ul>
199-
{course.responsibilities.map((responsibility) => (
200-
<li key={responsibility.id}>
201-
{responsibility.user.last_name} {responsibility.user.first_names}
202-
</li>
203-
))}
204-
</ul>
205-
</Box>
195+
<ul>
196+
{course.responsibilities.map((responsibility) => (
197+
<li key={responsibility.id}>
198+
{responsibility.user.last_name} {responsibility.user.first_names}
199+
</li>
200+
))}
201+
</ul>
202+
</Box>
206203
)}
207204
</>
208205
)}

src/client/hooks/useChatScroll.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ export const useChatScroll = (appContainerRef, endOfConversationRef) => {
8383
}
8484

8585
const smoothScrollTo = (duration: number) => {
86-
if(!shouldScroll.current)
87-
{
86+
if (!shouldScroll.current) {
8887
return
8988
}
9089
//should scroll but there might be another scroll frame going so first it should be cancelled

src/client/hooks/useUpdateUrlLang.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const useUpdateUrlLang = () => {
1010
const { user } = useCurrentUser()
1111
const [params, setParams] = useSearchParams()
1212
const langParam = params.get('lang')
13-
const [lang, setStorageLang] = useLocalStorageState('lang', 'en')
13+
const [lang, setStorageLang] = useLocalStorageState('lang', 'en')
1414
useEffect(() => {
1515
const updatedLangFromLocal = localStorage.getItem('lang')
1616

src/server/db/migrations/20250804_00_add_created_by_user_id_column.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ export const up: Migration = async ({ context: queryInterface }) => {
1111
}
1212

1313
export const down: Migration = async ({ context: queryInterface }) => {
14-
await queryInterface.removeColumn('responsibilities','created_by_user_id')
14+
await queryInterface.removeColumn('responsibilities', 'created_by_user_id')
1515
}

src/server/db/models/responsibilities.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ Responsibility.init(
3131
type: DataTypes.STRING,
3232
allowNull: false,
3333
},
34-
createdByUserId: { // tells who manually created the responsibility, (null = created by updater)
34+
createdByUserId: {
35+
// tells who manually created the responsibility, (null = created by updater)
3536
type: DataTypes.STRING,
36-
allowNull: true
37-
}
37+
allowNull: true,
38+
},
3839
},
3940
{
4041
underscored: true,

src/server/routes/course.ts

Lines changed: 67 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,16 @@ 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 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) {
170170
throw ApplicationError.Forbidden('Unauthorized')
171171
}
172172
return hasFullAccess
173173
}
174174

175175
// returns a chatInstance, throws an chat instacne not found if not found
176176
const getChatInstance = async (id) => {
177-
178177
const chatInstance = await ChatInstance.findOne({
179178
where: { courseId: id },
180179
include: [
@@ -309,87 +308,84 @@ courseRouter.put('/:id', async (req, res) => {
309308
res.send(chatInstance)
310309
})
311310

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)
318320
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+
}
331322

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+
}
332331

333332
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
342340
}
343341
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) {
381379
const createdResponsibility = await Responsibility.create({
382380
userId: assignedUserId,
383381
chatInstanceId: chatInstance.id,
384-
createdByUserId: user.id
382+
createdByUserId: user.id,
385383
})
386384

387385
res.json(createdResponsibility)
388386
return
389387
}
390-
res.status(500).send('Unknown error occurred')
388+
res.status(500).send('Unknown error occurred')
391389
})
392390

393-
394-
395391
export default courseRouter

src/server/util/zodSchemas.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { z } from 'zod';
1+
import { z } from 'zod'
22

33
export const cleanIdStringSchema = z.string().regex(/^[a-zA-Z0-9]+$/, {
44
message: 'String must only contain letters',
5-
});
5+
})

0 commit comments

Comments
 (0)