64 lines
3.0 KiB
TypeScript
64 lines
3.0 KiB
TypeScript
// AK-P16b · bookings list route (M06 Block C). Loader builds BookingListVM from the
|
|
// tenant's appointments, server-side owner-filtered (read_own → own provider rows
|
|
// only, DoD 6). Module off -> locked empty; missing bookings.read_own -> 404.
|
|
// Interim ~/ui screen with tab buckets (AD-28); final polish follows D-P16.
|
|
|
|
import { type LoaderFunctionArgs, useLoaderData } from "react-router";
|
|
import { buildBookingListVM, bookingStatusKey, type BookingListRow, type BookingListVM } from "@hivegrid/view-models";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { loadBookingContext, requireBookingPermission, canReadAllBookings, BOOKING_PERMISSIONS } from "~/modules/booking/routes/bookingAccess.server";
|
|
import { BOOKING_MODULE_KEY, bookingDetailPath, bookingPagesListPath, notFoundResponse } from "~/modules/booking/routes/bookingRoutePaths";
|
|
import { listAppointmentsForTenant } from "~/modules/booking/db/booking.db.server";
|
|
import { BookingListPage } from "~/components/screens/booking/BookingListPage";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
|
|
type LoaderData = { title: string; vm: BookingListVM; nowIso: string; pagesHref: string | null };
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadBookingContext({ request, params });
|
|
const title = `${t("bookings.list.title")} | ${process.env.APP_NAME}`;
|
|
|
|
const nowIso = new Date().toISOString();
|
|
if (!ctx.hasModule) {
|
|
return Response.json({ title, vm: buildBookingListVM({ rows: [] }), nowIso, pagesHref: null } satisfies LoaderData);
|
|
}
|
|
try {
|
|
requireBookingPermission(ctx, BOOKING_PERMISSIONS.readOwn);
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
|
|
const tenantSlug = params.tenant ?? "";
|
|
// Reachability (DoD B.6): the booking-pages admin list has no sidebar entry of
|
|
// its own — it is linked inline from here, only for users who may view it
|
|
// (bookings.read_all mirrors the pages route's own loader gate).
|
|
const pagesHref = canReadAllBookings(ctx) ? bookingPagesListPath(tenantSlug) : null;
|
|
const appointments = await listAppointmentsForTenant({ tenantId: ctx.tenantId, ownerUserId: canReadAllBookings(ctx) ? null : ctx.userId });
|
|
const rows: BookingListRow[] = appointments.map((a) => ({
|
|
id: a.id,
|
|
startAtIso: a.startAt.toISOString(),
|
|
endAtIso: a.endAt.toISOString(),
|
|
status: a.status,
|
|
statusKey: bookingStatusKey(a.status),
|
|
customerName: a.customerName,
|
|
customerEmail: a.customerEmail,
|
|
pageTitle: a.pageTitle,
|
|
providerUserId: a.providerUserId,
|
|
detailHref: bookingDetailPath(tenantSlug, a.id),
|
|
}));
|
|
|
|
return Response.json({ title, vm: buildBookingListVM({ rows }), nowIso, pagesHref } satisfies LoaderData);
|
|
};
|
|
|
|
export default function BookingsListRoute() {
|
|
const { vm, nowIso, pagesHref } = useLoaderData<LoaderData>();
|
|
return <BookingListPage vm={vm} nowIso={nowIso} pagesHref={pagesHref} />;
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: BOOKING_MODULE_KEY };
|