1
1
import { Router } from 'express'
2
2
import z from 'zod/v4'
3
3
import { EMBED_DIM } from '../../../config'
4
- import { ChatInstance , ChatInstanceRagIndex , RagFile , RagIndex , Responsibility } from '../../db/models'
4
+ import { ChatInstance , ChatInstanceRagIndex , Enrolment , RagFile , RagIndex , Responsibility } from '../../db/models'
5
5
import type { RequestWithUser } from '../../types'
6
- import type { User } from '../../../shared/user'
7
6
import { ApplicationError } from '../../util/ApplicationError'
8
7
import { TEST_COURSES } from '../../../shared/testData'
9
8
import ragIndexRouter , { ragIndexMiddleware } from './ragIndex'
9
+ import { ChatInstanceAccess , getChatInstanceAccess } from '../../services/chatInstances/access'
10
10
11
11
const router = Router ( )
12
12
@@ -17,22 +17,12 @@ const IndexCreationSchema = z.object({
17
17
dim : z . number ( ) . min ( EMBED_DIM ) . max ( EMBED_DIM ) . default ( EMBED_DIM ) ,
18
18
} )
19
19
20
- const hasChatInstanceRagPermission = ( user : User , chatInstance : ChatInstance ) => {
21
- const isResponsible = chatInstance . responsibilities ?. some ( ( r ) => r . userId === user . id )
22
- return isResponsible || user . isAdmin
23
- }
24
-
25
20
router . post ( '/indices' , async ( req , res ) => {
26
21
const { user } = req as RequestWithUser
27
22
const { name, dim, chatInstanceId, language } = IndexCreationSchema . parse ( req . body )
28
23
29
24
const chatInstance = await ChatInstance . findByPk ( chatInstanceId , {
30
25
include : [
31
- {
32
- model : Responsibility ,
33
- as : 'responsibilities' ,
34
- required : true , // Ensure the user is responsible for the course
35
- } ,
36
26
{
37
27
model : RagIndex ,
38
28
as : 'ragIndices' ,
@@ -44,7 +34,7 @@ router.post('/indices', async (req, res) => {
44
34
throw ApplicationError . NotFound ( 'Invalid chat instance id' )
45
35
}
46
36
47
- if ( ! hasChatInstanceRagPermission ( user , chatInstance ) ) {
37
+ if ( ( await getChatInstanceAccess ( user , chatInstance ) ) < ChatInstanceAccess . TEACHER ) {
48
38
throw ApplicationError . Forbidden ( 'Cannot create index, user is not responsible for the course' )
49
39
}
50
40
@@ -89,10 +79,7 @@ router.get('/indices', async (req, res) => {
89
79
let chatInstance : ChatInstance | null = null
90
80
if ( chatInstanceId ) {
91
81
chatInstance = await ChatInstance . findByPk ( chatInstanceId , {
92
- include : [
93
- { model : Responsibility , as : 'responsibilities' , required : ! user . isAdmin } ,
94
- { model : RagIndex , as : 'ragIndices' , required : false } ,
95
- ] ,
82
+ include : [ { model : RagIndex , as : 'ragIndices' , required : false } ] ,
96
83
} )
97
84
98
85
if ( ! chatInstance ) {
@@ -104,8 +91,8 @@ router.get('/indices', async (req, res) => {
104
91
return
105
92
}
106
93
107
- if ( ! hasChatInstanceRagPermission ( user , chatInstance ) ) {
108
- throw ApplicationError . Forbidden ( 'Forbidden ' )
94
+ if ( ( await getChatInstanceAccess ( user , chatInstance ) ) < ChatInstanceAccess . STUDENT ) {
95
+ throw ApplicationError . Forbidden ( 'Not allowed to use rag index. You must be at least an enrolled student. ' )
109
96
}
110
97
} else {
111
98
if ( ! user . isAdmin ) {
0 commit comments