315 lines
8 KiB
TypeScript
315 lines
8 KiB
TypeScript
import { prisma } from '@/prisma';
|
|
import { auth } from '@/auth';
|
|
import { NextResponse } from 'next/server';
|
|
import { z } from 'zod/v4';
|
|
import { userIdSchema } from '@/lib/validation/user';
|
|
|
|
const postParticipantSchema = z.object({
|
|
userId: userIdSchema,
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /api/event/{eventID}/participant:
|
|
* get:
|
|
* summary: Get participants of an event
|
|
* description: Returns all participants of a specific event.
|
|
* tags:
|
|
* - Event_Participant
|
|
* parameters:
|
|
* - in: path
|
|
* name: eventID
|
|
* required: true
|
|
* schema:
|
|
* type: string
|
|
* description: The ID of the event.
|
|
* responses:
|
|
* 200:
|
|
* description: List of participants for the event.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* success:
|
|
* type: boolean
|
|
* participants:
|
|
* type: array
|
|
* items:
|
|
* $ref: '#/components/schemas/Participant'
|
|
* 401:
|
|
* description: Not authenticated.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/ErrorResponse'
|
|
* example:
|
|
* success: false
|
|
* message: Not authenticated
|
|
* 404:
|
|
* description: User not found.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/ErrorResponse'
|
|
* example:
|
|
* success: false
|
|
* message: User not found
|
|
* 403:
|
|
* description: User is not a participant or organizer of this event.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/ErrorResponse'
|
|
* example:
|
|
* success: false
|
|
* message: User is not a participant or organizer of this event
|
|
*/
|
|
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 isParticipant = await prisma.meetingParticipant.findFirst({
|
|
where: {
|
|
meeting_id: eventID,
|
|
user_id: dbUser.id,
|
|
},
|
|
});
|
|
|
|
const isOrganizer = await prisma.meeting.findFirst({
|
|
where: {
|
|
id: eventID,
|
|
organizer_id: dbUser.id,
|
|
},
|
|
});
|
|
|
|
if (!isParticipant && !isOrganizer) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
message: 'User is not a participant or organizer of this event',
|
|
},
|
|
{ status: 403 },
|
|
);
|
|
}
|
|
|
|
const participants = await prisma.meetingParticipant.findMany({
|
|
where: {
|
|
meeting_id: eventID,
|
|
},
|
|
select: {
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
},
|
|
},
|
|
status: true,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
participants,
|
|
});
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /api/event/{eventID}/participant:
|
|
* post:
|
|
* summary: Add a participant to an event
|
|
* description: Adds a user as a participant to a specific event.
|
|
* tags:
|
|
* - Event_Participant
|
|
* parameters:
|
|
* - in: path
|
|
* name: eventID
|
|
* required: true
|
|
* schema:
|
|
* type: string
|
|
* description: The ID of the event.
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* userId:
|
|
* type: string
|
|
* description: The ID of the user to add as a participant.
|
|
* responses:
|
|
* 200:
|
|
* description: Participant added successfully.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* success:
|
|
* type: boolean
|
|
* participant:
|
|
* $ref: '#/components/schemas/Participant'
|
|
* 400:
|
|
* description: Bad request due to invalid input data.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/ErrorResponse'
|
|
* example:
|
|
* success: false
|
|
* message: 'Invalid input data'
|
|
* 401:
|
|
* description: Not authenticated.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/ErrorResponse'
|
|
* example:
|
|
* success: false
|
|
* message: Not authenticated
|
|
* 404:
|
|
* description: User not found.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/ErrorResponse'
|
|
* example:
|
|
* success: false
|
|
* message: User not found
|
|
* 403:
|
|
* description: Only organizers can add participants.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/ErrorResponse'
|
|
* example:
|
|
* success: false
|
|
* message: Only organizers can add participants
|
|
* 409:
|
|
* description: User is already a participant of this event.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/ErrorResponse'
|
|
* example:
|
|
* success: false
|
|
* message: User is already a participant of this event
|
|
*/
|
|
export const POST = 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 isOrganizer = await prisma.meeting.findFirst({
|
|
where: {
|
|
id: eventID,
|
|
organizer_id: dbUser.id,
|
|
},
|
|
});
|
|
|
|
if (!isOrganizer) {
|
|
return NextResponse.json(
|
|
{ success: false, message: 'Only organizers can add participants' },
|
|
{ status: 403 },
|
|
);
|
|
}
|
|
|
|
const dataRaw = await req.json();
|
|
const data = await postParticipantSchema.safeParseAsync(dataRaw);
|
|
if (!data.success) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
message: 'Invalid request data',
|
|
errors: data.error.issues,
|
|
},
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
const { userId } = data.data;
|
|
|
|
const participantExists = await prisma.meetingParticipant.findFirst({
|
|
where: {
|
|
meeting_id: eventID,
|
|
user_id: userId,
|
|
},
|
|
});
|
|
|
|
if (participantExists) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
message: 'User is already a participant of this event',
|
|
},
|
|
{ status: 409 },
|
|
);
|
|
}
|
|
|
|
const newParticipant = await prisma.meetingParticipant.create({
|
|
data: {
|
|
meeting_id: eventID,
|
|
user_id: userId,
|
|
},
|
|
include: {
|
|
user: true,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
participant: {
|
|
user: {
|
|
id: newParticipant.user.id,
|
|
name: newParticipant.user.name,
|
|
},
|
|
status: newParticipant.status,
|
|
},
|
|
});
|
|
});
|