Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/web/pages/api/book/instant-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ async function handler(req: NextApiRequest & { userId?: number }) {
// TODO: We should do the run-time schema validation here and pass a typed bookingData instead and then run-time schema could be removed from createBooking. Then we can remove the any type from req.body.
const booking = await instantBookingService.createBooking({
bookingData: req.body,
bookingMeta: {
userUuid: session?.user?.uuid,
impersonatedByUserUuid: null,
},
});

return booking;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ export class CreatedAuditActionService implements IAuditActionService {

async getDisplayTitle({ storedData, dbStore }: GetDisplayTitleParams): Promise<TranslationWithParams> {
const { fields } = this.parseStored(storedData);
if (fields.status === BookingStatus.AWAITING_HOST) {
return { key: "booking_audit_action.created_awaiting_host", params: {} };
}
const hostUser = fields.hostUserUuid ? dbStore.getUserByUuid(fields.hostUserUuid) : null;
const hostName = hostUser?.name || "Unknown";
const hostName = hostUser?.name ?? "Unknown";
if (fields.seatReferenceUid) {
return { key: "booking_audit_action.created_with_seat", params: { host: hostName } };
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { InstantBookingCreateService } from "@calcom/features/bookings/lib/service/InstantBookingCreateService";
import { moduleLoader as bookingEventHandlerModuleLoader } from "@calcom/features/bookings/di/BookingEventHandlerService.module";
import { createModule, bindModuleToClassOnToken } from "@calcom/features/di/di";
import { moduleLoader as featuresRepositoryModuleLoader } from "@calcom/features/di/modules/FeaturesRepository";
import { DI_TOKENS } from "@calcom/features/di/tokens";
import { moduleLoader as prismaModuleLoader } from "@calcom/features/di/modules/Prisma";

Expand All @@ -14,6 +16,8 @@ const loadModule = bindModuleToClassOnToken({
depsMap: {
// TODO: In a followup PR, we aim to remove prisma dependency and instead inject the repositories as dependencies.
prismaClient: prismaModuleLoader,
bookingEventHandler: bookingEventHandlerModuleLoader,
featuresRepository: featuresRepositoryModuleLoader,
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,168 @@ describe("handleInstantMeeting", () => {
})
).rejects.toThrow("Only Team Event Types are supported for Instant Meeting");
});

it("should fire booking audit event with correct data when org has booking-audit feature", async () => {
const { BookingEventHandlerService } = await import("../onBookingEvents/BookingEventHandlerService");
const onBookingCreatedSpy = vi
.spyOn(BookingEventHandlerService.prototype, "onBookingCreated")
.mockResolvedValue(undefined);

const instantBookingCreateService = getInstantBookingCreateService();
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
credentials: [getGoogleCalendarCredential()],
selectedCalendars: [TestData.selectedCalendars.google],
});

const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });

await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
users: [{ id: 101 }],
team: { id: 1 },
instantMeetingExpiryTimeOffsetInSeconds: 90,
},
],
organizer,
apps: [TestData.apps["daily-video"], TestData.apps["google-calendar"]],
})
);

mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
mockCalendarToHaveNoBusySlots("googlecalendar", {
create: { uid: "MOCKED_GOOGLE_CALENDAR_EVENT_ID" },
});

const mockBookingData: CreateInstantBookingData = {
eventTypeId: 1,
timeZone: "UTC",
language: "en",
start: `${plus1DateString}T04:00:00.000Z`,
end: `${plus1DateString}T04:45:00.000Z`,
responses: {
name: "Test User",
email: "test@example.com",
attendeePhoneNumber: "+918888888888",
},
metadata: {},
instant: true,
};

const result = await instantBookingCreateService.createBooking({
bookingData: mockBookingData,
});

expect(result.message).toBe("Success");
expect(result.bookingId).toBeDefined();

expect(onBookingCreatedSpy).toHaveBeenCalledTimes(1);

const callArgs = onBookingCreatedSpy.mock.calls[0][0];
expect(callArgs.payload.booking.uid).toBe(result.bookingUid);
expect(callArgs.payload.config.isDryRun).toBe(false);
expect(callArgs.actor).toEqual(
expect.objectContaining({ identifiedBy: expect.any(String) })
);
expect(callArgs.auditData).toEqual(
expect.objectContaining({
startTime: expect.any(Number),
endTime: expect.any(Number),
status: expect.any(String),
})
);
expect(callArgs.source).toEqual(expect.any(String));

onBookingCreatedSpy.mockRestore();
});

it("should not throw when booking audit event fails", async () => {
const { BookingEventHandlerService } = await import("../onBookingEvents/BookingEventHandlerService");
const onBookingCreatedSpy = vi
.spyOn(BookingEventHandlerService.prototype, "onBookingCreated")
.mockRejectedValue(new Error("Audit event handler failure"));

const instantBookingCreateService = getInstantBookingCreateService();
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
credentials: [getGoogleCalendarCredential()],
selectedCalendars: [TestData.selectedCalendars.google],
});

const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });

await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slotInterval: 45,
length: 45,
users: [{ id: 101 }],
team: { id: 1 },
instantMeetingExpiryTimeOffsetInSeconds: 90,
},
],
organizer,
apps: [TestData.apps["daily-video"], TestData.apps["google-calendar"]],
})
);

mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
mockCalendarToHaveNoBusySlots("googlecalendar", {
create: { uid: "MOCKED_GOOGLE_CALENDAR_EVENT_ID" },
});

const mockBookingData: CreateInstantBookingData = {
eventTypeId: 1,
timeZone: "UTC",
language: "en",
start: `${plus1DateString}T04:00:00.000Z`,
end: `${plus1DateString}T04:45:00.000Z`,
responses: {
name: "Test User",
email: "test@example.com",
attendeePhoneNumber: "+918888888888",
},
metadata: {},
instant: true,
};

const result = await instantBookingCreateService.createBooking({
bookingData: mockBookingData,
});

expect(result.message).toBe("Success");
expect(result.bookingId).toBeDefined();
expect(onBookingCreatedSpy).toHaveBeenCalled();

onBookingCreatedSpy.mockRestore();
});
});
});

Expand Down
Loading
Loading