1+ import { supa } from '../db.ts' ;
2+ import { PermissionError } from '../errors.ts' ;
3+ import { DefaultRoles , instructorRole , Permissions } from '../../../../../shared/schema/course.ts' ;
4+
5+ export async function isUserMemberOfCourse ( user_id : string , course_id : string ) : Promise < boolean > {
6+ const { data, error } = await supa . from ( 'course_members' )
7+ . select ( '*' )
8+ . eq ( 'user_id' , user_id )
9+ . eq ( 'course_id' , course_id ) ;
10+
11+ if ( error ) {
12+ throw new Error ( `Failed to check if user ${ user_id } is a member of course ${ course_id } : ${ error . message } ` ) ;
13+ }
14+
15+ return data . length > 0 ;
16+ }
17+
18+ export async function getRoleInCourse ( user_id : string , course_id : string ) : Promise < DefaultRoles > {
19+ const { data : courseRoleData , error : courseRoleError } = await supa . from ( 'course_members' )
20+ . select ( 'role_id' )
21+ . eq ( 'user_id' , user_id )
22+ . eq ( 'course_id' , course_id ) ;
23+
24+ if ( courseRoleError ) {
25+ throw new Error ( `Failed to get role for user ${ user_id } in course ${ course_id } : ${ courseRoleError . message } ` ) ;
26+ }
27+
28+ if ( ! courseRoleData || courseRoleData . length !== 1 ) {
29+ throw new PermissionError ( `User ${ user_id } is not a member of course ${ course_id } ` ) ;
30+ }
31+
32+ const role_id = courseRoleData [ 0 ] . role_id ;
33+ const { data : roleData , error : roleError } = await supa . from ( 'account_roles' )
34+ . select ( 'name' )
35+ . eq ( 'id' , role_id ) ;
36+
37+ if ( roleError ) {
38+ console . error ( `Failed to get role for user ${ user_id } in course ${ course_id } : ${ roleError . message } ` ) ;
39+ }
40+
41+ if ( ! roleData || roleData . length !== 1 ) {
42+ throw new Error ( `Role ${ role_id } for user ${ user_id } in course ${ course_id } was expected but to exist but did not` ) ;
43+ }
44+
45+ return roleData [ 0 ] . name as DefaultRoles ;
46+ }
47+
48+ export async function isUserInstructorInCourse ( user_id : string , course_id : string ) : Promise < boolean > {
49+ if ( ! isUserMemberOfCourse ( user_id , course_id ) ) {
50+ return false ;
51+ }
52+
53+ const role = await getRoleInCourse ( user_id , course_id ) ;
54+ return role === instructorRole ;
55+ }
56+
57+ export async function getUserPermissionsInCourse ( user_id : string , course_id : string ) : Promise < Permissions > {
58+ // TODO: This likely need to be more complicated or refactored to only get the default permissions
59+ const { data : courseRoleData , error : courseRoleError } = await supa . from ( 'course_members' )
60+ . select ( 'role_id' )
61+ . eq ( 'user_id' , user_id )
62+ . eq ( 'course_id' , course_id ) ;
63+
64+ if ( courseRoleError ) {
65+ throw new Error ( `Failed to get role for user ${ user_id } in course ${ course_id } : ${ courseRoleError . message } ` ) ;
66+ }
67+
68+ if ( ! courseRoleData || courseRoleData . length !== 1 ) {
69+ throw new PermissionError ( `User ${ user_id } is not a member of course ${ course_id } ` ) ;
70+ }
71+
72+ const role_id = courseRoleData [ 0 ] . role_id ;
73+ const { data : roleData , error : roleError } = await supa . from ( 'account_roles' )
74+ . select ( 'default_permissions' )
75+ . eq ( 'id' , role_id ) ;
76+
77+ if ( roleError ) {
78+ console . error ( `Failed to get permissions for user ${ user_id } in course ${ course_id } : ${ roleError . message } ` ) ;
79+ }
80+
81+ if ( ! roleData || roleData . length !== 1 ) {
82+ throw new Error ( `Role ${ role_id } for user ${ user_id } in course ${ course_id } was expected but to exist but did not` ) ;
83+ }
84+
85+ return roleData [ 0 ] . default_permissions as Permissions ;
86+ }
0 commit comments