chore: Integrate add guests booking audit (#26568)
* chore: Integrate add guests booking audit Co-Authored-By: [email protected] <[email protected]> * chore: Add actionSource parameter to support API_V2 audit logging Co-Authored-By: [email protected] <[email protected]> * refactor: update attendee audit action service to use new email schema - Modified the `AttendeeAddedAuditActionService` to replace the old attendees schema with a new `emailSchema` for better validation. - Adjusted the `addGuestsHandler` to pass the action source explicitly and updated the audit logging to reflect the new structure. - Cleaned up the code for better readability and consistency. * test: add happy path integration test for attendee added action Co-Authored-By: [email protected] <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
b517adfb66
commit
c9bb03b602
5 files changed
+206
-60
No files matched your search
@@ -1,8 +1,13 @@
|
||||
import { z } from "zod";
|
||||
import { emailSchema } from "@calcom/lib/emailSchema";
|
||||
|
||||
import { StringArrayChangeSchema } from "../common/changeSchemas";
|
||||
import { AuditActionServiceHelper } from "./AuditActionServiceHelper";
|
||||
import type { IAuditActionService, TranslationWithParams, GetDisplayTitleParams, GetDisplayJsonParams } from "./IAuditActionService";
|
||||
import type {
|
||||
IAuditActionService,
|
||||
TranslationWithParams,
|
||||
GetDisplayTitleParams,
|
||||
GetDisplayJsonParams,
|
||||
} from "./IAuditActionService";
|
||||
|
||||
/**
|
||||
* Attendee Added Audit Action Service
|
||||
@@ -11,73 +16,67 @@ import type { IAuditActionService, TranslationWithParams, GetDisplayTitleParams,
|
||||
|
||||
// Module-level because it is passed to IAuditActionService type outside the class scope
|
||||
const fieldsSchemaV1 = z.object({
|
||||
attendees: StringArrayChangeSchema,
|
||||
added: z.array(emailSchema),
|
||||
});
|
||||
|
||||
export class AttendeeAddedAuditActionService implements IAuditActionService {
|
||||
readonly VERSION = 1;
|
||||
public static readonly TYPE = "ATTENDEE_ADDED" as const;
|
||||
private static dataSchemaV1 = z.object({
|
||||
version: z.literal(1),
|
||||
fields: fieldsSchemaV1,
|
||||
readonly VERSION = 1;
|
||||
public static readonly TYPE = "ATTENDEE_ADDED" 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 = AttendeeAddedAuditActionService.dataSchemaV1;
|
||||
// Union of all versions
|
||||
public static readonly storedFieldsSchema = AttendeeAddedAuditActionService.fieldsSchemaV1;
|
||||
private helper: AuditActionServiceHelper<
|
||||
typeof AttendeeAddedAuditActionService.latestFieldsSchema,
|
||||
typeof AttendeeAddedAuditActionService.storedDataSchema
|
||||
>;
|
||||
|
||||
constructor() {
|
||||
this.helper = new AuditActionServiceHelper({
|
||||
latestVersion: this.VERSION,
|
||||
latestFieldsSchema: AttendeeAddedAuditActionService.latestFieldsSchema,
|
||||
storedDataSchema: AttendeeAddedAuditActionService.storedDataSchema,
|
||||
});
|
||||
private static fieldsSchemaV1 = fieldsSchemaV1;
|
||||
public static readonly latestFieldsSchema = fieldsSchemaV1;
|
||||
// Union of all versions
|
||||
public static readonly storedDataSchema = AttendeeAddedAuditActionService.dataSchemaV1;
|
||||
// Union of all versions
|
||||
public static readonly storedFieldsSchema = AttendeeAddedAuditActionService.fieldsSchemaV1;
|
||||
private helper: AuditActionServiceHelper<
|
||||
typeof AttendeeAddedAuditActionService.latestFieldsSchema,
|
||||
typeof AttendeeAddedAuditActionService.storedDataSchema
|
||||
>;
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.helper = new AuditActionServiceHelper({
|
||||
latestVersion: this.VERSION,
|
||||
latestFieldsSchema: AttendeeAddedAuditActionService.latestFieldsSchema,
|
||||
storedDataSchema: AttendeeAddedAuditActionService.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.attendee_added" };
|
||||
}
|
||||
|
||||
async getDisplayTitle(_: GetDisplayTitleParams): Promise<TranslationWithParams> {
|
||||
return { key: "booking_audit_action.attendee_added" };
|
||||
}
|
||||
|
||||
getDisplayJson({
|
||||
storedData,
|
||||
}: GetDisplayJsonParams): AttendeeAddedAuditDisplayData {
|
||||
const { fields } = this.parseStored({ version: storedData.version, fields: storedData.fields });
|
||||
const previousAttendeesSet = new Set(fields.attendees.old ?? []);
|
||||
const addedAttendees = fields.attendees.new.filter(
|
||||
(email) => !previousAttendeesSet.has(email)
|
||||
);
|
||||
return {
|
||||
addedAttendees,
|
||||
};
|
||||
}
|
||||
getDisplayJson({ storedData }: GetDisplayJsonParams): AttendeeAddedAuditDisplayData {
|
||||
const { fields } = this.parseStored({ version: storedData.version, fields: storedData.fields });
|
||||
return {
|
||||
addedAttendees: fields.added,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export type AttendeeAddedAuditData = z.infer<typeof fieldsSchemaV1>;
|
||||
|
||||
export type AttendeeAddedAuditDisplayData = {
|
||||
addedAttendees: string[];
|
||||
addedAttendees: string[];
|
||||
};
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
import { describe, expect, it, beforeEach, afterEach } from "vitest";
|
||||
|
||||
import type { BookingStatus } from "@calcom/prisma/enums";
|
||||
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";
|
||||
|
||||
describe("Attendee Added 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();
|
||||
|
||||
const owner = await createTestUser({ name: "Test 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 guests are added to a booking", () => {
|
||||
it("should create audit record with correct added attendees data", async () => {
|
||||
const actor = makeUserActor(testData.owner.uuid);
|
||||
const newGuestEmails = ["[email protected]", "[email protected]"];
|
||||
|
||||
await bookingAuditTaskConsumer.onBookingAction({
|
||||
bookingUid: testData.booking.uid,
|
||||
actor,
|
||||
action: "ATTENDEE_ADDED",
|
||||
source: "WEBAPP",
|
||||
operationId: `op-${Date.now()}`,
|
||||
data: {
|
||||
added: newGuestEmails,
|
||||
},
|
||||
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,
|
||||
});
|
||||
|
||||
expect(result.bookingUid).toBe(testData.booking.uid);
|
||||
expect(result.auditLogs).toHaveLength(1);
|
||||
|
||||
const auditLog = result.auditLogs[0];
|
||||
expect(auditLog.bookingUid).toBe(testData.booking.uid);
|
||||
expect(auditLog.action).toBe("ATTENDEE_ADDED");
|
||||
expect(auditLog.type).toBe("RECORD_UPDATED");
|
||||
|
||||
const displayData = auditLog.displayJson as { addedAttendees: string[] };
|
||||
expect(displayData).toBeDefined();
|
||||
expect(displayData.addedAttendees).toEqual(newGuestEmails);
|
||||
|
||||
expect(auditLog.actor.displayName).toBe("Test Audit User");
|
||||
expect(auditLog.actor.displayEmail).toBe(testData.owner.email);
|
||||
expect(auditLog.actor.userUuid).toBe(testData.owner.uuid);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user