feat: platform toggle calendar event creation (#21788)

* feat: add areCalendarEventsEnabled to PlatformOAuthClient table

* feat: areCalendarEvents enabled in handleNewBooking and v2

* feat: v2 handle areCalendarEventsEnabled

* feat: toggle areCalendarEventsEnabled on frontend

* feat: enable areCalendarEventsEnabled for recurring,confirm,paid

* refactor: use placeholder calendar event

* remove merge conflicts

* chore: bump libraries

* chore: bump libraries
This commit is contained in:
Lauris Skraucis authored and GitHub committed 2025-06-13 14:15:47 +00:00
1 parent d7fd055171
commit 4a53ec0b74
33 files changed
+525 -22

No files matched your search

@@ -14,7 +14,7 @@ import sendPayload from "@calcom/features/webhooks/lib/sendOrSchedulePayload";
import type { EventPayloadType, EventTypeInfo } from "@calcom/features/webhooks/lib/sendPayload";
import { getVideoCallUrlFromCalEvent } from "@calcom/lib/CalEventParser";
import type { EventManagerUser } from "@calcom/lib/EventManager";
import EventManager from "@calcom/lib/EventManager";
import EventManager, { placeholderCreatedEvent } from "@calcom/lib/EventManager";
import { getBookerBaseUrl } from "@calcom/lib/getBookerUrl/server";
import getOrgIdFromMemberOrTeamId from "@calcom/lib/getOrgIdFromMemberOrTeamId";
import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType";
@@ -88,7 +88,8 @@ export async function handleConfirmation(args: {
const eventTypeMetadata = EventTypeMetaDataSchema.parse(eventType?.metadata || {});
const apps = eventTypeAppMetadataOptionalSchema.parse(eventTypeMetadata?.apps);
const eventManager = new EventManager(user, apps);
const scheduleResult = await eventManager.create(evt);
const areCalendarEventsEnabled = platformClientParams?.areCalendarEventsEnabled ?? true;
const scheduleResult = areCalendarEventsEnabled ? await eventManager.create(evt) : placeholderCreatedEvent;
const results = scheduleResult.results;
const metadata: AdditionalInformation = {};
const workflows = await getAllWorkflowsFromEventType(eventType, booking.userId);
@@ -43,7 +43,7 @@ import {
scheduleTrigger,
} from "@calcom/features/webhooks/lib/scheduleTrigger";
import { getVideoCallUrlFromCalEvent } from "@calcom/lib/CalEventParser";
import EventManager from "@calcom/lib/EventManager";
import EventManager, { placeholderCreatedEvent } from "@calcom/lib/EventManager";
import { handleAnalyticsEvents } from "@calcom/lib/analyticsManager/handleAnalyticsEvents";
import { shouldIgnoreContactOwner } from "@calcom/lib/bookings/routing/utils";
import { getUsernameList } from "@calcom/lib/defaultEvents";
@@ -373,6 +373,7 @@ export type PlatformParams = {
platformBookingUrl?: string;
platformRescheduleUrl?: string;
platformBookingLocation?: string;
areCalendarEventsEnabled?: boolean;
};
export type BookingHandlerInput = {
@@ -397,6 +398,7 @@ async function handler(
platformBookingLocation,
hostname,
forcedSlug,
areCalendarEventsEnabled = true,
} = input;
const isPlatformBooking = !!platformClientId;
@@ -1737,7 +1739,7 @@ async function handler(
// Create a booking
} else if (isConfirmedByDefault) {
// Use EventManager to conditionally use all needed integrations.
const createManager = await eventManager.create(evt);
const createManager = areCalendarEventsEnabled ? await eventManager.create(evt) : placeholderCreatedEvent;
if (evt.location) {
booking.location = evt.location;
}
@@ -9,6 +9,7 @@ export type PlatformParams = {
platformBookingUrl?: string;
platformRescheduleUrl?: string;
platformBookingLocation?: string;
areCalendarEventsEnabled?: boolean;
};
export type BookingHandlerInput = {
@@ -45,6 +46,7 @@ export const handleNewRecurringBooking = async (input: BookingHandlerInput): Pro
platformCancelUrl: input.platformCancelUrl,
platformBookingUrl: input.platformBookingUrl,
platformBookingLocation: input.platformBookingLocation,
areCalendarEventsEnabled: input.areCalendarEventsEnabled,
};
if (isRoundRobin) {
+15 -3
View File
@@ -8,7 +8,9 @@ import { doesBookingRequireConfirmation } from "@calcom/features/bookings/lib/do
import { getAllCredentialsIncludeServiceAccountKey } from "@calcom/features/bookings/lib/getAllCredentialsForUsersOnEvent/getAllCredentials";
import { handleConfirmation } from "@calcom/features/bookings/lib/handleConfirmation";
import stripe from "@calcom/features/ee/payments/server/stripe";
import EventManager from "@calcom/lib/EventManager";
import { getPlatformParams } from "@calcom/features/platform-oauth-client/get-platform-params";
import { PlatformOAuthClientRepository } from "@calcom/features/platform-oauth-client/platform-oauth-client.repository";
import EventManager, { placeholderCreatedEvent } from "@calcom/lib/EventManager";
import { IS_PRODUCTION } from "@calcom/lib/constants";
import { getErrorFromUnknown } from "@calcom/lib/errors";
import { HttpError as HttpCode } from "@calcom/lib/http-error";
@@ -80,9 +82,18 @@ const handleSetupSuccess = async (event: Stripe.Event) => {
metadata,
});
const platformOAuthClientRepository = new PlatformOAuthClientRepository();
const platformOAuthClient = user.isPlatformManaged
? await platformOAuthClientRepository.getByUserId(user.id)
: null;
const areCalendarEventsEnabled = platformOAuthClient?.areCalendarEventsEnabled ?? true;
const areEmailsEnabled = platformOAuthClient?.areEmailsEnabled ?? true;
if (!requiresConfirmation) {
const eventManager = new EventManager({ ...user, credentials: allCredentials }, metadata?.apps);
const scheduleResult = await eventManager.create(evt);
const scheduleResult = areCalendarEventsEnabled
? await eventManager.create(evt)
: placeholderCreatedEvent;
bookingData.references = { create: scheduleResult.referencesToCreate };
bookingData.status = BookingStatus.ACCEPTED;
}
@@ -114,8 +125,9 @@ const handleSetupSuccess = async (event: Stripe.Event) => {
bookingId: booking.id,
booking,
paid: true,
platformClientParams: platformOAuthClient ? getPlatformParams(platformOAuthClient) : undefined,
});
} else {
} else if (areEmailsEnabled) {
await sendOrganizerRequestEmail({ ...evt }, eventType.metadata);
await sendAttendeeRequestEmailAndSMS({ ...evt }, evt.attendees[0], eventType.metadata);
}
@@ -0,0 +1,12 @@
import type { PlatformOAuthClient } from "@prisma/client";
export function getPlatformParams(platformOAuthClient: PlatformOAuthClient) {
return {
platformClientId: platformOAuthClient.id,
platformCancelUrl: platformOAuthClient.bookingCancelRedirectUri,
platformRescheduleUrl: platformOAuthClient.bookingRescheduleRedirectUri,
platformBookingUrl: platformOAuthClient.bookingRedirectUri,
arePlatformEmailsEnabled: platformOAuthClient.areEmailsEnabled,
areCalendarEventsEnabled: platformOAuthClient.areCalendarEventsEnabled,
};
}
@@ -0,0 +1,5 @@
import type { PlatformOAuthClient } from "@prisma/client";
export interface IPlatformOAuthClientRepository {
getByUserId(userId: number): Promise<PlatformOAuthClient | null>;
}
@@ -0,0 +1,24 @@
import { captureException } from "@sentry/nextjs";
import prisma from "@calcom/prisma";
import type { IPlatformOAuthClientRepository } from "./platform-oauth-client.repository.interface";
export class PlatformOAuthClientRepository implements IPlatformOAuthClientRepository {
async getByUserId(userId: number) {
try {
return prisma.platformOAuthClient.findFirst({
where: {
users: {
some: {
id: userId,
},
},
},
});
} catch (err) {
captureException(err);
throw err;
}
}
}
+2
View File
@@ -1142,3 +1142,5 @@ export default class EventManager {
return this.appOptions[credential.appId as keyof typeof this.appOptions];
}
}
export const placeholderCreatedEvent = { results: [], referencesToCreate: [] };
+1
View File
@@ -91,6 +91,7 @@ export async function getBooking(bookingId: number) {
name: true,
locale: true,
destinationCalendar: true,
isPlatformManaged: true,
},
},
},
+16 -3
View File
@@ -5,7 +5,9 @@ import { doesBookingRequireConfirmation } from "@calcom/features/bookings/lib/do
import { getAllCredentialsIncludeServiceAccountKey } from "@calcom/features/bookings/lib/getAllCredentialsForUsersOnEvent/getAllCredentials";
import { handleBookingRequested } from "@calcom/features/bookings/lib/handleBookingRequested";
import { handleConfirmation } from "@calcom/features/bookings/lib/handleConfirmation";
import EventManager from "@calcom/lib/EventManager";
import { getPlatformParams } from "@calcom/features/platform-oauth-client/get-platform-params";
import { PlatformOAuthClientRepository } from "@calcom/features/platform-oauth-client/platform-oauth-client.repository";
import EventManager, { placeholderCreatedEvent } from "@calcom/lib/EventManager";
import { HttpError as HttpCode } from "@calcom/lib/http-error";
import { getBooking } from "@calcom/lib/payment/getBooking";
import prisma from "@calcom/prisma";
@@ -33,10 +35,20 @@ export async function handlePaymentSuccess(paymentId: number, bookingId: number)
});
const isConfirmed = booking.status === BookingStatus.ACCEPTED;
const platformOAuthClientRepository = new PlatformOAuthClientRepository();
const platformOAuthClient = userWithCredentials.isPlatformManaged
? await platformOAuthClientRepository.getByUserId(userWithCredentials.id)
: null;
const areCalendarEventsEnabled = platformOAuthClient?.areCalendarEventsEnabled ?? true;
const areEmailsEnabled = platformOAuthClient?.areEmailsEnabled ?? true;
if (isConfirmed) {
const apps = eventTypeAppMetadataOptionalSchema.parse(eventType?.metadata?.apps);
const eventManager = new EventManager({ ...userWithCredentials, credentials: allCredentials }, apps);
const scheduleResult = await eventManager.create(evt);
const scheduleResult = areCalendarEventsEnabled
? await eventManager.create(evt)
: placeholderCreatedEvent;
bookingData.references = { create: scheduleResult.referencesToCreate };
}
@@ -76,6 +88,7 @@ export async function handlePaymentSuccess(paymentId: number, bookingId: number)
bookingId: booking.id,
booking,
paid: true,
platformClientParams: platformOAuthClient ? getPlatformParams(platformOAuthClient) : undefined,
});
} else {
await handleBookingRequested({
@@ -84,7 +97,7 @@ export async function handlePaymentSuccess(paymentId: number, bookingId: number)
});
log.debug(`handling booking request for eventId ${eventType.id}`);
}
} else {
} else if (areEmailsEnabled) {
await sendScheduledEmailsAndSMS({ ...evt }, undefined, undefined, undefined, eventType.metadata);
}
+3 -1
View File
@@ -1,3 +1,5 @@
import EventManager from "@calcom/lib/EventManager";
export { getPublicEvent, type PublicEventType } from "@calcom/features/eventtypes/lib/getPublicEvent";
export { getBulkUserEventTypes, getBulkTeamEventTypes } from "@calcom/lib/event-types/getBulkEventTypes";
@@ -17,7 +19,7 @@ export {
} from "@calcom/prisma/zod-utils";
export { validateCustomEventName } from "@calcom/lib/event";
export { EventManager };
export { getEventTypeById } from "@calcom/lib/event-types/getEventTypeById";
export { getEventTypesByViewer } from "@calcom/lib/event-types/getEventTypesByViewer";
export type { EventType } from "@calcom/lib/event-types/getEventTypeById";
@@ -6,6 +6,10 @@ import { PERMISSION_MAP } from "@calcom/platform-constants";
export const ARE_DEFAULT_EVENT_TYPES_ENABLED_DOCS =
"If true, when creating a managed user the managed user will have 4 default event types: 30 and 60 minutes without Cal video, 30 and 60 minutes with Cal video. Set this as false if you want to create a managed user and then manually create event types for the user.";
export const ARE_CALENDAR_EVENTS_ENABLED_DOCS =
"If true and if managed user has calendar connected, calendar events will be created. Disable it if you manually create calendar events. Default to true.";
export class CreateOAuthClientInput {
@IsOptional()
@IsString()
@@ -60,4 +64,14 @@ export class CreateOAuthClientInput {
description: ARE_DEFAULT_EVENT_TYPES_ENABLED_DOCS,
})
areDefaultEventTypesEnabled? = false;
@IsOptional()
@IsBoolean()
@Transform(({ value }) => value ?? true)
@ApiPropertyOptional({
type: Boolean,
default: true,
description: ARE_CALENDAR_EVENTS_ENABLED_DOCS,
})
areCalendarEventsEnabled? = true;
}
@@ -1,7 +1,10 @@
import { ApiPropertyOptional } from "@nestjs/swagger";
import { IsArray, IsBoolean, IsOptional, IsString } from "class-validator";
import { ARE_DEFAULT_EVENT_TYPES_ENABLED_DOCS } from "./create-oauth-client.input";
import {
ARE_CALENDAR_EVENTS_ENABLED_DOCS,
ARE_DEFAULT_EVENT_TYPES_ENABLED_DOCS,
} from "./create-oauth-client.input";
export class UpdateOAuthClientInput {
@IsOptional()
@@ -47,4 +50,12 @@ export class UpdateOAuthClientInput {
description: ARE_DEFAULT_EVENT_TYPES_ENABLED_DOCS,
})
areDefaultEventTypesEnabled?: boolean;
@IsOptional()
@IsBoolean()
@ApiPropertyOptional({
type: Boolean,
description: ARE_CALENDAR_EVENTS_ENABLED_DOCS,
})
areCalendarEventsEnabled?: boolean;
}
@@ -3,6 +3,8 @@ import { IsArray, IsEnum, IsString, IsNumber, IsOptional, IsDate, IsBoolean, IsU
import { PERMISSION_MAP } from "@calcom/platform-constants";
import { ARE_CALENDAR_EVENTS_ENABLED_DOCS } from "../inputs";
export class PlatformOAuthClientDto {
@ApiProperty({ example: "clsx38nbl0001vkhlwin9fmt0" })
@IsString()
@@ -56,6 +58,10 @@ export class PlatformOAuthClientDto {
})
areDefaultEventTypesEnabled!: boolean;
@IsBoolean()
@ApiProperty({ example: true, description: ARE_CALENDAR_EVENTS_ENABLED_DOCS })
areCalendarEventsEnabled!: boolean;
@ApiPropertyOptional({ example: "https://example.com/booking-redirect" })
@IsOptional()
@IsUrl()
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "PlatformOAuthClient" ADD COLUMN "areCalendarEventsEnabled" BOOLEAN NOT NULL DEFAULT true;
+1
View File
@@ -1786,6 +1786,7 @@ model PlatformOAuthClient {
bookingRescheduleRedirectUri String?
areEmailsEnabled Boolean @default(false)
areDefaultEventTypesEnabled Boolean @default(true)
areCalendarEventsEnabled Boolean @default(true)
createdAt DateTime @default(now())
}
+1
View File
@@ -259,6 +259,7 @@ const PlatformClientParamsSchema = z.object({
platformCancelUrl: z.string().nullable().optional(),
platformBookingUrl: z.string().nullable().optional(),
platformBookingLocation: z.string().optional(),
areCalendarEventsEnabled: z.boolean().optional(),
});
export type PlatformClientParams = z.infer<typeof PlatformClientParamsSchema>;