diff --git a/src/app/admin/stops/page.tsx b/src/app/admin/stops/page.tsx index 289882d..a76ed3e 100644 --- a/src/app/admin/stops/page.tsx +++ b/src/app/admin/stops/page.tsx @@ -1,13 +1,8 @@ -import StopsDashboardClient from "@/components/admin/stops/StopsDashboardClient"; -import StopsHeaderActions from "@/components/admin/StopsHeaderActions"; +import { pool } from "@/lib/db"; import { getAdminUser } from "@/lib/admin-permissions"; import AdminAccessDenied from "@/components/admin/AdminAccessDenied"; import { PageHeader } from "@/components/admin/design-system"; import { redirect } from "next/navigation"; -import { pool } from "@/lib/db"; -import { type Stop } from "@/components/admin/stops/types"; - -const PAGE_SIZE = 50; const StopIcon = () => ( @@ -22,77 +17,43 @@ interface PageProps { export default async function AdminStopsPage({ searchParams }: PageProps) { const params = await searchParams; - const page = Math.max(1, parseInt(params.page ?? "1", 10) || 1); - const offset = (page - 1) * PAGE_SIZE; - const adminUser = await getAdminUser(); if (!adminUser) return ; + if (!adminUser.can_manage_stops) redirect("/admin/pickup"); - if (!adminUser.can_manage_stops) { - redirect("/admin/pickup"); - } - - let stops: Stop[] = []; + let stops: unknown[] = []; + let error: string | null = null; let totalCount = 0; - let error: { message: string } | null = null; try { - const brandFilter = adminUser.brand_id - ? `AND brand_id = '${adminUser.brand_id}'` - : ""; + // Platform admin sees all stops, brand admin sees their brand's stops + const brandCondition = adminUser.brand_id + ? `brand_id = '${adminUser.brand_id}'` + : "1=1"; - // Get total count for pagination - const countResult = await pool.query<{ count: string }>(` - SELECT count(*) as count FROM stops WHERE status = 'active' ${brandFilter} - `); + const countResult = await pool.query<{ count: string }>( + `SELECT count(*) as count FROM stops WHERE ${brandCondition}` + ); totalCount = parseInt(countResult.rows[0]?.count ?? "0", 10); - // Get paginated stops - const { rows } = await pool.query(` - SELECT - s.id, - s.city, - s.state, - s.date, - s.time, - s.location, - s.status, - s.brand_id, - s.address, - s.zip, - s.cutoff_date, - b.name as brand_name - FROM stops s - LEFT JOIN brands b ON b.id = s.brand_id - WHERE s.status = 'active' - ${brandFilter} - ORDER BY s.date ASC - LIMIT ${PAGE_SIZE} OFFSET ${offset} - `); - - stops = rows.map((r) => ({ - id: r.id, - city: r.city ?? "", - state: r.state ?? "", - date: r.date ? String(r.date) : "", - time: r.time ?? "", - location: r.location ?? "", - active: r.status === "active", - brand_id: r.brand_id, - address: r.address, - zip: r.zip, - cutoff_time: r.cutoff_date, - status: r.status ?? "active", - brands: r.brand_name ? { name: r.brand_name } : { name: "" }, - })); + const { rows } = await pool.query( + `SELECT s.id, s.city, s.state, s.date, s."time", s.location, s.status, + s.brand_id, s.address, s.zip, s.cutoff_date, + b.name as brand_name + FROM stops s + LEFT JOIN brands b ON b.id = s.brand_id + WHERE ${brandCondition} + ORDER BY s.date ASC + LIMIT 200` + ); + stops = rows; + console.log("[admin/stops] Fetched", rows.length, "stops, total:", totalCount); } catch (e) { - console.error("[admin/stops] Failed to load stops:", e); - error = { message: e instanceof Error ? e.message : String(e) }; + error = e instanceof Error ? e.message : String(e); + console.error("[admin/stops] Query error:", error); } - const totalPages = Math.ceil(totalCount / PAGE_SIZE); - if (error) { return (
@@ -103,12 +64,16 @@ export default async function AdminStopsPage({ searchParams }: PageProps) {
/ Stops & Routes -

- Error loading stops -

-
-              {error.message}
+            

Error loading stops

+
+              {error}
             
+
+

Debug info:

+

adminUser.brand_id: {adminUser.brand_id ?? "null"}

+

adminUser.role: {adminUser.role}

+

adminUser.email: {adminUser.email}

+
@@ -125,19 +90,65 @@ export default async function AdminStopsPage({ searchParams }: PageProps) { ]} icon={} title="Stops & Routes" - subtitle={adminUser?.brand_id ? "Managing stops for your brand." : "Manage routes, pickup locations, dates, and cutoff times."} - actions={} + subtitle={`${totalCount} stops total`} /> - - {/* Content */} -
- +
+ {stops.length === 0 ? ( +
+
+ + + + +
+

No stops found

+

+ {adminUser.brand_id + ? "No stops for your brand yet. Upload a schedule or add stops manually." + : "No stops in the database. Import the Tuxedo Tour schedule."} +

+ +
+ ) : ( +
+ + + + + + + + + + + + {stops.map((s: any) => ( + + + + + + + + ))} + +
CityLocationDateStatusBrand
{s.city}, {s.state}{s.location}{String(s.date)} + {s.status} + {s.brand_name ?? "—"}
+
+ )}
);