99 lines
2.5 KiB
TypeScript
99 lines
2.5 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) {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session || session.user.role !== 'ADMIN') {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
const locationId = searchParams.get('locationId');
|
|
const status = searchParams.get('status');
|
|
|
|
const where: any = {};
|
|
if (locationId) where.locationId = locationId;
|
|
if (status) where.status = status;
|
|
|
|
const photoboxes = await prisma.photobox.findMany({
|
|
where,
|
|
include: {
|
|
location: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
city: true,
|
|
},
|
|
},
|
|
_count: {
|
|
select: {
|
|
bookings: true,
|
|
},
|
|
},
|
|
},
|
|
orderBy: {
|
|
createdAt: 'desc',
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({ photoboxes });
|
|
} catch (error: any) {
|
|
console.error('Photobox fetch error:', error);
|
|
return NextResponse.json(
|
|
{ error: error.message || 'Failed to fetch photoboxes' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
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 {
|
|
locationId,
|
|
model,
|
|
serialNumber,
|
|
description,
|
|
purchaseDate,
|
|
} = body;
|
|
|
|
if (!locationId || !model || !serialNumber) {
|
|
return NextResponse.json(
|
|
{ error: 'Missing required fields' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const photobox = await prisma.photobox.create({
|
|
data: {
|
|
locationId,
|
|
model,
|
|
serialNumber,
|
|
description,
|
|
purchaseDate: purchaseDate ? new Date(purchaseDate) : null,
|
|
status: 'AVAILABLE',
|
|
active: true,
|
|
},
|
|
include: {
|
|
location: true,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({ photobox }, { status: 201 });
|
|
} catch (error: any) {
|
|
console.error('Photobox creation error:', error);
|
|
return NextResponse.json(
|
|
{ error: error.message || 'Failed to create photobox' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|