import { calcomResponseDataSchema, calcomSchema, unwrapCalcomData, } from "@automate.ax/integration-contracts/calcom" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { BOOKING_API_VERSION, BOOKING_ATTENDEE_API_VERSION, BOOKING_LIST_API_VERSION, CALCOM_ACCOUNT, CALCOM_DATE_TIME_SCHEMA, CALCOM_ID_SCHEMA, CALCOM_UID_SCHEMA, calcomAccountRequirement, calcomApi, calcomPath, compact, } from "./lib" const BOOKING_READ = calcomAccountRequirement() const BOOKING_WRITE = calcomAccountRequirement() const PUBLIC_BOOKING = calcomAccountRequirement() const BOOKING_STATUS_SCHEMA = z.enum([ "upcoming", "recurring", "past", "cancelled", "unconfirmed", ]) const SORT_SCHEMA = z.enum(["asc", "desc"]) const BOOKING_UID_INPUT_SCHEMA = z.object({ bookingUid: CALCOM_UID_SCHEMA }) const BOOKING_QUERY_SCHEMA = z.object({ afterCreatedAt: CALCOM_DATE_TIME_SCHEMA.optional(), afterStart: CALCOM_DATE_TIME_SCHEMA.optional(), afterUpdatedAt: CALCOM_DATE_TIME_SCHEMA.optional(), attendeeEmail: z.email().optional(), attendeeName: z.string().trim().min(1).optional(), beforeCreatedAt: CALCOM_DATE_TIME_SCHEMA.optional(), beforeEnd: CALCOM_DATE_TIME_SCHEMA.optional(), beforeUpdatedAt: CALCOM_DATE_TIME_SCHEMA.optional(), bookingUid: CALCOM_UID_SCHEMA.optional(), cursor: z.string().trim().min(1).optional(), eventTypeId: CALCOM_ID_SCHEMA.optional(), eventTypeIds: CALCOM_ID_SCHEMA.array().min(1).optional(), limit: z.number().int().min(1).max(100).prefault(50), sortCreated: SORT_SCHEMA.optional(), sortEnd: SORT_SCHEMA.optional(), sortStart: SORT_SCHEMA.optional(), sortUpdatedAt: SORT_SCHEMA.optional(), status: BOOKING_STATUS_SCHEMA.optional(), teamId: CALCOM_ID_SCHEMA.optional(), teamIds: CALCOM_ID_SCHEMA.array().min(1).optional(), }) const BOOKING_PAGE_SCHEMA = z.object({ items: z .union([ calcomSchema("BookingOutput_2024_08_13"), calcomSchema("RecurringBookingOutput_2024_08_13"), calcomSchema("GetSeatedBookingOutput_2024_08_13"), calcomSchema("GetRecurringSeatedBookingOutput_2024_08_13"), ]) .array(), pagination: calcomSchema("CursorPaginationMeta_2026_05_01"), }) const REASSIGN_BOOKING_RESPONSE_SCHEMA = z.object({ data: z.object({ bookingUid: z.string(), reassignedTo: z.object({ displayEmail: z.string(), email: z.string(), id: z.number().int(), name: z.string(), }), }), status: z.enum(["error", "success"]), }) /** Creates a regular, recurring, seated, or instant Cal.com booking. */ export const createCalcomBooking = defineAction("Create Cal.com booking") .describe( "Creates a booking for an event type at an available UTC start time.", ) .account(CALCOM_ACCOUNT, PUBLIC_BOOKING) .input( z.union([ calcomSchema("CreateBookingInput_2024_08_13"), calcomSchema("CreateInstantBookingInput_2024_08_13"), calcomSchema("CreateRecurringBookingInput_2024_08_13"), ]), ) .output(calcomResponseDataSchema("CreateBookingOutput_2024_08_13")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input }) => calcomApi(account) .request("bookings", { apiVersion: BOOKING_API_VERSION, body: input, method: "POST", responseSchema: "CreateBookingOutput_2024_08_13", }) .then(unwrapCalcomData), ) /** Lists bookings visible to the connected Cal.com account. */ export const listCalcomBookings = defineAction("List Cal.com bookings") .describe( "Lists bookings with lifecycle, attendee, event-type, team, and time filters.", ) .account(CALCOM_ACCOUNT, BOOKING_READ) .input(BOOKING_QUERY_SCHEMA) .output(BOOKING_PAGE_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const { eventTypeIds, teamIds, ...query } = input const response = await calcomApi(account).request("bookings", { apiVersion: BOOKING_LIST_API_VERSION, query: compact({ ...query, eventTypeIds: eventTypeIds?.join(","), teamsIds: teamIds?.join(","), }), responseSchema: "GetBookingsOutput_2026_05_01", }) return { items: response.data, pagination: response.pagination } }) /** Retrieves one Cal.com booking by UID. */ export const getCalcomBooking = defineAction("Get Cal.com booking") .describe("Retrieves a regular, recurring, or seated booking by UID.") .account(CALCOM_ACCOUNT, PUBLIC_BOOKING) .input(BOOKING_UID_INPUT_SCHEMA) .output(calcomResponseDataSchema("GetBookingOutput_2024_08_13")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request(calcomPath`bookings/${input.bookingUid}`, { apiVersion: BOOKING_API_VERSION, responseSchema: "GetBookingOutput_2024_08_13", }) .then(unwrapCalcomData), ) /** Reschedules an existing booking or one seat in a seated booking. */ export const rescheduleCalcomBooking = defineAction( "Reschedule Cal.com booking", ) .describe("Moves an existing booking to a new available UTC start time.") .account(CALCOM_ACCOUNT, PUBLIC_BOOKING) .input( z.union([ BOOKING_UID_INPUT_SCHEMA.and( calcomSchema("RescheduleBookingInput_2024_08_13"), ), BOOKING_UID_INPUT_SCHEMA.and( calcomSchema("RescheduleSeatedBookingInput_2024_08_13"), ), ]), ) .output(calcomResponseDataSchema("RescheduleBookingOutput_2024_08_13")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input: { bookingUid, ...body } }) => calcomApi(account) .request(calcomPath`bookings/${bookingUid}/reschedule`, { apiVersion: BOOKING_API_VERSION, body, method: "POST", responseSchema: "RescheduleBookingOutput_2024_08_13", }) .then(unwrapCalcomData), ) /** Cancels a booking, recurrence, or seated attendee. */ export const cancelCalcomBooking = defineAction("Cancel Cal.com booking") .describe("Cancels a booking and optionally records a cancellation reason.") .account(CALCOM_ACCOUNT, PUBLIC_BOOKING) .input( z.union([ BOOKING_UID_INPUT_SCHEMA.and( calcomSchema("CancelBookingInput_2024_08_13"), ), BOOKING_UID_INPUT_SCHEMA.and( calcomSchema("CancelSeatedBookingInput_2024_08_13"), ), ]), ) .output(calcomResponseDataSchema("CancelBookingOutput_2024_08_13")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input: { bookingUid, ...body } }) => calcomApi(account) .request(calcomPath`bookings/${bookingUid}/cancel`, { apiVersion: BOOKING_API_VERSION, body, method: "POST", responseSchema: "CancelBookingOutput_2024_08_13", }) .then(unwrapCalcomData), ) /** Requests that an attendee choose a new time. */ export const requestCalcomBookingReschedule = defineAction( "Request Cal.com booking reschedule", ) .describe("Cancels a booking and emails its attendee a rescheduling link.") .account(CALCOM_ACCOUNT, BOOKING_WRITE) .input( BOOKING_UID_INPUT_SCHEMA.and( calcomSchema("RequestRescheduleInput_2024_08_13"), ), ) .output(calcomSchema("RequestRescheduleOutput_2024_08_13")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input: { bookingUid, ...body } }) => calcomApi(account).request( calcomPath`bookings/${bookingUid}/request-reschedule`, { apiVersion: BOOKING_API_VERSION, body, method: "POST", responseSchema: "RequestRescheduleOutput_2024_08_13", }, ), ) /** Confirms a pending Cal.com booking. */ export const confirmCalcomBooking = defineAction("Confirm Cal.com booking") .describe("Accepts a booking that requires host confirmation.") .account(CALCOM_ACCOUNT, BOOKING_WRITE) .input(BOOKING_UID_INPUT_SCHEMA) .output(calcomResponseDataSchema("GetBookingOutput_2024_08_13")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input }) => calcomApi(account) .request(calcomPath`bookings/${input.bookingUid}/confirm`, { apiVersion: BOOKING_API_VERSION, method: "POST", responseSchema: "GetBookingOutput_2024_08_13", }) .then(unwrapCalcomData), ) /** Declines a pending Cal.com booking. */ export const declineCalcomBooking = defineAction("Decline Cal.com booking") .describe("Rejects a booking that requires host confirmation.") .account(CALCOM_ACCOUNT, BOOKING_WRITE) .input( BOOKING_UID_INPUT_SCHEMA.and( calcomSchema("DeclineBookingInput_2024_08_13"), ), ) .output(calcomResponseDataSchema("GetBookingOutput_2024_08_13")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input: { bookingUid, ...body } }) => calcomApi(account) .request(calcomPath`bookings/${bookingUid}/decline`, { apiVersion: BOOKING_API_VERSION, body, method: "POST", responseSchema: "GetBookingOutput_2024_08_13", }) .then(unwrapCalcomData), ) /** Marks a host or attendee present or absent. */ export const markCalcomBookingAbsence = defineAction( "Mark Cal.com booking absence", ) .describe("Updates the no-show state for one booking participant.") .account(CALCOM_ACCOUNT, BOOKING_WRITE) .input( BOOKING_UID_INPUT_SCHEMA.and( calcomSchema("MarkAbsentBookingInput_2024_08_13"), ), ) .output(calcomResponseDataSchema("MarkAbsentBookingOutput_2024_08_13")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input: { bookingUid, ...body } }) => calcomApi(account) .request(calcomPath`bookings/${bookingUid}/mark-absent`, { apiVersion: BOOKING_API_VERSION, body, method: "POST", responseSchema: "MarkAbsentBookingOutput_2024_08_13", }) .then(unwrapCalcomData), ) /** Reassigns a round-robin booking to an automatically selected host. */ export const reassignCalcomBooking = defineAction("Reassign Cal.com booking") .describe("Reassigns a round-robin booking to the next available host.") .account(CALCOM_ACCOUNT, BOOKING_WRITE) .input(BOOKING_UID_INPUT_SCHEMA) .output(REASSIGN_BOOKING_RESPONSE_SCHEMA.shape.data) .retry({ replaySafety: "unsafe" }) .handler( async ({ account, input }) => REASSIGN_BOOKING_RESPONSE_SCHEMA.parse( await calcomApi(account).requestJson( calcomPath`bookings/${input.bookingUid}/reassign`, { apiVersion: BOOKING_API_VERSION, method: "POST", }, ), ).data, ) /** Reassigns a round-robin booking to a selected host. */ export const reassignCalcomBookingToHost = defineAction( "Reassign Cal.com booking to host", ) .describe("Moves a round-robin booking to a specific Cal.com user.") .account(CALCOM_ACCOUNT, BOOKING_WRITE) .input( z .object({ bookingUid: CALCOM_UID_SCHEMA, userId: CALCOM_ID_SCHEMA }) .and(calcomSchema("ReassignToUserBookingInput_2024_08_13")), ) .output(REASSIGN_BOOKING_RESPONSE_SCHEMA.shape.data) .retry({ replaySafety: "unsafe" }) .handler( async ({ account, input: { bookingUid, userId, ...body } }) => REASSIGN_BOOKING_RESPONSE_SCHEMA.parse( await calcomApi(account).requestJson( calcomPath`bookings/${bookingUid}/reassign/${userId}`, { apiVersion: BOOKING_API_VERSION, body, method: "POST", }, ), ).data, ) /** Updates a booking location and its connected calendar event. */ export const updateCalcomBookingLocation = defineAction( "Update Cal.com booking location", ) .describe("Changes a booking location and notifies attendees.") .account(CALCOM_ACCOUNT, BOOKING_WRITE) .input( BOOKING_UID_INPUT_SCHEMA.and( calcomSchema("UpdateBookingLocationInput_2024_08_13"), ), ) .output(calcomResponseDataSchema("UpdateBookingLocationOutput_2024_08_13")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input: { bookingUid, ...body } }) => calcomApi(account) .request(calcomPath`bookings/${bookingUid}/location`, { apiVersion: BOOKING_ATTENDEE_API_VERSION, body, method: "PATCH", responseSchema: "UpdateBookingLocationOutput_2024_08_13", }) .then(unwrapCalcomData), ) /** Adds one attendee to an existing booking. */ export const addCalcomBookingAttendee = defineAction( "Add Cal.com booking attendee", ) .describe("Adds one named attendee to an existing booking.") .account(CALCOM_ACCOUNT, BOOKING_WRITE) .input( BOOKING_UID_INPUT_SCHEMA.and(calcomSchema("AddAttendeeInput_2024_08_13")), ) .output(calcomResponseDataSchema("AddAttendeeOutput_2024_08_13")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input: { bookingUid, ...body } }) => calcomApi(account) .request(calcomPath`bookings/${bookingUid}/attendees`, { apiVersion: BOOKING_ATTENDEE_API_VERSION, body, method: "POST", responseSchema: "AddAttendeeOutput_2024_08_13", }) .then(unwrapCalcomData), ) /** Adds guest email addresses to an existing booking. */ export const addCalcomBookingGuests = defineAction("Add Cal.com booking guests") .describe("Adds up to 10 guests per request and 30 guests per booking.") .account(CALCOM_ACCOUNT, BOOKING_WRITE) .input( BOOKING_UID_INPUT_SCHEMA.and(calcomSchema("AddGuestsInput_2024_08_13")), ) .output(calcomResponseDataSchema("AddGuestsOutput_2024_08_13")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input: { bookingUid, ...body } }) => calcomApi(account) .request(calcomPath`bookings/${bookingUid}/guests`, { apiVersion: BOOKING_ATTENDEE_API_VERSION, body, method: "POST", responseSchema: "AddGuestsOutput_2024_08_13", }) .then(unwrapCalcomData), ) /** Lists attendees for one booking. */ export const listCalcomBookingAttendees = defineAction( "List Cal.com booking attendees", ) .describe("Lists the attendees attached to one booking UID.") .account(CALCOM_ACCOUNT, BOOKING_READ) .input(BOOKING_UID_INPUT_SCHEMA) .output(calcomResponseDataSchema("GetBookingAttendeesOutput_2024_08_13")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request(calcomPath`bookings/${input.bookingUid}/attendees`, { apiVersion: BOOKING_ATTENDEE_API_VERSION, responseSchema: "GetBookingAttendeesOutput_2024_08_13", }) .then(unwrapCalcomData), ) /** Retrieves one booking attendee by ID. */ export const getCalcomBookingAttendee = defineAction( "Get Cal.com booking attendee", ) .describe("Retrieves one attendee attached to a booking.") .account(CALCOM_ACCOUNT, BOOKING_READ) .input( z.object({ attendeeId: CALCOM_ID_SCHEMA, bookingUid: CALCOM_UID_SCHEMA }), ) .output(calcomResponseDataSchema("GetBookingAttendeeOutput_2024_08_13")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request( calcomPath`bookings/${input.bookingUid}/attendees/${input.attendeeId}`, { apiVersion: BOOKING_ATTENDEE_API_VERSION, responseSchema: "GetBookingAttendeeOutput_2024_08_13", }, ) .then(unwrapCalcomData), ) /** Retrieves add-to-calendar links for one booking. */ export const getCalcomBookingCalendarLinks = defineAction( "Get Cal.com booking calendar links", ) .describe("Returns Google, Microsoft, Outlook, and ICS links for a booking.") .account(CALCOM_ACCOUNT, BOOKING_READ) .input(BOOKING_UID_INPUT_SCHEMA) .output(calcomResponseDataSchema("CalendarLinksOutput_2024_08_13")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request(calcomPath`bookings/${input.bookingUid}/calendar-links`, { apiVersion: BOOKING_API_VERSION, responseSchema: "CalendarLinksOutput_2024_08_13", }) .then(unwrapCalcomData), ) /** Retrieves provider references attached to one booking. */ export const getCalcomBookingReferences = defineAction( "Get Cal.com booking references", ) .describe("Returns calendar and conferencing references for a booking.") .account(CALCOM_ACCOUNT, BOOKING_READ) .input( BOOKING_UID_INPUT_SCHEMA.extend({ type: z .enum([ "google_calendar", "office365_calendar", "daily_video", "google_video", "office365_video", "zoom_video", ]) .optional(), }), ) .output(calcomResponseDataSchema("BookingReferencesOutput_2024_08_13")) .retry({ replaySafety: "safe" }) .handler(({ account, input: { bookingUid, ...query } }) => calcomApi(account) .request(calcomPath`bookings/${bookingUid}/references`, { apiVersion: BOOKING_API_VERSION, query, responseSchema: "BookingReferencesOutput_2024_08_13", }) .then(unwrapCalcomData), ) /** Retrieves Cal Video recordings for one booking. */ export const getCalcomBookingRecordings = defineAction( "Get Cal.com booking recordings", ) .describe("Returns the recordings available for a Cal Video booking.") .account(CALCOM_ACCOUNT, BOOKING_READ) .input(BOOKING_UID_INPUT_SCHEMA) .output(calcomResponseDataSchema("GetBookingRecordingsOutput")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request(calcomPath`bookings/${input.bookingUid}/recordings`, { apiVersion: BOOKING_API_VERSION, responseSchema: "GetBookingRecordingsOutput", }) .then(unwrapCalcomData), ) /** Retrieves Cal Video transcript download links for one booking. */ export const getCalcomBookingTranscripts = defineAction( "Get Cal.com booking transcripts", ) .describe( "Returns available transcript download links for a Cal Video booking.", ) .account(CALCOM_ACCOUNT, BOOKING_READ) .input(BOOKING_UID_INPUT_SCHEMA) .output(calcomResponseDataSchema("GetBookingTranscriptsOutput")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request(calcomPath`bookings/${input.bookingUid}/transcripts`, { apiVersion: BOOKING_API_VERSION, responseSchema: "GetBookingTranscriptsOutput", }) .then(unwrapCalcomData), ) /** Retrieves Cal Video meeting sessions for one booking. */ export const getCalcomBookingConferencingSessions = defineAction( "Get Cal.com booking conferencing sessions", ) .describe("Returns participant sessions recorded by Cal Video.") .account(CALCOM_ACCOUNT, BOOKING_READ) .input(BOOKING_UID_INPUT_SCHEMA) .output(calcomResponseDataSchema("GetBookingVideoSessionsOutput")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request(calcomPath`bookings/${input.bookingUid}/conferencing-sessions`, { apiVersion: BOOKING_API_VERSION, responseSchema: "GetBookingVideoSessionsOutput", }) .then(unwrapCalcomData), ) /** Retrieves the routing decision trace for one booking. */ export const getCalcomBookingRoutingTrace = defineAction( "Get Cal.com booking routing trace", ) .describe( "Returns the routing-form, CRM, and host-selection trace for a booking.", ) .account(CALCOM_ACCOUNT, BOOKING_READ) .input(BOOKING_UID_INPUT_SCHEMA) .output(calcomResponseDataSchema("GetBookingRoutingTraceOutput_2024_08_13")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request(calcomPath`bookings/${input.bookingUid}/routing-trace`, { apiVersion: BOOKING_ATTENDEE_API_VERSION, responseSchema: "GetBookingRoutingTraceOutput_2024_08_13", }) .then(unwrapCalcomData), )