- Prisma: Booking.totalPriceCents (parsed from the order webhook's total_price string), populated in booking.server.ts so "revenue by method" is real data, not a placeholder. - app/services/dashboard.server.ts: pure aggregation over injected booking data (CLAUDE.md — no DB calls in the math) — bucketByLocalDate (each booking grouped under its own location's local calendar day, not a shared UTC day), revenueByMethod (confirmed/fulfilled only), utilizationByDateLocation (booked vs. summed SlotTemplate capacity for that weekday, capped at 100%), upcomingFulfillments, and a CSV writer/formatter with proper quote-escaping. - /app/dashboard: filterable (date range, location, method, status) view with revenue-by-method cards, a capacity-utilization list (color-coded by load), an upcoming-fulfillments table with one-click confirmed->fulfilled/no_show status transitions, and a full by-day booking list. - /app/dashboard/export: a resource route (loader only, no component) streaming the same filtered bookings as a downloadable CSV — kept separate from the dashboard route specifically so it can import dashboard.server.ts freely without the client-bundling constraint the main route has to respect (see below). Fixed the same class of server/client bundling bug from the Phase 5 commit before it could ship, this time by construction: dashboard.server.ts's aggregation functions are called only inside app.dashboard.tsx's `loader`, never referenced by the default-exported component (which only reads useLoaderData() output) — verified this holds by actually running `npm run build`, not just tsc/vitest, which both stay silent about this class of error. Also hit (and fixed) the same "loader Dates arrive as strings on the client" issue from Phase 1: swapped DateTime.fromJSDate for DateTime.fromISO in the two places the component formats a booking's slotStart. Verified: lint, typecheck, 102 unit tests (+16 new for dashboard.server.ts, +2 new for totalPriceCents parsing), 18 integration tests, both builds, and a live script exercising the full aggregation pipeline (revenue exclusion of cancelled bookings, utilization math, CSV output) against the Postgres container. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
59 lines
2.2 KiB
TypeScript
59 lines
2.2 KiB
TypeScript
import type { LoaderFunctionArgs } from "@remix-run/node";
|
|
import { DateTime } from "luxon";
|
|
import type { Method } from "@prisma/client";
|
|
import { authenticate } from "../shopify.server";
|
|
import db from "../db.server";
|
|
import { bookingsToCsv, type BookingSummary } from "../services/dashboard.server";
|
|
|
|
// Resource route (no default export/component) — safe to hit directly for
|
|
// a file download, and safe to import dashboard.server.ts freely since
|
|
// there's no client component here for Remix to worry about bundling it into.
|
|
export const loader = async ({ request }: LoaderFunctionArgs) => {
|
|
const { session } = await authenticate.admin(request);
|
|
const url = new URL(request.url);
|
|
|
|
const locationIdFilter = url.searchParams.get("locationId") || undefined;
|
|
const methodFilter = (url.searchParams.get("method") as Method | null) || undefined;
|
|
const statusFilter = url.searchParams.get("status") || undefined;
|
|
const startDate = url.searchParams.get("startDate") || DateTime.now().toISODate()!;
|
|
const endDate = url.searchParams.get("endDate") || DateTime.now().plus({ days: 7 }).toISODate()!;
|
|
|
|
const rangeStart = DateTime.fromISO(startDate, { zone: "utc" }).toJSDate();
|
|
const rangeEnd = DateTime.fromISO(endDate, { zone: "utc" }).plus({ days: 1 }).toJSDate();
|
|
|
|
const bookings = await db.booking.findMany({
|
|
where: {
|
|
shopDomain: session.shop,
|
|
locationId: locationIdFilter,
|
|
method: methodFilter,
|
|
status: statusFilter,
|
|
slotStart: { gte: rangeStart, lt: rangeEnd },
|
|
},
|
|
include: { location: true },
|
|
orderBy: { slotStart: "asc" },
|
|
});
|
|
|
|
const summaries: BookingSummary[] = bookings.map((b) => ({
|
|
id: b.id,
|
|
orderName: b.orderName,
|
|
locationId: b.locationId,
|
|
locationName: b.location.name,
|
|
method: b.method,
|
|
slotStart: b.slotStart,
|
|
slotEnd: b.slotEnd,
|
|
timezone: b.location.timezone,
|
|
status: b.status,
|
|
totalPriceCents: b.totalPriceCents,
|
|
customerEmail: b.customerEmail,
|
|
}));
|
|
|
|
const csv = bookingsToCsv(summaries);
|
|
|
|
return new Response(csv, {
|
|
headers: {
|
|
"Content-Type": "text/csv; charset=utf-8",
|
|
"Content-Disposition": `attachment; filename="dispatch-${startDate}-to-${endDate}.csv"`,
|
|
},
|
|
});
|
|
};
|