* feat: add endpoint to add attendees to existing bookings - Created POST /v2/bookings/:bookingUid/attendees endpoint - Added AddAttendeesInput_2024_08_13 for input validation - Added AddAttendeesOutput_2024_08_13 for response format - Created BookingAttendeesService_2024_08_13 for business logic - Created BookingAttendeesController_2024_08_13 for API endpoint - Added validation to check for duplicate attendee emails - Integrated with existing booking and event type repositories - Added validateAndTransformAddAttendeesInput method to InputBookingsService - Fixed pre-existing ESLint no-prototype-builtins warnings - Left placeholder for custom booking field validation logic Co-Authored-By: somay@cal.com <somaychauhan98@gmail.com> * refactor: move booking attendee operations to dedicated repository * refactor: move repository files into dedicated repositories directory * feat: validate guests field availability before adding attendees to booking * feat: migrate addAttendees API to use existing addGuests handler * refactor: remove unused validateAndTransformAddAttendeesInput method from InputBookingsService * refactor: rename attendees to guests in booking API endpoints and types * refactor: rename booking-attendees to booking-guests for consistency * WIP: add e2e tests for add booking guests endpoint * faet: improve guest booking tests * refactor: extract getHtml method in email templates * feat: add email toggle support for guest invites based on OAuth client settings * refactor: addGuests handler * feat: add SMS notifications when adding guests to existing bookings * refactor: rename add-attendees to add-guests for consistent terminology * refactor: added repository pattern in addGuests.handler * test: add attendee scheduled email spy to booking guests tests * fix: use event type team ID instead of user org ID for booking permission check * Update BookingEmailSmsHandler.ts * Remove comments * refactor: rename booking guests to booking attendees * refactor: rename guest-related methods to use attendees terminology for consistency * update api docs * refactor: restructure addGuests handler to top * refactor: update guest email format to use object structure in booking tests * docs: clarify API version header requirement for booking attendees endpoint * docs: add email notification details to booking attendees API documentation * refactor: rename booking attendees to guests for consistency * refactor: rename attendees to guests in booking API endpoints * feat: add email validation for guest invites * feat: improve error handling for guest booking failures --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
357 lines
11 KiB
TypeScript
357 lines
11 KiB
TypeScript
import { default as cloneDeep } from "lodash/cloneDeep";
|
|
import type { Logger } from "tslog";
|
|
|
|
import dayjs from "@calcom/dayjs";
|
|
import {
|
|
allowDisablingHostConfirmationEmails,
|
|
allowDisablingAttendeeConfirmationEmails,
|
|
} from "@calcom/ee/workflows/lib/allowDisablingStandardEmails";
|
|
import type { Workflow as WorkflowType } from "@calcom/ee/workflows/lib/types";
|
|
import {
|
|
sendRoundRobinRescheduledEmailsAndSMS,
|
|
sendRoundRobinScheduledEmailsAndSMS,
|
|
sendRoundRobinCancelledEmailsAndSMS,
|
|
sendRescheduledEmailsAndSMS,
|
|
sendScheduledEmailsAndSMS,
|
|
sendOrganizerRequestEmail,
|
|
sendAttendeeRequestEmailAndSMS,
|
|
sendAddGuestsEmailsAndSMS,
|
|
} from "@calcom/emails";
|
|
import type { BookingType } from "@calcom/features/bookings/lib/handleNewBooking/originalRescheduledBookingUtils";
|
|
import type { EventNameObjectType } from "@calcom/features/eventtypes/lib/eventNaming";
|
|
import { getPiiFreeCalendarEvent } from "@calcom/lib/piiFreeData";
|
|
import { safeStringify } from "@calcom/lib/safeStringify";
|
|
import { getTranslation } from "@calcom/lib/server/i18n";
|
|
import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat";
|
|
import type { DestinationCalendar, Prisma, User } from "@calcom/prisma/client";
|
|
import type { SchedulingType } from "@calcom/prisma/enums";
|
|
import type { EventTypeMetadata } from "@calcom/prisma/zod-utils";
|
|
import type { AdditionalInformation, CalendarEvent, Person } from "@calcom/types/Calendar";
|
|
|
|
export const BookingActionMap = {
|
|
confirmed: "BOOKING_CONFIRMED",
|
|
rescheduled: "BOOKING_RESCHEDULED",
|
|
requested: "BOOKING_REQUESTED",
|
|
} as const;
|
|
|
|
type EmailAndSmsPayload = {
|
|
evt: CalendarEvent;
|
|
eventType: {
|
|
metadata?: EventTypeMetadata;
|
|
schedulingType: SchedulingType | null;
|
|
};
|
|
};
|
|
|
|
type RescheduleEmailAndSmsPayload = EmailAndSmsPayload & {
|
|
rescheduleReason?: string;
|
|
additionalInformation: AdditionalInformation;
|
|
additionalNotes: string | null | undefined;
|
|
iCalUID: string;
|
|
users: (Pick<User, "id" | "name" | "timeZone" | "locale" | "email"> & {
|
|
destinationCalendar: DestinationCalendar | null;
|
|
isFixed?: boolean;
|
|
})[];
|
|
changedOrganizer?: boolean;
|
|
isRescheduledByBooker: boolean;
|
|
originalRescheduledBooking: NonNullable<BookingType>;
|
|
};
|
|
|
|
type ConfirmedEmailAndSmsPayload = EmailAndSmsPayload & {
|
|
workflows: WorkflowType[];
|
|
eventNameObject: EventNameObjectType;
|
|
additionalInformation: AdditionalInformation;
|
|
additionalNotes: string | null | undefined;
|
|
customInputs: Prisma.JsonObject | null | undefined;
|
|
};
|
|
|
|
type RequestedEmailAndSmsPayload = EmailAndSmsPayload & {
|
|
attendees?: Person[];
|
|
additionalNotes?: string | null;
|
|
};
|
|
|
|
type AddGuestsEmailAndSmsPayload = EmailAndSmsPayload & {
|
|
newGuests: string[];
|
|
};
|
|
|
|
type RescheduledSideEffectsPayload = {
|
|
action: typeof BookingActionMap.rescheduled;
|
|
data: RescheduleEmailAndSmsPayload;
|
|
};
|
|
|
|
type ConfirmedSideEffectsPayload = {
|
|
action: typeof BookingActionMap.confirmed;
|
|
data: ConfirmedEmailAndSmsPayload;
|
|
};
|
|
|
|
type RequestedSideEffectsPayload = {
|
|
action: typeof BookingActionMap.requested;
|
|
data: RequestedEmailAndSmsPayload;
|
|
};
|
|
|
|
export type EmailsAndSmsSideEffectsPayload =
|
|
| RescheduledSideEffectsPayload
|
|
| RequestedSideEffectsPayload
|
|
| ConfirmedSideEffectsPayload;
|
|
|
|
export interface IBookingEmailSmsHandler {
|
|
logger: Logger<unknown>;
|
|
}
|
|
|
|
export class BookingEmailSmsHandler {
|
|
private readonly log: Logger<unknown>;
|
|
|
|
constructor(dependencies: IBookingEmailSmsHandler) {
|
|
this.log = dependencies.logger.getSubLogger({ prefix: ["BookingEmailSmsHandler"] });
|
|
}
|
|
|
|
public async send(payload: EmailsAndSmsSideEffectsPayload) {
|
|
const { action, data } = payload;
|
|
|
|
if (action === BookingActionMap.rescheduled) {
|
|
if (data.eventType.schedulingType === "ROUND_ROBIN") return this._handleRoundRobinRescheduled(data);
|
|
return this._handleRescheduled(data);
|
|
}
|
|
|
|
if (action === BookingActionMap.confirmed) return this._handleConfirmed(data);
|
|
if (action === BookingActionMap.requested) return this._handleRequested(data);
|
|
|
|
this.log.warn("Unknown email/SMS action requested.", { action });
|
|
}
|
|
|
|
/**
|
|
* Handles notifications for a RESCHEDULED booking.
|
|
*/
|
|
private async _handleRescheduled(data: RescheduleEmailAndSmsPayload) {
|
|
const {
|
|
evt,
|
|
eventType: { metadata },
|
|
rescheduleReason,
|
|
additionalNotes,
|
|
additionalInformation,
|
|
} = data;
|
|
|
|
await sendRescheduledEmailsAndSMS(
|
|
{
|
|
...evt,
|
|
additionalInformation,
|
|
additionalNotes,
|
|
cancellationReason: `$RCH$${rescheduleReason || ""}`,
|
|
},
|
|
metadata
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Handles notifications for a RESCHEDULED RR booking.
|
|
*/
|
|
private async _handleRoundRobinRescheduled(data: RescheduleEmailAndSmsPayload) {
|
|
const {
|
|
evt,
|
|
eventType: { metadata },
|
|
originalRescheduledBooking,
|
|
rescheduleReason,
|
|
additionalNotes,
|
|
changedOrganizer,
|
|
additionalInformation,
|
|
users,
|
|
isRescheduledByBooker,
|
|
iCalUID,
|
|
} = data;
|
|
const copyEvent = cloneDeep(evt);
|
|
const copyEventAdditionalInfo = {
|
|
...copyEvent,
|
|
additionalInformation,
|
|
additionalNotes,
|
|
cancellationReason: `$RCH$${rescheduleReason || ""}`,
|
|
};
|
|
const cancelledRRHostEvt = cloneDeep(copyEventAdditionalInfo);
|
|
this.log.debug("Emails: Sending rescheduled emails for booking confirmation");
|
|
|
|
const originalBookingMemberEmails: Person[] = [];
|
|
|
|
for (const user of originalRescheduledBooking.attendees) {
|
|
const translate = await getTranslation(user.locale ?? "en", "common");
|
|
originalBookingMemberEmails.push({
|
|
name: user.name,
|
|
email: user.email,
|
|
timeZone: user.timeZone,
|
|
phoneNumber: user.phoneNumber,
|
|
language: { translate, locale: user.locale ?? "en" },
|
|
});
|
|
}
|
|
if (originalRescheduledBooking.user) {
|
|
const translate = await getTranslation(originalRescheduledBooking.user.locale ?? "en", "common");
|
|
const originalOrganizer = originalRescheduledBooking.user;
|
|
|
|
originalBookingMemberEmails.push({
|
|
...originalRescheduledBooking.user,
|
|
username: originalRescheduledBooking.user.username ?? undefined,
|
|
timeFormat: getTimeFormatStringFromUserTimeFormat(originalRescheduledBooking.user.timeFormat),
|
|
name: originalRescheduledBooking.user.name || "",
|
|
language: { translate, locale: originalRescheduledBooking.user.locale ?? "en" },
|
|
});
|
|
|
|
if (changedOrganizer) {
|
|
cancelledRRHostEvt.title = originalRescheduledBooking.title;
|
|
cancelledRRHostEvt.startTime =
|
|
dayjs(originalRescheduledBooking?.startTime).utc().format() || copyEventAdditionalInfo.startTime;
|
|
cancelledRRHostEvt.endTime =
|
|
dayjs(originalRescheduledBooking?.endTime).utc().format() || copyEventAdditionalInfo.endTime;
|
|
cancelledRRHostEvt.organizer = {
|
|
email: originalOrganizer.email,
|
|
name: originalOrganizer.name || "",
|
|
timeZone: originalOrganizer.timeZone,
|
|
language: { translate, locale: originalOrganizer.locale || "en" },
|
|
};
|
|
}
|
|
}
|
|
|
|
const newBookingMemberEmails: Person[] = [
|
|
...(copyEvent.team?.members || []),
|
|
copyEvent.organizer,
|
|
...copyEvent.attendees,
|
|
];
|
|
|
|
const matchOriginalMemberWithNewMember = (originalMember: Person, newMember: Person) =>
|
|
originalMember.email === newMember.email;
|
|
|
|
const newBookedMembers = newBookingMemberEmails.filter(
|
|
(member) => !originalBookingMemberEmails.some((om) => matchOriginalMemberWithNewMember(om, member))
|
|
);
|
|
const cancelledMembers = originalBookingMemberEmails.filter(
|
|
(member) => !newBookingMemberEmails.some((nm) => matchOriginalMemberWithNewMember(member, nm))
|
|
);
|
|
const rescheduledMembers = newBookingMemberEmails.filter((member) =>
|
|
originalBookingMemberEmails.some((om) => matchOriginalMemberWithNewMember(om, member))
|
|
);
|
|
|
|
const reassignedTo = users.find(
|
|
(user) => !user.isFixed && newBookedMembers.some((member) => member.email === user.email)
|
|
);
|
|
|
|
try {
|
|
await Promise.all([
|
|
sendRoundRobinRescheduledEmailsAndSMS(
|
|
{ ...copyEventAdditionalInfo, iCalUID },
|
|
rescheduledMembers,
|
|
metadata
|
|
),
|
|
sendRoundRobinScheduledEmailsAndSMS({
|
|
calEvent: copyEventAdditionalInfo,
|
|
members: newBookedMembers,
|
|
eventTypeMetadata: metadata,
|
|
}),
|
|
sendRoundRobinCancelledEmailsAndSMS(
|
|
cancelledRRHostEvt,
|
|
cancelledMembers,
|
|
metadata,
|
|
reassignedTo
|
|
? {
|
|
name: reassignedTo.name,
|
|
email: reassignedTo.email,
|
|
...(isRescheduledByBooker && { reason: "Booker Rescheduled" }),
|
|
}
|
|
: undefined
|
|
),
|
|
]);
|
|
} catch (err) {
|
|
this.log.error("Failed to send rescheduled round robin event related emails", err);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handles notifications for a newly CONFIRMED booking.
|
|
*/
|
|
private async _handleConfirmed(data: ConfirmedEmailAndSmsPayload) {
|
|
const {
|
|
evt,
|
|
eventType: { metadata },
|
|
workflows,
|
|
eventNameObject,
|
|
additionalInformation,
|
|
additionalNotes,
|
|
customInputs,
|
|
} = data;
|
|
|
|
let isHostConfirmationEmailsDisabled = metadata?.disableStandardEmails?.confirmation?.host || false;
|
|
if (isHostConfirmationEmailsDisabled) {
|
|
isHostConfirmationEmailsDisabled = allowDisablingHostConfirmationEmails(workflows);
|
|
}
|
|
|
|
let isAttendeeConfirmationEmailDisabled =
|
|
metadata?.disableStandardEmails?.confirmation?.attendee || false;
|
|
if (isAttendeeConfirmationEmailDisabled) {
|
|
isAttendeeConfirmationEmailDisabled = allowDisablingAttendeeConfirmationEmails(workflows);
|
|
}
|
|
|
|
try {
|
|
await sendScheduledEmailsAndSMS(
|
|
{ ...evt, additionalInformation, additionalNotes, customInputs },
|
|
eventNameObject,
|
|
isHostConfirmationEmailsDisabled,
|
|
isAttendeeConfirmationEmailDisabled,
|
|
metadata
|
|
);
|
|
} catch (err) {
|
|
this.log.error("Failed to send scheduled event related emails", err);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handles notifications when a booking REQUEST is made (requires confirmation).
|
|
*/
|
|
private async _handleRequested(data: RequestedEmailAndSmsPayload) {
|
|
const {
|
|
evt,
|
|
eventType: { metadata },
|
|
attendees,
|
|
additionalNotes,
|
|
} = data;
|
|
if (!attendees?.length) {
|
|
this.log.error("Requested action called without attendee details.");
|
|
return;
|
|
}
|
|
this.log.debug(
|
|
"Action: BOOKING_REQUESTED. Sending request emails.",
|
|
safeStringify({ calEvent: getPiiFreeCalendarEvent(evt) })
|
|
);
|
|
|
|
const eventWithNotes = { ...evt, additionalNotes };
|
|
|
|
try {
|
|
await Promise.all([
|
|
sendOrganizerRequestEmail(eventWithNotes, metadata),
|
|
sendAttendeeRequestEmailAndSMS(eventWithNotes, attendees[0], metadata),
|
|
]);
|
|
} catch (err) {
|
|
this.log.error("Failed to send requested event related emails", err);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handles notifications when guests are added to an existing booking.
|
|
*/
|
|
public async handleAddGuests(data: AddGuestsEmailAndSmsPayload) {
|
|
const {
|
|
evt,
|
|
eventType: { metadata },
|
|
newGuests,
|
|
} = data;
|
|
|
|
this.log.debug(
|
|
"Action: ADD_GUESTS. Sending add guests emails and SMS.",
|
|
safeStringify({ calEvent: getPiiFreeCalendarEvent(evt) })
|
|
);
|
|
|
|
try {
|
|
await sendAddGuestsEmailsAndSMS({
|
|
calEvent: evt,
|
|
newGuests,
|
|
eventTypeMetadata: metadata,
|
|
});
|
|
} catch (err) {
|
|
this.log.error("Failed to send add guests related emails and SMS", err);
|
|
}
|
|
}
|
|
}
|