Files
Atlas/app/api/photoboxes/[id]/route.ts
2025-11-12 20:21:32 +01:00

120 lines
3.2 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
export async function GET(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const session = await getServerSession(authOptions);
if (!session || session.user.role !== 'ADMIN') {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const photobox = await prisma.photobox.findUnique({
where: { id: params.id },
include: {
location: true,
bookings: {
select: {
id: true,
bookingNumber: true,
eventDate: true,
status: true,
customerName: true,
},
orderBy: {
eventDate: 'desc',
},
},
},
});
if (!photobox) {
return NextResponse.json({ error: 'Photobox not found' }, { status: 404 });
}
return NextResponse.json({ photobox });
} catch (error: any) {
console.error('Photobox fetch error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to fetch photobox' },
{ status: 500 }
);
}
}
export async function PATCH(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const session = await getServerSession(authOptions);
if (!session || session.user.role !== 'ADMIN') {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const body = await request.json();
const {
model,
serialNumber,
status,
active,
description,
lastMaintenance,
} = body;
const updateData: any = {};
if (model !== undefined) updateData.model = model;
if (serialNumber !== undefined) updateData.serialNumber = serialNumber;
if (status !== undefined) updateData.status = status;
if (active !== undefined) updateData.active = active;
if (description !== undefined) updateData.description = description;
if (lastMaintenance !== undefined) {
updateData.lastMaintenance = lastMaintenance ? new Date(lastMaintenance) : null;
}
const photobox = await prisma.photobox.update({
where: { id: params.id },
data: updateData,
include: {
location: true,
},
});
return NextResponse.json({ photobox });
} catch (error: any) {
console.error('Photobox update error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to update photobox' },
{ status: 500 }
);
}
}
export async function DELETE(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const session = await getServerSession(authOptions);
if (!session || session.user.role !== 'ADMIN') {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
await prisma.photobox.delete({
where: { id: params.id },
});
return NextResponse.json({ success: true });
} catch (error: any) {
console.error('Photobox deletion error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to delete photobox' },
{ status: 500 }
);
}
}