import { CALENDLY_ORGANIZATION_MEMBERSHIP_SCHEMA, CALENDLY_ORGANIZATION_SCHEMA, CALENDLY_URI_SCHEMA, CALENDLY_USER_SCHEMA, CALENDLY_UUID_SCHEMA, } from "@automate.ax/integration-contracts/calendly" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { CALENDLY_ACCOUNT, CALENDLY_PAGE_INFO_SCHEMA, CALENDLY_PAGE_INPUT_SCHEMA, calendlyAccountRequirement, collectionResponse, compactCalendlyQuery, getCalendlyApi, resourceResponse, toPage, } from "./lib" const USER_ACCOUNT = calendlyAccountRequirement("users:read") const ORGANIZATION_ACCOUNT = calendlyAccountRequirement("organizations:read") export const getCurrentCalendlyUser = defineAction("Get current Calendly user") .describe( "Retrieves the connected Calendly user and current organization URI.", ) .account(CALENDLY_ACCOUNT, USER_ACCOUNT) .input(z.object({})) .output(CALENDLY_USER_SCHEMA) .retry({ replaySafety: "safe" }) .handler(({ account }) => getCalendlyApi(account) .request("users/me", { responseSchema: resourceResponse(CALENDLY_USER_SCHEMA), }) .then(({ resource }) => resource), ) export const getCalendlyUser = defineAction("Get Calendly user") .describe("Retrieves one Calendly user by UUID.") .account(CALENDLY_ACCOUNT, USER_ACCOUNT) .input(z.object({ userId: CALENDLY_UUID_SCHEMA })) .output(CALENDLY_USER_SCHEMA) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getCalendlyApi(account) .request(`users/${encodeURIComponent(input.userId)}`, { responseSchema: resourceResponse(CALENDLY_USER_SCHEMA), }) .then(({ resource }) => resource), ) export const getCalendlyOrganization = defineAction("Get Calendly organization") .describe( "Retrieves one Calendly organization, including its plan and stage.", ) .account(CALENDLY_ACCOUNT, ORGANIZATION_ACCOUNT) .input(z.object({ organizationId: CALENDLY_UUID_SCHEMA })) .output(CALENDLY_ORGANIZATION_SCHEMA) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getCalendlyApi(account) .request(`organizations/${encodeURIComponent(input.organizationId)}`, { responseSchema: resourceResponse(CALENDLY_ORGANIZATION_SCHEMA), }) .then(({ resource }) => resource), ) export const getCalendlyOrganizationMembership = defineAction( "Get Calendly organization membership", ) .describe("Retrieves one Calendly organization membership by UUID.") .account(CALENDLY_ACCOUNT, ORGANIZATION_ACCOUNT) .input(z.object({ membershipId: CALENDLY_UUID_SCHEMA })) .output(CALENDLY_ORGANIZATION_MEMBERSHIP_SCHEMA) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getCalendlyApi(account) .request( `organization_memberships/${encodeURIComponent(input.membershipId)}`, { responseSchema: resourceResponse( CALENDLY_ORGANIZATION_MEMBERSHIP_SCHEMA, ), }, ) .then(({ resource }) => resource), ) export const listCalendlyOrganizationMemberships = defineAction( "List Calendly organization memberships", ) .describe( "Lists Calendly members filtered by organization, user, email, or role.", ) .account(CALENDLY_ACCOUNT, ORGANIZATION_ACCOUNT) .input( CALENDLY_PAGE_INPUT_SCHEMA.extend({ email: z.email().optional(), organization: CALENDLY_URI_SCHEMA.optional(), role: z.enum(["admin", "owner", "user"]).optional(), user: CALENDLY_URI_SCHEMA.optional(), }), ) .output( z.object({ items: CALENDLY_ORGANIZATION_MEMBERSHIP_SCHEMA.array(), pageInfo: CALENDLY_PAGE_INFO_SCHEMA, }), ) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getCalendlyApi(account) .request("organization_memberships", { query: compactCalendlyQuery(input), responseSchema: collectionResponse( CALENDLY_ORGANIZATION_MEMBERSHIP_SCHEMA, ), }) .then(toPage), )