177 lines
5.0 KiB
TypeScript
177 lines
5.0 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
|
|
import {
|
|
createAdminBookingLifecycleService,
|
|
createPrismaAdminBookingLifecycleRepository,
|
|
type AdminBookingManageProof,
|
|
} from "@hivegrid/db/admin-booking-lifecycle.server";
|
|
import { runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
|
|
|
import { db } from "~/utils/db.server";
|
|
|
|
import {
|
|
hashAdminBookingManageNonce,
|
|
type AdminBookingManageClaims,
|
|
} from "./admin-booking-manage-token.server";
|
|
import {
|
|
loadAdminPublicBookingPage,
|
|
submitAdminPublicBooking,
|
|
} from "./admin-public-booking.server";
|
|
|
|
export async function loadAdminManagedBooking(input: {
|
|
readonly claims: AdminBookingManageClaims;
|
|
readonly date: string;
|
|
}) {
|
|
return runWithRlsContext({ userId: input.claims.adminUserId }, async () => {
|
|
const booking = await readManagedBooking(input.claims);
|
|
if (!booking) return null;
|
|
const page = await loadAdminPublicBookingPage({
|
|
slug: booking.bookingPage.slug,
|
|
date: input.date,
|
|
});
|
|
return page ? { booking, page } : null;
|
|
});
|
|
}
|
|
|
|
export async function cancelAdminManagedBooking(input: {
|
|
readonly claims: AdminBookingManageClaims;
|
|
readonly reason: string;
|
|
readonly now?: Date;
|
|
}) {
|
|
const now = input.now ?? new Date();
|
|
return runWithRlsContext({ userId: input.claims.adminUserId }, async () => {
|
|
const booking = await readManagedBooking(input.claims, now);
|
|
if (!booking) throw new AdminPublicBookingManageError("booking_unavailable");
|
|
return lifecycle().cancelPublic({
|
|
bookingId: booking.id,
|
|
reason: input.reason,
|
|
actor: publicActor(input.claims.adminUserId),
|
|
manageProof: proof(input.claims, now),
|
|
});
|
|
});
|
|
}
|
|
|
|
export async function rescheduleAdminManagedBooking(input: {
|
|
readonly claims: AdminBookingManageClaims;
|
|
readonly date: string;
|
|
readonly startsAt: Date;
|
|
readonly endsAt: Date;
|
|
readonly now?: Date;
|
|
}) {
|
|
const now = input.now ?? new Date();
|
|
return runWithRlsContext({ userId: input.claims.adminUserId }, async () => {
|
|
const previous = await readManagedBooking(input.claims, now);
|
|
const participant = previous?.participants[0];
|
|
if (!previous || !participant) {
|
|
throw new AdminPublicBookingManageError("booking_unavailable");
|
|
}
|
|
const replacement = await submitAdminPublicBooking(
|
|
{
|
|
slug: previous.bookingPage.slug,
|
|
date: input.date,
|
|
startsAt: input.startsAt,
|
|
endsAt: input.endsAt,
|
|
firstName: previous.contact.firstName,
|
|
lastName: previous.contact.lastName,
|
|
email: participant.email,
|
|
timeZone: previous.bookerTimeZone,
|
|
locale: participant.locale ?? "de",
|
|
answers: toAnswers(previous.intakeAnswers),
|
|
submissionId: rescheduleSubmissionId(
|
|
input.claims.nonce,
|
|
input.startsAt
|
|
),
|
|
manualHostAdminUserId: previous.adminUserId,
|
|
dealId: previous.dealId,
|
|
reschedule: {
|
|
claims: input.claims,
|
|
nonceHash: hashAdminBookingManageNonce(
|
|
input.claims.nonce
|
|
),
|
|
},
|
|
},
|
|
now
|
|
);
|
|
return replacement;
|
|
});
|
|
}
|
|
|
|
async function readManagedBooking(
|
|
claims: AdminBookingManageClaims,
|
|
now = new Date()
|
|
) {
|
|
return db.adminBooking.findFirst({
|
|
where: {
|
|
id: claims.bookingId,
|
|
adminUserId: claims.adminUserId,
|
|
manageTokenVersion: claims.version,
|
|
manageTokenExpiresAt: { gt: now },
|
|
status: {
|
|
in: ["reserved", "confirmed", "provider_sync_failed"],
|
|
},
|
|
},
|
|
include: {
|
|
bookingPage: { select: { slug: true, title: true } },
|
|
meetingType: { select: { name: true, durationMinutes: true } },
|
|
contact: { select: { firstName: true, lastName: true } },
|
|
participants: {
|
|
where: { isOrganizer: false },
|
|
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
|
|
take: 1,
|
|
select: { email: true, locale: true },
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
function lifecycle() {
|
|
return createAdminBookingLifecycleService({
|
|
repository: createPrismaAdminBookingLifecycleRepository(db),
|
|
authorize: () => undefined,
|
|
});
|
|
}
|
|
|
|
function proof(
|
|
claims: AdminBookingManageClaims,
|
|
now: Date
|
|
): AdminBookingManageProof {
|
|
return {
|
|
version: claims.version,
|
|
nonceHash: hashAdminBookingManageNonce(claims.nonce),
|
|
now,
|
|
};
|
|
}
|
|
|
|
function publicActor(adminUserId: string) {
|
|
return {
|
|
adminUserId,
|
|
reason: "Öffentliche Terminverwaltung",
|
|
};
|
|
}
|
|
|
|
function toAnswers(value: unknown): Readonly<Record<string, string>> {
|
|
if (!value || typeof value !== "object" || Array.isArray(value)) return {};
|
|
return Object.fromEntries(
|
|
Object.entries(value).filter(
|
|
(entry): entry is [string, string] => typeof entry[1] === "string"
|
|
)
|
|
);
|
|
}
|
|
|
|
function rescheduleSubmissionId(nonce: string, startsAt: Date): string {
|
|
return createHash("sha256")
|
|
.update("admin-booking-reschedule")
|
|
.update("\0")
|
|
.update(nonce)
|
|
.update("\0")
|
|
.update(startsAt.toISOString())
|
|
.digest("hex");
|
|
}
|
|
|
|
export class AdminPublicBookingManageError extends Error {
|
|
constructor(readonly code: string) {
|
|
super(code);
|
|
this.name = "AdminPublicBookingManageError";
|
|
}
|
|
}
|