chore: Integrate edit location booking audit (#26569)

## What does this PR do?

Integrates booking audit logging for the edit location functionality, following the pattern established in PR #26046 (booking creation/rescheduling audit).

This PR adds audit logging when a booking's location is changed through:
1. **Web app** (tRPC handler): `packages/trpc/server/routers/viewer/bookings/editLocation.handler.ts`
2. **API v2**: `apps/api/v2/src/ee/bookings/2024-08-13/services/booking-location.service.ts`

### Changes:
- Added `actionSource` as a **required** parameter to `editLocationHandler` (no fallback)
- Added optional `userUuid` parameter (defaults to logged-in user's uuid)
- Added `ValidActionSource` type that excludes "UNKNOWN" for client-facing APIs
- Captures old location before update for audit data
- Calls `BookingEventHandlerService.onLocationChanged()` after successful location update
- Web app uses `actionSource: "WEBAPP"`, API v2 uses `actionSource: "API_V2"`
- Updated router to explicitly pass `actionSource: "WEBAPP"`
- Updated test to pass `actionSource: "WEBAPP"`
- **API v2**: Uses NestJS dependency injection pattern with `BookingEventHandlerService` injected via constructor

### Updates since last revision:
- **Created `BookingEventHandlerModule`** (`apps/api/v2/src/lib/modules/booking-event-handler.module.ts`) to encapsulate `BookingEventHandlerService` and its dependencies (Logger, TaskerService, HashedLinkService, BookingAuditProducerService)
- Updated both bookings modules (2024-04-15 and 2024-08-13) to import `BookingEventHandlerModule` instead of listing individual providers
- This reduces code duplication and makes dependency management cleaner

## Mandatory Tasks (DO NOT REMOVE)

- [x] I have self-reviewed the code (A decent size PR without self-review might be rejected).
- [x] I have updated the developer docs in /docs if this PR makes changes that would require a [documentation change](https://cal.com/docs). N/A - no documentation changes needed.
- [x] I confirm automated tests are in place that prove my fix is effective or that my feature works. N/A - using existing audit infrastructure that is already tested.

## How should this be tested?

1. Update a booking's location through the web app
2. Update a booking's location through API v2
3. Verify audit logs are created with:
   - Correct `bookingUid`
   - Correct `actor` (user who made the change)
   - Correct `source` ("WEBAPP" or "API_V2")
   - Correct `auditData.location.old` and `auditData.location.new` values

## Checklist

- [x] My code follows the style guidelines of this project
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have checked if my changes generate no new warnings

## Human Review Checklist

- [ ] Verify all callers of `editLocationHandler` pass `actionSource` (router updated, test updated)
- [ ] Verify `organizationId` derivation is correct in both handlers (tRPC uses `booking.user?.profiles?.[0]?.organizationId`, API v2 uses `existingBookingHost.organizationId`)
- [ ] Confirm audit call placement after location update is intentional (audit failures would fail the operation even though location was already updated)
- [ ] Note: API v2 has its own implementation and does NOT reuse `editLocationHandler` - this is correct
- [ ] Verify `BookingEventHandlerModule` properly exports `BookingEventHandlerService` and is imported in both bookings modules
- [ ] Verify the `updateBookingLocationInDb` return value (`{ updatedLocation }`) is destructured and used correctly for audit data
- [ ] Verify API v2 uses `bookingLocation` for audit `new` value, while tRPC uses `updatedLocation` from DB update

---


Link to Devin run: https://app.devin.ai/sessions/fd1d439779674050a26ea3fa7d799943
Requested by: @hariombalhara
This commit is contained in:
Hariom Balhara authored and GitHub committed 2026-01-20 18:40:48 +05:30
1 parent 39ae54d682
commit 7ec8601bd6
10 files changed
+326 -82

No files matched your search

@@ -3,7 +3,11 @@ import { z } from "zod";
import { getHumanReadableLocationValue } from "@calcom/app-store/locations";
import { StringChangeSchema } from "../common/changeSchemas";
import { AuditActionServiceHelper } from "./AuditActionServiceHelper";
import type { IAuditActionService, TranslationWithParams, GetDisplayTitleParams } from "./IAuditActionService";
import type {
IAuditActionService,
TranslationWithParams,
GetDisplayTitleParams,
} from "./IAuditActionService";
import { getTranslation } from "@calcom/lib/server/i18n";
/**
* Location Changed Audit Action Service
@@ -12,67 +16,67 @@ import { getTranslation } from "@calcom/lib/server/i18n";
// Module-level because it is passed to IAuditActionService type outside the class scope
const fieldsSchemaV1 = z.object({
location: StringChangeSchema,
location: StringChangeSchema,
});
export class LocationChangedAuditActionService implements IAuditActionService {
readonly VERSION = 1;
public static readonly TYPE = "LOCATION_CHANGED" as const;
private static dataSchemaV1 = z.object({
version: z.literal(1),
fields: fieldsSchemaV1,
readonly VERSION = 1;
public static readonly TYPE = "LOCATION_CHANGED" 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 = LocationChangedAuditActionService.dataSchemaV1;
// Union of all versions
public static readonly storedFieldsSchema = LocationChangedAuditActionService.fieldsSchemaV1;
private helper: AuditActionServiceHelper<
typeof LocationChangedAuditActionService.latestFieldsSchema,
typeof LocationChangedAuditActionService.storedDataSchema
>;
constructor() {
this.helper = new AuditActionServiceHelper({
latestVersion: this.VERSION,
latestFieldsSchema: LocationChangedAuditActionService.latestFieldsSchema,
storedDataSchema: LocationChangedAuditActionService.storedDataSchema,
});
private static fieldsSchemaV1 = fieldsSchemaV1;
public static readonly latestFieldsSchema = fieldsSchemaV1;
// Union of all versions
public static readonly storedDataSchema = LocationChangedAuditActionService.dataSchemaV1;
// Union of all versions
public static readonly storedFieldsSchema = LocationChangedAuditActionService.fieldsSchemaV1;
private helper: AuditActionServiceHelper<
typeof LocationChangedAuditActionService.latestFieldsSchema,
typeof LocationChangedAuditActionService.storedDataSchema
>;
}
constructor() {
this.helper = new AuditActionServiceHelper({
latestVersion: this.VERSION,
latestFieldsSchema: LocationChangedAuditActionService.latestFieldsSchema,
storedDataSchema: LocationChangedAuditActionService.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({ storedData }: GetDisplayTitleParams): Promise<TranslationWithParams> {
const { fields } = this.parseStored(storedData);
// TODO: Ideally we want to translate the location label to the user's locale
// We currently don't accept requesting user's translate fn here, fix it later.
const t = await getTranslation("en", "common");
async getDisplayTitle({ storedData }: GetDisplayTitleParams): Promise<TranslationWithParams> {
const { fields } = this.parseStored(storedData);
// TODO: Ideally we want to translate the location label to the user's locale
// We currently don't accept requesting user's translate fn here, fix it later.
const t = await getTranslation("en", "common");
const fromLocation = getHumanReadableLocationValue(fields.location.old, t);
const toLocation = getHumanReadableLocationValue(fields.location.new, t);
const fromLocation = getHumanReadableLocationValue(fields.location.old, t);
const toLocation = getHumanReadableLocationValue(fields.location.new, t);
return {
key: "booking_audit_action.location_changed_from_to",
params: { fromLocation, toLocation },
};
}
return {
key: "booking_audit_action.location_changed_from_to",
params: { fromLocation, toLocation },
};
}
}
export type LocationChangedAuditData = z.infer<typeof fieldsSchemaV1>;
@@ -0,0 +1,173 @@
import { describe, expect, it, beforeEach, afterEach } from "vitest";
import type { BookingAuditTaskConsumer } from "../BookingAuditTaskConsumer";
import type { BookingAuditViewerService } from "../BookingAuditViewerService";
import { makeUserActor } from "../../makeActor";
import { getBookingAuditTaskConsumer } from "../../../di/BookingAuditTaskConsumer.container";
import { getBookingAuditViewerService } from "../../../di/BookingAuditViewerService.container";
import {
createTestUser,
createTestOrganization,
createTestMembership,
createTestEventType,
createTestBooking,
enableFeatureForOrganization,
cleanupTestData,
} from "./integration-utils";
import { BookingStatus } from "@calcom/prisma/enums";
describe("Location Changed Action Integration", () => {
let bookingAuditTaskConsumer: BookingAuditTaskConsumer;
let bookingAuditViewerService: BookingAuditViewerService;
let testData: {
owner: { id: number; uuid: string; email: string };
attendee: { id: number; email: string };
organization: { id: number };
eventType: { id: number };
booking: { uid: string; startTime: Date; endTime: Date; status: BookingStatus };
};
beforeEach(async () => {
bookingAuditTaskConsumer = getBookingAuditTaskConsumer();
bookingAuditViewerService = getBookingAuditViewerService();
// Setup basic test data using Test Data Builder pattern
const owner = await createTestUser({ name: "Location Audit User" });
const organization = await createTestOrganization();
await createTestMembership(owner.id, organization.id);
await enableFeatureForOrganization(organization.id, "booking-audit");
const eventType = await createTestEventType(owner.id);
const attendee = await createTestUser({ name: "Test Attendee" });
const booking = await createTestBooking(owner.id, eventType.id, {
attendees: [
{
email: attendee.email,
name: attendee.name || "Test Attendee",
timeZone: "UTC",
},
],
});
testData = {
owner: { id: owner.id, uuid: owner.uuid, email: owner.email },
attendee: { id: attendee.id, email: attendee.email },
organization: { id: organization.id },
eventType: { id: eventType.id },
booking: {
uid: booking.uid,
startTime: booking.startTime,
endTime: booking.endTime,
status: booking.status,
},
};
});
afterEach(async () => {
if (!testData) return;
await cleanupTestData({
bookingUid: testData.booking?.uid,
userUuids: testData.owner?.uuid ? [testData.owner.uuid] : [],
attendeeEmails: testData.attendee?.email ? [testData.attendee.email] : [],
eventTypeId: testData.eventType?.id,
organizationId: testData.organization?.id,
userIds: [testData.owner?.id, testData.attendee?.id].filter((id): id is number => id !== undefined),
featureSlug: "booking-audit",
});
});
describe("when location is changed", () => {
it("should create audit record and retrieve it with correct location data", async () => {
const actor = makeUserActor(testData.owner.uuid);
const operationId = `op-${Date.now()}`;
// Simulate LOCATION_CHANGED action
await bookingAuditTaskConsumer.onBookingAction({
bookingUid: testData.booking.uid,
actor,
action: "LOCATION_CHANGED",
source: "WEBAPP",
operationId,
data: {
location: {
old: "Zoom",
new: "Google Meet",
},
},
timestamp: Date.now(),
});
// Retrieve logs
const result = await bookingAuditViewerService.getAuditLogsForBooking({
bookingUid: testData.booking.uid,
userId: testData.owner.id,
userEmail: testData.owner.email,
userTimeZone: "UTC",
organizationId: testData.organization.id,
});
expect(result.bookingUid).toBe(testData.booking.uid);
// Find the specific log we just created
const auditLog = result.auditLogs.find((log) => log.operationId === operationId);
expect(auditLog).toBeDefined();
if (!auditLog) throw new Error("Audit log not found");
expect(auditLog.action).toBe("LOCATION_CHANGED");
// The type identifier from the service
expect(auditLog.type).toBe("RECORD_UPDATED");
expect(auditLog.displayJson).toBeNull();
// Verify the title params are correct
expect(auditLog.actionDisplayTitle).toBeDefined();
expect(auditLog.actionDisplayTitle.key).toBe("booking_audit_action.location_changed_from_to");
expect(auditLog.actionDisplayTitle.params).toEqual({
fromLocation: "Zoom",
toLocation: "Google Meet",
});
});
it("should handle location change where old value was null", async () => {
const actor = makeUserActor(testData.owner.uuid);
const operationId = `op-initial-${Date.now()}`;
// Simulate setting location for the first time (old is null)
await bookingAuditTaskConsumer.onBookingAction({
bookingUid: testData.booking.uid,
actor,
action: "LOCATION_CHANGED",
source: "WEBAPP",
operationId,
data: {
location: {
old: null,
new: "In Person",
},
},
timestamp: Date.now(),
});
const result = await bookingAuditViewerService.getAuditLogsForBooking({
bookingUid: testData.booking.uid,
userId: testData.owner.id,
userEmail: testData.owner.email,
userTimeZone: "UTC",
organizationId: testData.organization.id,
});
const auditLog = result.auditLogs.find((log) => log.operationId === operationId);
expect(auditLog).toBeDefined();
expect(auditLog!.displayJson).toBeNull();
expect(auditLog!.actionDisplayTitle.params).toEqual({
fromLocation: "No location defined",
toLocation: "In Person",
});
});
});
});
@@ -248,16 +248,20 @@ export class BookingEventHandlerService {
}
async onLocationChanged(params: OnLocationChangedParams) {
const { bookingUid, actor, organizationId, auditData, source, operationId, context } = params;
await this.bookingAuditProducerService.queueLocationChangedAudit({
bookingUid,
actor,
organizationId,
source,
operationId,
data: auditData,
context,
});
try {
const { bookingUid, actor, organizationId, auditData, source, operationId, context } = params;
await this.bookingAuditProducerService.queueLocationChangedAudit({
bookingUid,
actor,
organizationId,
source,
operationId,
data: auditData,
context,
});
} catch (error) {
this.log.error("Error while onLocationChanged", safeStringify(error));
}
}
async onAttendeeNoShowUpdated(params: OnAttendeeNoShowUpdatedParams) {