feat: Disable booking emails to guests (#25217)

* feat: disable SMS org setting

* chore: undo

* fix: cal evnet

* feat: add more settings

* tests: add email manager unit tests

* fix: update

* fix: type error

* fix: test

* refactor: UI

* refactor: UI

* perf: fetch only once

* perf: fetch only once

* chore: use org

* fix: test

* test: add unit tests

* refactor: email manager

* fix: add confirmation dialog for individual checkbox

* fix: sms

* chore: common.json
This commit is contained in:
Udit Takkar authored and GitHub committed 2025-12-03 17:21:31 +00:00
1 parent 1c3ced5b70
commit 35d6c41fff
32 files changed
+936 -27

No files matched your search

+152
View File
@@ -0,0 +1,152 @@
import { describe, expect, it, vi, beforeEach } from "vitest";
import type { EventTypeMetadata } from "@calcom/prisma/zod-utils";
import { shouldSkipAttendeeEmailWithSettings, fetchOrganizationEmailSettings } from "./email-manager";
const mockGetEmailSettings = vi.fn();
vi.mock("@calcom/features/organizations/repositories/OrganizationSettingsRepository", () => ({
OrganizationSettingsRepository: vi.fn().mockImplementation(() => ({
getEmailSettings: mockGetEmailSettings,
})),
}));
vi.mock("@calcom/prisma", () => ({
prisma: {},
}));
describe("shouldSkipAttendeeEmailWithSettings", () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe.each([
["confirmation", "disableAttendeeConfirmationEmail"],
["cancellation", "disableAttendeeCancellationEmail"],
["rescheduled", "disableAttendeeRescheduledEmail"],
["request", "disableAttendeeRequestEmail"],
["reassigned", "disableAttendeeReassignedEmail"],
["awaiting_payment", "disableAttendeeAwaitingPaymentEmail"],
["reschedule_request", "disableAttendeeRescheduleRequestEmail"],
["location_change", "disableAttendeeLocationChangeEmail"],
["new_event", "disableAttendeeNewEventEmail"],
] as const)("Email type: %s", (emailType, settingKey) => {
it(`should skip email when organization has ${settingKey} enabled`, async () => {
const orgSettings = {
disableAttendeeConfirmationEmail: settingKey === "disableAttendeeConfirmationEmail",
disableAttendeeCancellationEmail: settingKey === "disableAttendeeCancellationEmail",
disableAttendeeRescheduledEmail: settingKey === "disableAttendeeRescheduledEmail",
disableAttendeeRequestEmail: settingKey === "disableAttendeeRequestEmail",
disableAttendeeReassignedEmail: settingKey === "disableAttendeeReassignedEmail",
disableAttendeeAwaitingPaymentEmail: settingKey === "disableAttendeeAwaitingPaymentEmail",
disableAttendeeRescheduleRequestEmail: settingKey === "disableAttendeeRescheduleRequestEmail",
disableAttendeeLocationChangeEmail: settingKey === "disableAttendeeLocationChangeEmail",
disableAttendeeNewEventEmail: settingKey === "disableAttendeeNewEventEmail",
};
const result = shouldSkipAttendeeEmailWithSettings(undefined, orgSettings, emailType);
expect(result).toBe(true);
});
it(`should send email when organization has ${settingKey} disabled`, async () => {
const orgSettings = {
disableAttendeeConfirmationEmail: false,
disableAttendeeCancellationEmail: false,
disableAttendeeRescheduledEmail: false,
disableAttendeeRequestEmail: false,
disableAttendeeReassignedEmail: false,
disableAttendeeAwaitingPaymentEmail: false,
disableAttendeeRescheduleRequestEmail: false,
disableAttendeeLocationChangeEmail: false,
disableAttendeeNewEventEmail: false,
};
const result = shouldSkipAttendeeEmailWithSettings(undefined, orgSettings, emailType);
expect(result).toBe(false);
});
});
describe("Metadata fallback", () => {
it("should skip email when metadata has disableStandardEmails.all.attendee enabled", () => {
const metadata: EventTypeMetadata = {
disableStandardEmails: {
all: {
attendee: true,
},
},
};
const result = shouldSkipAttendeeEmailWithSettings(metadata, null, "confirmation");
expect(result).toBe(true);
});
});
describe("Priority: organization settings override metadata", () => {
it("should skip email when org setting is enabled even if metadata allows", () => {
const orgSettings = {
disableAttendeeConfirmationEmail: true,
disableAttendeeCancellationEmail: false,
disableAttendeeRescheduledEmail: false,
disableAttendeeRequestEmail: false,
disableAttendeeReassignedEmail: false,
disableAttendeeAwaitingPaymentEmail: false,
disableAttendeeRescheduleRequestEmail: false,
disableAttendeeLocationChangeEmail: false,
disableAttendeeNewEventEmail: false,
};
const metadata: EventTypeMetadata = {
disableStandardEmails: {
confirmation: {
attendee: false,
},
},
};
const result = shouldSkipAttendeeEmailWithSettings(metadata, orgSettings, "confirmation");
expect(result).toBe(true);
});
});
describe("Edge cases", () => {
it("should send email when organizationSettings is null", () => {
const result = shouldSkipAttendeeEmailWithSettings(undefined, null, "confirmation");
expect(result).toBe(false);
});
it("should send email when emailType is undefined", () => {
const orgSettings = {
disableAttendeeConfirmationEmail: true,
disableAttendeeCancellationEmail: false,
disableAttendeeRescheduledEmail: false,
disableAttendeeRequestEmail: false,
disableAttendeeReassignedEmail: false,
disableAttendeeAwaitingPaymentEmail: false,
disableAttendeeRescheduleRequestEmail: false,
disableAttendeeLocationChangeEmail: false,
disableAttendeeNewEventEmail: false,
};
const result = shouldSkipAttendeeEmailWithSettings(undefined, orgSettings, undefined);
expect(result).toBe(false);
});
it("should send email when metadata is undefined and org settings are disabled", () => {
const orgSettings = {
disableAttendeeConfirmationEmail: false,
disableAttendeeCancellationEmail: false,
disableAttendeeRescheduledEmail: false,
disableAttendeeRequestEmail: false,
disableAttendeeReassignedEmail: false,
disableAttendeeAwaitingPaymentEmail: false,
disableAttendeeRescheduleRequestEmail: false,
disableAttendeeLocationChangeEmail: false,
disableAttendeeNewEventEmail: false,
};
const result = shouldSkipAttendeeEmailWithSettings(undefined, orgSettings, "confirmation");
expect(result).toBe(false);
});
});
});
+110 -25
View File
@@ -5,10 +5,12 @@ import dayjs from "@calcom/dayjs";
import type BaseEmail from "@calcom/emails/templates/_base-email";
import type { EventNameObjectType } from "@calcom/features/eventtypes/lib/eventNaming";
import { getEventName } from "@calcom/features/eventtypes/lib/eventNaming";
import { OrganizationSettingsRepository } from "@calcom/features/organizations/repositories/OrganizationSettingsRepository";
import { formatCalEvent } from "@calcom/lib/formatCalendarEvent";
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
import { withReporting } from "@calcom/lib/sentryWrapper";
import { prisma } from "@calcom/prisma";
import type { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
import type { CalendarEvent, Person } from "@calcom/types/Calendar";
@@ -21,6 +23,7 @@ import EventRequestSMS from "../sms/attendee/event-request-sms";
import EventRequestToRescheduleSMS from "../sms/attendee/event-request-to-reschedule-sms";
import EventSuccessfullyReScheduledSMS from "../sms/attendee/event-rescheduled-sms";
import EventSuccessfullyScheduledSMS from "../sms/attendee/event-scheduled-sms";
import { EmailType } from "./email-types";
import AttendeeAddGuestsEmail from "./templates/attendee-add-guests-email";
import AttendeeAwaitingPaymentEmail from "./templates/attendee-awaiting-payment-email";
import AttendeeCancelledEmail from "./templates/attendee-cancelled-email";
@@ -56,7 +59,48 @@ const sendEmail = (prepare: () => BaseEmail) => {
});
};
const eventTypeDisableAttendeeEmail = (metadata?: EventTypeMetadata) => {
export const fetchOrganizationEmailSettings = async (organizationId?: number | null | undefined) => {
if (!organizationId) return null;
const repo = new OrganizationSettingsRepository(prisma);
return await repo.getEmailSettings(organizationId);
};
export const shouldSkipAttendeeEmailWithSettings = (
metadata: EventTypeMetadata | undefined,
organizationSettings: Awaited<ReturnType<typeof fetchOrganizationEmailSettings>>,
emailType?: EmailType
): boolean => {
if (organizationSettings && emailType) {
switch (emailType) {
case EmailType.CONFIRMATION:
if (organizationSettings.disableAttendeeConfirmationEmail) return true;
break;
case EmailType.CANCELLATION:
if (organizationSettings.disableAttendeeCancellationEmail) return true;
break;
case EmailType.RESCHEDULED:
if (organizationSettings.disableAttendeeRescheduledEmail) return true;
break;
case EmailType.REQUEST:
if (organizationSettings.disableAttendeeRequestEmail) return true;
break;
case EmailType.REASSIGNED:
if (organizationSettings.disableAttendeeReassignedEmail) return true;
break;
case EmailType.AWAITING_PAYMENT:
if (organizationSettings.disableAttendeeAwaitingPaymentEmail) return true;
break;
case EmailType.RESCHEDULE_REQUEST:
if (organizationSettings.disableAttendeeRescheduleRequestEmail) return true;
break;
case EmailType.LOCATION_CHANGE:
if (organizationSettings.disableAttendeeLocationChangeEmail) return true;
break;
case EmailType.NEW_EVENT:
if (organizationSettings.disableAttendeeNewEventEmail) return true;
break;
}
}
return !!metadata?.disableStandardEmails?.all?.attendee;
};
@@ -73,6 +117,7 @@ const _sendScheduledEmailsAndSMS = async (
) => {
const formattedCalEvent = formatCalEvent(calEvent);
const emailsToSend: Promise<unknown>[] = [];
const organizationSettings = await fetchOrganizationEmailSettings(calEvent.organizationId);
if (!hostEmailDisabled && !eventTypeDisableHostEmail(eventTypeMetadata)) {
emailsToSend.push(sendEmail(() => new OrganizerScheduledEmail({ calEvent: formattedCalEvent })));
@@ -86,7 +131,10 @@ const _sendScheduledEmailsAndSMS = async (
}
}
if (!attendeeEmailDisabled && !eventTypeDisableAttendeeEmail(eventTypeMetadata)) {
if (
!attendeeEmailDisabled &&
!shouldSkipAttendeeEmailWithSettings(eventTypeMetadata, organizationSettings, EmailType.CONFIRMATION)
) {
emailsToSend.push(
...formattedCalEvent.attendees.map((attendee) => {
return sendEmail(
@@ -154,13 +202,16 @@ export const sendRoundRobinRescheduledEmailsAndSMS = async (
const calendarEvent = formatCalEvent(calEvent);
const emailsAndSMSToSend: Promise<unknown>[] = [];
const successfullyReScheduledSMS = new EventSuccessfullyReScheduledSMS(calEvent);
const organizationSettings = await fetchOrganizationEmailSettings(calEvent.organizationId);
for (const person of teamMembersAndAttendees) {
const isAttendee = calendarEvent.attendees.some((attendee) => attendee.email === person.email);
const isTeamMember = !!calendarEvent.team?.members.some((member) => member.email === person.email);
if (isAttendee && !isTeamMember) {
if (!eventTypeDisableAttendeeEmail(eventTypeMetadata)) {
if (
!shouldSkipAttendeeEmailWithSettings(eventTypeMetadata, organizationSettings, EmailType.RESCHEDULED)
) {
emailsAndSMSToSend.push(sendEmail(() => new AttendeeRescheduledEmail(calendarEvent, person)));
if (person.phoneNumber) {
emailsAndSMSToSend.push(successfullyReScheduledSMS.sendSMSToAttendee(person));
@@ -186,7 +237,9 @@ export const sendReassignedUpdatedEmailsAndSMS = async ({
calEvent: CalendarEvent;
eventTypeMetadata?: EventTypeMetadata;
}) => {
if (eventTypeDisableAttendeeEmail(eventTypeMetadata)) return;
const organizationSettings = await fetchOrganizationEmailSettings(calEvent.organizationId);
if (shouldSkipAttendeeEmailWithSettings(eventTypeMetadata, organizationSettings, EmailType.REASSIGNED))
return;
const emailsToSend = calEvent.attendees.map((attendee) =>
sendEmail(() => new AttendeeUpdatedEmail(calEvent, attendee))
@@ -252,6 +305,7 @@ const _sendRescheduledEmailsAndSMS = async (
) => {
const calendarEvent = formatCalEvent(calEvent);
const emailsToSend: Promise<unknown>[] = [];
const organizationSettings = await fetchOrganizationEmailSettings(calEvent.organizationId);
if (!eventTypeDisableHostEmail(eventTypeMetadata)) {
emailsToSend.push(sendEmail(() => new OrganizerRescheduledEmail({ calEvent: calendarEvent })));
@@ -265,7 +319,7 @@ const _sendRescheduledEmailsAndSMS = async (
}
}
if (!eventTypeDisableAttendeeEmail(eventTypeMetadata)) {
if (!shouldSkipAttendeeEmailWithSettings(eventTypeMetadata, organizationSettings, EmailType.RESCHEDULED)) {
emailsToSend.push(
...calendarEvent.attendees.map((attendee) => {
return sendEmail(() => new AttendeeRescheduledEmail(calendarEvent, attendee));
@@ -291,10 +345,11 @@ export const sendRescheduledSeatEmailAndSMS = async (
const clonedCalEvent = cloneDeep(calendarEvent);
const emailsToSend: Promise<unknown>[] = [];
const organizationSettings = await fetchOrganizationEmailSettings(calEvent.organizationId);
if (!eventTypeDisableHostEmail(eventTypeMetadata))
emailsToSend.push(sendEmail(() => new OrganizerRescheduledEmail({ calEvent: calendarEvent })));
if (!eventTypeDisableAttendeeEmail(eventTypeMetadata))
if (!shouldSkipAttendeeEmailWithSettings(eventTypeMetadata, organizationSettings, EmailType.RESCHEDULED))
emailsToSend.push(sendEmail(() => new AttendeeRescheduledEmail(clonedCalEvent, attendee)));
const successfullyReScheduledSMS = new EventSuccessfullyReScheduledSMS(calEvent);
@@ -315,6 +370,7 @@ export const sendScheduledSeatsEmailsAndSMS = async (
const calendarEvent = formatCalEvent(calEvent);
const emailsToSend: Promise<unknown>[] = [];
const organizationSettings = await fetchOrganizationEmailSettings(calEvent.organizationId);
if (!hostEmailDisabled && !eventTypeDisableHostEmail(eventTypeMetadata)) {
emailsToSend.push(sendEmail(() => new OrganizerScheduledEmail({ calEvent: calendarEvent, newSeat })));
@@ -328,7 +384,10 @@ export const sendScheduledSeatsEmailsAndSMS = async (
}
}
if (!attendeeEmailDisabled && !eventTypeDisableAttendeeEmail(eventTypeMetadata)) {
if (
!attendeeEmailDisabled &&
!shouldSkipAttendeeEmailWithSettings(eventTypeMetadata, organizationSettings, EmailType.CONFIRMATION)
) {
emailsToSend.push(
sendEmail(
() =>
@@ -356,8 +415,9 @@ export const sendCancelledSeatEmailsAndSMS = async (
const formattedCalEvent = formatCalEvent(calEvent);
const clonedCalEvent = cloneDeep(formattedCalEvent);
const emailsToSend: Promise<unknown>[] = [];
const organizationSettings = await fetchOrganizationEmailSettings(calEvent.organizationId);
if (!eventTypeDisableAttendeeEmail(eventTypeMetadata))
if (!shouldSkipAttendeeEmailWithSettings(eventTypeMetadata, organizationSettings, EmailType.CANCELLATION))
emailsToSend.push(sendEmail(() => new AttendeeCancelledSeatEmail(clonedCalEvent, cancelledAttendee)));
if (!eventTypeDisableHostEmail(eventTypeMetadata))
emailsToSend.push(
@@ -402,7 +462,8 @@ const _sendAttendeeRequestEmailAndSMS = async (
attendee: Person,
eventTypeMetadata?: EventTypeMetadata
) => {
if (eventTypeDisableAttendeeEmail(eventTypeMetadata)) return;
const organizationSettings = await fetchOrganizationEmailSettings(calEvent.organizationId);
if (shouldSkipAttendeeEmailWithSettings(eventTypeMetadata, organizationSettings, EmailType.REQUEST)) return;
const calendarEvent = formatCalEvent(calEvent);
await sendEmail(() => new AttendeeRequestEmail(calendarEvent, attendee));
@@ -419,7 +480,8 @@ export const sendDeclinedEmailsAndSMS = async (
calEvent: CalendarEvent,
eventTypeMetadata?: EventTypeMetadata
) => {
if (eventTypeDisableAttendeeEmail(eventTypeMetadata)) return;
const organizationSettings = await fetchOrganizationEmailSettings(calEvent.organizationId);
if (shouldSkipAttendeeEmailWithSettings(eventTypeMetadata, organizationSettings, EmailType.REQUEST)) return;
const calendarEvent = formatCalEvent(calEvent);
const emailsToSend: Promise<unknown>[] = [];
@@ -444,6 +506,7 @@ export const sendCancelledEmailsAndSMS = async (
const emailsToSend: Promise<unknown>[] = [];
const calEventLength = calendarEvent.length;
const eventDuration = dayjs(calEvent.endTime).diff(calEvent.startTime, "minutes");
const organizationSettings = await fetchOrganizationEmailSettings(calEvent.organizationId);
if (typeof calEventLength !== "number") {
logger.error(
@@ -464,7 +527,7 @@ export const sendCancelledEmailsAndSMS = async (
}
}
if (!eventTypeDisableAttendeeEmail(eventTypeMetadata)) {
if (!shouldSkipAttendeeEmailWithSettings(eventTypeMetadata, organizationSettings, EmailType.CANCELLATION)) {
emailsToSend.push(
...calendarEvent.attendees.map((attendee) => {
return sendEmail(
@@ -519,20 +582,24 @@ export const sendAwaitingPaymentEmailAndSMS = async (
calEvent: CalendarEvent,
eventTypeMetadata?: EventTypeMetadata
) => {
if (eventTypeDisableAttendeeEmail(eventTypeMetadata)) return;
const organizationSettings = await fetchOrganizationEmailSettings(calEvent.organizationId);
const emailsToSend: Promise<unknown>[] = [];
emailsToSend.push(
...calEvent.attendees.map((attendee) => {
return sendEmail(() => new AttendeeAwaitingPaymentEmail(calEvent, attendee));
})
);
if (
!shouldSkipAttendeeEmailWithSettings(eventTypeMetadata, organizationSettings, EmailType.AWAITING_PAYMENT)
) {
emailsToSend.push(
...calEvent.attendees.map((attendee) => {
return sendEmail(() => new AttendeeAwaitingPaymentEmail(calEvent, attendee));
})
);
}
await Promise.all(emailsToSend);
const awaitingPaymentSMS = new AwaitingPaymentSMS(calEvent);
await awaitingPaymentSMS.sendSMSToAttendees();
};
export const sendRequestRescheduleEmailAndSMS = async (
calEvent: CalendarEvent,
metadata: { rescheduleLink: string },
@@ -540,11 +607,18 @@ export const sendRequestRescheduleEmailAndSMS = async (
) => {
const emailsToSend: Promise<unknown>[] = [];
const calendarEvent = formatCalEvent(calEvent);
const organizationSettings = await fetchOrganizationEmailSettings(calEvent.organizationId);
if (!eventTypeDisableHostEmail(eventTypeMetadata)) {
emailsToSend.push(sendEmail(() => new OrganizerRequestedToRescheduleEmail(calendarEvent, metadata)));
}
if (!eventTypeDisableAttendeeEmail(eventTypeMetadata)) {
if (
!shouldSkipAttendeeEmailWithSettings(
eventTypeMetadata,
organizationSettings,
EmailType.RESCHEDULE_REQUEST
)
) {
emailsToSend.push(sendEmail(() => new AttendeeWasRequestedToRescheduleEmail(calendarEvent, metadata)));
}
@@ -560,6 +634,7 @@ export const sendLocationChangeEmailsAndSMS = async (
const calendarEvent = formatCalEvent(calEvent);
const emailsToSend: Promise<unknown>[] = [];
const organizationSettings = await fetchOrganizationEmailSettings(calEvent.organizationId);
if (!eventTypeDisableHostEmail(eventTypeMetadata)) {
emailsToSend.push(sendEmail(() => new OrganizerLocationChangeEmail({ calEvent: calendarEvent })));
@@ -573,7 +648,9 @@ export const sendLocationChangeEmailsAndSMS = async (
}
}
if (!eventTypeDisableAttendeeEmail(eventTypeMetadata)) {
if (
!shouldSkipAttendeeEmailWithSettings(eventTypeMetadata, organizationSettings, EmailType.LOCATION_CHANGE)
) {
emailsToSend.push(
...calendarEvent.attendees.map((attendee) => {
return sendEmail(() => new AttendeeLocationChangeEmail(calendarEvent, attendee));
@@ -634,17 +711,25 @@ export const sendAddGuestsEmailsAndSMS = async (args: {
}
}
if (!eventTypeDisableAttendeeEmail(eventTypeMetadata)) {
const eventScheduledSMS = new EventSuccessfullyScheduledSMS(calEvent);
const eventScheduledSMS = new EventSuccessfullyScheduledSMS(calEvent);
const organizationSettings = await fetchOrganizationEmailSettings(calEvent.organizationId);
for (const attendee of calendarEvent.attendees) {
if (newGuests.includes(attendee.email)) {
for (const attendee of calendarEvent.attendees) {
if (newGuests.includes(attendee.email)) {
// New guests get confirmation emails
if (
!shouldSkipAttendeeEmailWithSettings(eventTypeMetadata, organizationSettings, EmailType.CONFIRMATION)
) {
emailsAndSMSToSend.push(sendEmail(() => new AttendeeScheduledEmail(calendarEvent, attendee)));
if (attendee.phoneNumber) {
emailsAndSMSToSend.push(eventScheduledSMS.sendSMSToAttendee(attendee));
}
} else {
}
} else {
if (
!shouldSkipAttendeeEmailWithSettings(eventTypeMetadata, organizationSettings, EmailType.NEW_EVENT)
) {
emailsAndSMSToSend.push(sendEmail(() => new AttendeeAddGuestsEmail(calendarEvent, attendee)));
}
}
+11
View File
@@ -0,0 +1,11 @@
export enum EmailType {
CONFIRMATION = "confirmation",
CANCELLATION = "cancellation",
RESCHEDULED = "rescheduled",
REQUEST = "request",
REASSIGNED = "reassigned",
AWAITING_PAYMENT = "awaiting_payment",
RESCHEDULE_REQUEST = "reschedule_request",
LOCATION_CHANGE = "location_change",
NEW_EVENT = "new_event",
}