import { calcomResponseDataSchema, calcomSchema, unwrapCalcomData, } from "@automate.ax/integration-contracts/calcom" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { CALCOM_ACCOUNT, calcomAccountRequirement, calcomApi } from "./lib" const PROFILE_READ = calcomAccountRequirement() const PROFILE_WRITE = calcomAccountRequirement() /** Retrieves the connected Cal.com profile. */ export const getCalcomProfile = defineAction("Get Cal.com profile") .describe( "Returns the connected user's identity, preferences, and profile metadata.", ) .account(CALCOM_ACCOUNT, PROFILE_READ) .input(z.object({})) .output(calcomResponseDataSchema("GetMeOutput")) .retry({ replaySafety: "safe" }) .handler(({ account }) => calcomApi(account) .request("me", { responseSchema: "GetMeOutput" }) .then(unwrapCalcomData), ) /** Updates the connected Cal.com profile. */ export const updateCalcomProfile = defineAction("Update Cal.com profile") .describe( "Updates supported identity, locale, time-zone, and profile settings.", ) .account(CALCOM_ACCOUNT, PROFILE_WRITE) .input(calcomSchema("UpdateManagedUserInput")) .output(calcomResponseDataSchema("UpdateMeOutput")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request("me", { body: input, method: "PATCH", responseSchema: "UpdateMeOutput", }) .then(unwrapCalcomData), ) /** Retrieves the connected user's booking frequency and duration limits. */ export const getCalcomBookingLimits = defineAction("Get Cal.com booking limits") .describe( "Returns booking frequency and duration limits for the connected user.", ) .account(CALCOM_ACCOUNT, PROFILE_READ) .input(z.object({})) .output(calcomResponseDataSchema("GetBookingLimitsOutput")) .retry({ replaySafety: "safe" }) .handler(({ account }) => calcomApi(account) .request("me/booking-limits", { responseSchema: "GetBookingLimitsOutput", }) .then(unwrapCalcomData), ) /** Replaces the connected user's booking frequency and duration limits. */ export const updateCalcomBookingLimits = defineAction( "Update Cal.com booking limits", ) .describe( "Sets booking frequency and duration limits for the connected user.", ) .account(CALCOM_ACCOUNT, PROFILE_WRITE) .input(calcomSchema("UpdateBookingLimitsInput")) .output(calcomResponseDataSchema("UpdateBookingLimitsOutput")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => calcomApi(account) .request("me/booking-limits", { body: input, method: "PATCH", responseSchema: "UpdateBookingLimitsOutput", }) .then(unwrapCalcomData), ) /** Removes every booking limit from the connected Cal.com profile. */ export const clearCalcomBookingLimits = defineAction( "Clear Cal.com booking limits", ) .describe( "Removes all booking frequency and duration limits for the connected user.", ) .account(CALCOM_ACCOUNT, PROFILE_WRITE) .input(z.object({})) .output(z.object({})) .retry({ replaySafety: "safe" }) .handler(async ({ account }) => { await calcomApi(account).requestVoid("me/booking-limits", { method: "DELETE", }) return {} })