(ctx: SubscriptionContext)
| 25 | }, |
| 26 | |
| 27 | async createSubscription(ctx: SubscriptionContext): Promise<SubscriptionResult | undefined> { |
| 28 | try { |
| 29 | const providerConfig = getProviderConfig(ctx.webhook) |
| 30 | const { apiKey, organization, triggerId } = providerConfig as { |
| 31 | apiKey?: string |
| 32 | organization?: string |
| 33 | triggerId?: string |
| 34 | } |
| 35 | |
| 36 | if (!apiKey) { |
| 37 | logger.warn(`[${ctx.requestId}] Missing apiKey for Calendly webhook creation.`, { |
| 38 | webhookId: ctx.webhook.id, |
| 39 | }) |
| 40 | throw new Error( |
| 41 | 'Personal Access Token is required to create Calendly webhook. Please provide your Calendly Personal Access Token.' |
| 42 | ) |
| 43 | } |
| 44 | |
| 45 | if (!organization) { |
| 46 | logger.warn(`[${ctx.requestId}] Missing organization URI for Calendly webhook creation.`, { |
| 47 | webhookId: ctx.webhook.id, |
| 48 | }) |
| 49 | throw new Error( |
| 50 | 'Organization URI is required to create Calendly webhook. Please provide your Organization URI from the "Get Current User" operation.' |
| 51 | ) |
| 52 | } |
| 53 | |
| 54 | if (!triggerId) { |
| 55 | logger.warn(`[${ctx.requestId}] Missing triggerId for Calendly webhook creation.`, { |
| 56 | webhookId: ctx.webhook.id, |
| 57 | }) |
| 58 | throw new Error('Trigger ID is required to create Calendly webhook') |
| 59 | } |
| 60 | |
| 61 | const notificationUrl = getNotificationUrl(ctx.webhook) |
| 62 | |
| 63 | const eventTypeMap: Record<string, string[]> = { |
| 64 | calendly_invitee_created: ['invitee.created'], |
| 65 | calendly_invitee_canceled: ['invitee.canceled'], |
| 66 | calendly_routing_form_submitted: ['routing_form_submission.created'], |
| 67 | calendly_webhook: [ |
| 68 | 'invitee.created', |
| 69 | 'invitee.canceled', |
| 70 | 'routing_form_submission.created', |
| 71 | ], |
| 72 | } |
| 73 | |
| 74 | const events = eventTypeMap[triggerId] || ['invitee.created'] |
| 75 | |
| 76 | const calendlyApiUrl = 'https://api.calendly.com/webhook_subscriptions' |
| 77 | |
| 78 | const requestBody = { |
| 79 | url: notificationUrl, |
| 80 | events, |
| 81 | organization, |
| 82 | scope: 'organization', |
| 83 | } |
| 84 |
nothing calls this directly
no test coverage detected