11import express , { Request , Response } from 'express' ;
22import { getUser , getUserByPhoneNumber } from '../services/userService' ;
3- import {
3+ import {
44 getEvent ,
5- createEvent ,
6- addCohost ,
5+ createEvent ,
6+ addCohost ,
77 removeCohost ,
88 updateEvent ,
99 deleteEvent ,
@@ -13,19 +13,20 @@ import {
1313 addTravelDetails
1414} from '../services/eventService' ;
1515import { verifyIdToken } from '../middleware/verifyIdToken' ;
16+ import { parseMultipartForm , uploadFilesToSupabase } from '../lib/fileUpload' ;
1617
1718const router = express . Router ( ) ;
1819
1920router . get ( '/:eventId' , verifyIdToken , async ( req : Request , res : Response ) => {
2021 try {
2122 const { eventId } = req . params ;
2223 const event = await getEvent ( eventId ) ;
23-
24+
2425 if ( ! event ) {
2526 res . status ( 404 ) . json ( { message : 'Event not found' } ) ;
2627 return ;
2728 }
28-
29+
2930 res . status ( 200 ) . json ( event ) ;
3031 } catch ( error ) {
3132 console . error ( error ) ;
@@ -36,20 +37,30 @@ router.get('/:eventId', verifyIdToken, async (req: Request, res: Response) => {
3637// Create Event
3738router . post ( '/create' , verifyIdToken , async ( req : Request , res : Response ) => {
3839 try {
39- const userId = req . userId
40- const { title, type, start_date_time, end_date_time, location, address, message, image} = req . body
41-
40+ const userId = req . userId ;
41+
42+ const { fields, files } = await parseMultipartForm ( req ) ;
43+
44+ const { title, type, start_date_time, end_date_time, location, address, message } = fields ;
45+
4246 if ( ! title || ! type || ! start_date_time || ( ! location && ! address ) ) {
43- res . status ( 401 ) . json ( { message : 'Missing required fields' } )
47+ res . status ( 401 ) . json ( { message : 'Missing required fields' } )
4448 return
4549 }
46-
50+
4751 const user = await getUser ( userId )
4852 if ( ! user ) {
49- res . status ( 404 ) . json ( { message : 'User not found' } )
53+ res . status ( 404 ) . json ( { message : 'User not found' } )
5054 return
5155 }
52-
56+
57+ let imageUrls : string [ ] = [ ] ;
58+ if ( files && files . length > 0 ) {
59+ imageUrls = await uploadFilesToSupabase ( files , 'event-images' ) ;
60+ }
61+
62+ const image = imageUrls . length > 0 ? imageUrls [ 0 ] : ( fields . image || '' ) ;
63+
5364 const { success, event, error } = await createEvent ( {
5465 title,
5566 type,
@@ -61,11 +72,11 @@ router.post('/create', verifyIdToken, async (req: Request, res: Response) => {
6172 image,
6273 hostId : userId
6374 } ) ;
64-
75+
6576 if ( success ) {
66- res . status ( 200 ) . json ( { message : 'Event created successfully' , event} )
77+ res . status ( 200 ) . json ( { message : 'Event created successfully' , event } )
6778 } else {
68- res . status ( 500 ) . json ( { message : error ?? 'Internal Server Error' } )
79+ res . status ( 500 ) . json ( { message : error ?? 'Internal Server Error' } )
6980 }
7081 } catch ( error ) {
7182 console . error ( error ) ;
@@ -76,29 +87,29 @@ router.post('/create', verifyIdToken, async (req: Request, res: Response) => {
7687router . patch ( '/update' , verifyIdToken , async ( req : Request , res : Response ) => {
7788 try {
7889 const userId = req . userId
79-
80- const { eventId, title, type, start_date_time, end_date_time, location, address, message} = req . body
90+
91+ const { eventId, title, type, start_date_time, end_date_time, location, address, message } = req . body
8192 if ( ! eventId ) {
82- res . status ( 404 ) . json ( { message : 'No event Id provided' } )
93+ res . status ( 404 ) . json ( { message : 'No event Id provided' } )
8394 }
84-
95+
8596 if ( ! title && ! type && ! start_date_time && ! end_date_time && ! location && ! address && ! message ) {
86- res . status ( 400 ) . json ( { message : 'Nothing to change' } )
97+ res . status ( 400 ) . json ( { message : 'Nothing to change' } )
8798 return
8899 }
89-
100+
90101 const user = await getUser ( userId )
91102 if ( ! user ) {
92- res . status ( 404 ) . json ( { message : 'User not found' } )
103+ res . status ( 404 ) . json ( { message : 'User not found' } )
93104 return
94105 }
95-
96- const { success, error, event} = await updateEvent ( eventId , { title, type, start_date_time, end_date_time, location, address, message} )
97-
106+
107+ const { success, error, event } = await updateEvent ( eventId , { title, type, start_date_time, end_date_time, location, address, message } )
108+
98109 if ( success ) {
99110 res . status ( 200 ) . json ( event )
100111 } else {
101- res . status ( 500 ) . json ( { message : error ?? 'Internal Server Error' } )
112+ res . status ( 500 ) . json ( { message : error ?? 'Internal Server Error' } )
102113 }
103114 } catch ( error ) {
104115 console . error ( error ) ;
@@ -110,31 +121,31 @@ router.patch('/update', verifyIdToken, async (req: Request, res: Response) => {
110121router . patch ( '/cohost/add' , verifyIdToken , async ( req : Request , res : Response ) => {
111122 try {
112123 const userId = req . userId
113- const { eventId, phoneNumber} = req . body
114-
124+ const { eventId, phoneNumber } = req . body
125+
115126 if ( ! eventId || ! phoneNumber ) {
116- res . status ( 401 ) . json ( { message : 'Bad request(body is missing one or both parameters)' } )
127+ res . status ( 401 ) . json ( { message : 'Bad request(body is missing one or both parameters)' } )
117128 return
118129 }
119-
130+
120131 const user = await getUser ( userId )
121132 if ( ! user ) {
122- res . status ( 404 ) . json ( { message : 'User not found' } )
133+ res . status ( 404 ) . json ( { message : 'User not found' } )
123134 return
124135 }
125-
136+
126137 const cohost = await getUserByPhoneNumber ( phoneNumber )
127138 if ( ! cohost ) {
128- res . status ( 404 ) . json ( { message : 'Cohost not found' } )
139+ res . status ( 404 ) . json ( { message : 'Cohost not found' } )
129140 return
130141 }
131-
142+
132143 const { success, event, error } = await addCohost ( eventId , cohost . id ) ;
133-
144+
134145 if ( success ) {
135- res . status ( 200 ) . json ( { message : 'Cohost added successfully' , event} )
146+ res . status ( 200 ) . json ( { message : 'Cohost added successfully' , event } )
136147 } else {
137- res . status ( 500 ) . json ( { message : error ?? 'Internal Server Error' } )
148+ res . status ( 500 ) . json ( { message : error ?? 'Internal Server Error' } )
138149 }
139150 } catch ( error ) {
140151 console . error ( error ) ;
@@ -146,31 +157,31 @@ router.patch('/cohost/add', verifyIdToken, async (req: Request, res: Response) =
146157router . patch ( '/cohost/remove' , verifyIdToken , async ( req : Request , res : Response ) => {
147158 try {
148159 const userId = req . userId
149- const { eventId, phoneNumber} = req . body
150-
160+ const { eventId, phoneNumber } = req . body
161+
151162 if ( ! eventId || ! phoneNumber ) {
152- res . status ( 401 ) . json ( { message : 'Missing required fields' } )
163+ res . status ( 401 ) . json ( { message : 'Missing required fields' } )
153164 return
154165 }
155-
166+
156167 const user = await getUser ( userId )
157168 if ( ! user ) {
158- res . status ( 404 ) . json ( { message : 'User not found' } )
169+ res . status ( 404 ) . json ( { message : 'User not found' } )
159170 return
160171 }
161-
172+
162173 const cohost = await getUserByPhoneNumber ( phoneNumber )
163174 if ( ! cohost ) {
164- res . status ( 404 ) . json ( { message : 'Cohost not found' } )
175+ res . status ( 404 ) . json ( { message : 'Cohost not found' } )
165176 return
166177 }
167-
178+
168179 const { success, event, error } = await removeCohost ( eventId , cohost . id ) ;
169-
180+
170181 if ( success ) {
171- res . status ( 200 ) . json ( { message : 'Cohost removed successfully' , event} )
182+ res . status ( 200 ) . json ( { message : 'Cohost removed successfully' , event } )
172183 } else {
173- res . status ( 500 ) . json ( { message : error ?? 'Internal Server Error' } )
184+ res . status ( 500 ) . json ( { message : error ?? 'Internal Server Error' } )
174185 }
175186 } catch ( error ) {
176187 console . error ( error ) ;
@@ -182,19 +193,31 @@ router.patch('/cohost/remove', verifyIdToken, async (req: Request, res: Response
182193router . post ( '/add-wedding-details' , verifyIdToken , async ( req : Request , res : Response ) => {
183194 try {
184195 const userId = req . userId ;
185- const { eventId, bride_name, groom_name, bride_details, groom_details, bride_groom_images, hashtag } = req . body ;
186-
196+
197+ const { fields, files } = await parseMultipartForm ( req ) ;
198+
199+ const { eventId, bride_name, groom_name, bride_details, groom_details, hashtag } = fields ;
200+
187201 if ( ! eventId || ! bride_name || ! groom_name ) {
188202 res . status ( 400 ) . json ( { message : 'Missing required fields: eventId, bride_name, groom_name' } ) ;
189203 return ;
190204 }
191-
205+
192206 const user = await getUser ( userId ) ;
193207 if ( ! user ) {
194208 res . status ( 404 ) . json ( { message : 'User not found' } ) ;
195209 return ;
196210 }
197-
211+
212+ // Upload files to Supabase if any
213+ let imageUrls : string [ ] = [ ] ;
214+ if ( files && files . length > 0 ) {
215+ imageUrls = await uploadFilesToSupabase ( files , 'wedding-images' ) ;
216+ }
217+
218+ // Use the uploaded image URLs or the bride_groom_images field from fields
219+ const bride_groom_images = imageUrls . length > 0 ? imageUrls : ( fields . bride_groom_images || [ ] ) ;
220+
198221 const { success, weddingDetails, error } = await addWeddingDetails ( eventId , {
199222 bride_name,
200223 groom_name,
@@ -203,7 +226,7 @@ router.post('/add-wedding-details', verifyIdToken, async (req: Request, res: Res
203226 bride_groom_images,
204227 hashtag
205228 } ) ;
206-
229+
207230 if ( success ) {
208231 res . status ( 200 ) . json ( { message : 'Wedding details added successfully' , weddingDetails } ) ;
209232 } else {
@@ -219,24 +242,34 @@ router.post('/add-wedding-details', verifyIdToken, async (req: Request, res: Res
219242router . post ( '/add-birthday-details' , verifyIdToken , async ( req : Request , res : Response ) => {
220243 try {
221244 const userId = req . userId ;
222- const { eventId, person_image, hashtag } = req . body ;
223-
245+
246+ const { fields, files } = await parseMultipartForm ( req ) ;
247+
248+ const { eventId, hashtag } = fields ;
249+
224250 if ( ! eventId ) {
225251 res . status ( 400 ) . json ( { message : 'Missing required field: eventId' } ) ;
226252 return ;
227253 }
228-
254+
229255 const user = await getUser ( userId ) ;
230256 if ( ! user ) {
231257 res . status ( 404 ) . json ( { message : 'User not found' } ) ;
232258 return ;
233259 }
234-
260+
261+ let imageUrls : string [ ] = [ ] ;
262+ if ( files && files . length > 0 ) {
263+ imageUrls = await uploadFilesToSupabase ( files , 'birthday-images' ) ;
264+ }
265+
266+ const person_image = imageUrls . length > 0 ? imageUrls [ 0 ] : ( fields . person_image || '' ) ;
267+
235268 const { success, birthdayDetails, error } = await addBirthdayDetails ( eventId , {
236269 person_image,
237270 hashtag
238271 } ) ;
239-
272+
240273 if ( success ) {
241274 res . status ( 200 ) . json ( { message : 'Birthday details added successfully' , birthdayDetails } ) ;
242275 } else {
@@ -253,25 +286,25 @@ router.post('/add-houseparty-details', verifyIdToken, async (req: Request, res:
253286 try {
254287 const userId = req . userId ;
255288 const { eventId, cost, rules, terms, tags } = req . body ;
256-
289+
257290 if ( ! eventId ) {
258291 res . status ( 400 ) . json ( { message : 'Missing required field: eventId' } ) ;
259292 return ;
260293 }
261-
294+
262295 const user = await getUser ( userId ) ;
263296 if ( ! user ) {
264297 res . status ( 404 ) . json ( { message : 'User not found' } ) ;
265298 return ;
266299 }
267-
300+
268301 const { success, housePartyDetails, error } = await addHousePartyDetails ( eventId , {
269302 cost,
270303 rules,
271304 terms,
272305 tags
273306 } ) ;
274-
307+
275308 if ( success ) {
276309 res . status ( 200 ) . json ( { message : 'House party details added successfully' , housePartyDetails } ) ;
277310 } else {
@@ -288,18 +321,18 @@ router.post('/add-travel-details', verifyIdToken, async (req: Request, res: Resp
288321 try {
289322 const userId = req . userId ;
290323 const { eventId, cost, terms, itinerary_included, itinerary_excluded, rules, tags } = req . body ;
291-
324+
292325 if ( ! eventId ) {
293326 res . status ( 400 ) . json ( { message : 'Missing required field: eventId' } ) ;
294327 return ;
295328 }
296-
329+
297330 const user = await getUser ( userId ) ;
298331 if ( ! user ) {
299332 res . status ( 404 ) . json ( { message : 'User not found' } ) ;
300333 return ;
301334 }
302-
335+
303336 const { success, travelDetails, error } = await addTravelDetails ( eventId , {
304337 cost,
305338 terms,
@@ -308,7 +341,7 @@ router.post('/add-travel-details', verifyIdToken, async (req: Request, res: Resp
308341 rules,
309342 tags
310343 } ) ;
311-
344+
312345 if ( success ) {
313346 res . status ( 200 ) . json ( { message : 'Travel details added successfully' , travelDetails } ) ;
314347 } else {
@@ -325,31 +358,31 @@ router.delete('/:eventId', verifyIdToken, async (req: Request, res: Response) =>
325358 try {
326359 const userId = req . userId ;
327360 const { eventId } = req . params ;
328-
361+
329362 if ( ! eventId ) {
330363 res . status ( 400 ) . json ( { message : 'Event ID is required' } ) ;
331364 return ;
332365 }
333-
366+
334367 const user = await getUser ( userId ) ;
335368 if ( ! user ) {
336369 res . status ( 404 ) . json ( { message : 'User not found' } ) ;
337370 return ;
338371 }
339-
372+
340373 const event = await getEvent ( eventId ) ;
341374 if ( ! event ) {
342375 res . status ( 404 ) . json ( { message : 'Event not found' } ) ;
343376 return ;
344377 }
345-
378+
346379 if ( event . hostId !== userId ) {
347380 res . status ( 403 ) . json ( { message : 'Only the host can delete this event' } ) ;
348381 return ;
349382 }
350-
383+
351384 const { success, error } = await deleteEvent ( eventId ) ;
352-
385+
353386 if ( success ) {
354387 res . status ( 200 ) . json ( { message : 'Event deleted successfully' } ) ;
355388 } else {
0 commit comments