fix(api): validate timestamps when creating events and allow setting a location

This commit is contained in:
Dominik 2025-06-16 10:07:17 +02:00
parent 2c114f0a25
commit e1e348acb6
Signed by: dominik
GPG key ID: 06A4003FC5049644
2 changed files with 31 additions and 1 deletions

View file

@ -397,6 +397,12 @@ export const PATCH = auth(async (req, { params }) => {
}
updateData.end_time = endTimeValidation.getTime().toString();
}
if (new Date(start_time) >= new Date(end_time)) {
return NextResponse.json(
{ success: false, message: 'start_time must be before end_time' },
{ status: 400 },
);
}
if (location) updateData.location = location;
if (status) {
const validStatuses = ['TENTATIVE', 'CONFIRMED', 'CANCELLED'];

View file

@ -131,6 +131,8 @@ export const GET = auth(async (req) => {
* end_time:
* type: string
* format: date-time
* location:
* type: string
* responses:
* 200:
* description: Event created successfully.
@ -184,7 +186,7 @@ export const POST = auth(async (req) => {
);
const body = await req.json();
const { title, description, start_time, end_time } = body;
const { title, description, start_time, end_time, location } = body;
if (!title || !start_time || !end_time) {
return NextResponse.json(
@ -193,12 +195,34 @@ export const POST = auth(async (req) => {
);
}
if (isNaN(new Date(start_time).getTime())) {
return NextResponse.json(
{ success: false, message: 'Invalid start_time' },
{ status: 400 },
);
}
if (isNaN(new Date(end_time).getTime())) {
return NextResponse.json(
{ success: false, message: 'Invalid end_time' },
{ status: 400 },
);
}
if (new Date(start_time) >= new Date(end_time)) {
return NextResponse.json(
{ success: false, message: 'start_time must be before end_time' },
{ status: 400 },
);
}
const newEvent = await prisma.meeting.create({
data: {
title,
description,
start_time,
end_time,
location: location || '',
organizer_id: req.auth.user.id,
},
});