({
webhook: webhookRecord,
workflow,
userId,
requestId,
}: SubscriptionContext)
| 98 | }, |
| 99 | |
| 100 | async createSubscription({ |
| 101 | webhook: webhookRecord, |
| 102 | workflow, |
| 103 | userId, |
| 104 | requestId, |
| 105 | }: SubscriptionContext): Promise<SubscriptionResult | undefined> { |
| 106 | try { |
| 107 | const { path, providerConfig } = webhookRecord as Record<string, unknown> |
| 108 | const config = (providerConfig as Record<string, unknown>) || {} |
| 109 | const { triggerId, credentialId } = config as { |
| 110 | triggerId?: string |
| 111 | credentialId?: string |
| 112 | } |
| 113 | |
| 114 | if (!credentialId) { |
| 115 | logger.warn(`[${requestId}] Missing credentialId for Attio webhook creation.`, { |
| 116 | webhookId: webhookRecord.id, |
| 117 | }) |
| 118 | throw new Error( |
| 119 | 'Attio account connection required. Please connect your Attio account in the trigger configuration and try again.' |
| 120 | ) |
| 121 | } |
| 122 | |
| 123 | const credentialOwner = await getCredentialOwner(credentialId, requestId) |
| 124 | const accessToken = credentialOwner |
| 125 | ? await refreshAccessTokenIfNeeded( |
| 126 | credentialOwner.accountId, |
| 127 | credentialOwner.userId, |
| 128 | requestId |
| 129 | ) |
| 130 | : null |
| 131 | |
| 132 | if (!accessToken) { |
| 133 | logger.warn( |
| 134 | `[${requestId}] Could not retrieve Attio access token for user ${userId}. Cannot create webhook.` |
| 135 | ) |
| 136 | throw new Error( |
| 137 | 'Attio account connection required. Please connect your Attio account in the trigger configuration and try again.' |
| 138 | ) |
| 139 | } |
| 140 | |
| 141 | const notificationUrl = `${getBaseUrl()}/api/webhooks/trigger/${path}` |
| 142 | |
| 143 | const { TRIGGER_EVENT_MAP } = await import('@/triggers/attio/utils') |
| 144 | |
| 145 | let subscriptions: Array<{ event_type: string; filter: null }> = [] |
| 146 | if (triggerId === 'attio_webhook') { |
| 147 | const allEvents = new Set<string>() |
| 148 | for (const events of Object.values(TRIGGER_EVENT_MAP)) { |
| 149 | for (const event of events) { |
| 150 | allEvents.add(event) |
| 151 | } |
| 152 | } |
| 153 | subscriptions = Array.from(allEvents).map((event_type) => ({ event_type, filter: null })) |
| 154 | } else { |
| 155 | const events = TRIGGER_EVENT_MAP[triggerId as string] |
| 156 | if (!events || events.length === 0) { |
| 157 | logger.warn(`[${requestId}] No event types mapped for trigger ${triggerId}`, { |
nothing calls this directly
no test coverage detected