@@ -23,6 +23,23 @@ export class IsSameUser extends PreExecutionRule {
2323 }
2424}
2525
26+ export class IsTicketOwner extends PreExecutionRule {
27+ error = new UnauthorizedError ( "Not authorized" ) ;
28+ public async execute (
29+ { USER , DB } : GraphqlContext ,
30+ fieldArgs : { input : { id : string } } ,
31+ ) {
32+ if ( ! USER || ! fieldArgs . input . id ) {
33+ return false ;
34+ }
35+ const IsTicketOwner = await DB . query . userTicketsSchema . findFirst ( {
36+ where : ( utc , { eq, and } ) =>
37+ and ( eq ( utc . userId , USER . id ) , eq ( utc . id , fieldArgs . input . id ) ) ,
38+ } ) ;
39+ return Boolean ( IsTicketOwner ) ;
40+ }
41+ }
42+
2643export class IsSuperAdmin extends PreExecutionRule {
2744 public execute ( { USER } : GraphqlContext , fieldArgs : { id ?: string } ) {
2845 if ( ! USER ) {
@@ -31,3 +48,119 @@ export class IsSuperAdmin extends PreExecutionRule {
3148 return Boolean ( USER . isSuperAdmin ) ;
3249 }
3350}
51+
52+ export class CanCreateEvent extends PreExecutionRule {
53+ public async execute (
54+ { USER , DB } : GraphqlContext ,
55+ fieldArgs : { input : { communityId : string } } ,
56+ ) {
57+ if ( ! USER || ! fieldArgs ?. input ?. communityId ) {
58+ return false ;
59+ }
60+ const user = await DB . query . usersToCommunitiesSchema . findFirst ( {
61+ where : ( utc , { eq, and } ) =>
62+ and ( eq ( utc . userId , USER . id ) , eq ( utc . role , "admin" ) ) ,
63+ } ) ;
64+
65+ return Boolean ( user ) ;
66+ }
67+ }
68+
69+ export class isCommunityCollaborator extends PreExecutionRule {
70+ public async execute (
71+ { USER , DB } : GraphqlContext ,
72+ fieldArgs : { input : { communityId : string } } ,
73+ ) {
74+ if ( ! USER || ! fieldArgs ?. input ?. communityId ) {
75+ return false ;
76+ }
77+ const user = await DB . query . communitySchema . findFirst ( {
78+ with : {
79+ usersToCommunities : {
80+ where : ( utc , { eq, and } ) =>
81+ and ( eq ( utc . userId , USER . id ) , eq ( utc . role , "admin" ) ) ,
82+ } ,
83+ } ,
84+ } ) ;
85+ return Boolean ( user ) ;
86+ }
87+ }
88+
89+ export class isCommunityAdmin extends PreExecutionRule {
90+ public async execute (
91+ { USER , DB } : GraphqlContext ,
92+ fieldArgs : { input : { communityId : string } } ,
93+ ) {
94+ if ( ! USER || ! fieldArgs ?. input ?. communityId ) {
95+ return false ;
96+ }
97+ const isCommunityAdmin = await DB . query . usersToCommunitiesSchema . findFirst ( {
98+ where : ( utc , { eq, and } ) =>
99+ and (
100+ eq ( utc . communityId , fieldArgs . input . communityId ) ,
101+ eq ( utc . userId , USER . id ) ,
102+ eq ( utc . role , "admin" ) ,
103+ ) ,
104+ } ) ;
105+
106+ return Boolean ( isCommunityAdmin ) ;
107+ }
108+ }
109+
110+ export class isEventAdmin extends PreExecutionRule {
111+ public async execute (
112+ { USER , DB } : GraphqlContext ,
113+ fieldArgs : { input : { eventId : string } } ,
114+ ) {
115+ if ( ! USER || ! fieldArgs ?. input ?. eventId ) {
116+ return false ;
117+ }
118+ const isEventAdmin = await DB . query . eventsToUsersSchema . findFirst ( {
119+ where : ( utc , { eq, and } ) =>
120+ and (
121+ eq ( utc . eventId , fieldArgs . input . eventId ) ,
122+ eq ( utc . userId , USER . id ) ,
123+ eq ( utc . role , "admin" ) ,
124+ ) ,
125+ } ) ;
126+
127+ return Boolean ( isEventAdmin ) ;
128+ }
129+ }
130+
131+ export class canApproveTicket extends PreExecutionRule {
132+ public async execute (
133+ { USER , DB } : GraphqlContext ,
134+ fieldArgs : { userTicketId : string } ,
135+ ) {
136+ if ( ! USER || ! fieldArgs ?. userTicketId ) {
137+ return false ;
138+ }
139+
140+ const userTicket = await DB . query . userTicketsSchema . findFirst ( {
141+ where : ( utc , { eq } ) => eq ( utc . id , fieldArgs . userTicketId ) ,
142+ with : {
143+ ticketTemplate : true ,
144+ } ,
145+ } ) ;
146+
147+ if ( ! userTicket ) {
148+ throw new GraphQLError ( "Ticket not found" ) ;
149+ }
150+
151+ if ( USER . isSuperAdmin ) {
152+ return true ;
153+ }
154+
155+ const isEventAdmin = await DB . query . eventsToUsersSchema . findFirst ( {
156+ where : ( utc , { eq, and } ) =>
157+ and (
158+ eq ( utc . eventId , userTicket ?. ticketTemplate . eventId ) ,
159+ eq ( utc . userId , USER . id ) ,
160+ eq ( utc . role , "admin" ) ,
161+ ) ,
162+ } ) ;
163+
164+ return Boolean ( isEventAdmin ) ;
165+ }
166+ }
0 commit comments