feat: user portal with trips, wifi, welcome message, chat; admin trips & messages mgmt
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
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(request: NextRequest) {
|
||||
try {
|
||||
await requireRole('admin');
|
||||
const userId = request.nextUrl.searchParams.get('user_id');
|
||||
|
||||
if (userId) {
|
||||
const { rows } = await db.query(
|
||||
`SELECT m.id, m.user_id, m.sender_role, m.message, m.created_at, u.username
|
||||
FROM messages m
|
||||
JOIN users u ON m.user_id = u.id
|
||||
WHERE m.user_id = $1
|
||||
ORDER BY m.created_at ASC`,
|
||||
[userId]
|
||||
);
|
||||
return NextResponse.json({ messages: rows });
|
||||
}
|
||||
|
||||
const { rows } = await db.query(
|
||||
`SELECT DISTINCT ON (m.user_id) m.user_id, m.created_at, u.username, m.message
|
||||
FROM messages m
|
||||
JOIN users u ON m.user_id = u.id
|
||||
ORDER BY m.user_id, m.created_at DESC`
|
||||
);
|
||||
return NextResponse.json({ threads: rows });
|
||||
} catch (error: any) {
|
||||
if (error.message === 'Unauthorized') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
console.error('GET /api/admin/messages error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
await requireRole('admin');
|
||||
const { user_id, message } = await request.json();
|
||||
|
||||
if (!user_id || !message || typeof message !== 'string') {
|
||||
return NextResponse.json({ error: 'user_id and message are required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const { rows } = await db.query(
|
||||
`INSERT INTO messages (user_id, sender_role, message) VALUES ($1, 'admin', $2) RETURNING *`,
|
||||
[user_id, message.trim()]
|
||||
);
|
||||
|
||||
return NextResponse.json({ message: rows[0] });
|
||||
} catch (error: any) {
|
||||
if (error.message === 'Unauthorized') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
console.error('POST /api/admin/messages error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
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 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
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 {
|
||||
const session = await requireRole('user');
|
||||
const userId = session.id;
|
||||
|
||||
const { rows } = await db.query(
|
||||
`SELECT id, sender_role, message, created_at
|
||||
FROM messages
|
||||
WHERE user_id = $1
|
||||
ORDER BY created_at ASC`,
|
||||
[userId]
|
||||
);
|
||||
|
||||
return NextResponse.json({ messages: rows });
|
||||
} catch (error: any) {
|
||||
if (error.message === 'Unauthorized') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
console.error('GET /api/user/messages error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const session = await requireRole('user');
|
||||
const userId = session.id;
|
||||
const { message } = await request.json();
|
||||
|
||||
if (!message || typeof message !== 'string' || message.trim().length === 0) {
|
||||
return NextResponse.json({ error: 'Message is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const { rows } = await db.query(
|
||||
`INSERT INTO messages (user_id, sender_role, message) VALUES ($1, 'user', $2) RETURNING *`,
|
||||
[userId, message.trim()]
|
||||
);
|
||||
|
||||
return NextResponse.json({ message: rows[0] });
|
||||
} catch (error: any) {
|
||||
if (error.message === 'Unauthorized') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
console.error('POST /api/user/messages error:', error);
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { NextResponse } from 'next/server';
|
||||
import { requireRole } from '@/lib/auth';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
export const dynamic = 'force-dynamic'; // Prevents static generation
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
@@ -32,12 +32,23 @@ export async function GET() {
|
||||
);
|
||||
const wifiPassword = configRows[0]?.value || '';
|
||||
|
||||
// Get user's trips (future and current)
|
||||
const { rows: trips } = await db.query(
|
||||
`SELECT t.id, t.room_id, t.check_in, t.check_out, t.notes, t.status, r.name as room_name
|
||||
FROM trips t
|
||||
LEFT JOIN rooms r ON t.room_id = r.id
|
||||
WHERE t.user_id = $1 AND t.check_out >= CURRENT_DATE
|
||||
ORDER BY t.check_in ASC`,
|
||||
[userId]
|
||||
);
|
||||
|
||||
return NextResponse.json({
|
||||
reservations,
|
||||
coupons,
|
||||
offers,
|
||||
benefits: benefits.map((b: { text: string }) => b.text),
|
||||
wifiPassword,
|
||||
trips,
|
||||
});
|
||||
} catch (error) {
|
||||
if ((error as Error).message === 'Unauthorized') {
|
||||
|
||||
Reference in New Issue
Block a user