106 lines
3.5 KiB
TypeScript
106 lines
3.5 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { requireRole } from '@/lib/auth';
|
|
import { db } from '@/lib/db';
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
export async function GET() {
|
|
try {
|
|
await requireRole('admin');
|
|
|
|
const { rows } = await db.query(
|
|
`SELECT t.id, t.user_id, t.room_id, t.check_in, t.check_out, t.notes, t.status, t.created_at,
|
|
u.username, r.name as room_name
|
|
FROM trips t
|
|
JOIN users u ON t.user_id = u.id
|
|
LEFT JOIN rooms r ON t.room_id = r.id
|
|
ORDER BY t.check_in DESC`
|
|
);
|
|
|
|
return NextResponse.json({ trips: rows });
|
|
} catch (error: any) {
|
|
if (error.message === 'Unauthorized') {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
console.error('GET /api/admin/trips error:', error);
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
await requireRole('admin');
|
|
const { user_id, room_id, check_in, check_out, notes, status } = await request.json();
|
|
|
|
if (!user_id || !check_in || !check_out) {
|
|
return NextResponse.json({ error: 'user_id, check_in, and check_out are required' }, { status: 400 });
|
|
}
|
|
|
|
const { rows } = await db.query(
|
|
`INSERT INTO trips (user_id, room_id, check_in, check_out, notes, status)
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
RETURNING *`,
|
|
[user_id, room_id || null, check_in, check_out, notes || null, status || 'confirmed']
|
|
);
|
|
|
|
return NextResponse.json({ trip: rows[0] });
|
|
} catch (error: any) {
|
|
if (error.message === 'Unauthorized') {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
console.error('POST /api/admin/trips error:', error);
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function PUT(request: NextRequest) {
|
|
try {
|
|
await requireRole('admin');
|
|
const { id, user_id, room_id, check_in, check_out, notes, status } = await request.json();
|
|
|
|
if (!id) {
|
|
return NextResponse.json({ error: 'Trip id is required' }, { status: 400 });
|
|
}
|
|
|
|
const { rows } = await db.query(
|
|
`UPDATE trips
|
|
SET user_id = COALESCE($1, user_id),
|
|
room_id = COALESCE($2, room_id),
|
|
check_in = COALESCE($3, check_in),
|
|
check_out = COALESCE($4, check_out),
|
|
notes = COALESCE($5, notes),
|
|
status = COALESCE($6, status)
|
|
WHERE id = $7
|
|
RETURNING *`,
|
|
[user_id, room_id, check_in, check_out, notes, status, id]
|
|
);
|
|
|
|
return NextResponse.json({ trip: rows[0] });
|
|
} catch (error: any) {
|
|
if (error.message === 'Unauthorized') {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
console.error('PUT /api/admin/trips error:', error);
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function DELETE(request: NextRequest) {
|
|
try {
|
|
await requireRole('admin');
|
|
const { id } = await request.json();
|
|
|
|
if (!id) {
|
|
return NextResponse.json({ error: 'Trip id is required' }, { status: 400 });
|
|
}
|
|
|
|
await db.query('DELETE FROM trips WHERE id = $1', [id]);
|
|
return NextResponse.json({ success: true });
|
|
} catch (error: any) {
|
|
if (error.message === 'Unauthorized') {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
console.error('DELETE /api/admin/trips error:', error);
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
|
}
|
|
} |