Scheduling infrastructure for absolutely everyone.
chore: Integrate reschedule request booking audit (#26527)
Hariom Balharaf3a37886e37d
diff --git a/apps/web/test/handlers/requestReschedule.test.ts b/apps/web/test/handlers/requestReschedule.test.tsindex 5e3f8388166..9b4ca962db6 100644--- a/apps/web/test/handlers/requestReschedule.test.ts+++ b/apps/web/test/handlers/requestReschedule.test.ts@@ -579,5 +579,6 @@ function getTrpcHandlerData({} as unknown as NonNullable<TrpcSessionUser>,},input: input,+ source: "WEBAPP" as const,};}diff --git a/packages/features/booking-audit/lib/actions/RescheduleRequestedAuditActionService.ts b/packages/features/booking-audit/lib/actions/RescheduleRequestedAuditActionService.tsindex a98b55e6363..0da44589f85 100644--- a/packages/features/booking-audit/lib/actions/RescheduleRequestedAuditActionService.ts+++ b/packages/features/booking-audit/lib/actions/RescheduleRequestedAuditActionService.ts@@ -1,8 +1,12 @@import { z } from "zod";-import { StringChangeSchema, BooleanChangeSchema } from "../common/changeSchemas";import { AuditActionServiceHelper } from "./AuditActionServiceHelper";-import type { IAuditActionService, TranslationWithParams, GetDisplayTitleParams, GetDisplayJsonParams } from "./IAuditActionService";+import type {+ IAuditActionService,+ TranslationWithParams,+ GetDisplayTitleParams,+ GetDisplayJsonParams,+} from "./IAuditActionService";/*** Reschedule Requested Audit Action Service@@ -11,73 +15,70 @@ import type { IAuditActionService, TranslationWithParams, GetDisplayTitleParams,// Module-level because it is passed to IAuditActionService type outside the class scopeconst fieldsSchemaV1 = z.object({- cancellationReason: StringChangeSchema,- cancelledBy: StringChangeSchema,- rescheduled: BooleanChangeSchema.optional(),+ rescheduleReason: z.string().nullable(),+ rescheduledRequestedBy: z.string().nullable(),});export class RescheduleRequestedAuditActionService implements IAuditActionService {- readonly VERSION = 1;- public static readonly TYPE = "RESCHEDULE_REQUESTED" as const;- private static dataSchemaV1 = z.object({- version: z.literal(1),- fields: fieldsSchemaV1,- });- private static fieldsSchemaV1 = fieldsSchemaV1;- public static readonly latestFieldsSchema = fieldsSchemaV1;- // Union of all versions- public static readonly storedDataSchema = RescheduleRequestedAuditActionService.dataSchemaV1;- // Union of all versions- public static readonly storedFieldsSchema = RescheduleRequestedAuditActionService.fieldsSchemaV1;- private helper: AuditActionServiceHelper<- typeof RescheduleRequestedAuditActionService.latestFieldsSchema,- typeof RescheduleRequestedAuditActionService.storedDataSchema- >;+ readonly VERSION = 1;+ public static readonly TYPE = "RESCHEDULE_REQUESTED" as const;+ private static dataSchemaV1 = z.object({+ version: z.literal(1),+ fields: fieldsSchemaV1,+ });+ private static fieldsSchemaV1 = fieldsSchemaV1;+ public static readonly latestFieldsSchema = fieldsSchemaV1;+ // Union of all versions+ public static readonly storedDataSchema = RescheduleRequestedAuditActionService.dataSchemaV1;+ // Union of all versions+ public static readonly storedFieldsSchema = RescheduleRequestedAuditActionService.fieldsSchemaV1;+ private helper: AuditActionServiceHelper<+ typeof RescheduleRequestedAuditActionService.latestFieldsSchema,+ typeof RescheduleRequestedAuditActionService.storedDataSchema+ >;- constructor() {- this.helper = new AuditActionServiceHelper({- latestVersion: this.VERSION,- latestFieldsSchema: RescheduleRequestedAuditActionService.latestFieldsSchema,- storedDataSchema: RescheduleRequestedAuditActionService.storedDataSchema,- });- }+ constructor() {+ this.helper = new AuditActionServiceHelper({+ latestVersion: this.VERSION,+ latestFieldsSchema: RescheduleRequestedAuditActionService.latestFieldsSchema,+ storedDataSchema: RescheduleRequestedAuditActionService.storedDataSchema,+ });+ }- getVersionedData(fields: unknown) {- return this.helper.getVersionedData(fields);- }+ getVersionedData(fields: unknown) {+ return this.helper.getVersionedData(fields);+ }- parseStored(data: unknown) {- return this.helper.parseStored(data);- }+ parseStored(data: unknown) {+ return this.helper.parseStored(data);+ }- getVersion(data: unknown): number {- return this.helper.getVersion(data);- }+ getVersion(data: unknown): number {+ return this.helper.getVersion(data);+ }- migrateToLatest(data: unknown) {- // V1-only: validate and return as-is (no migration needed)- const validated = fieldsSchemaV1.parse(data);- return { isMigrated: false, latestData: validated };- }+ migrateToLatest(data: unknown) {+ // V1-only: validate and return as-is (no migration needed)+ const validated = fieldsSchemaV1.parse(data);+ return { isMigrated: false, latestData: validated };+ }- async getDisplayTitle(_: GetDisplayTitleParams): Promise<TranslationWithParams> {- return { key: "booking_audit_action.reschedule_requested" };- }+ async getDisplayTitle(_: GetDisplayTitleParams): Promise<TranslationWithParams> {+ return { key: "booking_audit_action.reschedule_requested" };+ }- getDisplayJson({- storedData,- }: GetDisplayJsonParams): RescheduleRequestedAuditDisplayData {- const { fields } = this.parseStored(storedData);- return {- reason: fields.cancellationReason.new ?? null,- requestedBy: fields.cancelledBy.new ?? null,- };- }+ getDisplayJson({ storedData }: GetDisplayJsonParams): RescheduleRequestedAuditDisplayData {+ const { fields } = this.parseStored(storedData);+ return {+ reason: fields.rescheduleReason ?? null,+ requestedBy: fields.rescheduledRequestedBy ?? null,+ };+ }}export type RescheduleRequestedAuditData = z.infer<typeof fieldsSchemaV1>;export type RescheduleRequestedAuditDisplayData = {- reason: string | null;- requestedBy: string | null;+ reason: string | null;+ requestedBy: string | null;};diff --git a/packages/trpc/server/routers/viewer/bookings/_router.tsx b/packages/trpc/server/routers/viewer/bookings/_router.tsxindex 2b456aceb0d..6d7f5fee855 100644--- a/packages/trpc/server/routers/viewer/bookings/_router.tsx+++ b/packages/trpc/server/routers/viewer/bookings/_router.tsx@@ -1,3 +1,5 @@+import type { ActionSource } from "@calcom/features/booking-audit/lib/types/actionSource";+import authedProcedure from "../../../procedures/authedProcedure";import publicProcedure from "../../../procedures/publicProcedure";import { router } from "../../../trpc";@@ -30,6 +32,7 @@ export const bookingsRouter = router({return requestRescheduleHandler({ctx,input,+ source: "WEBAPP"});}),diff --git a/packages/trpc/server/routers/viewer/bookings/requestReschedule.handler.ts b/packages/trpc/server/routers/viewer/bookings/requestReschedule.handler.tsindex 27102492c42..7ee5bd3fbd0 100644--- a/packages/trpc/server/routers/viewer/bookings/requestReschedule.handler.ts+++ b/packages/trpc/server/routers/viewer/bookings/requestReschedule.handler.ts@@ -5,6 +5,9 @@ import { getDelegationCredentialOrRegularCredential } from "@calcom/app-store/deimport { getUsersCredentialsIncludeServiceAccountKey } from "@calcom/app-store/delegationCredential";import dayjs from "@calcom/dayjs";import { sendRequestRescheduleEmailAndSMS } from "@calcom/emails/email-manager";+import { makeUserActor } from "@calcom/features/booking-audit/lib/makeActor";+import type { ActionSource } from "@calcom/features/booking-audit/lib/types/actionSource";+import { getBookingEventHandlerService } from "@calcom/features/bookings/di/BookingEventHandlerService.container";import { getCalEventResponses } from "@calcom/features/bookings/lib/getCalEventResponses";import { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository";import { deleteMeeting } from "@calcom/features/conferencing/lib/videoClient";@@ -43,9 +46,10 @@ type RequestRescheduleOptions = {user: NonNullable<TrpcSessionUser>;};input: TRequestRescheduleInputSchema;+ source: ActionSource;};const log = logger.getSubLogger({ prefix: ["requestRescheduleHandler"] });-export const requestRescheduleHandler = async ({ ctx, input }: RequestRescheduleOptions) => {+export const requestRescheduleHandler = async ({ ctx, input, source }: RequestRescheduleOptions) => {const { user } = ctx;const { bookingUid, rescheduleReason: cancellationReason } = input;log.debug("Started", safeStringify({ bookingUid }));@@ -305,4 +309,16 @@ export const requestRescheduleHandler = async ({ ctx, input }: RequestReschedule}));await Promise.all(promises);++ const bookingEventHandlerService = getBookingEventHandlerService();+ await bookingEventHandlerService.onRescheduleRequested({+ bookingUid: bookingToReschedule.uid,+ actor: makeUserActor(user.uuid),+ organizationId: orgId ?? null,+ source,+ auditData: {+ rescheduleReason: cancellationReason ?? null,+ rescheduledRequestedBy: user.email,+ },+ });};