* feat: add booking attendees endpoint to API v2 Co-Authored-By: [email protected] <[email protected]> * feat: add rate limiting to booking attendees endpoint Co-Authored-By: [email protected] <[email protected]> * refactor: simplify attendees output to id, bookingId, name, email, timeZone Co-Authored-By: [email protected] <[email protected]> * test: add E2E tests for booking attendees endpoint Co-Authored-By: [email protected] <[email protected]> * chore: update bookings repository * fixup: add pbac guards and update service logic * chore: update openapi spec * test: add rate limiting E2E test for booking attendees endpoint Co-Authored-By: [email protected] <[email protected]> * fix: tests * fix: return 404 instead of 403 for non-existent booking in BookingPbacGuard The BookingPbacGuard was returning 403 (Forbidden) for non-existent bookings because doesUserIdHaveAccessToBooking returns false when a booking doesn't exist, which the guard treated as an access denial. Added an explicit booking existence check in the guard before the access check, so non-existent bookings now correctly return 404 (Not Found) as documented in the PR description. Updated the E2E test to expect 404 for non-existent booking UIDs. Issue identified by cubic. Co-Authored-By: unknown <> * fixup * fix: return 404 instead of 403 for non-existent booking in attendees endpoint BookingPbacGuard now checks booking existence before the access check, returning 404 (Not Found) instead of 403 (Forbidden) for non-existent booking UIDs. Updated the E2E test assertion and description to match. Issue identified by cubic (confidence 9/10). Co-Authored-By: unknown <> * chore: implement PR feedback * chore: update tests * fixup * chore: update endpoint decsription * feat: endpoint to retrieve specific attendee * chore: update e2e tests * chore: implement cubic feedback * fix: update test to expect 403 for non-existent booking UID (BookingPbacGuard behavior) Co-Authored-By: [email protected] <[email protected]> * fix: merge conflicts * feat: endpoint to get attendees * chore: update findByUidIncludeEventTypeAttendeesAndUser method * chore: implement PR feedback * fix: e2e tests * chore: update e2e tests * fixup fixup * fix: remove phoneNumber assertion since it's optional and not provided in test * chore: implement PR feedback * fix: keep the same output shape for get attendees and get attendee endpoint * chore: update openapi spec --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: bot_apk <[email protected]>
195 lines
5.6 KiB
TypeScript
195 lines
5.6 KiB
TypeScript
import { eventTypeMetaDataSchemaWithTypedApps } from "@calcom/app-store/zod-utils";
|
|
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 { BookingEmailSmsHandler } from "@calcom/features/bookings/lib/BookingEmailSmsHandler";
|
|
import { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository";
|
|
import { getFeaturesRepository } from "@calcom/features/di/containers/FeaturesRepository";
|
|
import { ErrorCode } from "@calcom/lib/errorCodes";
|
|
import { ErrorWithCode } from "@calcom/lib/errors";
|
|
import logger from "@calcom/lib/logger";
|
|
import { prisma } from "@calcom/prisma";
|
|
import type {
|
|
Booking,
|
|
TUser,
|
|
} from "@calcom/trpc/server/routers/viewer/bookings/addGuests.handler";
|
|
import {
|
|
buildCalendarEvent,
|
|
getBooking,
|
|
getOrganizerData,
|
|
prepareAttendeesList,
|
|
sanitizeAndFilterGuests,
|
|
updateBookingAttendees,
|
|
updateCalendarEvent,
|
|
validateGuestsFieldEnabled,
|
|
validateUserPermissions,
|
|
} from "@calcom/trpc/server/routers/viewer/bookings/addGuests.handler";
|
|
import type { TAddGuestsInputSchema } from "@calcom/trpc/server/routers/viewer/bookings/addGuests.schema";
|
|
import type { CalendarEvent } from "@calcom/types/Calendar";
|
|
|
|
type Attendee = TAddGuestsInputSchema["guests"][number];
|
|
|
|
type AddAttendeeInput = {
|
|
bookingId: number;
|
|
attendee: Attendee;
|
|
user: TUser;
|
|
emailsEnabled?: boolean;
|
|
actionSource: ActionSource;
|
|
};
|
|
|
|
export type CreatedAttendee = {
|
|
id: number;
|
|
bookingId: number;
|
|
email: string;
|
|
name: string;
|
|
timeZone: string;
|
|
locale: string | null;
|
|
phoneNumber: string | null;
|
|
};
|
|
|
|
export class BookingAttendeesService {
|
|
async getBookingAttendees(bookingUid: string) {
|
|
const bookingRepository = new BookingRepository(prisma);
|
|
const booking =
|
|
await bookingRepository.findByUidIncludeEventTypeAttendeesAndUser({
|
|
bookingUid,
|
|
});
|
|
|
|
if (!booking) {
|
|
throw new Error(`Booking with uid ${bookingUid} not found`);
|
|
}
|
|
|
|
return booking.attendees;
|
|
}
|
|
|
|
async getBookingAttendee(bookingUid: string, attendeeId: number) {
|
|
const bookingRepository = new BookingRepository(prisma);
|
|
const booking =
|
|
await bookingRepository.findByUidIncludeEventTypeAttendeesAndUser({
|
|
bookingUid,
|
|
});
|
|
|
|
if (!booking) {
|
|
throw new Error(`Booking with uid ${bookingUid} not found`);
|
|
}
|
|
|
|
const attendee = booking.attendees.find((a) => a.id === attendeeId);
|
|
if (!attendee) {
|
|
throw new ErrorWithCode(
|
|
ErrorCode.NotFound,
|
|
`Attendee with id ${attendeeId} not found in booking ${bookingUid}`
|
|
);
|
|
}
|
|
|
|
return attendee;
|
|
}
|
|
|
|
async addAttendee({
|
|
bookingId,
|
|
attendee,
|
|
user,
|
|
emailsEnabled = true,
|
|
actionSource,
|
|
}: AddAttendeeInput): Promise<CreatedAttendee> {
|
|
const booking = await getBooking(bookingId);
|
|
|
|
await validateUserPermissions(booking, user);
|
|
|
|
validateGuestsFieldEnabled(booking);
|
|
|
|
const organizer = await getOrganizerData(booking.userId);
|
|
|
|
const validatedAttendees = await sanitizeAndFilterGuests(
|
|
[attendee],
|
|
booking
|
|
);
|
|
|
|
const newAttendeeDetails = validatedAttendees.map((a) => ({
|
|
name: a.name || "",
|
|
email: a.email,
|
|
timeZone: a.timeZone || organizer.timeZone,
|
|
locale: a.language || organizer.locale,
|
|
phoneNumber: a.phoneNumber || null,
|
|
}));
|
|
|
|
const attendeeEmail = attendee.email;
|
|
|
|
const updatedBooking = await updateBookingAttendees(
|
|
bookingId,
|
|
newAttendeeDetails,
|
|
[attendeeEmail],
|
|
booking
|
|
);
|
|
|
|
const allAttendees = await prepareAttendeesList(updatedBooking.attendees);
|
|
|
|
const evt = await buildCalendarEvent(booking, organizer, allAttendees);
|
|
|
|
await updateCalendarEvent(booking, evt);
|
|
|
|
if (emailsEnabled) {
|
|
await this.sendAttendeeNotification(evt, booking, attendeeEmail);
|
|
}
|
|
|
|
const bookingEventHandlerService = getBookingEventHandlerService();
|
|
const featuresRepository = getFeaturesRepository();
|
|
const organizationId = user.organizationId ?? null;
|
|
const isBookingAuditEnabled = organizationId
|
|
? await featuresRepository.checkIfTeamHasFeature(
|
|
organizationId,
|
|
"booking-audit"
|
|
)
|
|
: false;
|
|
|
|
await bookingEventHandlerService.onAttendeeAdded({
|
|
bookingUid: booking.uid,
|
|
actor: makeUserActor(user.uuid),
|
|
organizationId,
|
|
source: actionSource,
|
|
auditData: {
|
|
added: [attendeeEmail],
|
|
},
|
|
isBookingAuditEnabled,
|
|
});
|
|
|
|
const createdAttendee = updatedBooking.attendees.find(
|
|
(a) => a.email.toLowerCase() === attendeeEmail.toLowerCase()
|
|
);
|
|
|
|
if (!createdAttendee) {
|
|
throw new Error("Attendee was created but could not be found");
|
|
}
|
|
|
|
return {
|
|
id: createdAttendee.id,
|
|
bookingId,
|
|
email: createdAttendee.email,
|
|
name: createdAttendee.name,
|
|
timeZone: createdAttendee.timeZone,
|
|
locale: createdAttendee.locale,
|
|
phoneNumber: createdAttendee.phoneNumber,
|
|
};
|
|
}
|
|
|
|
private async sendAttendeeNotification(
|
|
evt: CalendarEvent,
|
|
booking: Booking,
|
|
attendeeEmail: string
|
|
): Promise<void> {
|
|
const emailsAndSmsHandler = new BookingEmailSmsHandler({
|
|
logger: logger,
|
|
});
|
|
|
|
await emailsAndSmsHandler.handleAddAttendee({
|
|
evt,
|
|
eventType: {
|
|
metadata: eventTypeMetaDataSchemaWithTypedApps.parse(
|
|
booking?.eventType?.metadata
|
|
),
|
|
schedulingType: booking.eventType?.schedulingType || null,
|
|
},
|
|
newGuests: [attendeeEmail],
|
|
});
|
|
}
|
|
}
|