fix: use correct column names for stops (cutoff_date not cutoff_time) and replace mock Supabase with real pool queries in stops detail/new pages
Deploy to route.crispygoat.com / deploy (push) Successful in 4m5s
Deploy to route.crispygoat.com / deploy (push) Successful in 4m5s
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { supabase } from "@/lib/supabase";
|
import { pool } from "@/lib/db";
|
||||||
import StopEditForm from "@/components/admin/StopEditForm";
|
import StopEditForm from "@/components/admin/StopEditForm";
|
||||||
import StopProductAssignment from "@/components/admin/StopProductAssignment";
|
import StopProductAssignment from "@/components/admin/StopProductAssignment";
|
||||||
import MessageCustomersSection from "@/components/admin/MessageCustomersSection";
|
import MessageCustomersSection from "@/components/admin/MessageCustomersSection";
|
||||||
@@ -26,7 +26,6 @@ interface Stop {
|
|||||||
cutoff_time: string | null;
|
cutoff_time: string | null;
|
||||||
status: string;
|
status: string;
|
||||||
location: string;
|
location: string;
|
||||||
slug: string;
|
|
||||||
active: boolean;
|
active: boolean;
|
||||||
brands?: { name: string; slug: string };
|
brands?: { name: string; slug: string };
|
||||||
}
|
}
|
||||||
@@ -48,15 +47,6 @@ interface ProductStop {
|
|||||||
export default async function StopDetailPage({ params }: StopDetailPageProps) {
|
export default async function StopDetailPage({ params }: StopDetailPageProps) {
|
||||||
const { id } = await params;
|
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();
|
const adminUser = await getAdminUser();
|
||||||
|
|
||||||
if (!adminUser) {
|
if (!adminUser) {
|
||||||
@@ -65,17 +55,24 @@ export default async function StopDetailPage({ params }: StopDetailPageProps) {
|
|||||||
|
|
||||||
if (!adminUser.can_manage_stops) redirect("/admin/pickup");
|
if (!adminUser.can_manage_stops) redirect("/admin/pickup");
|
||||||
|
|
||||||
if (adminUser.brand_id && stop?.brand_id !== adminUser.brand_id) {
|
// Fetch stop
|
||||||
return <AdminAccessDenied />;
|
const { rows: stopsRows } = await pool.query<Stop & { brand_name: string; brand_slug: string }>(
|
||||||
}
|
`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 (
|
return (
|
||||||
<main className="min-h-screen px-6 py-12" style={{ backgroundColor: "var(--admin-bg)" }}>
|
<main className="min-h-screen px-6 py-12" style={{ backgroundColor: "var(--admin-bg)" }}>
|
||||||
<div className="mx-auto max-w-4xl">
|
<div className="mx-auto max-w-4xl">
|
||||||
<h1 className="text-3xl font-bold text-red-600">Stop not found</h1>
|
<h1 className="text-3xl font-bold text-red-600">Stop not found</h1>
|
||||||
<pre className="mt-4 rounded-xl bg-white border border-stone-200 p-4 text-sm text-stone-600">
|
<pre className="mt-4 rounded-xl bg-white border border-stone-200 p-4 text-sm text-stone-600">
|
||||||
{error?.message ?? "Stop not found"}
|
{id}
|
||||||
</pre>
|
</pre>
|
||||||
<Link
|
<Link
|
||||||
href="/admin/stops"
|
href="/admin/stops"
|
||||||
@@ -88,30 +85,68 @@ export default async function StopDetailPage({ params }: StopDetailPageProps) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const [{ data: allProducts }, { data: productStops }] = await Promise.all([
|
if (adminUser.brand_id && stopRow.brand_id !== adminUser.brand_id) {
|
||||||
supabase
|
return <AdminAccessDenied />;
|
||||||
.from("products")
|
}
|
||||||
.select("id, name, type, price")
|
|
||||||
.eq("brand_id", stop.brand_id)
|
|
||||||
.eq("active", true) as unknown as { data: Product[] | null },
|
|
||||||
supabase
|
|
||||||
.from("product_stops")
|
|
||||||
.select("id, product_id, products(id, name, type, price)")
|
|
||||||
.eq("stop_id", id) as unknown as { data: ProductStop[] | null },
|
|
||||||
]);
|
|
||||||
|
|
||||||
const assignedProducts = (productStops ?? [])
|
const stop: Stop = {
|
||||||
.map((ps: ProductStop) => ({
|
id: stopRow.id,
|
||||||
id: ps.id,
|
brand_id: stopRow.brand_id,
|
||||||
product_id: ps.product_id,
|
city: stopRow.city ?? "",
|
||||||
products: ps.products ? {
|
state: stopRow.state ?? "",
|
||||||
name: ps.products.name,
|
address: stopRow.address ?? null,
|
||||||
type: ps.products.type,
|
zip: stopRow.zip ?? null,
|
||||||
price: ps.products.price,
|
date: stopRow.date ? String(stopRow.date) : "",
|
||||||
image_url: ps.products.image_url,
|
time: stopRow.time ?? "",
|
||||||
} : null,
|
cutoff_date: stopRow.cutoff_date ? String(stopRow.cutoff_date) : null,
|
||||||
}))
|
cutoff_time: stopRow.cutoff_date ? String(stopRow.cutoff_date) : null,
|
||||||
.filter(Boolean);
|
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 (
|
return (
|
||||||
<main className="min-h-screen px-6 py-12" style={{ backgroundColor: "var(--admin-bg)" }}>
|
<main className="min-h-screen px-6 py-12" style={{ backgroundColor: "var(--admin-bg)" }}>
|
||||||
@@ -168,11 +203,11 @@ export default async function StopDetailPage({ params }: StopDetailPageProps) {
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{stop.cutoff_time && (
|
{stop.cutoff_date && (
|
||||||
<div>
|
<div>
|
||||||
<p className="text-sm font-medium text-stone-500">Cutoff</p>
|
<p className="text-sm font-medium text-stone-500">Cutoff</p>
|
||||||
<p className="mt-1 text-lg font-semibold text-stone-950">
|
<p className="mt-1 text-lg font-semibold text-stone-950">
|
||||||
{new Date(stop.cutoff_time).toLocaleString()}
|
{new Date(stop.cutoff_date + "T00:00:00").toLocaleDateString()}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@@ -199,7 +234,7 @@ export default async function StopDetailPage({ params }: StopDetailPageProps) {
|
|||||||
<div className="mt-6">
|
<div className="mt-6">
|
||||||
<StopProductAssignment
|
<StopProductAssignment
|
||||||
stopId={stop.id}
|
stopId={stop.id}
|
||||||
allProducts={allProducts ?? []}
|
allProducts={allProducts}
|
||||||
assignedProducts={assignedProducts}
|
assignedProducts={assignedProducts}
|
||||||
callerUid={adminUser.user_id}
|
callerUid={adminUser.user_id}
|
||||||
/>
|
/>
|
||||||
@@ -221,14 +256,14 @@ export default async function StopDetailPage({ params }: StopDetailPageProps) {
|
|||||||
date: stop.date,
|
date: stop.date,
|
||||||
time: stop.time,
|
time: stop.time,
|
||||||
location: stop.location,
|
location: stop.location,
|
||||||
slug: stop.slug,
|
slug: stop.brands?.slug ?? "",
|
||||||
active: stop.active,
|
active: stop.active,
|
||||||
brand_id: stop.brand_id,
|
brand_id: stop.brand_id,
|
||||||
address: stop.address,
|
address: stop.address,
|
||||||
zip: stop.zip,
|
zip: stop.zip,
|
||||||
cutoff_time: stop.cutoff_time,
|
cutoff_time: stop.cutoff_time,
|
||||||
}}
|
}}
|
||||||
brands={brands ?? []}
|
brands={brandRows ?? []}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -247,4 +282,4 @@ export default async function StopDetailPage({ params }: StopDetailPageProps) {
|
|||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { supabase } from "@/lib/supabase";
|
import { pool } from "@/lib/db";
|
||||||
import NewStopForm from "@/components/admin/NewStopForm";
|
import NewStopForm from "@/components/admin/NewStopForm";
|
||||||
import StopProductAssignment from "@/components/admin/StopProductAssignment";
|
import StopProductAssignment from "@/components/admin/StopProductAssignment";
|
||||||
import { getAdminUser } from "@/lib/admin-permissions";
|
import { getAdminUser } from "@/lib/admin-permissions";
|
||||||
@@ -32,24 +32,35 @@ export default async function NewStopPage({
|
|||||||
|
|
||||||
let duplicateFrom: Stop | null = null;
|
let duplicateFrom: Stop | null = null;
|
||||||
if (duplicate) {
|
if (duplicate) {
|
||||||
const { data } = await supabase
|
const { rows } = await pool.query<Stop>(
|
||||||
.from("stops")
|
`SELECT city, state, location, date, time, brand_id, active, address, zip, cutoff_date
|
||||||
.select("city, state, location, date, time, brand_id, active, address, zip, cutoff_time")
|
FROM stops WHERE id = $1 LIMIT 1`,
|
||||||
.eq("id", duplicate)
|
[duplicate]
|
||||||
.single() as unknown as { data: Stop | null };
|
);
|
||||||
duplicateFrom = data;
|
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 =
|
const brandId =
|
||||||
adminUser.brand_id ?? "64294306-5f42-463d-a5e8-2ad6c81a96de";
|
adminUser.brand_id ?? "64294306-5f42-463d-a5e8-2ad6c81a96de";
|
||||||
|
|
||||||
const [{ data: allProducts }] = await Promise.all([
|
const { rows: productRows } = await pool.query<{ id: string; name: string; type: string; price: number }>(
|
||||||
supabase
|
`SELECT id, name, type, price FROM products WHERE brand_id = $1 AND active = true ORDER BY name`,
|
||||||
.from("products")
|
[brandId]
|
||||||
.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 },
|
|
||||||
]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="min-h-screen px-6 py-12" style={{ backgroundColor: "var(--admin-bg)" }}>
|
<main className="min-h-screen px-6 py-12" style={{ backgroundColor: "var(--admin-bg)" }}>
|
||||||
@@ -79,4 +90,4 @@ export default async function NewStopPage({
|
|||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -49,7 +49,7 @@ export default async function AdminStopsPage({ searchParams }: PageProps) {
|
|||||||
totalCount = parseInt(countResult.rows[0]?.count ?? "0", 10);
|
totalCount = parseInt(countResult.rows[0]?.count ?? "0", 10);
|
||||||
|
|
||||||
// Get paginated stops
|
// Get paginated stops
|
||||||
const { rows } = await pool.query<Stop& { brand_name: string }>(`
|
const { rows } = await pool.query<Stop & { brand_name: string } & { cutoff_date: string | null }>(`
|
||||||
SELECT
|
SELECT
|
||||||
s.id,
|
s.id,
|
||||||
s.city,
|
s.city,
|
||||||
@@ -61,7 +61,7 @@ export default async function AdminStopsPage({ searchParams }: PageProps) {
|
|||||||
s.brand_id,
|
s.brand_id,
|
||||||
s.address,
|
s.address,
|
||||||
s.zip,
|
s.zip,
|
||||||
s.cutoff_time,
|
s.cutoff_date,
|
||||||
b.name as brand_name
|
b.name as brand_name
|
||||||
FROM stops s
|
FROM stops s
|
||||||
LEFT JOIN brands b ON b.id = s.brand_id
|
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,
|
brand_id: r.brand_id,
|
||||||
address: r.address,
|
address: r.address,
|
||||||
zip: r.zip,
|
zip: r.zip,
|
||||||
cutoff_time: r.cutoff_time,
|
cutoff_time: r.cutoff_date,
|
||||||
status: r.status ?? "active",
|
status: r.status ?? "active",
|
||||||
brands: r.brand_name ? { name: r.brand_name } : { name: "" },
|
brands: r.brand_name ? { name: r.brand_name } : { name: "" },
|
||||||
}));
|
}));
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ export type Stop = {
|
|||||||
address?: string | null;
|
address?: string | null;
|
||||||
zip?: string | null;
|
zip?: string | null;
|
||||||
cutoff_time?: string | null;
|
cutoff_time?: string | null;
|
||||||
|
cutoff_date?: string | null;
|
||||||
brands: { name: string } | { name: string }[];
|
brands: { name: string } | { name: string }[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user