import { calcomResponseDataSchema, calcomSchema, unwrapCalcomData, } from "@automate.ax/integration-contracts/calcom" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { BOOKING_ATTENDEE_API_VERSION, CALCOM_ACCOUNT, CALCOM_DATE_TIME_SCHEMA, CALCOM_ID_SCHEMA, EVENT_TYPE_API_VERSION, SCHEDULE_API_VERSION, calcomAccountRequirement, calcomApi, calcomPath, compact, } from "./lib" const TEAM_PROFILE_READ = calcomAccountRequirement() const TEAM_PROFILE_WRITE = calcomAccountRequirement() const TEAM_BOOKING_READ = calcomAccountRequirement() const TEAM_EVENT_TYPE_READ = calcomAccountRequirement() const TEAM_EVENT_TYPE_WRITE = calcomAccountRequirement() const TEAM_SCHEDULE_READ = calcomAccountRequirement() const TEAM_ID_SCHEMA = z.object({ teamId: CALCOM_ID_SCHEMA }) const TEAM_EVENT_TYPE_ID_SCHEMA = TEAM_ID_SCHEMA.extend({ eventTypeId: CALCOM_ID_SCHEMA, }) const TEAM_BOOKING_STATUS_SCHEMA = z.enum([ "cancelled", "past", "recurring", "unconfirmed", "upcoming", ]) const TEAM_BOOKING_SORT_SCHEMA = z.enum(["asc", "desc"]) const TEAM_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("PaginationMetaDto"), }) /** Creates a Cal.com team. */ export const createCalcomTeam = defineAction("Create Cal.com team") .describe("Creates a team in the connected user's organization.") .account(CALCOM_ACCOUNT, TEAM_PROFILE_WRITE) .input(calcomSchema("CreateTeamInput")) .output(calcomResponseDataSchema("CreateTeamOutput")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input }) => calcomApi(account) .request("teams", { body: input, method: "POST", responseSchema: "CreateTeamOutput", }) .then(unwrapCalcomData), ) /** Lists Cal.com teams visible to the connected user. */ export const listCalcomTeams = defineAction("List Cal.com teams") .describe("Lists teams the connected user can access.") .account(CALCOM_ACCOUNT, TEAM_PROFILE_READ) .input(z.object({})) .output(calcomResponseDataSchema("GetTeamsOutput")) .retry({ replaySafety: "safe" }) .handler(({ account }) => calcomApi(account) .request("teams", { responseSchema: "GetTeamsOutput" }) .then(unwrapCalcomData), ) /** Retrieves one Cal.com team. */ export const getCalcomTeam = defineAction("Get Cal.com team") .describe("Retrieves one team by numeric ID.") .account(CALCOM_ACCOUNT, TEAM_PROFILE_READ) .input(TEAM_ID_SCHEMA) .output(calcomResponseDataSchema("GetTeamOutput")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request(calcomPath`teams/${input.teamId}`, { responseSchema: "GetTeamOutput", }) .then(unwrapCalcomData), ) /** Updates one Cal.com team. */ export const updateCalcomTeam = defineAction("Update Cal.com team") .describe("Updates supported profile and scheduling settings for a team.") .account(CALCOM_ACCOUNT, TEAM_PROFILE_WRITE) .input(TEAM_ID_SCHEMA.and(calcomSchema("UpdateOrgTeamDto"))) .output(calcomResponseDataSchema("UpdateTeamOutput")) .retry({ replaySafety: "safe" }) .handler(({ account, input: { teamId, ...body } }) => calcomApi(account) .request(calcomPath`teams/${teamId}`, { body, method: "PATCH", responseSchema: "UpdateTeamOutput", }) .then(unwrapCalcomData), ) /** Deletes one Cal.com team. */ export const deleteCalcomTeam = defineAction("Delete Cal.com team") .describe( "Permanently deletes one team and its team-owned scheduling resources.", ) .account(CALCOM_ACCOUNT, TEAM_PROFILE_WRITE) .input(TEAM_ID_SCHEMA) .output(calcomResponseDataSchema("OrgTeamOutputResponseDto")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request(calcomPath`teams/${input.teamId}`, { method: "DELETE", responseSchema: "OrgTeamOutputResponseDto", }) .then(unwrapCalcomData), ) /** Lists bookings owned by one Cal.com team. */ export const listCalcomTeamBookings = defineAction("List Cal.com team bookings") .describe("Lists a team's bookings with lifecycle and time filters.") .account(CALCOM_ACCOUNT, TEAM_BOOKING_READ) .input( TEAM_ID_SCHEMA.extend({ 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: z.string().trim().min(1).optional(), eventTypeId: CALCOM_ID_SCHEMA.optional(), eventTypeIds: CALCOM_ID_SCHEMA.array().min(1).optional(), skip: z.number().int().nonnegative().prefault(0), sortCreated: TEAM_BOOKING_SORT_SCHEMA.optional(), sortEnd: TEAM_BOOKING_SORT_SCHEMA.optional(), sortStart: TEAM_BOOKING_SORT_SCHEMA.optional(), sortUpdatedAt: TEAM_BOOKING_SORT_SCHEMA.optional(), status: TEAM_BOOKING_STATUS_SCHEMA.array().min(1).optional(), take: z.number().int().min(1).max(250).prefault(100), }), ) .output(TEAM_BOOKING_PAGE_SCHEMA) .retry({ replaySafety: "safe" }) .handler( async ({ account, input: { eventTypeIds, status, teamId, ...query } }) => { const response = await calcomApi(account).request( calcomPath`teams/${teamId}/bookings`, { apiVersion: BOOKING_ATTENDEE_API_VERSION, query: compact({ ...query, eventTypeIds: eventTypeIds?.join(","), status: status?.join(","), }), responseSchema: "GetBookingsOutput_2024_08_13", }, ) return { items: response.data, pagination: response.pagination } }, ) /** Lists availability schedules for one Cal.com team. */ export const listCalcomTeamSchedules = defineAction( "List Cal.com team schedules", ) .describe("Lists availability schedules owned by members of one team.") .account(CALCOM_ACCOUNT, TEAM_SCHEDULE_READ) .input( TEAM_ID_SCHEMA.extend({ skip: z.number().int().nonnegative().prefault(0), take: z.number().int().positive().max(250).prefault(250), }), ) .output(calcomResponseDataSchema("GetSchedulesOutput_2024_06_11")) .retry({ replaySafety: "safe" }) .handler(({ account, input: { teamId, ...query } }) => calcomApi(account) .request(calcomPath`teams/${teamId}/schedules`, { apiVersion: SCHEDULE_API_VERSION, query, responseSchema: "GetSchedulesOutput_2024_06_11", }) .then(unwrapCalcomData), ) /** Creates a team-owned Cal.com event type. */ export const createCalcomTeamEventType = defineAction( "Create Cal.com team event type", ) .describe( "Creates a round-robin, collective, or managed event type for one team.", ) .account(CALCOM_ACCOUNT, TEAM_EVENT_TYPE_WRITE) .input( TEAM_ID_SCHEMA.and(calcomSchema("CreateTeamEventTypeInput_2026_06_12")), ) .output(calcomResponseDataSchema("CreateTeamEventTypeOutput_2026_06_12")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input: { teamId, ...body } }) => calcomApi(account) .request(calcomPath`teams/${teamId}/event-types`, { apiVersion: EVENT_TYPE_API_VERSION, body, method: "POST", responseSchema: "CreateTeamEventTypeOutput_2026_06_12", }) .then(unwrapCalcomData), ) /** Lists team-owned Cal.com event types. */ export const listCalcomTeamEventTypes = defineAction( "List Cal.com team event types", ) .describe("Lists every event type owned by one team.") .account(CALCOM_ACCOUNT, TEAM_EVENT_TYPE_READ) .input( TEAM_ID_SCHEMA.extend({ eventSlug: z.string().trim().min(1).optional(), hostsLimit: z.number().int().positive().optional(), sortCreatedAt: z.enum(["asc", "desc"]).optional(), }), ) .output(calcomResponseDataSchema("GetTeamEventTypesOutput_2026_06_12")) .retry({ replaySafety: "safe" }) .handler(({ account, input: { teamId, ...query } }) => calcomApi(account) .request(calcomPath`teams/${teamId}/event-types`, { apiVersion: EVENT_TYPE_API_VERSION, query, responseSchema: "GetTeamEventTypesOutput_2026_06_12", }) .then(unwrapCalcomData), ) /** Retrieves one team-owned Cal.com event type. */ export const getCalcomTeamEventType = defineAction( "Get Cal.com team event type", ) .describe("Retrieves one team-owned event type by numeric ID.") .account(CALCOM_ACCOUNT, TEAM_EVENT_TYPE_READ) .input(TEAM_EVENT_TYPE_ID_SCHEMA) .output(calcomResponseDataSchema("GetTeamEventTypeOutput_2026_06_12")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request( calcomPath`teams/${input.teamId}/event-types/${input.eventTypeId}`, { apiVersion: EVENT_TYPE_API_VERSION, responseSchema: "GetTeamEventTypeOutput_2026_06_12", }, ) .then(unwrapCalcomData), ) /** Updates one team-owned Cal.com event type. */ export const updateCalcomTeamEventType = defineAction( "Update Cal.com team event type", ) .describe("Updates hosts, scheduling, booking, and availability settings.") .account(CALCOM_ACCOUNT, TEAM_EVENT_TYPE_WRITE) .input( TEAM_EVENT_TYPE_ID_SCHEMA.and( calcomSchema("UpdateTeamEventTypeInput_2026_06_12"), ), ) .output(calcomResponseDataSchema("UpdateTeamEventTypeOutput_2026_06_12")) .retry({ replaySafety: "safe" }) .handler(({ account, input: { eventTypeId, teamId, ...body } }) => calcomApi(account) .request(calcomPath`teams/${teamId}/event-types/${eventTypeId}`, { apiVersion: EVENT_TYPE_API_VERSION, body, method: "PATCH", responseSchema: "UpdateTeamEventTypeOutput_2026_06_12", }) .then(unwrapCalcomData), ) /** Deletes one team-owned Cal.com event type. */ export const deleteCalcomTeamEventType = defineAction( "Delete Cal.com team event type", ) .describe("Permanently deletes one team-owned event type.") .account(CALCOM_ACCOUNT, TEAM_EVENT_TYPE_WRITE) .input(TEAM_EVENT_TYPE_ID_SCHEMA) .output(calcomResponseDataSchema("DeleteTeamEventTypeOutput")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request( calcomPath`teams/${input.teamId}/event-types/${input.eventTypeId}`, { apiVersion: EVENT_TYPE_API_VERSION, method: "DELETE", responseSchema: "DeleteTeamEventTypeOutput", }, ) .then(unwrapCalcomData), )