Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -0,0 +1,226 @@
|
||||
import { AdminBookingLifecycleError } from "@hivegrid/db/admin-booking-lifecycle.server";
|
||||
import { AdminBookingError } from "@hivegrid/db/admin-booking.server";
|
||||
import { buildAdminPublicBookingManageVM } from "@hivegrid/view-models";
|
||||
import {
|
||||
useActionData,
|
||||
useLoaderData,
|
||||
useNavigation,
|
||||
type ActionFunctionArgs,
|
||||
type LoaderFunctionArgs,
|
||||
} from "react-router";
|
||||
|
||||
import { AdminPublicBookingManagePage } from "~/components/screens/admin/sales/booking/AdminPublicBookingManagePage";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import {
|
||||
issueAdminBookingManageToken,
|
||||
verifyAdminBookingManageToken,
|
||||
} from "~/services/admin/admin-booking-manage-token.server";
|
||||
import {
|
||||
AdminPublicBookingManageError,
|
||||
cancelAdminManagedBooking,
|
||||
loadAdminManagedBooking,
|
||||
rescheduleAdminManagedBooking,
|
||||
} from "~/services/admin/admin-public-booking-manage.server";
|
||||
import { isAdminPublicBookingHost } from "~/services/admin/admin-public-booking.server";
|
||||
import { NOT_FOUND_ENVELOPE } from "~/utils/errors.server";
|
||||
|
||||
type ActionData = {
|
||||
readonly result: "cancelled" | "rescheduled" | null;
|
||||
readonly replacementManageUrl: string | null;
|
||||
readonly errorCode: string | null;
|
||||
};
|
||||
|
||||
export async function loader({ request, params }: LoaderFunctionArgs) {
|
||||
assertBookingHost(request);
|
||||
const claims = claimsFromRequest(params.token);
|
||||
const url = new URL(request.url);
|
||||
const timeZone = validTimeZone(url.searchParams.get("tz"));
|
||||
const date =
|
||||
validDate(url.searchParams.get("date")) ??
|
||||
dateInTimeZone(new Date(), timeZone);
|
||||
const [managed, translations] = await Promise.all([
|
||||
loadAdminManagedBooking({ claims, date }),
|
||||
getTranslations(request),
|
||||
]);
|
||||
if (!managed) throw Response.json(NOT_FOUND_ENVELOPE, { status: 404 });
|
||||
return {
|
||||
vm: buildAdminPublicBookingManageVM({
|
||||
title: managed.booking.bookingPage.title,
|
||||
meetingTypeName: managed.booking.meetingType.name,
|
||||
startsAt: managed.booking.startsAt,
|
||||
date,
|
||||
timeZone,
|
||||
locale: translations.locale,
|
||||
slots: managed.page.slots,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
export async function action({ request, params }: ActionFunctionArgs) {
|
||||
assertBookingHost(request);
|
||||
assertSameOrigin(request);
|
||||
const token = requiredToken(params.token);
|
||||
const claims = claimsFromRequest(token);
|
||||
const form = await request.formData();
|
||||
try {
|
||||
const intent = requiredFormText(form, "intent");
|
||||
if (intent === "cancel") {
|
||||
await cancelAdminManagedBooking({
|
||||
claims,
|
||||
reason: requiredFormText(form, "reason"),
|
||||
});
|
||||
return Response.json({
|
||||
result: "cancelled",
|
||||
replacementManageUrl: null,
|
||||
errorCode: null,
|
||||
} satisfies ActionData);
|
||||
}
|
||||
if (intent !== "reschedule") {
|
||||
throw new AdminPublicBookingManageError("invalid_request");
|
||||
}
|
||||
const [startsAt, endsAt] = parseSlot(
|
||||
requiredFormText(form, "slot")
|
||||
);
|
||||
const replacement = await rescheduleAdminManagedBooking({
|
||||
claims,
|
||||
date: requiredFormText(form, "date"),
|
||||
startsAt,
|
||||
endsAt,
|
||||
});
|
||||
const replacementToken = issueAdminBookingManageToken({
|
||||
bookingId: replacement.booking.id,
|
||||
adminUserId: replacement.booking.adminUserId,
|
||||
version: replacement.booking.manageTokenVersion,
|
||||
expiresAt: replacement.booking.manageTokenExpiresAt,
|
||||
secret: manageSecret(),
|
||||
});
|
||||
return Response.json({
|
||||
result: "rescheduled",
|
||||
replacementManageUrl: `${new URL(request.url).origin}/manage/${replacementToken}`,
|
||||
errorCode: null,
|
||||
} satisfies ActionData);
|
||||
} catch (error) {
|
||||
const errorCode =
|
||||
error instanceof AdminPublicBookingManageError ||
|
||||
error instanceof AdminBookingError ||
|
||||
error instanceof AdminBookingLifecycleError
|
||||
? error.code
|
||||
: "invalid_request";
|
||||
return Response.json(
|
||||
{
|
||||
result: null,
|
||||
replacementManageUrl: null,
|
||||
errorCode,
|
||||
} satisfies ActionData,
|
||||
{ status: errorCode === "slot_taken" ? 409 : 422 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default function AdminPublicBookingManageRoute() {
|
||||
const { vm } = useLoaderData<typeof loader>();
|
||||
const actionData = useActionData<ActionData>();
|
||||
const navigation = useNavigation();
|
||||
return (
|
||||
<AdminPublicBookingManagePage
|
||||
vm={vm}
|
||||
result={actionData?.result ?? null}
|
||||
replacementManageUrl={actionData?.replacementManageUrl ?? null}
|
||||
errorCode={actionData?.errorCode ?? null}
|
||||
pending={navigation.state === "submitting"}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function claimsFromRequest(token: string | undefined) {
|
||||
try {
|
||||
return verifyAdminBookingManageToken({
|
||||
token: requiredToken(token),
|
||||
secret: manageSecret(),
|
||||
});
|
||||
} catch {
|
||||
throw Response.json(NOT_FOUND_ENVELOPE, { status: 404 });
|
||||
}
|
||||
}
|
||||
|
||||
function manageSecret(): string {
|
||||
const secret = process.env.SESSION_SECRET?.trim() ?? "";
|
||||
if (secret.length < 32) {
|
||||
throw new AdminPublicBookingManageError("manage_unavailable");
|
||||
}
|
||||
return secret;
|
||||
}
|
||||
|
||||
function assertBookingHost(request: Request): void {
|
||||
if (!isAdminPublicBookingHost(request)) {
|
||||
throw Response.json(NOT_FOUND_ENVELOPE, { status: 404 });
|
||||
}
|
||||
}
|
||||
|
||||
function assertSameOrigin(request: Request): void {
|
||||
const origin = request.headers.get("origin");
|
||||
const fetchSite = request.headers.get("sec-fetch-site");
|
||||
if (
|
||||
(origin && origin !== new URL(request.url).origin) ||
|
||||
(fetchSite && fetchSite !== "same-origin" && fetchSite !== "none")
|
||||
) {
|
||||
throw Response.json(NOT_FOUND_ENVELOPE, { status: 404 });
|
||||
}
|
||||
}
|
||||
|
||||
function requiredToken(value: string | undefined): string {
|
||||
if (!value || value.length > 2_048) {
|
||||
throw Response.json(NOT_FOUND_ENVELOPE, { status: 404 });
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function requiredFormText(form: FormData, name: string): string {
|
||||
const value = form.get(name);
|
||||
if (typeof value !== "string" || !value.trim()) {
|
||||
throw new AdminPublicBookingManageError("invalid_request");
|
||||
}
|
||||
return value.trim();
|
||||
}
|
||||
|
||||
function parseSlot(value: string): readonly [Date, Date] {
|
||||
const [rawStart, rawEnd, ...extra] = value.split("|");
|
||||
const startsAt = new Date(rawStart ?? Number.NaN);
|
||||
const endsAt = new Date(rawEnd ?? Number.NaN);
|
||||
if (
|
||||
extra.length > 0 ||
|
||||
!Number.isFinite(startsAt.getTime()) ||
|
||||
!Number.isFinite(endsAt.getTime()) ||
|
||||
endsAt <= startsAt
|
||||
) {
|
||||
throw new AdminPublicBookingManageError("invalid_slot");
|
||||
}
|
||||
return [startsAt, endsAt];
|
||||
}
|
||||
|
||||
function validDate(value: string | null): string | null {
|
||||
return value && /^\d{4}-\d{2}-\d{2}$/u.test(value) ? value : null;
|
||||
}
|
||||
|
||||
function validTimeZone(value: string | null): string {
|
||||
const candidate = value?.trim() || "Europe/Berlin";
|
||||
try {
|
||||
new Intl.DateTimeFormat("de-DE", { timeZone: candidate }).format(
|
||||
new Date()
|
||||
);
|
||||
return candidate;
|
||||
} catch {
|
||||
return "Europe/Berlin";
|
||||
}
|
||||
}
|
||||
|
||||
function dateInTimeZone(now: Date, timeZone: string): string {
|
||||
const parts = new Intl.DateTimeFormat("en-CA", {
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
timeZone,
|
||||
}).formatToParts(now);
|
||||
const byType = new Map(parts.map((part) => [part.type, part.value]));
|
||||
return `${byType.get("year")}-${byType.get("month")}-${byType.get("day")}`;
|
||||
}
|
||||
Reference in new issue
Block a user