1- import type { FastifyInstance , FastifyRequest , FastifyReply } from 'fastify' ;
2- import { createEventSchema , joinEventSchema } from '../validations/event.validation.js' ;
3-
41import { generateUniqueSlug } from '../utils/slug.js'
2+ import { createEventSchema } from '../validations/event.validation.js' ;
3+
4+ import type { FastifyInstance , FastifyRequest , FastifyReply } from 'fastify' ;
55
66
77type EventDetails = {
@@ -58,30 +58,17 @@ type EventWithAttendees = {
5858}
5959
6060export async function eventRoutes ( app :FastifyInstance ) {
61- app . post ( '/' , { preHandler : [ async ( request , reply ) => {
62- const server = request . server as any ;
63- if ( typeof server ?. authenticate === 'function' ) { await server . authenticate ( request , reply ) ; return }
64- if ( typeof ( app as any ) . authenticate === 'function' ) { await ( app as any ) . authenticate ( request , reply ) ; return }
65- try { await request . jwtVerify ( ) } catch ( e ) { reply . status ( 401 ) . send ( { error : 'Unauthorized' } ) }
66- } ] } , async ( request : FastifyRequest < {
67- Body : {
68- name : string ,
69- description ?: string ,
70- startDate : string ,
71- location : string ,
72- endDate : string ,
73- isPublic ?: boolean
74- } } > , reply : FastifyReply ) => {
75- const userId = ( request . user as any ) . id ;
61+ app . post < { Body : { name : string ; description ?: string ; startDate : string ; location : string ; endDate : string ; isPublic ?: boolean ; } } > ( '/' , { preHandler : [ ( req , reply ) => app . authenticate ( req , reply ) ] } , async ( request , reply ) => {
62+ const userId = request . user . id ;
7663 const parsed = createEventSchema . safeParse ( request . body ) ;
7764 if ( ! parsed . success ) {
7865 return reply . status ( 400 ) . send ( { error : 'Bad request' } )
7966 }
8067
8168 const { name, description, startDate, endDate, isPublic , location} = parsed . data
8269
83- let finalSlug = await generateUniqueSlug ( name , async ( slug ) => {
84- const existing = await app . prisma . event . findUnique ( { where : { slug : slug } } )
70+ const finalSlug = await generateUniqueSlug ( name , async ( slug ) => {
71+ const existing = await app . prisma . event . findUnique ( { where : { slug} } )
8572
8673 return ! ! existing
8774 } )
@@ -95,7 +82,7 @@ export async function eventRoutes(app:FastifyInstance) {
9582 name,
9683 description,
9784 slug : finalSlug ,
98- location : location ,
85+ location,
9986 startDate : startDateObj ,
10087 endDate : endDateObj ,
10188 isPublic : isPublic ?? true ,
@@ -104,7 +91,7 @@ export async function eventRoutes(app:FastifyInstance) {
10491 } )
10592
10693 return reply . status ( 201 ) . send ( newEvent ) ;
107- } catch ( error ) {
94+ } catch ( _error ) {
10895 app . log . error ( 'Failed to create event' ) ;
10996 return reply . status ( 500 ) . send ( { error : 'Failed to create event' } )
11097 }
@@ -153,8 +140,8 @@ export async function eventRoutes(app:FastifyInstance) {
153140 return response ;
154141 } )
155142
156- app . post ( '/:slug/join' , { preHandler : [ async ( request , reply ) => { const server = request . server as any ; if ( typeof server ?. authenticate === 'function' ) { await server . authenticate ( request , reply ) ; return } if ( typeof ( app as any ) . authenticate === 'function' ) { await ( app as any ) . authenticate ( request , reply ) ; return } try { await request . jwtVerify ( ) } catch ( e ) { reply . status ( 401 ) . send ( { error : 'Unauthorized' } ) } } ] } , async ( request : FastifyRequest < { Params : { slug : string } } > , reply : FastifyReply ) => {
157- const userId = ( request . user as any ) . id ;
143+ app . post < { Params : { slug : string } } > ( '/:slug/join' , { preHandler : [ ( req , reply ) => app . authenticate ( req , reply ) ] } , async ( request , reply ) => {
144+ const userId = request . user . id ;
158145 const paramsSlug = request . params . slug ;
159146
160147 const event = await app . prisma . event . findUnique ( {
@@ -171,7 +158,7 @@ export async function eventRoutes(app:FastifyInstance) {
171158 await app . prisma . eventAttendee . create ( {
172159 data : {
173160 eventId : event . id ,
174- userId : userId ,
161+ userId,
175162 joinedAt : new Date ( )
176163 }
177164 } )
@@ -186,9 +173,9 @@ export async function eventRoutes(app:FastifyInstance) {
186173 }
187174
188175 } )
176+ app . delete < { Params : { slug : string } } > ( '/:slug/leave' , { preHandler : [ ( req , reply ) => app . authenticate ( req , reply ) ] } , async ( request , reply ) => {
189177
190- app . delete ( '/:slug/leave' , { preHandler : [ async ( request , reply ) => { const server = request . server as any ; if ( typeof server ?. authenticate === 'function' ) { await server . authenticate ( request , reply ) ; return } if ( typeof ( app as any ) . authenticate === 'function' ) { await ( app as any ) . authenticate ( request , reply ) ; return } try { await request . jwtVerify ( ) } catch ( e ) { reply . status ( 401 ) . send ( { error : 'Unauthorized' } ) } } ] } , async ( request : FastifyRequest < { Params : { slug : string } } > , reply : FastifyReply ) => {
191- const userId = ( request . user as any ) . id ;
178+ const userId = request . user . id ;
192179 const paramsSlug = request . params . slug ;
193180
194181 const event = await app . prisma . event . findUnique ( {
@@ -205,7 +192,7 @@ export async function eventRoutes(app:FastifyInstance) {
205192 await app . prisma . eventAttendee . delete ( {
206193 where : {
207194 userId_eventId : {
208- userId : userId ,
195+ userId,
209196 eventId : event . id
210197 }
211198 }
0 commit comments