import { integrationScope } from "../types" import type { IntegrationAccountRequirement, TriggerDefinition, TriggerPresentationContext, } from "../types" const WEBHOOK_SCOPE = "webhooks:write" const SCHEDULED_SCOPE = integrationScope.and( WEBHOOK_SCOPE, "scheduled_events:read", ) const EVENT_TYPE_SCOPE = integrationScope.and(WEBHOOK_SCOPE, "event_types:read") const ROUTING_SCOPE = integrationScope.and(WEBHOOK_SCOPE, "routing_forms:read") const ALL_SCOPE = integrationScope.and( WEBHOOK_SCOPE, "event_types:read", "routing_forms:read", "scheduled_events:read", ) export const calendlyTriggerDefinitions = { calendlyEventTypeCreated: definition( "calendly.event-type.created", EVENT_TYPE_SCOPE, ), calendlyEventTypeDeleted: definition( "calendly.event-type.deleted", EVENT_TYPE_SCOPE, ), calendlyEventTypeUpdated: definition( "calendly.event-type.updated", EVENT_TYPE_SCOPE, ), calendlyInviteeCanceled: definition( "calendly.invitee.canceled", SCHEDULED_SCOPE, ), calendlyInviteeCreated: definition( "calendly.invitee.created", SCHEDULED_SCOPE, ), calendlyInviteeNoShowCreated: definition( "calendly.invitee-no-show.created", SCHEDULED_SCOPE, ), calendlyInviteeNoShowDeleted: definition( "calendly.invitee-no-show.deleted", SCHEDULED_SCOPE, ), calendlyRoutingFormSubmissionCreated: definition( "calendly.routing-form-submission.created", ROUTING_SCOPE, ), calendlySchedulingEvent: definition("calendly.scheduling.event", ALL_SCOPE), } as const satisfies Record /** * Builds a Calendly trigger catalog definition. * * @param type - Public event type. * @param requiredScope - Combined OAuth scope requirement. */ function definition( type: TType, requiredScope: Parameters[number], ) { return { account: { connectionOptions: [ { connectionMethodId: "oauth", requiredScopes: [requiredScope] }, ], serviceId: "calendly", } as const satisfies IntegrationAccountRequirement, getPrimary: calendlyScopePrimary, type, } as const } /** * Presents the provider webhook scope as a trigger identity. * * @param context - Trigger presentation context. * @throws When the stored trigger configuration lacks a scope. */ function calendlyScopePrimary(context: TriggerPresentationContext) { if ( typeof context.config !== "object" || context.config === null || !("scope" in context.config) || typeof context.config.scope !== "string" ) { throw new TypeError("Calendly trigger configuration requires a scope.") } return { copyable: false, value: context.config.scope } }