Buchungssystem erste überarbeitungen

This commit is contained in:
PxlLoewe
2025-09-20 00:28:53 +02:00
parent a612cf9951
commit ba027957ce
9 changed files with 136 additions and 228 deletions

View File

@@ -5,6 +5,7 @@ import { getServerSession } from "../../auth/[...nextauth]/auth";
// DELETE /api/booking/[id] - Delete a booking
export const DELETE = async (req: NextRequest, { params }: { params: { id: string } }) => {
try {
console.log(params);
const session = await getServerSession();
if (!session?.user) {
return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
@@ -117,14 +118,8 @@ export const PUT = async (req: NextRequest, { params }: { params: { id: string }
endTime: new Date(endTime),
},
include: {
user: {
select: {
id: true,
firstname: true,
lastname: true,
},
},
station: {
User: true,
Station: {
select: {
id: true,
bosCallsignShort: true,

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@repo/db";
import { getPublicUser, prisma } from "@repo/db";
import { getServerSession } from "../auth/[...nextauth]/auth";
// GET /api/booking - Get all bookings for the timeline
@@ -10,47 +10,14 @@ export const GET = async (req: NextRequest) => {
return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
}
const { searchParams } = new URL(req.url);
const startDate = searchParams.get("startDate");
const endDate = searchParams.get("endDate");
const whereClause: Record<string, unknown> = {};
// Filter by date range if provided
if (startDate && endDate) {
whereClause.OR = [
{
startTime: {
gte: new Date(startDate),
lte: new Date(endDate),
},
},
{
endTime: {
gte: new Date(startDate),
lte: new Date(endDate),
},
},
{
AND: [
{ startTime: { lte: new Date(startDate) } },
{ endTime: { gte: new Date(endDate) } },
],
},
];
}
const { searchParams } = req.nextUrl;
const filter = JSON.parse(searchParams.get("filter") || "{}");
const bookings = await prisma.booking.findMany({
where: whereClause,
where: filter,
include: {
user: {
select: {
id: true,
firstname: true,
lastname: true,
},
},
station: {
User: true,
Station: {
select: {
id: true,
bosCallsign: true,
@@ -63,7 +30,12 @@ export const GET = async (req: NextRequest) => {
},
});
return NextResponse.json({ bookings });
return NextResponse.json(
bookings.map((b) => ({
...b,
user: b.User && getPublicUser(b.User),
})),
);
} catch (error) {
console.error("Error fetching bookings:", error);
return NextResponse.json({ error: "Internal server error" }, { status: 500 });
@@ -154,14 +126,14 @@ export const POST = async (req: NextRequest) => {
endTime: new Date(endTime),
},
include: {
user: {
User: {
select: {
id: true,
firstname: true,
lastname: true,
},
},
station: {
Station: {
select: {
id: true,
bosCallsign: true,
@@ -171,7 +143,7 @@ export const POST = async (req: NextRequest) => {
},
});
return NextResponse.json({ booking }, { status: 201 });
return NextResponse.json(booking, { status: 201 });
} catch (error) {
console.error("Error creating booking:", error);
return NextResponse.json({ error: "Internal server error" }, { status: 500 });