feat: add reservation system for public and private events
- Add private_event_reservations table with contact info fields - Add status column to social_event_reservations (pending/confirmed/cancelled) - Create /api/private-event-reservations CRUD endpoints - Update social_event_reservations API to support guest submissions - Add Reservations tab to Public Events admin section - Add full reservation management UI for Private Events - Support filtering by status (pending/confirmed/cancelled/all) - Allow confirm/cancel/reactivate/delete actions
This commit is contained in:
+188
-2
@@ -767,6 +767,44 @@ function CalendarEditor({ event, setEvent, onSave, onCancel }: any) {
|
|||||||
function SocialSection({ events, setEvents, images, onToast }: any) {
|
function SocialSection({ events, setEvents, images, onToast }: any) {
|
||||||
const [editing, setEditing] = useState<SocialEvent | null>(null);
|
const [editing, setEditing] = useState<SocialEvent | null>(null);
|
||||||
const [confirmDel, setConfirmDel] = useState<number | null>(null);
|
const [confirmDel, setConfirmDel] = useState<number | null>(null);
|
||||||
|
const [tab, setTab] = useState<'events' | 'reservations'>('events');
|
||||||
|
const [reservations, setReservations] = useState<any[]>([]);
|
||||||
|
const [statusFilter, setStatusFilter] = useState<'pending' | 'confirmed' | 'cancelled' | 'all'>('pending');
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (tab === 'reservations') loadReservations();
|
||||||
|
}, [tab, statusFilter]);
|
||||||
|
|
||||||
|
const loadReservations = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
|
const url = statusFilter === 'all' ? '/api/social_event_reservations' : `/api/social_event_reservations?status=${statusFilter}`;
|
||||||
|
const res = await fetch(url);
|
||||||
|
const data = await res.json();
|
||||||
|
setReservations(data.data || []);
|
||||||
|
} catch { onToast('Error loading reservations', 'error'); }
|
||||||
|
setLoading(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateStatus = async (id: number, status: 'confirmed' | 'cancelled') => {
|
||||||
|
try {
|
||||||
|
await fetch(`/api/social_event_reservations/${id}`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ status }),
|
||||||
|
});
|
||||||
|
setReservations(r => r.map(x => x.id === id ? { ...x, status } : x));
|
||||||
|
onToast(`Reservation ${status}`, 'success');
|
||||||
|
} catch { onToast('Error updating reservation', 'error'); }
|
||||||
|
};
|
||||||
|
|
||||||
|
const delReservation = async (id: number) => {
|
||||||
|
await fetch(`/api/social_event_reservations/${id}`, { method: 'DELETE' });
|
||||||
|
setReservations(r => r.filter(x => x.id !== id));
|
||||||
|
onToast('Reservation deleted', 'success');
|
||||||
|
setConfirmDel(null);
|
||||||
|
};
|
||||||
|
|
||||||
const save = async (ev: SocialEvent) => {
|
const save = async (ev: SocialEvent) => {
|
||||||
try {
|
try {
|
||||||
@@ -792,8 +830,18 @@ function SocialSection({ events, setEvents, images, onToast }: any) {
|
|||||||
setConfirmDel(null);
|
setConfirmDel(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const formatDate = (d: string | null) => d ? new Date(d).toLocaleDateString() : '-';
|
||||||
|
const contactMethodIcon = (m: string) => m === 'email' ? '✉️' : m === 'whatsapp' ? '📱' : '📞';
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
|
<div style={{ display: 'flex', gap: '12px', marginBottom: '16px', borderBottom: '1px solid #e5e7eb', paddingBottom: '12px' }}>
|
||||||
|
<Btn size="sm" variant={tab === 'events' ? 'primary' : 'secondary'} onClick={() => setTab('events')}>🎪 Events</Btn>
|
||||||
|
<Btn size="sm" variant={tab === 'reservations' ? 'primary' : 'secondary'} onClick={() => setTab('reservations')}>📅 Reservations</Btn>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{tab === 'events' ? (
|
||||||
|
<>
|
||||||
<SecHead title="🎪 Public Events" count={events.length}
|
<SecHead title="🎪 Public Events" count={events.length}
|
||||||
action={<Btn onClick={() => setEditing({ id: 0, name: '', description: '', price: null, date: null, photos: [], featured_photo: null, active: true, sort_order: events.length })} size="sm">+ New Event</Btn>} />
|
action={<Btn onClick={() => setEditing({ id: 0, name: '', description: '', price: null, date: null, photos: [], featured_photo: null, active: true, sort_order: events.length })} size="sm">+ New Event</Btn>} />
|
||||||
{events.length === 0 ? <Empty icon="🎪" title="No public events" /> : (
|
{events.length === 0 ? <Empty icon="🎪" title="No public events" /> : (
|
||||||
@@ -813,6 +861,57 @@ function SocialSection({ events, setEvents, images, onToast }: any) {
|
|||||||
)}
|
)}
|
||||||
{editing && <SocialEditor event={editing} setEvent={setEditing} images={images} onSave={save} onCancel={() => setEditing(null)} />}
|
{editing && <SocialEditor event={editing} setEvent={setEditing} images={images} onSave={save} onCancel={() => setEditing(null)} />}
|
||||||
{confirmDel !== null && <Confirm message="Delete this event?" onCancel={() => setConfirmDel(null)} onConfirm={() => del(confirmDel)} />}
|
{confirmDel !== null && <Confirm message="Delete this event?" onCancel={() => setConfirmDel(null)} onConfirm={() => del(confirmDel)} />}
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<SecHead title="📅 Public Event Reservations" count={reservations.length} />
|
||||||
|
<div style={{ display: 'flex', gap: '8px', marginBottom: '16px' }}>
|
||||||
|
{(['pending', 'confirmed', 'cancelled', 'all'] as const).map(s => (
|
||||||
|
<Btn key={s} size="sm" variant={statusFilter === s ? 'primary' : 'secondary'} onClick={() => setStatusFilter(s)}>
|
||||||
|
{s.charAt(0).toUpperCase() + s.slice(1)}
|
||||||
|
</Btn>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
{loading ? <Empty icon="⏳" title="Loading..." /> : reservations.length === 0 ? <Empty icon="📅" title="No reservations" desc={`No ${statusFilter === 'all' ? '' : statusFilter} reservations`} /> : (
|
||||||
|
<div style={{ display: 'grid', gap: '12px' }}>
|
||||||
|
{reservations.map((r: any) => (
|
||||||
|
<div key={r.id} style={{ background: '#fff', borderRadius: 12, padding: '16px', border: `1px solid ${r.status === 'pending' ? '#f59e0b' : r.status === 'confirmed' ? '#10b981' : '#ef4444'}` }}>
|
||||||
|
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', marginBottom: '12px' }}>
|
||||||
|
<div>
|
||||||
|
<div style={{ fontWeight: 600, fontSize: '16px', color: C.navy }}>{r.event_name}</div>
|
||||||
|
<div style={{ fontSize: '13px', color: '#666', marginTop: '4px' }}>{r.guests} guests</div>
|
||||||
|
</div>
|
||||||
|
<span style={{
|
||||||
|
padding: '4px 10px', borderRadius: 12, fontSize: '12px', fontWeight: 600,
|
||||||
|
background: r.status === 'confirmed' ? '#d1fae5' : r.status === 'cancelled' ? '#fee2e2' : '#fef3c7',
|
||||||
|
color: r.status === 'confirmed' ? '#047857' : r.status === 'cancelled' ? '#b91c1c' : '#b45309'
|
||||||
|
}}>{r.status}</span>
|
||||||
|
</div>
|
||||||
|
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '8px', fontSize: '13px', marginBottom: '12px' }}>
|
||||||
|
<div><strong>Contact:</strong> {r.contact_name || r.first_name || r.username || '-'}</div>
|
||||||
|
<div><strong>Method:</strong> {r.contact_method ? contactMethodIcon(r.contact_method) : '-'} {r.contact_method || '-'}</div>
|
||||||
|
{r.contact_email && <div><strong>Email:</strong> {r.contact_email}</div>}
|
||||||
|
{r.contact_phone && <div><strong>Phone:</strong> {r.contact_phone}</div>}
|
||||||
|
</div>
|
||||||
|
{r.notes && <div style={{ fontSize: '13px', color: '#666', marginBottom: '12px', fontStyle: 'italic' }}>"{r.notes}"</div>}
|
||||||
|
<div style={{ display: 'flex', gap: '8px' }}>
|
||||||
|
{r.status === 'pending' && (
|
||||||
|
<>
|
||||||
|
<Btn size="sm" variant="secondary" onClick={() => updateStatus(r.id, 'confirmed')}>✓ Confirm</Btn>
|
||||||
|
<Btn size="sm" variant="danger" onClick={() => updateStatus(r.id, 'cancelled')}>✗ Cancel</Btn>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
{r.status === 'confirmed' && <Btn size="sm" variant="danger" onClick={() => updateStatus(r.id, 'cancelled')}>✗ Cancel</Btn>}
|
||||||
|
{r.status === 'cancelled' && <Btn size="sm" variant="secondary" onClick={() => updateStatus(r.id, 'confirmed')}>✓ Reactivate</Btn>}
|
||||||
|
<Btn size="sm" variant="danger" onClick={() => setConfirmDel(r.id)}>🗑 Delete</Btn>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{confirmDel !== null && <Confirm message="Delete this reservation?" onCancel={() => setConfirmDel(null)} onConfirm={() => delReservation(confirmDel)} />}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -877,10 +976,97 @@ function SocialEditor({ event, setEvent, images, onSave, onCancel }: any) {
|
|||||||
|
|
||||||
/* ─── Rooms Section ─── */
|
/* ─── Rooms Section ─── */
|
||||||
function PrivateEventsSection({ onToast }: { onToast: (msg: string, type: 'success' | 'error') => void }) {
|
function PrivateEventsSection({ onToast }: { onToast: (msg: string, type: 'success' | 'error') => void }) {
|
||||||
|
const [reservations, setReservations] = useState<any[]>([]);
|
||||||
|
const [statusFilter, setStatusFilter] = useState<'pending' | 'confirmed' | 'cancelled' | 'all'>('pending');
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [confirmDel, setConfirmDel] = useState<number | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
loadReservations();
|
||||||
|
}, [statusFilter]);
|
||||||
|
|
||||||
|
const loadReservations = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
|
const url = statusFilter === 'all' ? '/api/private-event-reservations' : `/api/private-event-reservations?status=${statusFilter}`;
|
||||||
|
const res = await fetch(url);
|
||||||
|
const data = await res.json();
|
||||||
|
setReservations(data.data || []);
|
||||||
|
} catch { onToast('Error loading reservations', 'error'); }
|
||||||
|
setLoading(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateStatus = async (id: number, status: 'confirmed' | 'cancelled') => {
|
||||||
|
try {
|
||||||
|
await fetch(`/api/private-event-reservations/${id}`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ status }),
|
||||||
|
});
|
||||||
|
setReservations(r => r.map(x => x.id === id ? { ...x, status } : x));
|
||||||
|
onToast(`Reservation ${status}`, 'success');
|
||||||
|
} catch { onToast('Error updating reservation', 'error'); }
|
||||||
|
};
|
||||||
|
|
||||||
|
const del = async (id: number) => {
|
||||||
|
await fetch(`/api/private-event-reservations/${id}`, { method: 'DELETE' });
|
||||||
|
setReservations(r => r.filter(x => x.id !== id));
|
||||||
|
onToast('Reservation deleted', 'success');
|
||||||
|
setConfirmDel(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
const formatDate = (d: string | null) => d ? new Date(d).toLocaleDateString() : '-';
|
||||||
|
const formatTime = (t: string | null) => t || '-';
|
||||||
|
const contactMethodIcon = (m: string) => m === 'email' ? '✉️' : m === 'whatsapp' ? '📱' : '📞';
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<SecHead title="🔒 Private Events" count={0} />
|
<SecHead title="🔒 Private Event Reservations" count={reservations.length} />
|
||||||
<Empty icon="🔒" title="Private Events" desc="Manage private event inquiries and bookings here. Contact form submissions for private events will appear in this section." />
|
<div style={{ display: 'flex', gap: '8px', marginBottom: '16px' }}>
|
||||||
|
{(['pending', 'confirmed', 'cancelled', 'all'] as const).map(s => (
|
||||||
|
<Btn key={s} size="sm" variant={statusFilter === s ? 'primary' : 'secondary'} onClick={() => setStatusFilter(s)}>
|
||||||
|
{s.charAt(0).toUpperCase() + s.slice(1)}
|
||||||
|
</Btn>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
{loading ? <Empty icon="⏳" title="Loading..." /> : reservations.length === 0 ? <Empty icon="🔒" title="No reservations" desc={`No ${statusFilter === 'all' ? '' : statusFilter} reservations`} /> : (
|
||||||
|
<div style={{ display: 'grid', gap: '12px' }}>
|
||||||
|
{reservations.map((r: any) => (
|
||||||
|
<div key={r.id} style={{ background: '#fff', borderRadius: 12, padding: '16px', border: `1px solid ${r.status === 'pending' ? '#f59e0b' : r.status === 'confirmed' ? '#10b981' : '#ef4444'}` }}>
|
||||||
|
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', marginBottom: '12px' }}>
|
||||||
|
<div>
|
||||||
|
<div style={{ fontWeight: 600, fontSize: '16px', color: C.navy }}>{r.event_name}</div>
|
||||||
|
<div style={{ fontSize: '13px', color: '#666', marginTop: '4px' }}>{r.guests} guests • {formatDate(r.event_date)} • {formatTime(r.event_time)}</div>
|
||||||
|
</div>
|
||||||
|
<span style={{
|
||||||
|
padding: '4px 10px', borderRadius: 12, fontSize: '12px', fontWeight: 600,
|
||||||
|
background: r.status === 'confirmed' ? '#d1fae5' : r.status === 'cancelled' ? '#fee2e2' : '#fef3c7',
|
||||||
|
color: r.status === 'confirmed' ? '#047857' : r.status === 'cancelled' ? '#b91c1c' : '#b45309'
|
||||||
|
}}>{r.status}</span>
|
||||||
|
</div>
|
||||||
|
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '8px', fontSize: '13px', marginBottom: '12px' }}>
|
||||||
|
<div><strong>Contact:</strong> {r.contact_name}</div>
|
||||||
|
<div><strong>Method:</strong> {contactMethodIcon(r.contact_method)} {r.contact_method}</div>
|
||||||
|
{r.contact_email && <div><strong>Email:</strong> {r.contact_email}</div>}
|
||||||
|
{r.contact_phone && <div><strong>Phone:</strong> {r.contact_phone}</div>}
|
||||||
|
</div>
|
||||||
|
{r.notes && <div style={{ fontSize: '13px', color: '#666', marginBottom: '12px', fontStyle: 'italic' }}>"{r.notes}"</div>}
|
||||||
|
<div style={{ display: 'flex', gap: '8px' }}>
|
||||||
|
{r.status === 'pending' && (
|
||||||
|
<>
|
||||||
|
<Btn size="sm" variant="secondary" onClick={() => updateStatus(r.id, 'confirmed')}>✓ Confirm</Btn>
|
||||||
|
<Btn size="sm" variant="danger" onClick={() => updateStatus(r.id, 'cancelled')}>✗ Cancel</Btn>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
{r.status === 'confirmed' && <Btn size="sm" variant="danger" onClick={() => updateStatus(r.id, 'cancelled')}>✗ Cancel</Btn>}
|
||||||
|
{r.status === 'cancelled' && <Btn size="sm" variant="secondary" onClick={() => updateStatus(r.id, 'confirmed')}>✓ Reactivate</Btn>}
|
||||||
|
<Btn size="sm" variant="danger" onClick={() => setConfirmDel(r.id)}>🗑 Delete</Btn>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{confirmDel !== null && <Confirm message="Delete this reservation?" onCancel={() => setConfirmDel(null)} onConfirm={() => del(confirmDel)} />}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
import { db } from '@/lib/db';
|
||||||
|
|
||||||
|
// GET - Get a single private event reservation
|
||||||
|
export async function GET(
|
||||||
|
request: NextRequest,
|
||||||
|
{ params }: { params: { id: string } }
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const { rows } = await db.query(
|
||||||
|
'SELECT * FROM private_event_reservations WHERE id = $1',
|
||||||
|
[params.id]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (rows.length === 0) {
|
||||||
|
return NextResponse.json({ error: 'Reservation not found' }, { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({ data: rows[0] });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching reservation:', error);
|
||||||
|
return NextResponse.json({ error: 'Failed to fetch reservation' }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PUT - Update reservation (status, notes, etc.)
|
||||||
|
export async function PUT(
|
||||||
|
request: NextRequest,
|
||||||
|
{ params }: { params: { id: string } }
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const body = await request.json();
|
||||||
|
const { status, notes } = body;
|
||||||
|
|
||||||
|
if (status && !['pending', 'confirmed', 'cancelled'].includes(status)) {
|
||||||
|
return NextResponse.json({ error: 'Invalid status' }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const updates: string[] = [];
|
||||||
|
const values: any[] = [];
|
||||||
|
let paramCount = 1;
|
||||||
|
|
||||||
|
if (status) {
|
||||||
|
updates.push(`status = $${paramCount++}`);
|
||||||
|
values.push(status);
|
||||||
|
}
|
||||||
|
if (notes !== undefined) {
|
||||||
|
updates.push(`notes = $${paramCount++}`);
|
||||||
|
values.push(notes);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (updates.length === 0) {
|
||||||
|
return NextResponse.json({ error: 'No fields to update' }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
values.push(params.id);
|
||||||
|
|
||||||
|
const { rows } = await db.query(
|
||||||
|
`UPDATE private_event_reservations SET ${updates.join(', ')} WHERE id = $${paramCount} RETURNING *`,
|
||||||
|
values
|
||||||
|
);
|
||||||
|
|
||||||
|
return NextResponse.json({ data: rows[0] });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error updating reservation:', error);
|
||||||
|
return NextResponse.json({ error: 'Failed to update reservation' }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELETE - Delete a reservation
|
||||||
|
export async function DELETE(
|
||||||
|
request: NextRequest,
|
||||||
|
{ params }: { params: { id: string } }
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
await db.query('DELETE FROM private_event_reservations WHERE id = $1', [params.id]);
|
||||||
|
return NextResponse.json({ success: true });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error deleting reservation:', error);
|
||||||
|
return NextResponse.json({ error: 'Failed to delete reservation' }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
import { db } from '@/lib/db';
|
||||||
|
|
||||||
|
// GET - List all private event reservations (with optional status filter)
|
||||||
|
export async function GET(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
const { searchParams } = new URL(request.url);
|
||||||
|
const status = searchParams.get('status');
|
||||||
|
|
||||||
|
let query = 'SELECT * FROM private_event_reservations';
|
||||||
|
const params: any[] = [];
|
||||||
|
|
||||||
|
if (status && ['pending', 'confirmed', 'cancelled'].includes(status)) {
|
||||||
|
query += ' WHERE status = $1 ORDER BY created_at DESC';
|
||||||
|
params.push(status);
|
||||||
|
} else {
|
||||||
|
query += ' ORDER BY status = \'pending\' DESC, created_at DESC';
|
||||||
|
}
|
||||||
|
|
||||||
|
const { rows } = await db.query(query, params);
|
||||||
|
return NextResponse.json({ data: rows });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching private event reservations:', error);
|
||||||
|
return NextResponse.json({ error: 'Failed to fetch reservations' }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST - Create a new private event reservation (public endpoint)
|
||||||
|
export async function POST(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
const body = await request.json();
|
||||||
|
const {
|
||||||
|
event_name,
|
||||||
|
contact_name,
|
||||||
|
contact_email,
|
||||||
|
contact_phone,
|
||||||
|
contact_method,
|
||||||
|
event_date,
|
||||||
|
event_time,
|
||||||
|
guests,
|
||||||
|
notes
|
||||||
|
} = body;
|
||||||
|
|
||||||
|
if (!event_name || !contact_name || !contact_method || !guests) {
|
||||||
|
return NextResponse.json({ error: 'Missing required fields' }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!['email', 'phone', 'whatsapp'].includes(contact_method)) {
|
||||||
|
return NextResponse.json({ error: 'Invalid contact method' }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const { rows } = await db.query(
|
||||||
|
`INSERT INTO private_event_reservations
|
||||||
|
(event_name, contact_name, contact_email, contact_phone, contact_method, event_date, event_time, guests, notes, status)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, 'pending')
|
||||||
|
RETURNING *`,
|
||||||
|
[event_name, contact_name, contact_email, contact_phone, contact_method, event_date, event_time, guests, notes]
|
||||||
|
);
|
||||||
|
|
||||||
|
return NextResponse.json({ data: rows[0] });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error creating private event reservation:', error);
|
||||||
|
return NextResponse.json({ error: 'Failed to create reservation' }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
import { db } from '@/lib/db';
|
||||||
|
import { requireAuth } from '@/lib/auth';
|
||||||
|
|
||||||
|
// GET - Get a single social event reservation
|
||||||
|
export async function GET(
|
||||||
|
request: NextRequest,
|
||||||
|
{ params }: { params: { id: string } }
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
await requireAuth();
|
||||||
|
const { rows } = await db.query(
|
||||||
|
`SELECT ser.*, se.name as event_name
|
||||||
|
FROM social_event_reservations ser
|
||||||
|
JOIN social_events se ON se.id = ser.social_event_id
|
||||||
|
WHERE ser.id = $1`,
|
||||||
|
[params.id]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (rows.length === 0) {
|
||||||
|
return NextResponse.json({ error: 'Reservation not found' }, { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({ data: rows[0] });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching reservation:', error);
|
||||||
|
return NextResponse.json({ error: 'Failed to fetch reservation' }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PUT - Update reservation status (admin only)
|
||||||
|
export async function PUT(
|
||||||
|
request: NextRequest,
|
||||||
|
{ params }: { params: { id: string } }
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const session = await requireAuth();
|
||||||
|
if (session.role !== 'admin') {
|
||||||
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 403 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = await request.json();
|
||||||
|
const { status } = body;
|
||||||
|
|
||||||
|
if (!status || !['pending', 'confirmed', 'cancelled'].includes(status)) {
|
||||||
|
return NextResponse.json({ error: 'Invalid status' }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const { rows } = await db.query(
|
||||||
|
`UPDATE social_event_reservations SET status = $1 WHERE id = $2 RETURNING *`,
|
||||||
|
[status, params.id]
|
||||||
|
);
|
||||||
|
|
||||||
|
return NextResponse.json({ data: rows[0] });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error updating reservation:', error);
|
||||||
|
return NextResponse.json({ error: 'Failed to update reservation' }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELETE - Delete a reservation (admin only)
|
||||||
|
export async function DELETE(
|
||||||
|
request: NextRequest,
|
||||||
|
{ params }: { params: { id: string } }
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const session = await requireAuth();
|
||||||
|
if (session.role !== 'admin') {
|
||||||
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 403 });
|
||||||
|
}
|
||||||
|
|
||||||
|
await db.query('DELETE FROM social_event_reservations WHERE id = $1', [params.id]);
|
||||||
|
return NextResponse.json({ success: true });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error deleting reservation:', error);
|
||||||
|
return NextResponse.json({ error: 'Failed to delete reservation' }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,20 +7,31 @@ export async function GET(request: Request) {
|
|||||||
const session = await requireAuth();
|
const session = await requireAuth();
|
||||||
const { searchParams } = new URL(request.url);
|
const { searchParams } = new URL(request.url);
|
||||||
const socialEventId = searchParams.get('social_event_id');
|
const socialEventId = searchParams.get('social_event_id');
|
||||||
|
const status = searchParams.get('status');
|
||||||
|
|
||||||
let query = `
|
let query = `
|
||||||
SELECT ser.*, u.username, u.first_name, u.last_name, se.name as event_name
|
SELECT ser.*, se.name as event_name
|
||||||
FROM social_event_reservations ser
|
FROM social_event_reservations ser
|
||||||
JOIN users u ON u.id = ser.user_id
|
|
||||||
JOIN social_events se ON se.id = ser.social_event_id
|
JOIN social_events se ON se.id = ser.social_event_id
|
||||||
`;
|
`;
|
||||||
const params: (number | string)[] = [];
|
const params: (number | string)[] = [];
|
||||||
|
|
||||||
if (session.role === 'admin') {
|
if (session.role === 'admin') {
|
||||||
|
const conditions: string[] = [];
|
||||||
|
let paramCount = 1;
|
||||||
|
|
||||||
if (socialEventId) {
|
if (socialEventId) {
|
||||||
query += ' WHERE ser.social_event_id = $1';
|
conditions.push(`ser.social_event_id = $${paramCount++}`);
|
||||||
params.push(parseInt(socialEventId, 10));
|
params.push(parseInt(socialEventId, 10));
|
||||||
}
|
}
|
||||||
|
if (status && ['pending', 'confirmed', 'cancelled'].includes(status)) {
|
||||||
|
conditions.push(`ser.status = $${paramCount++}`);
|
||||||
|
params.push(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (conditions.length > 0) {
|
||||||
|
query += ' WHERE ' + conditions.join(' AND ');
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
query += ' WHERE ser.user_id = $1';
|
query += ' WHERE ser.user_id = $1';
|
||||||
params.push(session.id);
|
params.push(session.id);
|
||||||
@@ -41,23 +52,38 @@ export async function GET(request: Request) {
|
|||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
const session = await requireAuth();
|
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
const { social_event_id, guests, notes } = body;
|
const { social_event_id, guests, notes, contact_name, contact_email, contact_phone, contact_method } = body;
|
||||||
|
|
||||||
if (!social_event_id || !guests) {
|
if (!social_event_id || !guests) {
|
||||||
return NextResponse.json({ error: 'Missing required fields' }, { status: 400 });
|
return NextResponse.json({ error: 'Missing required fields' }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try to authenticate, but allow guest submissions
|
||||||
|
let userId: number | null = null;
|
||||||
|
try {
|
||||||
|
const session = await requireAuth();
|
||||||
|
userId = session.id;
|
||||||
|
} catch {
|
||||||
|
// Guest submission - require contact info
|
||||||
|
if (!contact_name || !contact_method) {
|
||||||
|
return NextResponse.json({ error: 'Contact name and method required for guest reservations' }, { status: 400 });
|
||||||
|
}
|
||||||
|
if (!['email', 'phone', 'whatsapp'].includes(contact_method)) {
|
||||||
|
return NextResponse.json({ error: 'Invalid contact method' }, { status: 400 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const { rows } = await db.query(
|
const { rows } = await db.query(
|
||||||
`INSERT INTO social_event_reservations (user_id, social_event_id, guests, notes)
|
`INSERT INTO social_event_reservations
|
||||||
VALUES ($1, $2, $3, $4)
|
(user_id, social_event_id, guests, notes, status, contact_name, contact_email, contact_phone, contact_method)
|
||||||
ON CONFLICT (user_id, social_event_id)
|
VALUES ($1, $2, $3, $4, 'pending', $5, $6, $7, $8)
|
||||||
DO UPDATE SET guests = EXCLUDED.guests, notes = EXCLUDED.notes, status = 'pending'
|
|
||||||
RETURNING *`,
|
RETURNING *`,
|
||||||
[session.id, parseInt(social_event_id, 10), parseInt(guests, 10), notes || '']
|
[userId, parseInt(social_event_id, 10), parseInt(guests, 10), notes || null, contact_name || null, contact_email || null, contact_phone || null, contact_method || null]
|
||||||
);
|
);
|
||||||
return NextResponse.json({ data: rows[0] });
|
return NextResponse.json({ data: rows[0] });
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('POST /api/social_event_reservations error:', error);
|
console.error('POST /api/social_event_reservations error:', error);
|
||||||
return NextResponse.json({ error: error.message || 'Unauthorized' }, { status: 401 });
|
return NextResponse.json({ error: error.message || 'Failed to create reservation' }, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -404,6 +404,32 @@ export async function initSchema() {
|
|||||||
);
|
);
|
||||||
`);
|
`);
|
||||||
|
|
||||||
|
await db.query(`
|
||||||
|
CREATE TABLE IF NOT EXISTS private_event_reservations (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
event_name VARCHAR(255) NOT NULL,
|
||||||
|
contact_name VARCHAR(255) NOT NULL,
|
||||||
|
contact_email VARCHAR(255),
|
||||||
|
contact_phone VARCHAR(50),
|
||||||
|
contact_method VARCHAR(20) NOT NULL CHECK (contact_method IN ('email', 'phone', 'whatsapp')),
|
||||||
|
event_date DATE,
|
||||||
|
event_time TIME,
|
||||||
|
guests INTEGER NOT NULL,
|
||||||
|
notes TEXT,
|
||||||
|
status VARCHAR(20) DEFAULT 'pending' CHECK (status IN ('pending', 'confirmed', 'cancelled')),
|
||||||
|
created_at TIMESTAMP DEFAULT NOW()
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
|
||||||
|
// Add status column to social_event_reservations if it doesn't exist
|
||||||
|
await db.query(`ALTER TABLE social_event_reservations ADD COLUMN IF NOT EXISTS status VARCHAR(20) DEFAULT 'pending' CHECK (status IN ('pending', 'confirmed', 'cancelled'))`);
|
||||||
|
|
||||||
|
// Add contact columns to social_event_reservations
|
||||||
|
await db.query(`ALTER TABLE social_event_reservations ADD COLUMN IF NOT EXISTS contact_name VARCHAR(255)`);
|
||||||
|
await db.query(`ALTER TABLE social_event_reservations ADD COLUMN IF NOT EXISTS contact_email VARCHAR(255)`);
|
||||||
|
await db.query(`ALTER TABLE social_event_reservations ADD COLUMN IF NOT EXISTS contact_phone VARCHAR(50)`);
|
||||||
|
await db.query(`ALTER TABLE social_event_reservations ALTER COLUMN user_id DROP NOT NULL`);
|
||||||
|
|
||||||
await db.query(`CREATE INDEX IF NOT EXISTS idx_orders_user ON orders(user_id)`);
|
await db.query(`CREATE INDEX IF NOT EXISTS idx_orders_user ON orders(user_id)`);
|
||||||
await db.query(`CREATE INDEX IF NOT EXISTS idx_orders_status ON orders(status)`);
|
await db.query(`CREATE INDEX IF NOT EXISTS idx_orders_status ON orders(status)`);
|
||||||
await db.query(`CREATE INDEX IF NOT EXISTS idx_order_items_order ON order_items(order_id)`);
|
await db.query(`CREATE INDEX IF NOT EXISTS idx_order_items_order ON order_items(order_id)`);
|
||||||
|
|||||||
Reference in New Issue
Block a user