8
8
getEventAdminPassword ,
9
9
getUsersforEvent ,
10
10
insertEvent ,
11
+ addEventAdmin ,
12
+ insertEvents4Admin ,
13
+ getAdminEvents
11
14
} from "../queries/adminQueries.js" ;
12
15
import {
13
16
EventIdValidator ,
@@ -55,21 +58,29 @@ const VerifyPaid = async (req: Request, res: Response) => {
55
58
}
56
59
57
60
const { user_email } = UserEmailValidator . parse ( req . body ) ;
58
- const { event_id } = EventIdValidator . parse ( req . body . admin ) ;
61
+ const { event_id } = EventIdValidator . parse ( req . body ) ;
62
+ console . log ( req . body . admin )
59
63
60
- const client = await pool . connect ( ) ;
61
- const result = await client . query ( allowIfPaid , [ user_email , event_id ] ) ;
62
- client . release ( ) ;
64
+ if ( req . body . admin . is_super_admin || ( req . body . admin . events_id . includes ( event_id ) ) ) {
65
+ const client = await pool . connect ( ) ;
66
+ const result = await client . query ( allowIfPaid , [ user_email , event_id ] ) ;
67
+ client . release ( ) ;
63
68
64
- if ( result . rows . length == 1 )
65
- return res
66
- . status ( 200 )
67
- . json ( { statusCode : 200 , body : { message : "Sucessfull" } } ) ;
68
- else
69
- return res . status ( 404 ) . json ( {
70
- statusCode : 404 ,
71
- body : { message : "User not Paid, User not allowed" } ,
72
- } ) ;
69
+ if ( result . rows . length == 1 )
70
+ return res
71
+ . status ( 200 )
72
+ . json ( { statusCode : 200 , body : { message : "Sucessfull" } } ) ;
73
+ else
74
+ return res . status ( 404 ) . json ( {
75
+ statusCode : 404 ,
76
+ body : { message : "User not Paid, User not allowed" } ,
77
+ } ) ;
78
+ } else {
79
+ return res . status ( 401 ) . json ( {
80
+ statusCode : 401 ,
81
+ body : { message : "Admin not Authorized" } ,
82
+ } ) ;
83
+ }
73
84
} ;
74
85
75
86
/* TO CREATE USER - "USER REGISTRATION"
@@ -114,28 +125,33 @@ const UserLogIn = async (req: Request, res: Response, next: NextFunction) => {
114
125
/* FOR EVENT CORDINATOR LOGIN
115
126
*/
116
127
const EventLogin = async ( req : Request , res : Response , next : NextFunction ) => {
117
- const { event_id , password } = EventLoginValidator . parse ( req . body ) ;
128
+ const { admin_id , password } = EventLoginValidator . parse ( req . body ) ;
118
129
119
130
const client = await pool . connect ( ) ;
120
- const data = await client . query ( getEventAdminPassword , [ event_id ] ) ;
131
+ const data = await client . query ( getEventAdminPassword , [ admin_id ] ) ;
121
132
122
133
client . release ( ) ;
123
134
124
135
if ( data . rows . length == 0 )
125
136
return res
126
137
. status ( 400 )
127
- . json ( { statusCode : 400 , body : { message : "Bad Request " } } ) ;
138
+ . json ( { statusCode : 400 , body : { message : "No Such Admin " } } ) ;
128
139
129
140
const user = data . rows [ 0 ] ;
130
- if ( await bcrypt . compare ( password , user . password ) ) next ( ) ;
141
+ if ( await bcrypt . compare ( password , user . password ) ) {
142
+ const events = await client . query ( getAdminEvents , [ admin_id ] )
143
+ let events_id : Array < string > = [ ]
144
+ events . rows . forEach ( ele => events_id . push ( ele . event_id ) )
145
+ req . body . events_id = events_id
146
+ next ( ) ;
147
+ }
131
148
else
132
149
return res
133
150
. status ( 401 )
134
151
. json ( { statusCode : 401 , body : { message : "Wrong Password" } } ) ;
135
152
} ;
136
153
137
- /* FOR GETTING USERS FROM A PARTICULAR EVENT
138
- */
154
+ /* FOR GETTING USERS FROM A PARTICULAR EVENT */
139
155
const GetUsersFromEvent = async ( req : Request , res : Response ) => {
140
156
if ( ! req . body . admin . is_event_admin ) {
141
157
return res
@@ -144,7 +160,7 @@ const GetUsersFromEvent = async (req: Request, res: Response) => {
144
160
}
145
161
146
162
const client = await pool . connect ( ) ;
147
- const { event_id } = EventIdValidator . parse ( req . body . admin ) ;
163
+ const { event_id } = EventIdValidator . parse ( req . body ) ;
148
164
const data = await client . query ( getUsersforEvent , [ event_id ] ) ;
149
165
150
166
return res . status ( 200 ) . json ( {
@@ -201,6 +217,27 @@ const CreateEvent = async (req: Request, res: Response) => {
201
217
. json ( { statusCode : 200 , body : { message : "Sucessfull" } } ) ;
202
218
} ;
203
219
220
+ const EventAdminSignUp = async ( req : Request , res : Response ) => {
221
+ const client = await pool . connect ( )
222
+ try {
223
+ const { admin_id, password, events_id} = req . body
224
+ const hashedPass = await bcrypt . hash ( password , 10 ) ;
225
+ await client . query ( begin )
226
+ await client . query ( addEventAdmin , [ admin_id , hashedPass ] )
227
+ await client . query ( insertEvents4Admin , [ admin_id , events_id ] )
228
+ await client . query ( commit )
229
+ return res
230
+ . status ( 200 )
231
+ . json ( { statusCode : 200 , body : { message : "Admin added with Events ID" } } ) ;
232
+ } catch ( err ) {
233
+ console . log ( err )
234
+ await client . query ( rollback )
235
+ return res
236
+ . status ( 500 )
237
+ . json ( { statusCode : 500 , body : { message : "Something went wrong" } } ) ;
238
+ }
239
+ }
240
+
204
241
export {
205
242
UpdatePaid ,
206
243
VerifyPaid ,
@@ -210,4 +247,5 @@ export {
210
247
GetUsersFromEvent ,
211
248
UpdateUserCart ,
212
249
CreateEvent ,
250
+ EventAdminSignUp
213
251
} ;
0 commit comments