import { prisma } from '@/prisma'; import { auth } from '@/auth'; import { NextResponse } from 'next/server'; import { z } from 'zod/v4'; const patchEventSchema = z.object({ title: z.string().optional(), description: z.string().optional(), start_time: z.iso.datetime().optional(), end_time: z.iso.datetime().optional(), location: z.string().optional(), status: z.enum(['TENTATIVE', 'CONFIRMED', 'CANCELLED']).optional(), }); /** * @swagger * /api/event/{eventID}: * get: * summary: Get details of a specific event * description: Returns the details of an event by its ID. * tags: * - Event * parameters: * - in: path * name: eventID * required: true * schema: * type: string * description: The ID of the event to retrieve. * responses: * 200: * description: Event details retrieved successfully. * content: * application/json: * schema: * type: object * properties: * success: * type: boolean * event: * $ref: "#/components/schemas/Event" * 401: * description: Not authenticated. * content: * application/json: * schema: * $ref: "#/components/schemas/ErrorResponse" * example: * success: false * message: Not authenticated * 404: * description: User or event not found. * content: * application/json: * schema: * $ref: "#/components/schemas/ErrorResponse" */ export const GET = auth(async (req, { params }) => { if (!req.auth) return NextResponse.json( { success: false, message: 'Not authenticated' }, { status: 401 }, ); if (!req.auth.user || !req.auth.user.id) return NextResponse.json( { success: false, message: 'User not found' }, { status: 404 }, ); const dbUser = await prisma.user.findUnique({ where: { id: req.auth.user.id, }, }); if (!dbUser) { return NextResponse.json( { success: false, message: 'User not found' }, { status: 404 }, ); } const eventID = (await params).eventID; const event = await prisma.meeting.findUnique({ where: { id: eventID, }, select: { id: true, title: true, description: true, start_time: true, end_time: true, status: true, location: true, organizer_id: true, created_at: true, updated_at: true, organizer: { select: { id: true, name: true, }, }, participants: { select: { user: { select: { id: true, name: true, }, }, status: true, }, }, }, }); if (!event) { return NextResponse.json( { success: false, message: 'Event not found' }, { status: 404 }, ); } return NextResponse.json( { success: true, event: event, }, { status: 200 }, ); }); /** * @swagger * /api/event/{eventID}: * delete: * summary: Delete a specific event * description: Deletes an event by its ID if the user is the organizer. * tags: * - Event * parameters: * - in: path * name: eventID * required: true * schema: * type: string * description: The ID of the event to delete. * responses: * 200: * description: Event deleted successfully. * content: * application/json: * schema: * type: object * properties: * success: * type: boolean * message: * type: string * 401: * description: Not authenticated. * content: * application/json: * schema: * $ref: "#/components/schemas/ErrorResponse" * example: * success: false * message: Not authenticated * 403: * description: User is not the organizer. * content: * application/json: * schema: * $ref: "#/components/schemas/ErrorResponse" * example: * success: false * message: You are not the organizer of this event * 404: * description: User or event not found. * content: * application/json: * schema: * $ref: "#/components/schemas/ErrorResponse" */ export const DELETE = auth(async (req, { params }) => { if (!req.auth) return NextResponse.json( { success: false, message: 'Not authenticated' }, { status: 401 }, ); if (!req.auth.user || !req.auth.user.id) return NextResponse.json( { success: false, message: 'User not found' }, { status: 404 }, ); const dbUser = await prisma.user.findUnique({ where: { id: req.auth.user.id, }, }); if (!dbUser) { return NextResponse.json( { success: false, message: 'User not found' }, { status: 404 }, ); } const eventID = (await params).eventID; const event = await prisma.meeting.findUnique({ where: { id: eventID, }, }); if (!event) { return NextResponse.json( { success: false, message: 'Event not found' }, { status: 404 }, ); } if (event.organizer_id !== dbUser.id) { return NextResponse.json( { success: false, message: 'You are not the organizer of this event' }, { status: 403 }, ); } await prisma.meeting.delete({ where: { id: eventID, }, }); return NextResponse.json( { success: true, message: 'Event deleted successfully' }, { status: 200 }, ); }); /** * @swagger * /api/event/{eventID}: * patch: * summary: Update a specific event * description: Updates an event by its ID if the user is the organizer. * tags: * - Event * parameters: * - in: path * name: eventID * required: true * schema: * type: string * description: The ID of the event to update. * requestBody: * required: true * content: * application/json: * schema: * type: object * properties: * title: * type: string * description: * type: string * start_time: * type: string * format: date-time * end_time: * type: string * format: date-time * location: * type: string * status: * type: string * enum: [TENTATIVE, CONFIRMED, CANCELLED] * responses: * 200: * description: Event updated successfully. * content: * application/json: * schema: * type: object * properties: * success: * type: boolean * event: * $ref: "#/components/schemas/Event" * 400: * description: Invalid request data. * content: * application/json: * schema: * $ref: "#/components/schemas/ZodErrorResponse" * 401: * description: Not authenticated. * content: * application/json: * schema: * $ref: "#/components/schemas/ErrorResponse" * example: * success: false * message: Not authenticated * 403: * description: User is not the organizer. * content: * application/json: * schema: * $ref: "#/components/schemas/ErrorResponse" * example: * success: false * message: You are not the organizer of this event * 404: * description: User or event not found. * content: * application/json: * schema: * $ref: "#/components/schemas/ErrorResponse" * example: * success: false * message: User or event not found */ export const PATCH = auth(async (req, { params }) => { if (!req.auth) return NextResponse.json( { success: false, message: 'Not authenticated' }, { status: 401 }, ); if (!req.auth.user || !req.auth.user.id) return NextResponse.json( { success: false, message: 'User not found' }, { status: 404 }, ); const dbUser = await prisma.user.findUnique({ where: { id: req.auth.user.id, }, }); if (!dbUser) { return NextResponse.json( { success: false, message: 'User not found' }, { status: 404 }, ); } const eventID = (await params).eventID; const event = await prisma.meeting.findUnique({ where: { id: eventID, }, }); if (!event) { return NextResponse.json( { success: false, message: 'Event not found' }, { status: 404 }, ); } if (event.organizer_id !== dbUser.id) { return NextResponse.json( { success: false, message: 'You are not the organizer of this event' }, { status: 403 }, ); } const dataRaw = await req.json(); const data = await patchEventSchema.safeParseAsync(dataRaw); if (!data.success) { return NextResponse.json( { success: false, message: 'Invalid input data', errors: data.error.issues, }, { status: 400 }, ); } const { title, description, start_time, end_time, location, status } = data.data; const updatedEvent = await prisma.meeting.update({ where: { id: eventID, }, data: { title, description, start_time, end_time, location, status, }, select: { id: true, title: true, description: true, start_time: true, end_time: true, status: true, location: true, organizer_id: true, created_at: true, updated_at: true, organizer: { select: { id: true, name: true, }, }, participants: { select: { user: { select: { id: true, name: true, }, }, status: true, }, }, }, }); return NextResponse.json( { success: true, event: updatedEvent, }, { status: 200 }, ); });