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
+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