95 lines
4.1 KiB
TypeScript
95 lines
4.1 KiB
TypeScript
// AK-P16b · shared loader/action guard for the booking module (mirrors
|
|
// calendarAccess). Booking is feature-gated by its OWN `booking` key
|
|
// (tenantHasModule, M06 DoD 7). The bookings.* bundle is cumulative (M06 Block K):
|
|
// read_own < read_all < write < admin. Module-off 404s on write (Bauprinzip
|
|
// A.5.3); a permission/owner miss is a 404, never a 403.
|
|
//
|
|
// The PUBLIC booking flow (/b/{tenant}/{slug}) does NOT use this guard — it resolves
|
|
// the tenant from the URL segment and has no logged-in user (see the public route).
|
|
|
|
import { CatalogError, foundationErrors } from "@hivegrid/foundation";
|
|
import { requireAuth } from "~/utils/loaders.middleware";
|
|
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
|
import { getUserInfo } from "~/utils/session.server";
|
|
import { tenantHasModule } from "~/utils/connector/tenantConnector.server";
|
|
import { getPermissionsByUser } from "~/utils/db/permissions/userRoles.db.server";
|
|
import { BOOKING_MODULE_KEY } from "./bookingRoutePaths";
|
|
|
|
/** Booking atomic permissions, cumulative bundle (M06 Block K). */
|
|
export const BOOKING_PERMISSIONS = {
|
|
/** See your OWN appointments + edit your OWN availability (default capability). */
|
|
readOwn: "bookings.read_own",
|
|
/** See all tenant appointments / pages / availability. */
|
|
readAll: "bookings.read_all",
|
|
/** Manage appointments (held/no_show/cancel/reschedule). */
|
|
write: "bookings.write",
|
|
/** Booking-page CRUD, foreign availability, provider re-assign, hard-delete. */
|
|
admin: "bookings.admin",
|
|
} as const;
|
|
|
|
export type BookingRouteContext = {
|
|
tenantId: string;
|
|
userId: string;
|
|
permissions: readonly string[];
|
|
hasModule: boolean;
|
|
};
|
|
|
|
/**
|
|
* Resolve the booking route context: auth + tenant + the `booking` module gate +
|
|
* the user's permissions. With `requireModule` a module-off tenant 404s (write
|
|
* paths); GET paths can render a locked empty VM from `hasModule === false`.
|
|
*/
|
|
export async function loadBookingContext({
|
|
request,
|
|
params,
|
|
requireModule = false,
|
|
}: {
|
|
request: Request;
|
|
params: Record<string, string | undefined>;
|
|
requireModule?: boolean;
|
|
}): Promise<BookingRouteContext> {
|
|
await requireAuth({ request, params });
|
|
const [tenantId, userInfo] = await Promise.all([getTenantIdFromUrl(params), getUserInfo(request)]);
|
|
if (!tenantId || !userInfo.userId) {
|
|
throw new CatalogError(foundationErrors.notFound);
|
|
}
|
|
const [hasModule, permissions] = await Promise.all([
|
|
tenantHasModule({ tenantId, userId: userInfo.userId, moduleKey: BOOKING_MODULE_KEY }),
|
|
getPermissionsByUser(userInfo.userId, tenantId),
|
|
]);
|
|
if (requireModule && !hasModule) {
|
|
throw new CatalogError(foundationErrors.notFound, { module: BOOKING_MODULE_KEY });
|
|
}
|
|
return { tenantId, userId: userInfo.userId, permissions, hasModule };
|
|
}
|
|
|
|
/** True if the user holds `permission` or the broader `bookings.admin` bundle. */
|
|
export function bookingHasPermission(ctx: BookingRouteContext, permission: string): boolean {
|
|
return ctx.permissions.includes(permission) || ctx.permissions.includes(BOOKING_PERMISSIONS.admin);
|
|
}
|
|
|
|
/** Require a bookings.* permission (admin-aware); 404 if missing (A.5.3). */
|
|
export function requireBookingPermission(ctx: BookingRouteContext, permission: string): BookingRouteContext {
|
|
if (!bookingHasPermission(ctx, permission)) {
|
|
throw new CatalogError(foundationErrors.notFound, { permission });
|
|
}
|
|
return ctx;
|
|
}
|
|
|
|
/** True if the user may see all appointments (read_all or admin), not just own. */
|
|
export function canReadAllBookings(ctx: BookingRouteContext): boolean {
|
|
return ctx.permissions.includes(BOOKING_PERMISSIONS.readAll) || ctx.permissions.includes(BOOKING_PERMISSIONS.admin);
|
|
}
|
|
|
|
/**
|
|
* Owner-check for appointment-scoped routes (M06 K.5): a `read_own` user may only
|
|
* see/act on appointments they hold (`providerUserId = self`) — UNLESS they hold
|
|
* read_all/admin. A miss is a 404, not a 403 (A.5.3).
|
|
*/
|
|
export function requireAppointmentOwner(ctx: BookingRouteContext, providerUserId: string): BookingRouteContext {
|
|
if (ctx.userId === providerUserId || canReadAllBookings(ctx)) {
|
|
return ctx;
|
|
}
|
|
throw new CatalogError(foundationErrors.notFound, { owner: "mismatch" });
|
|
}
|