({
webhook,
workflow,
userId,
requestId,
request,
}: SubscriptionContext)
| 568 | }, |
| 569 | |
| 570 | async createSubscription({ |
| 571 | webhook, |
| 572 | workflow, |
| 573 | userId, |
| 574 | requestId, |
| 575 | request, |
| 576 | }: SubscriptionContext): Promise<SubscriptionResult | undefined> { |
| 577 | const config = getProviderConfig(webhook) |
| 578 | |
| 579 | if (config.triggerId !== 'microsoftteams_chat_subscription') { |
| 580 | return undefined |
| 581 | } |
| 582 | |
| 583 | const credentialId = config.credentialId as string | undefined |
| 584 | const chatId = config.chatId as string | undefined |
| 585 | |
| 586 | if (!credentialId) { |
| 587 | logger.warn(`[${requestId}] Missing credentialId for Teams chat subscription ${webhook.id}`) |
| 588 | throw new Error( |
| 589 | 'Microsoft Teams credentials are required. Please connect your Microsoft account in the trigger configuration.' |
| 590 | ) |
| 591 | } |
| 592 | |
| 593 | if (!chatId) { |
| 594 | logger.warn(`[${requestId}] Missing chatId for Teams chat subscription ${webhook.id}`) |
| 595 | throw new Error( |
| 596 | 'Chat ID is required to create a Teams subscription. Please provide a valid chat ID.' |
| 597 | ) |
| 598 | } |
| 599 | |
| 600 | const credentialOwner = await getCredentialOwner(credentialId, requestId) |
| 601 | const accessToken = credentialOwner |
| 602 | ? await refreshAccessTokenIfNeeded( |
| 603 | credentialOwner.accountId, |
| 604 | credentialOwner.userId, |
| 605 | requestId |
| 606 | ) |
| 607 | : null |
| 608 | if (!accessToken) { |
| 609 | logger.error(`[${requestId}] Failed to get access token for Teams subscription ${webhook.id}`) |
| 610 | throw new Error( |
| 611 | 'Failed to authenticate with Microsoft Teams. Please reconnect your Microsoft account and try again.' |
| 612 | ) |
| 613 | } |
| 614 | |
| 615 | const existingSubscriptionId = config.externalSubscriptionId as string | undefined |
| 616 | if (existingSubscriptionId) { |
| 617 | try { |
| 618 | const checkRes = await fetch( |
| 619 | `https://graph.microsoft.com/v1.0/subscriptions/${existingSubscriptionId}`, |
| 620 | { method: 'GET', headers: { Authorization: `Bearer ${accessToken}` } } |
| 621 | ) |
| 622 | if (checkRes.ok) { |
| 623 | logger.info( |
| 624 | `[${requestId}] Teams subscription ${existingSubscriptionId} already exists for webhook ${webhook.id}` |
| 625 | ) |
| 626 | return { providerConfigUpdates: { externalSubscriptionId: existingSubscriptionId } } |
| 627 | } |
nothing calls this directly
no test coverage detected