Skip to content

Commit f55e6af

Browse files
committed
fix(events): add organizerId to EventDetails response
1 parent c36d35b commit f55e6af

1 file changed

Lines changed: 19 additions & 30 deletions

File tree

apps/backend/src/routes/event.ts

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
2-
import { createEventSchema, joinEventSchema} from '../validations/event.validation.js';
3-
41
import {generateUniqueSlug} from '../utils/slug.js'
2+
import { createEventSchema} from '../validations/event.validation.js';
3+
4+
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
55

66

77
type EventDetails = {
@@ -10,6 +10,7 @@ type EventDetails = {
1010
slug: string;
1111
location: string;
1212
description: string | null;
13+
organizerId: string;
1314
organizerUsername: string;
1415
organizerDisplayName: string;
1516
startDate: Date;
@@ -57,31 +58,18 @@ type EventWithAttendees = {
5758
}[];
5859
}
5960

60-
export 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+
export async function eventRoutes(app:FastifyInstance): Promise<void> {
62+
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) => {
63+
const userId = request.user.id;
7664
const parsed = createEventSchema.safeParse(request.body);
7765
if(!parsed.success){
7866
return reply.status(400).send({error: 'Bad request'})
7967
}
8068

8169
const {name, description, startDate, endDate, isPublic ,location} = parsed.data
8270

83-
let finalSlug = await generateUniqueSlug(name, async(slug) => {
84-
const existing = await app.prisma.event.findUnique({where: {slug : slug}})
71+
const finalSlug = await generateUniqueSlug(name, async(slug) => {
72+
const existing = await app.prisma.event.findUnique({where: {slug}})
8573

8674
return !!existing
8775
})
@@ -95,7 +83,7 @@ export async function eventRoutes(app:FastifyInstance) {
9583
name,
9684
description,
9785
slug: finalSlug,
98-
location: location,
86+
location,
9987
startDate: startDateObj,
10088
endDate: endDateObj,
10189
isPublic: isPublic ?? true,
@@ -104,7 +92,7 @@ export async function eventRoutes(app:FastifyInstance) {
10492
})
10593

10694
return reply.status(201).send(newEvent);
107-
} catch (error) {
95+
} catch (_error) {
10896
app.log.error('Failed to create event');
10997
return reply.status(500).send({error: 'Failed to create event'})
11098
}
@@ -142,6 +130,7 @@ export async function eventRoutes(app:FastifyInstance) {
142130
slug: details.slug,
143131
description: details.description,
144132
location: details.location,
133+
organizerId: details.organizerId,
145134
organizerUsername: details.organizer.username,
146135
organizerDisplayName: details.organizer.displayName,
147136
startDate: details.startDate,
@@ -153,8 +142,8 @@ export async function eventRoutes(app:FastifyInstance) {
153142
return response;
154143
})
155144

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;
145+
app.post<{ Params: { slug: string } }>('/:slug/join', {preHandler: [(req, reply) => app.authenticate(req, reply)]}, async(request, reply) => {
146+
const userId = request.user.id;
158147
const paramsSlug = request.params.slug;
159148

160149
const event = await app.prisma.event.findUnique({
@@ -171,7 +160,7 @@ export async function eventRoutes(app:FastifyInstance) {
171160
await app.prisma.eventAttendee.create({
172161
data: {
173162
eventId: event.id,
174-
userId: userId,
163+
userId,
175164
joinedAt: new Date()
176165
}
177166
})
@@ -186,9 +175,9 @@ export async function eventRoutes(app:FastifyInstance) {
186175
}
187176

188177
})
178+
app.delete<{Params: {slug: string}}>('/:slug/leave',{preHandler: [(req, reply) => app.authenticate(req, reply)]}, async(request, reply) => {
189179

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;
180+
const userId = request.user.id;
192181
const paramsSlug = request.params.slug;
193182

194183
const event = await app.prisma.event.findUnique({
@@ -205,12 +194,12 @@ export async function eventRoutes(app:FastifyInstance) {
205194
await app.prisma.eventAttendee.delete({
206195
where: {
207196
userId_eventId: {
208-
userId: userId,
197+
userId,
209198
eventId: event.id
210199
}
211200
}
212201
})
213-
return reply.status(204).send({message: 'User left'})
202+
return reply.status(204).send()
214203
} catch (error:any) {
215204
if(error.code === 'P2025'){
216205
return reply.status(404).send({error: 'User not found'})

0 commit comments

Comments
 (0)