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
+111
@@ -0,0 +1,111 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { TenantUserStatus } from "~/application/enums/tenants/TenantUserStatus";
|
||||
|
||||
const mocks = vi.hoisted(() => {
|
||||
const tx = {
|
||||
bookingPage: { findFirst: vi.fn(), create: vi.fn(), update: vi.fn() },
|
||||
bookingPageProvider: { deleteMany: vi.fn(), createMany: vi.fn() },
|
||||
tenantUser: { findMany: vi.fn() },
|
||||
form: { findFirst: vi.fn() },
|
||||
};
|
||||
return {
|
||||
tx,
|
||||
withTenantRLS: vi.fn(async (_tenantId: string, callback: (client: typeof tx) => Promise<unknown>) => callback(tx)),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("~/utils/db.server", () => ({ db: {} }));
|
||||
vi.mock("~/utils/db/.server/tenantRls.server", () => ({ withTenantRLS: mocks.withTenantRLS }));
|
||||
|
||||
import { createBookingPage, updateBookingPage, type BookingPageInput } from "./booking.db.server";
|
||||
|
||||
const input: BookingPageInput = {
|
||||
tenantId: "tenant-a",
|
||||
createdByUserId: "admin-a",
|
||||
slug: "beratung",
|
||||
title: "Beratung",
|
||||
description: null,
|
||||
durationMinutes: 30,
|
||||
confirmationEmailSubject: "Bestätigt",
|
||||
confirmationEmailBody: "Dein Termin ist bestätigt.",
|
||||
isPublished: true,
|
||||
providerUserIds: ["provider-a"],
|
||||
intakeFormId: "form-a",
|
||||
};
|
||||
|
||||
describe("booking page tenant boundaries", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mocks.tx.bookingPage.findFirst.mockResolvedValue({ id: "page-a" });
|
||||
mocks.tx.bookingPage.create.mockResolvedValue({ id: "page-a" });
|
||||
mocks.tx.tenantUser.findMany.mockResolvedValue([{ userId: "provider-a" }]);
|
||||
mocks.tx.form.findFirst.mockResolvedValue({ id: "form-a" });
|
||||
});
|
||||
|
||||
it("rejects a booking page owned by another tenant before any write", async () => {
|
||||
mocks.tx.bookingPage.findFirst.mockResolvedValueOnce(null);
|
||||
|
||||
await expect(updateBookingPage("page-from-tenant-b", input)).resolves.toBe(false);
|
||||
|
||||
expect(mocks.withTenantRLS).toHaveBeenCalledWith("tenant-a", expect.any(Function));
|
||||
expect(mocks.tx.bookingPage.findFirst).toHaveBeenCalledWith({
|
||||
where: { id: "page-from-tenant-b", tenantId: "tenant-a", archivedAt: null },
|
||||
select: { id: true },
|
||||
});
|
||||
expect(mocks.tx.tenantUser.findMany).not.toHaveBeenCalled();
|
||||
expect(mocks.tx.bookingPage.update).not.toHaveBeenCalled();
|
||||
expect(mocks.tx.bookingPageProvider.deleteMany).not.toHaveBeenCalled();
|
||||
expect(mocks.tx.bookingPageProvider.createMany).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects a provider who is not an active member of the tenant", async () => {
|
||||
mocks.tx.tenantUser.findMany.mockResolvedValueOnce([]);
|
||||
|
||||
await expect(updateBookingPage("page-a", { ...input, providerUserIds: ["provider-from-tenant-b"] })).resolves.toBe(false);
|
||||
|
||||
expect(mocks.tx.tenantUser.findMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
tenantId: "tenant-a",
|
||||
userId: { in: ["provider-from-tenant-b"] },
|
||||
status: TenantUserStatus.ACTIVE,
|
||||
},
|
||||
select: { userId: true },
|
||||
});
|
||||
expect(mocks.tx.bookingPage.update).not.toHaveBeenCalled();
|
||||
expect(mocks.tx.bookingPageProvider.deleteMany).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects an intake form that is not visible in the tenant", async () => {
|
||||
mocks.tx.form.findFirst.mockResolvedValueOnce(null);
|
||||
|
||||
await expect(updateBookingPage("page-a", { ...input, intakeFormId: "form-from-tenant-b" })).resolves.toBe(false);
|
||||
|
||||
expect(mocks.tx.form.findFirst).toHaveBeenCalledWith({
|
||||
where: { id: "form-from-tenant-b", tenantId: "tenant-a", status: { not: "archived" } },
|
||||
select: { id: true },
|
||||
});
|
||||
expect(mocks.tx.bookingPage.update).not.toHaveBeenCalled();
|
||||
expect(mocks.tx.bookingPageProvider.deleteMany).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("tenant-scopes the page update and provider replacement", async () => {
|
||||
await expect(updateBookingPage("page-a", { ...input, providerUserIds: ["provider-a", "provider-a"] })).resolves.toBe(true);
|
||||
|
||||
expect(mocks.tx.bookingPage.update).toHaveBeenCalledWith(expect.objectContaining({ where: { id: "page-a", tenantId: "tenant-a" } }));
|
||||
expect(mocks.tx.bookingPageProvider.deleteMany).toHaveBeenCalledWith({
|
||||
where: { bookingPageId: "page-a", tenantId: "tenant-a" },
|
||||
});
|
||||
expect(mocks.tx.bookingPageProvider.createMany).toHaveBeenCalledWith({
|
||||
data: [{ tenantId: "tenant-a", bookingPageId: "page-a", providerUserId: "provider-a", displayOrder: 0 }],
|
||||
});
|
||||
});
|
||||
|
||||
it("validates references before creating a booking page", async () => {
|
||||
mocks.tx.form.findFirst.mockResolvedValueOnce(null);
|
||||
|
||||
await expect(createBookingPage({ ...input, intakeFormId: "form-from-tenant-b" })).resolves.toBeNull();
|
||||
|
||||
expect(mocks.tx.bookingPage.create).not.toHaveBeenCalled();
|
||||
expect(mocks.tx.bookingPageProvider.createMany).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
+98
-10
@@ -3,6 +3,8 @@
|
||||
// guard. Slot config + provider list feed the pure slot engine.
|
||||
|
||||
import { db } from "~/utils/db.server";
|
||||
import { TenantUserStatus } from "~/application/enums/tenants/TenantUserStatus";
|
||||
import { withTenantRLS, type TenantRlsClient } from "~/utils/db/.server/tenantRls.server";
|
||||
import type { SlotEngineConfig } from "../services/slotEngine";
|
||||
|
||||
export interface PublicBookingPage {
|
||||
@@ -147,6 +149,8 @@ export interface AppointmentDetail {
|
||||
intakeAnswers: Record<string, unknown>;
|
||||
cancellationReason: string | null;
|
||||
notesInternal: string | null;
|
||||
/** The booking page's slot config, so the reschedule picker reuses the engine. */
|
||||
pageConfig: SlotEngineConfig;
|
||||
}
|
||||
|
||||
/** One appointment's detail (M06 Block D). Returns providerUserId so the route owner-checks. */
|
||||
@@ -163,7 +167,17 @@ export async function getAppointmentDetail(tenantId: string, appointmentId: stri
|
||||
intakeAnswers: true,
|
||||
cancellationReason: true,
|
||||
notesInternal: true,
|
||||
bookingPage: { select: { title: true } },
|
||||
bookingPage: {
|
||||
select: {
|
||||
title: true,
|
||||
durationMinutes: true,
|
||||
bufferBeforeMinutes: true,
|
||||
bufferAfterMinutes: true,
|
||||
slotIncrementMinutes: true,
|
||||
minNoticeHours: true,
|
||||
maxAdvanceDays: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!row) {
|
||||
@@ -183,6 +197,7 @@ export async function getAppointmentDetail(tenantId: string, appointmentId: stri
|
||||
intakeAnswers: (row.intakeAnswers as Record<string, unknown>) ?? {},
|
||||
cancellationReason: row.cancellationReason,
|
||||
notesInternal: row.notesInternal,
|
||||
pageConfig: toConfig(row.bookingPage),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -215,13 +230,14 @@ export interface BookingPageEdit {
|
||||
confirmationEmailBody: string;
|
||||
isPublished: boolean;
|
||||
providerUserIds: string[];
|
||||
intakeFormId: string | null;
|
||||
}
|
||||
|
||||
/** Load a booking page for the editor (M06 Block E.2). */
|
||||
export async function getBookingPageForEdit(tenantId: string, pageId: string): Promise<BookingPageEdit | null> {
|
||||
const row = await db.bookingPage.findFirst({
|
||||
where: { id: pageId, tenantId, archivedAt: null },
|
||||
select: { id: true, slug: true, title: true, description: true, durationMinutes: true, confirmationEmailSubject: true, confirmationEmailBody: true, isPublished: true, providers: { select: { providerUserId: true } } },
|
||||
select: { id: true, slug: true, title: true, description: true, durationMinutes: true, confirmationEmailSubject: true, confirmationEmailBody: true, isPublished: true, intakeFormId: true, providers: { select: { providerUserId: true } } },
|
||||
});
|
||||
if (!row) {
|
||||
return null;
|
||||
@@ -236,9 +252,25 @@ export async function getBookingPageForEdit(tenantId: string, pageId: string): P
|
||||
confirmationEmailBody: row.confirmationEmailBody,
|
||||
isPublished: row.isPublished,
|
||||
providerUserIds: row.providers.map((p) => p.providerUserId),
|
||||
intakeFormId: row.intakeFormId,
|
||||
};
|
||||
}
|
||||
|
||||
export interface IntakeFormOption {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
/** Non-archived tenant forms selectable as a booking page's intake form (AK-P16c link). */
|
||||
export async function listIntakeFormOptions(tenantId: string): Promise<IntakeFormOption[]> {
|
||||
const rows = await db.form.findMany({
|
||||
where: { tenantId, status: { not: "archived" } },
|
||||
select: { id: true, name: true },
|
||||
orderBy: { updatedAt: "desc" },
|
||||
});
|
||||
return rows.map((r) => ({ id: r.id, name: r.name }));
|
||||
}
|
||||
|
||||
export interface BookingPageInput {
|
||||
tenantId: string;
|
||||
createdByUserId: string;
|
||||
@@ -250,11 +282,51 @@ export interface BookingPageInput {
|
||||
confirmationEmailBody: string;
|
||||
isPublished: boolean;
|
||||
providerUserIds: string[];
|
||||
intakeFormId?: string | null;
|
||||
}
|
||||
|
||||
async function validateBookingPageReferences(
|
||||
tx: TenantRlsClient,
|
||||
input: Pick<BookingPageInput, "tenantId" | "providerUserIds" | "intakeFormId">,
|
||||
): Promise<string[] | null> {
|
||||
const providerUserIds = [...new Set(input.providerUserIds)];
|
||||
if (providerUserIds.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const activeProviders = await tx.tenantUser.findMany({
|
||||
where: {
|
||||
tenantId: input.tenantId,
|
||||
userId: { in: providerUserIds },
|
||||
status: TenantUserStatus.ACTIVE,
|
||||
},
|
||||
select: { userId: true },
|
||||
});
|
||||
const activeProviderIds = new Set(activeProviders.map((provider) => provider.userId));
|
||||
if (providerUserIds.some((providerUserId) => !activeProviderIds.has(providerUserId))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (input.intakeFormId) {
|
||||
const intakeForm = await tx.form.findFirst({
|
||||
where: { id: input.intakeFormId, tenantId: input.tenantId, status: { not: "archived" } },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!intakeForm) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return providerUserIds;
|
||||
}
|
||||
|
||||
/** Create a booking page + its provider links (M06 Block E.2). */
|
||||
export async function createBookingPage(input: BookingPageInput): Promise<string> {
|
||||
return db.$transaction(async (tx) => {
|
||||
export async function createBookingPage(input: BookingPageInput): Promise<string | null> {
|
||||
return withTenantRLS(input.tenantId, async (tx) => {
|
||||
const providerUserIds = await validateBookingPageReferences(tx, input);
|
||||
if (!providerUserIds) {
|
||||
return null;
|
||||
}
|
||||
const page = await tx.bookingPage.create({
|
||||
data: {
|
||||
tenantId: input.tenantId,
|
||||
@@ -266,21 +338,35 @@ export async function createBookingPage(input: BookingPageInput): Promise<string
|
||||
confirmationEmailSubject: input.confirmationEmailSubject,
|
||||
confirmationEmailBody: input.confirmationEmailBody,
|
||||
isPublished: input.isPublished,
|
||||
intakeFormId: input.intakeFormId ?? null,
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
await tx.bookingPageProvider.createMany({
|
||||
data: input.providerUserIds.map((providerUserId, index) => ({ tenantId: input.tenantId, bookingPageId: page.id, providerUserId, displayOrder: index })),
|
||||
data: providerUserIds.map((providerUserId, index) => ({ tenantId: input.tenantId, bookingPageId: page.id, providerUserId, displayOrder: index })),
|
||||
});
|
||||
return page.id;
|
||||
});
|
||||
}
|
||||
|
||||
/** Update a booking page + replace its provider links (M06 Block E.2). */
|
||||
export async function updateBookingPage(pageId: string, input: BookingPageInput): Promise<void> {
|
||||
await db.$transaction(async (tx) => {
|
||||
export async function updateBookingPage(pageId: string, input: BookingPageInput): Promise<boolean> {
|
||||
return withTenantRLS(input.tenantId, async (tx) => {
|
||||
const page = await tx.bookingPage.findFirst({
|
||||
where: { id: pageId, tenantId: input.tenantId, archivedAt: null },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!page) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const providerUserIds = await validateBookingPageReferences(tx, input);
|
||||
if (!providerUserIds) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await tx.bookingPage.update({
|
||||
where: { id: pageId },
|
||||
where: { id: page.id, tenantId: input.tenantId },
|
||||
data: {
|
||||
slug: input.slug,
|
||||
title: input.title,
|
||||
@@ -289,12 +375,14 @@ export async function updateBookingPage(pageId: string, input: BookingPageInput)
|
||||
confirmationEmailSubject: input.confirmationEmailSubject,
|
||||
confirmationEmailBody: input.confirmationEmailBody,
|
||||
isPublished: input.isPublished,
|
||||
intakeFormId: input.intakeFormId ?? null,
|
||||
},
|
||||
});
|
||||
await tx.bookingPageProvider.deleteMany({ where: { bookingPageId: pageId } });
|
||||
await tx.bookingPageProvider.deleteMany({ where: { bookingPageId: page.id, tenantId: input.tenantId } });
|
||||
await tx.bookingPageProvider.createMany({
|
||||
data: input.providerUserIds.map((providerUserId, index) => ({ tenantId: input.tenantId, bookingPageId: pageId, providerUserId, displayOrder: index })),
|
||||
data: providerUserIds.map((providerUserId, index) => ({ tenantId: input.tenantId, bookingPageId: page.id, providerUserId, displayOrder: index })),
|
||||
});
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user