feat: create BookingHistoryViewerService to combine audit logs with routing form submissions (#26045)

* feat: create BookingHistoryViewerService to combine audit logs with routing form submissions

Co-Authored-By: [email protected] <[email protected]>

* refactor(booking): rename audit log query and enhance type safety

- Updated the booking logs view to use the new getBookingHistory query instead of getAuditLogs.
- Introduced DisplayBookingAuditLog type for improved clarity in BookingAuditViewerService.
- Refactored BookingHistoryViewerService to utilize the new DisplayBookingAuditLog type and added sorting functionality for history logs.
- Adjusted related types and methods to ensure consistency across services.

* refactor(routing-forms): streamline imports and enhance type definitions

- Consolidated type exports and imports from the features library for better organization.
- Removed redundant type definitions and functions in zod.ts, findFieldValueByIdentifier.ts, getFieldIdentifier.ts, and parseRoutingFormResponse.ts.
- Introduced new utility functions for handling field responses and parsing routing form responses.
- Improved type safety and clarity across routing form response handling.

* fix: remove double prefix from uniqueId in form submission entry

Co-Authored-By: [email protected] <[email protected]>

* 1c97f9cc5d50416788c01876fe539bcc9750e9b2 (#26453)

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
Hariom BalharaandDevin AI authored and GitHub committed 2026-01-05 11:11:05 +00:00
1 parent a7fd7890a7
commit 9d39590510
24 files changed
+341 -111

No files matched your search

@@ -0,0 +1,21 @@
import { PrismaRoutingFormResponseRepository } from "@calcom/lib/server/repository/PrismaRoutingFormResponseRepository";
import { ROUTING_FORM_DI_TOKENS } from "@calcom/features/routing-forms/di/tokens";
import { bindModuleToClassOnToken } from "@calcom/features/di/di";
import { moduleLoader as prismaModuleLoader } from "@calcom/features/di/modules/Prisma";
import { createModule } from "../../di/di";
export const routingFormResponseRepositoryModule = createModule();
const token = ROUTING_FORM_DI_TOKENS.ROUTING_FORM_RESPONSE_REPOSITORY;
const moduleToken = ROUTING_FORM_DI_TOKENS.ROUTING_FORM_RESPONSE_REPOSITORY_MODULE;
const loadModule = bindModuleToClassOnToken({
module: routingFormResponseRepositoryModule,
moduleToken,
token,
classs: PrismaRoutingFormResponseRepository,
dep: prismaModuleLoader,
});
export const moduleLoader = {
token,
loadModule,
};
@@ -0,0 +1,4 @@
export const ROUTING_FORM_DI_TOKENS = {
ROUTING_FORM_RESPONSE_REPOSITORY: Symbol("RoutingFormResponseRepository"),
ROUTING_FORM_RESPONSE_REPOSITORY_MODULE: Symbol("RoutingFormResponseRepositoryModule"),
};
@@ -0,0 +1,21 @@
import getFieldIdentifier from "./getFieldIdentifier";
import type { RoutingFormResponseData } from "./types";
type FindFieldValueByIdentifierResult =
| { success: true; data: string | string[] | number | null }
| { success: false; error: string };
export function findFieldValueByIdentifier(
data: RoutingFormResponseData,
identifier: string
): FindFieldValueByIdentifierResult {
const field = data.fields.find((field) => getFieldIdentifier(field) === identifier);
if (!field) {
return { success: false, error: `Field with identifier ${identifier} not found` };
}
const fieldValue = data.response[field.id]?.value;
return { success: true, data: fieldValue ?? null };
}
@@ -0,0 +1,6 @@
import type { Field } from "./types";
const getFieldIdentifier = (field: Field) => field.identifier || field.label;
export default getFieldIdentifier;
@@ -0,0 +1,16 @@
import { parseRoutingFormResponse } from "./parseRoutingFormResponse";
import { findFieldValueByIdentifier } from "./findFieldValueByIdentifier";
export const getFieldResponseByIdentifier = ({
responsePayload,
formFields,
identifier,
}: {
responsePayload: unknown;
formFields: unknown;
identifier: string;
}) => {
const parsedResponse = parseRoutingFormResponse(responsePayload, formFields);
const emailFieldResult = findFieldValueByIdentifier(parsedResponse, identifier);
return emailFieldResult;
};
@@ -0,0 +1,9 @@
import { zodNonRouterField, routingFormResponseInDbSchema } from "./zod";
import type { RoutingFormResponseData } from "./types";
export function parseRoutingFormResponse(rawResponse: unknown, formFields: unknown): RoutingFormResponseData {
const response = routingFormResponseInDbSchema.parse(rawResponse);
const fields = zodNonRouterField.array().parse(formFields);
return { response, fields };
}
@@ -0,0 +1,22 @@
import type z from "zod";
import type { zodNonRouterField, routingFormResponseInDbSchema } from "./zod";
export type FormResponse = Record<
// Field ID
string,
{
value: number | string | string[];
label: string;
identifier?: string;
}
>;
export type Field = z.infer<typeof zodNonRouterField>;
export type Fields = Field[];
export type RoutingFormResponseData = {
fields: z.infer<typeof zodNonRouterField>[];
response: z.infer<typeof routingFormResponseInDbSchema>;
};
@@ -0,0 +1,56 @@
import { z } from "zod";
export type FieldOption = {
label: string;
id: string | null;
};
export type TNonRouterField = {
id: string;
label: string;
identifier?: string;
placeholder?: string;
type: string;
/** @deprecated in favour of `options` */
selectText?: string;
required?: boolean;
deleted?: boolean;
options?: FieldOption[];
};
// Note: zodNonRouterField is NOT annotated with z.ZodType because it uses .extend() below
// which requires the full ZodObject type to be preserved
export const zodNonRouterField = z.object({
id: z.string(),
label: z.string(),
identifier: z.string().optional(),
placeholder: z.string().optional(),
type: z.string(),
/**
* @deprecated in favour of `options`
*/
selectText: z.string().optional(),
required: z.boolean().optional(),
deleted: z.boolean().optional(),
options: z
.array(
z.object({
label: z.string(),
// To keep backwards compatibility with the options generated from legacy selectText, we allow saving null as id
// It helps in differentiating whether the routing logic should consider the option.label as value or option.id as value.
// This is important for legacy routes which has option.label saved in conditions and it must keep matching with the value of the option
id: z.string().or(z.null()),
})
)
.optional(),
});
// This is different from FormResponse in types.d.ts in that it has label optional. We don't seem to be using label at this point, so we might want to use this only while saving the response when Routing Form is submitted
// Record key is formFieldId
export const routingFormResponseInDbSchema = z.record(
z.object({
label: z.string().optional(),
value: z.union([z.string(), z.number(), z.array(z.string())]),
})
);