The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Add a connection health check to the Microsoft Teams app (#28568)
nitin0dbb141ef473
diff --git a/packages/twenty-apps/public/teams/src/constants/universal-identifiers.ts b/packages/twenty-apps/public/teams/src/constants/universal-identifiers.tsindex deff8ae026f..1f0be11b127 100644--- a/packages/twenty-apps/public/teams/src/constants/universal-identifiers.ts+++ b/packages/twenty-apps/public/teams/src/constants/universal-identifiers.ts@@ -6,3 +6,6 @@ export const DEFAULT_ROLE_UNIVERSAL_IDENTIFIER =export const SETTINGS_FRONT_COMPONENT_UNIVERSAL_IDENTIFIER ='2e97c12f-0f64-40aa-99ca-39b8a0a98e86';++export const HEALTH_CHECK_UNIVERSAL_IDENTIFIER =+ 'a7955ac8-3333-486d-81b4-ef6106d0113f';diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/enqueue-teams-assistant-request.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/enqueue-teams-assistant-request.tsindex 1b87194d362..6203b3bde7f 100644--- a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/enqueue-teams-assistant-request.ts+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/enqueue-teams-assistant-request.ts@@ -1,18 +1,13 @@-import { RELEASE_FLAGS } from 'src/constants/release-flags';-import { CHAT_ENABLED_APPLICATION_VARIABLE_KEY } from 'src/features/chat/constants/chat-enabled-application-variable-key';import { type TeamsActivitiesDispatchPayload } from 'src/features/chat/logic-functions/types/teams-activities-dispatch-payload.type';import { type TeamsActivitiesEnqueueResult } from 'src/features/chat/logic-functions/types/teams-activities-enqueue-result.type';import { enqueueTeamsAssistantRequestRecord } from 'src/features/chat/logic-functions/utils/enqueue-teams-assistant-request-record';+import { isTeamsChatEnabled } from 'src/features/chat/logic-functions/utils/is-teams-chat-enabled';import { parseTeamsAssistantRequest } from 'src/features/chat/logic-functions/utils/parse-teams-assistant-request';-import { isFeatureEnabled } from 'src/utils/is-feature-enabled';export const enqueueTeamsAssistantRequest = async (payload: TeamsActivitiesDispatchPayload,): Promise<TeamsActivitiesEnqueueResult> => {- const isChatEnabled = isFeatureEnabled({- isAvailable: RELEASE_FLAGS.IS_CHAT_ASSISTANT_RELEASED,- settingValue: process.env[CHAT_ENABLED_APPLICATION_VARIABLE_KEY],- });+ const isChatEnabled = isTeamsChatEnabled();if (!isChatEnabled) {return { ok: true, skipped: 'Chat is disabled' };diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/is-teams-chat-enabled.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/is-teams-chat-enabled.tsnew file mode 100644index 00000000000..f3d2b60dfbd--- /dev/null+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/is-teams-chat-enabled.ts@@ -0,0 +1,9 @@+import { RELEASE_FLAGS } from 'src/constants/release-flags';+import { CHAT_ENABLED_APPLICATION_VARIABLE_KEY } from 'src/features/chat/constants/chat-enabled-application-variable-key';+import { isFeatureEnabled } from 'src/utils/is-feature-enabled';++export const isTeamsChatEnabled = (): boolean =>+ isFeatureEnabled({+ isAvailable: RELEASE_FLAGS.IS_CHAT_ASSISTANT_RELEASED,+ settingValue: process.env[CHAT_ENABLED_APPLICATION_VARIABLE_KEY],+ });diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/register-teams-connection.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/register-teams-connection.tsindex c88ba6d6917..c88c14cf8a2 100644--- a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/register-teams-connection.ts+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/register-teams-connection.ts@@ -1,23 +1,18 @@import { isNonEmptyString } from '@sniptt/guards';import { getConnection, kv } from 'twenty-sdk/logic-function';-import { RELEASE_FLAGS } from 'src/constants/release-flags';-import { CHAT_ENABLED_APPLICATION_VARIABLE_KEY } from 'src/features/chat/constants/chat-enabled-application-variable-key';import { buildTeamsConnectedAccountTenantKvKey } from 'src/features/chat/logic-functions/utils/build-teams-connected-account-tenant-kv-key';import { buildTeamsTenantKvKey } from 'src/features/chat/logic-functions/utils/build-teams-tenant-kv-key';import { fetchTeamsTenantId } from 'src/features/chat/logic-functions/utils/fetch-teams-tenant-id';+import { isTeamsChatEnabled } from 'src/features/chat/logic-functions/utils/is-teams-chat-enabled';import { releaseTeamsConnectionTenant } from 'src/features/chat/logic-functions/utils/release-teams-connection-tenant';-import { isFeatureEnabled } from 'src/utils/is-feature-enabled';export const registerTeamsConnection = async ({connectedAccountId,}: {connectedAccountId: string;}): Promise<{ claimedTenantId: string | null }> => {- const isChatEnabled = isFeatureEnabled({- isAvailable: RELEASE_FLAGS.IS_CHAT_ASSISTANT_RELEASED,- settingValue: process.env[CHAT_ENABLED_APPLICATION_VARIABLE_KEY],- });+ const isChatEnabled = isTeamsChatEnabled();const connection = isChatEnabled? await getConnection(connectedAccountId): undefined;diff --git a/packages/twenty-apps/public/teams/src/logic-functions/__tests__/teams-health-check.test.ts b/packages/twenty-apps/public/teams/src/logic-functions/__tests__/teams-health-check.test.tsnew file mode 100644index 00000000000..56e69f72868--- /dev/null+++ b/packages/twenty-apps/public/teams/src/logic-functions/__tests__/teams-health-check.test.ts@@ -0,0 +1,68 @@+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++import { TRANSCRIPTS_ENABLED_APPLICATION_VARIABLE_KEY } from 'src/features/transcripts/constants/transcripts-enabled-application-variable-key';+import { teamsHealthCheckHandler } from 'src/logic-functions/teams-health-check';++const { listConnectionsMock } = vi.hoisted(() => ({+ listConnectionsMock: vi.fn(),+}));++vi.mock('twenty-sdk/logic-function', async (importOriginal) => ({+ ...(await importOriginal<Record<string, unknown>>()),+ listConnections: listConnectionsMock,+}));++describe('teamsHealthCheckHandler', () => {+ beforeEach(() => {+ vi.clearAllMocks();+ vi.stubEnv(TRANSCRIPTS_ENABLED_APPLICATION_VARIABLE_KEY, 'true');+ listConnectionsMock.mockResolvedValue([]);+ });++ afterEach(() => {+ vi.unstubAllEnvs();+ });++ it('should report a missing connection with a button to the connections', async () => {+ await expect(teamsHealthCheckHandler()).resolves.toEqual({+ status: 'WARNING',+ title: 'Microsoft Teams is not connected',+ description: expect.stringContaining('No working Microsoft Teams'),+ action: { label: 'Add connection', location: '#general' },+ });++ expect(listConnectionsMock).toHaveBeenCalledWith({+ providerName: 'microsoft-teams',+ });+ });++ it('should report nothing once a working connection exists', async () => {+ listConnectionsMock.mockResolvedValue([+ { id: 'connection-1', authFailedAt: '2026-10-01T00:00:00.000Z' },+ { id: 'connection-2', authFailedAt: null },+ ]);++ await expect(teamsHealthCheckHandler()).resolves.toEqual({ status: 'OK' });+ });++ it('should ask to reconnect when every connection was rejected', async () => {+ listConnectionsMock.mockResolvedValue([+ { id: 'connection-1', authFailedAt: '2026-10-01T00:00:00.000Z' },+ ]);++ await expect(teamsHealthCheckHandler()).resolves.toEqual({+ status: 'ERROR',+ title: 'Microsoft Teams connection needs to be reconnected',+ description: expect.stringContaining('Microsoft rejected the stored'),+ action: { label: 'Manage connection', location: '#general' },+ });+ });++ it('should report nothing when no feature needs a connection', async () => {+ vi.stubEnv(TRANSCRIPTS_ENABLED_APPLICATION_VARIABLE_KEY, 'false');++ await expect(teamsHealthCheckHandler()).resolves.toEqual({ status: 'OK' });++ expect(listConnectionsMock).not.toHaveBeenCalled();+ });+});diff --git a/packages/twenty-apps/public/teams/src/logic-functions/teams-health-check.ts b/packages/twenty-apps/public/teams/src/logic-functions/teams-health-check.tsnew file mode 100644index 00000000000..061b5a63f71--- /dev/null+++ b/packages/twenty-apps/public/teams/src/logic-functions/teams-health-check.ts@@ -0,0 +1,54 @@+import { isNonEmptyArray } from '@sniptt/guards';+import {+ type ApplicationHealthCheckResult,+ defineHealthCheck,+} from 'twenty-sdk/define';+import { listConnections } from 'twenty-sdk/logic-function';+import { isDefined } from 'twenty-sdk/utils';++import { HEALTH_CHECK_UNIVERSAL_IDENTIFIER } from 'src/constants/universal-identifiers';+import { isTeamsChatEnabled } from 'src/features/chat/logic-functions/utils/is-teams-chat-enabled';+import { TEAMS_PROVIDER_NAME } from 'src/features/transcripts/constants/teams-provider-name';+import { isTeamsTranscriptImportEnabled } from 'src/features/transcripts/logic-functions/utils/is-teams-transcript-import-enabled';++export const teamsHealthCheckHandler =+ async (): Promise<ApplicationHealthCheckResult> => {+ if (!isTeamsChatEnabled() && !isTeamsTranscriptImportEnabled()) {+ return { status: 'OK' };+ }++ const connections = await listConnections({+ providerName: TEAMS_PROVIDER_NAME,+ });++ if (!isNonEmptyArray(connections)) {+ return {+ status: 'WARNING',+ title: 'Microsoft Teams is not connected',+ description:+ 'No working Microsoft Teams connection was found in this workspace, so the app cannot reach Teams. Add a connection to start importing meeting transcripts.',+ action: { label: 'Add connection', location: '#general' },+ };+ }++ if (connections.every((connection) => isDefined(connection.authFailedAt))) {+ return {+ status: 'ERROR',+ title: 'Microsoft Teams connection needs to be reconnected',+ description:+ 'Microsoft rejected the stored credentials, so meeting transcripts are no longer imported. Remove the Microsoft Teams connection and add it again.',+ action: { label: 'Manage connection', location: '#general' },+ };+ }++ return { status: 'OK' };+ };++export default defineHealthCheck({+ universalIdentifier: HEALTH_CHECK_UNIVERSAL_IDENTIFIER,+ name: 'teams-health-check',+ description:+ 'Warns on the app settings page when no Microsoft Teams account is connected or every connection needs to be reconnected, since transcripts and chat both rely on a connection.',+ timeoutSeconds: 30,+ handler: teamsHealthCheckHandler,+});