import { calcomResponseDataSchema, calcomSchema, unwrapCalcomData, } from "@automate.ax/integration-contracts/calcom" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { CALCOM_ACCOUNT, CALCOM_ID_SCHEMA, CALCOM_UID_SCHEMA, EVENT_TYPE_API_VERSION, PRIVATE_LINK_API_VERSION, calcomAccountRequirement, calcomApi, calcomPath, } from "./lib" const EVENT_TYPE_READ = calcomAccountRequirement() const EVENT_TYPE_WRITE = calcomAccountRequirement() const EVENT_TYPE_ID_SCHEMA = z.object({ eventTypeId: CALCOM_ID_SCHEMA }) const EVENT_TYPE_LIST_SCHEMA = z .object({ eventSlug: z.string().trim().min(1).optional(), orgId: CALCOM_ID_SCHEMA.optional(), orgSlug: z.string().trim().min(1).optional(), sortCreatedAt: z.enum(["asc", "desc"]).optional(), username: z.string().trim().min(1).optional(), usernames: z.string().trim().min(1).array().min(2).optional(), }) .refine(({ eventSlug, username }) => !eventSlug || username, { message: "username is required when eventSlug is provided.", path: ["username"], }) const EVENT_TYPE_HISTORY_INPUT_SCHEMA = EVENT_TYPE_ID_SCHEMA.extend({ cursor: z.string().trim().min(1).optional(), limit: z.number().int().min(1).max(50).prefault(25), }) const EVENT_TYPE_HISTORY_SCHEMA = z.object({ auditLogs: calcomSchema("EventTypeHistoryAuditLog_2024_06_14").array(), eventTypeId: CALCOM_ID_SCHEMA, pagination: calcomSchema("EventTypeHistoryPagination_2024_06_14"), }) const PRIVATE_LINK_ID_SCHEMA = EVENT_TYPE_ID_SCHEMA.extend({ linkId: CALCOM_UID_SCHEMA, }) /** Creates a personal Cal.com event type. */ export const createCalcomEventType = defineAction("Create Cal.com event type") .describe( "Creates a personal event type with scheduling, booking, and availability settings.", ) .account(CALCOM_ACCOUNT, EVENT_TYPE_WRITE) .input(calcomSchema("CreateEventTypeInput_2026_06_12")) .output(calcomResponseDataSchema("CreateEventTypeOutput_2026_06_12")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input }) => calcomApi(account) .request("event-types", { apiVersion: EVENT_TYPE_API_VERSION, body: input, method: "POST", responseSchema: "CreateEventTypeOutput_2026_06_12", }) .then(unwrapCalcomData), ) /** Lists personal Cal.com event types. */ export const listCalcomEventTypes = defineAction("List Cal.com event types") .describe("Lists the personal event types visible to the connected user.") .account(CALCOM_ACCOUNT, EVENT_TYPE_READ) .input(EVENT_TYPE_LIST_SCHEMA) .output(calcomResponseDataSchema("GetEventTypesOutput_2026_06_12")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request("event-types", { apiVersion: EVENT_TYPE_API_VERSION, query: { ...input, usernames: input.usernames?.join(","), }, responseSchema: "GetEventTypesOutput_2026_06_12", }) .then(unwrapCalcomData), ) /** Retrieves one personal Cal.com event type. */ export const getCalcomEventType = defineAction("Get Cal.com event type") .describe("Retrieves one personal event type by numeric ID.") .account(CALCOM_ACCOUNT, EVENT_TYPE_READ) .input(EVENT_TYPE_ID_SCHEMA) .output(calcomResponseDataSchema("GetEventTypeOutput_2026_06_12")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request(calcomPath`event-types/${input.eventTypeId}`, { apiVersion: EVENT_TYPE_API_VERSION, responseSchema: "GetEventTypeOutput_2026_06_12", }) .then(unwrapCalcomData), ) /** Updates one personal Cal.com event type. */ export const updateCalcomEventType = defineAction("Update Cal.com event type") .describe( "Updates scheduling, booking, and availability settings for an event type.", ) .account(CALCOM_ACCOUNT, EVENT_TYPE_WRITE) .input( EVENT_TYPE_ID_SCHEMA.and(calcomSchema("UpdateEventTypeInput_2026_06_12")), ) .output(calcomResponseDataSchema("UpdateEventTypeOutput_2026_06_12")) .retry({ replaySafety: "safe" }) .handler(({ account, input: { eventTypeId, ...body } }) => calcomApi(account) .request(calcomPath`event-types/${eventTypeId}`, { apiVersion: EVENT_TYPE_API_VERSION, body, method: "PATCH", responseSchema: "UpdateEventTypeOutput_2026_06_12", }) .then(unwrapCalcomData), ) /** Deletes one personal Cal.com event type. */ export const deleteCalcomEventType = defineAction("Delete Cal.com event type") .describe("Permanently deletes one personal event type.") .account(CALCOM_ACCOUNT, EVENT_TYPE_WRITE) .input(EVENT_TYPE_ID_SCHEMA) .output(calcomResponseDataSchema("DeleteEventTypeOutput_2024_06_14")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request(calcomPath`event-types/${input.eventTypeId}`, { apiVersion: EVENT_TYPE_API_VERSION, method: "DELETE", responseSchema: "DeleteEventTypeOutput_2024_06_14", }) .then(unwrapCalcomData), ) /** Retrieves the change history for one Cal.com event type. */ export const getCalcomEventTypeHistory = defineAction( "Get Cal.com event type history", ) .describe("Returns the recorded configuration history for one event type.") .account(CALCOM_ACCOUNT, EVENT_TYPE_READ) .input(EVENT_TYPE_HISTORY_INPUT_SCHEMA) .output(EVENT_TYPE_HISTORY_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const response = await calcomApi(account).request( calcomPath`event-types/${input.eventTypeId}/history`, { apiVersion: EVENT_TYPE_API_VERSION, query: { cursor: input.cursor, limit: input.limit }, responseSchema: "GetEventTypeHistoryOutput_2024_06_14", }, ) return { ...response.data, pagination: response.pagination } }) /** Retrieves effective scheduling configuration for an event type. */ export const getCalcomEventTypeSchedulingConfig = defineAction( "Get Cal.com event type scheduling config", ) .describe( "Returns the effective schedule and booking configuration for one event type.", ) .account(CALCOM_ACCOUNT, EVENT_TYPE_READ) .input(EVENT_TYPE_ID_SCHEMA) .output(calcomResponseDataSchema("GetSchedulingConfigOutput")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request(calcomPath`event-types/${input.eventTypeId}/scheduling-config`, { apiVersion: EVENT_TYPE_API_VERSION, responseSchema: "GetSchedulingConfigOutput", }) .then(unwrapCalcomData), ) /** Creates a private scheduling link for an event type. */ export const createCalcomPrivateLink = defineAction( "Create Cal.com private link", ) .describe( "Creates a private booking link with an optional usage limit and expiry.", ) .account(CALCOM_ACCOUNT, EVENT_TYPE_WRITE) .input(EVENT_TYPE_ID_SCHEMA.and(calcomSchema("CreatePrivateLinkInput"))) .output(calcomResponseDataSchema("CreatePrivateLinkOutput")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input: { eventTypeId, ...body } }) => calcomApi(account) .request(calcomPath`event-types/${eventTypeId}/private-links`, { apiVersion: PRIVATE_LINK_API_VERSION, body, method: "POST", responseSchema: "CreatePrivateLinkOutput", }) .then(unwrapCalcomData), ) /** Lists private scheduling links for an event type. */ export const listCalcomPrivateLinks = defineAction("List Cal.com private links") .describe("Lists every private booking link attached to one event type.") .account(CALCOM_ACCOUNT, EVENT_TYPE_READ) .input(EVENT_TYPE_ID_SCHEMA) .output(calcomResponseDataSchema("GetPrivateLinksOutput")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request(calcomPath`event-types/${input.eventTypeId}/private-links`, { apiVersion: PRIVATE_LINK_API_VERSION, responseSchema: "GetPrivateLinksOutput", }) .then(unwrapCalcomData), ) /** Updates a private scheduling link. */ export const updateCalcomPrivateLink = defineAction( "Update Cal.com private link", ) .describe("Updates the usage limit or expiry for a private booking link.") .account(CALCOM_ACCOUNT, EVENT_TYPE_WRITE) .input(PRIVATE_LINK_ID_SCHEMA.and(calcomSchema("UpdatePrivateLinkBody"))) .output(calcomResponseDataSchema("UpdatePrivateLinkOutput")) .retry({ replaySafety: "safe" }) .handler(({ account, input: { eventTypeId, linkId, ...body } }) => calcomApi(account) .request(calcomPath`event-types/${eventTypeId}/private-links/${linkId}`, { apiVersion: PRIVATE_LINK_API_VERSION, body, method: "PATCH", responseSchema: "UpdatePrivateLinkOutput", }) .then(unwrapCalcomData), ) /** Deletes a private scheduling link. */ export const deleteCalcomPrivateLink = defineAction( "Delete Cal.com private link", ) .describe("Permanently removes one private booking link.") .account(CALCOM_ACCOUNT, EVENT_TYPE_WRITE) .input(PRIVATE_LINK_ID_SCHEMA) .output(calcomResponseDataSchema("DeletePrivateLinkOutput")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request( calcomPath`event-types/${input.eventTypeId}/private-links/${input.linkId}`, { apiVersion: PRIVATE_LINK_API_VERSION, method: "DELETE", responseSchema: "DeletePrivateLinkOutput", }, ) .then(unwrapCalcomData), )