diff --git a/src/app/admin/stops/[id]/page.tsx b/src/app/admin/stops/[id]/page.tsx index 66ce477..7b135a2 100644 --- a/src/app/admin/stops/[id]/page.tsx +++ b/src/app/admin/stops/[id]/page.tsx @@ -1,4 +1,4 @@ -import { supabase } from "@/lib/supabase"; +import { pool } from "@/lib/db"; import StopEditForm from "@/components/admin/StopEditForm"; import StopProductAssignment from "@/components/admin/StopProductAssignment"; import MessageCustomersSection from "@/components/admin/MessageCustomersSection"; @@ -26,7 +26,6 @@ interface Stop { cutoff_time: string | null; status: string; location: string; - slug: string; active: boolean; brands?: { name: string; slug: string }; } @@ -48,15 +47,6 @@ interface ProductStop { export default async function StopDetailPage({ params }: StopDetailPageProps) { const { id } = await params; - const [{ data: stop, error }, { data: brands }] = await Promise.all([ - supabase - .from("stops") - .select("*, brands(name, slug)") - .eq("id", id) - .single() as unknown as { data: Stop | null; error: { message: string } | null }, - supabase.from("brands").select("id, name, slug") as unknown as { data: { id: string; name: string; slug: string }[] | null }, - ]); - const adminUser = await getAdminUser(); if (!adminUser) { @@ -65,17 +55,24 @@ export default async function StopDetailPage({ params }: StopDetailPageProps) { if (!adminUser.can_manage_stops) redirect("/admin/pickup"); - if (adminUser.brand_id && stop?.brand_id !== adminUser.brand_id) { - return ; - } + // Fetch stop + const { rows: stopsRows } = await pool.query( + `SELECT s.*, b.name as brand_name, b.slug as brand_slug + FROM stops s + LEFT JOIN brands b ON b.id = s.brand_id + WHERE s.id = $1 + LIMIT 1`, + [id] + ); + const stopRow = stopsRows[0]; - if (error || !stop) { + if (!stopRow) { return (

Stop not found

-            {error?.message ?? "Stop not found"}
+            {id}
           
; + } - const assignedProducts = (productStops ?? []) - .map((ps: ProductStop) => ({ - id: ps.id, - product_id: ps.product_id, - products: ps.products ? { - name: ps.products.name, - type: ps.products.type, - price: ps.products.price, - image_url: ps.products.image_url, - } : null, - })) - .filter(Boolean); + const stop: Stop = { + id: stopRow.id, + brand_id: stopRow.brand_id, + city: stopRow.city ?? "", + state: stopRow.state ?? "", + address: stopRow.address ?? null, + zip: stopRow.zip ?? null, + date: stopRow.date ? String(stopRow.date) : "", + time: stopRow.time ?? "", + cutoff_date: stopRow.cutoff_date ? String(stopRow.cutoff_date) : null, + cutoff_time: stopRow.cutoff_date ? String(stopRow.cutoff_date) : null, + status: stopRow.status ?? "active", + location: stopRow.location ?? "", + active: stopRow.status === "active", + brands: { name: stopRow.brand_name ?? "", slug: stopRow.brand_slug ?? "" }, + }; + + // Fetch all products for this brand + const { rows: productRows } = await pool.query<{ id: string; name: string; type: string; price: number; image_url: string | null }>( + `SELECT id, name, type, price, image_url + FROM products + WHERE brand_id = $1 AND active = true + ORDER BY name`, + [stop.brand_id] + ); + + // Fetch product-stop assignments + const { rows: productStopRows } = await pool.query<{ id: string; product_id: string; name: string; type: string; price: number; image_url: string | null }>( + `SELECT ps.id, ps.product_id, p.name, p.type, p.price, p.image_url + FROM product_stops ps + JOIN products p ON p.id = ps.product_id + WHERE ps.stop_id = $1`, + [id] + ); + + // Fetch brands list + const { rows: brandRows } = await pool.query<{ id: string; name: string; slug: string }>( + `SELECT id, name, slug FROM brands ORDER BY name` + ); + + const allProducts = productRows.map((p) => ({ + id: p.id, + name: p.name, + type: p.type, + price: Number(p.price), + image_url: p.image_url, + })); + + const assignedProducts = productStopRows.map((ps) => ({ + id: ps.id, + product_id: ps.product_id, + products: { + name: ps.name, + type: ps.type, + price: Number(ps.price), + image_url: ps.image_url, + }, + })); return (
@@ -168,11 +203,11 @@ export default async function StopDetailPage({ params }: StopDetailPageProps) {

)} - {stop.cutoff_time && ( + {stop.cutoff_date && (

Cutoff

- {new Date(stop.cutoff_time).toLocaleString()} + {new Date(stop.cutoff_date + "T00:00:00").toLocaleDateString()}

)} @@ -199,7 +234,7 @@ export default async function StopDetailPage({ params }: StopDetailPageProps) {
@@ -221,14 +256,14 @@ export default async function StopDetailPage({ params }: StopDetailPageProps) { date: stop.date, time: stop.time, location: stop.location, - slug: stop.slug, + slug: stop.brands?.slug ?? "", active: stop.active, brand_id: stop.brand_id, address: stop.address, zip: stop.zip, cutoff_time: stop.cutoff_time, }} - brands={brands ?? []} + brands={brandRows ?? []} />
@@ -247,4 +282,4 @@ export default async function StopDetailPage({ params }: StopDetailPageProps) {
); -} +} \ No newline at end of file diff --git a/src/app/admin/stops/new/page.tsx b/src/app/admin/stops/new/page.tsx index 2b508c5..97d9294 100644 --- a/src/app/admin/stops/new/page.tsx +++ b/src/app/admin/stops/new/page.tsx @@ -1,4 +1,4 @@ -import { supabase } from "@/lib/supabase"; +import { pool } from "@/lib/db"; import NewStopForm from "@/components/admin/NewStopForm"; import StopProductAssignment from "@/components/admin/StopProductAssignment"; import { getAdminUser } from "@/lib/admin-permissions"; @@ -32,24 +32,35 @@ export default async function NewStopPage({ let duplicateFrom: Stop | null = null; if (duplicate) { - const { data } = await supabase - .from("stops") - .select("city, state, location, date, time, brand_id, active, address, zip, cutoff_time") - .eq("id", duplicate) - .single() as unknown as { data: Stop | null }; - duplicateFrom = data; + const { rows } = await pool.query( + `SELECT city, state, location, date, time, brand_id, active, address, zip, cutoff_date + FROM stops WHERE id = $1 LIMIT 1`, + [duplicate] + ); + const row = rows[0]; + if (row) { + duplicateFrom = { + city: row.city ?? "", + state: row.state ?? "", + location: row.location ?? "", + date: row.date ? String(row.date) : "", + time: row.time ?? "", + brand_id: row.brand_id, + active: row.active ?? true, + address: row.address ?? null, + zip: row.zip ?? null, + cutoff_time: row.cutoff_time ?? null, + }; + } } const brandId = adminUser.brand_id ?? "64294306-5f42-463d-a5e8-2ad6c81a96de"; - const [{ data: allProducts }] = await Promise.all([ - supabase - .from("products") - .select("id, name, type, price") - .eq("brand_id", brandId) - .eq("active", true) as unknown as { data: { id: string; name: string; type: string; price: number }[] | null }, - ]); + const { rows: productRows } = await pool.query<{ id: string; name: string; type: string; price: number }>( + `SELECT id, name, type, price FROM products WHERE brand_id = $1 AND active = true ORDER BY name`, + [brandId] + ); return (
@@ -79,4 +90,4 @@ export default async function NewStopPage({
); -} +} \ No newline at end of file diff --git a/src/app/admin/stops/page.tsx b/src/app/admin/stops/page.tsx index aeb2215..1e6277f 100644 --- a/src/app/admin/stops/page.tsx +++ b/src/app/admin/stops/page.tsx @@ -49,7 +49,7 @@ export default async function AdminStopsPage({ searchParams }: PageProps) { totalCount = parseInt(countResult.rows[0]?.count ?? "0", 10); // Get paginated stops - const { rows } = await pool.query(` + const { rows } = await pool.query(` SELECT s.id, s.city, @@ -61,7 +61,7 @@ export default async function AdminStopsPage({ searchParams }: PageProps) { s.brand_id, s.address, s.zip, - s.cutoff_time, + s.cutoff_date, b.name as brand_name FROM stops s LEFT JOIN brands b ON b.id = s.brand_id @@ -82,7 +82,7 @@ export default async function AdminStopsPage({ searchParams }: PageProps) { brand_id: r.brand_id, address: r.address, zip: r.zip, - cutoff_time: r.cutoff_time, + cutoff_time: r.cutoff_date, status: r.status ?? "active", brands: r.brand_name ? { name: r.brand_name } : { name: "" }, })); diff --git a/src/components/admin/stops/types.ts b/src/components/admin/stops/types.ts index 87e8d2c..46cdd07 100644 --- a/src/components/admin/stops/types.ts +++ b/src/components/admin/stops/types.ts @@ -13,6 +13,7 @@ export type Stop = { address?: string | null; zip?: string | null; cutoff_time?: string | null; + cutoff_date?: string | null; brands: { name: string } | { name: string }[]; };