Skip to content

The open alternative to Salesforce, designed for AI.

feat(teams): claim the Microsoft tenant for the workspace on connect (#27003)

Abdul Rahman0d46a05498a6
diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/constants/teams-connected-account-tenant-kv-key-prefix.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/constants/teams-connected-account-tenant-kv-key-prefix.ts
new file mode 100644
index 00000000000..bd072503f4e
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/constants/teams-connected-account-tenant-kv-key-prefix.ts
@@ -0,0 +1,2 @@
+export const TEAMS_CONNECTED_ACCOUNT_TENANT_KV_KEY_PREFIX =
+ 'teams-connected-account-tenant';
diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/constants/teams-tenant-kv-key-prefix.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/constants/teams-tenant-kv-key-prefix.ts
new file mode 100644
index 00000000000..89de6028ccb
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/constants/teams-tenant-kv-key-prefix.ts
@@ -0,0 +1 @@
+export const TEAMS_TENANT_KV_KEY_PREFIX = 'teams-tenant';
diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/teams-register-connection.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/teams-register-connection.ts
new file mode 100644
index 00000000000..0fee3bb9d41
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/teams-register-connection.ts
@@ -0,0 +1,13 @@
+import { defineLogicFunction } from 'twenty-sdk/define';
+
+import { TEAMS_REGISTER_CONNECTION_UNIVERSAL_IDENTIFIER } from 'src/features/chat/constants/universal-identifiers';
+import { registerTeamsConnection } from 'src/features/chat/logic-functions/utils/register-teams-connection';
+
+export default defineLogicFunction({
+ universalIdentifier: TEAMS_REGISTER_CONNECTION_UNIVERSAL_IDENTIFIER,
+ name: 'teams-register-connection',
+ description:
+ 'Runs when a Microsoft Teams connection is added. For a workspace-shared connection with chat enabled, reads the Microsoft tenant id from Graph and claims it for this workspace under the server-scoped teams-tenant:<tenant_id> key, so activities from that tenant route here. Personal connections are left alone.',
+ timeoutSeconds: 30,
+ handler: registerTeamsConnection,
+});
diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/teams-tenant-release.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/teams-tenant-release.ts
new file mode 100644
index 00000000000..d8427c32f89
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/teams-tenant-release.ts
@@ -0,0 +1,13 @@
+import { defineLogicFunction } from 'twenty-sdk/define';
+
+import { TEAMS_TENANT_RELEASE_UNIVERSAL_IDENTIFIER } from 'src/features/chat/constants/universal-identifiers';
+import { releaseTeamsConnectionTenant } from 'src/features/chat/logic-functions/utils/release-teams-connection-tenant';
+
+export default defineLogicFunction({
+ universalIdentifier: TEAMS_TENANT_RELEASE_UNIVERSAL_IDENTIFIER,
+ name: 'teams-tenant-release',
+ description:
+ 'Runs when a Microsoft Teams connection is removed. Releases the teams-tenant:<tenant_id> claim that connection took, unless another connection in this workspace still holds the same tenant, so another workspace can connect it.',
+ timeoutSeconds: 30,
+ handler: releaseTeamsConnectionTenant,
+});
diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/__tests__/register-teams-connection.test.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/__tests__/register-teams-connection.test.ts
new file mode 100644
index 00000000000..ce3751746c7
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/__tests__/register-teams-connection.test.ts
@@ -0,0 +1,130 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+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 { registerTeamsConnection } from 'src/features/chat/logic-functions/utils/register-teams-connection';
+
+const {
+ featureFlags,
+ kvGetMock,
+ kvSetMock,
+ getConnectionMock,
+ fetchGraphJsonMock,
+ releaseMock,
+} = vi.hoisted(() => ({
+ featureFlags: { IS_CHAT_ASSISTANT_ENABLED: true },
+ kvGetMock: vi.fn(),
+ kvSetMock: vi.fn(),
+ getConnectionMock: vi.fn(),
+ fetchGraphJsonMock: vi.fn(),
+ releaseMock: vi.fn(),
+}));
+
+vi.mock('src/constants/feature-flags', () => ({
+ FEATURE_FLAGS: featureFlags,
+}));
+
+vi.mock('twenty-sdk/logic-function', () => ({
+ kv: { get: kvGetMock, set: kvSetMock },
+ getConnection: getConnectionMock,
+}));
+
+vi.mock(
+ 'src/features/transcripts/logic-functions/utils/fetch-graph-json',
+ () => ({ fetchGraphJson: fetchGraphJsonMock }),
+);
+
+vi.mock(
+ 'src/features/chat/logic-functions/utils/release-teams-connection-tenant',
+ () => ({ releaseTeamsConnectionTenant: releaseMock }),
+);
+
+const CONNECTED_ACCOUNT_ID = 'connected-account-id';
+const TENANT_ID = 'tenant-id';
+
+describe('registerTeamsConnection', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ featureFlags.IS_CHAT_ASSISTANT_ENABLED = true;
+ process.env[CHAT_ENABLED_APPLICATION_VARIABLE_KEY] = 'true';
+ getConnectionMock.mockResolvedValue({
+ visibility: 'workspace',
+ accessToken: 'graph-token',
+ });
+ fetchGraphJsonMock.mockResolvedValue({ value: [{ id: TENANT_ID }] });
+ kvGetMock.mockResolvedValue(null);
+ });
+
+ it('should claim the tenant of a workspace-shared connection and remember it for release', async () => {
+ expect(
+ await registerTeamsConnection({
+ connectedAccountId: CONNECTED_ACCOUNT_ID,
+ }),
+ ).toEqual({ claimedTenantId: TENANT_ID });
+ expect(fetchGraphJsonMock).toHaveBeenCalledWith({
+ accessToken: 'graph-token',
+ url: 'organization?$select=id',
+ });
+ expect(kvSetMock).toHaveBeenNthCalledWith(
+ 1,
+ buildTeamsConnectedAccountTenantKvKey(CONNECTED_ACCOUNT_ID),
+ TENANT_ID,
+ );
+ expect(kvSetMock).toHaveBeenNthCalledWith(
+ 2,
+ buildTeamsTenantKvKey(TENANT_ID),
+ null,
+ { scope: 'SERVER' },
+ );
+ });
+
+ it('should release the previous tenant when a reconnect signs in to another tenant', async () => {
+ kvGetMock.mockResolvedValue('previous-tenant-id');
+
+ expect(
+ await registerTeamsConnection({
+ connectedAccountId: CONNECTED_ACCOUNT_ID,
+ }),
+ ).toEqual({ claimedTenantId: TENANT_ID });
+ expect(releaseMock).toHaveBeenCalledWith({
+ connectedAccountId: CONNECTED_ACCOUNT_ID,
+ });
+ expect(releaseMock.mock.invocationCallOrder[0]).toBeLessThan(
+ kvSetMock.mock.invocationCallOrder[0],
+ );
+ });
+
+ it('should release the claim instead of claiming when the connection is personal', async () => {
+ getConnectionMock.mockResolvedValue({
+ visibility: 'user',
+ accessToken: 'graph-token',
+ });
+
+ expect(
+ await registerTeamsConnection({
+ connectedAccountId: CONNECTED_ACCOUNT_ID,
+ }),
+ ).toEqual({ claimedTenantId: null });
+ expect(releaseMock).toHaveBeenCalledWith({
+ connectedAccountId: CONNECTED_ACCOUNT_ID,
+ });
+ expect(fetchGraphJsonMock).not.toHaveBeenCalled();
+ expect(kvSetMock).not.toHaveBeenCalled();
+ });
+
+ it('should release the claim instead of claiming while chat is disabled for the workspace', async () => {
+ process.env[CHAT_ENABLED_APPLICATION_VARIABLE_KEY] = 'false';
+
+ expect(
+ await registerTeamsConnection({
+ connectedAccountId: CONNECTED_ACCOUNT_ID,
+ }),
+ ).toEqual({ claimedTenantId: null });
+ expect(releaseMock).toHaveBeenCalledWith({
+ connectedAccountId: CONNECTED_ACCOUNT_ID,
+ });
+ expect(getConnectionMock).not.toHaveBeenCalled();
+ expect(kvSetMock).not.toHaveBeenCalled();
+ });
+});
diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/__tests__/release-teams-connection-tenant.test.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/__tests__/release-teams-connection-tenant.test.ts
new file mode 100644
index 00000000000..e4dbc5bb337
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/__tests__/release-teams-connection-tenant.test.ts
@@ -0,0 +1,141 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+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 { releaseTeamsConnectionTenant } from 'src/features/chat/logic-functions/utils/release-teams-connection-tenant';
+
+const { kvStore, unreadableKvKeys, kvDeleteMock, listConnectionsMock } =
+ vi.hoisted(() => ({
+ kvStore: new Map<string, string | null>(),
+ unreadableKvKeys: new Set<string>(),
+ kvDeleteMock: vi.fn(),
+ listConnectionsMock: vi.fn(),
+ }));
+
+vi.mock('twenty-sdk/logic-function', () => ({
+ kv: {
+ get: async (key: string) => {
+ if (unreadableKvKeys.has(key)) {
+ throw new Error('kv unavailable');
+ }
+
+ return kvStore.get(key) ?? null;
+ },
+ set: async (key: string, value: string | null) => {
+ kvStore.set(key, value);
+ },
+ delete: async (key: string, options?: { scope: string }) => {
+ kvDeleteMock(key, options);
+
+ return kvStore.delete(key);
+ },
+ },
+ listConnections: listConnectionsMock,
+}));
+
+const TENANT_ID = 'tenant-id';
+
+describe('releaseTeamsConnectionTenant', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ kvStore.clear();
+ unreadableKvKeys.clear();
+ kvStore.set(buildTeamsConnectedAccountTenantKvKey('leaving'), TENANT_ID);
+ kvStore.set(buildTeamsTenantKvKey(TENANT_ID), null);
+ listConnectionsMock.mockResolvedValue([{ id: 'leaving' }]);
+ });
+
+ it('should release the tenant claim and forget the connection', async () => {
+ expect(
+ await releaseTeamsConnectionTenant({ connectedAccountId: 'leaving' }),
+ ).toEqual({ releasedTenantId: TENANT_ID });
+ expect(kvDeleteMock).toHaveBeenCalledWith(
+ buildTeamsTenantKvKey(TENANT_ID),
+ {
+ scope: 'SERVER',
+ },
+ );
+ expect(kvDeleteMock).toHaveBeenCalledWith(
+ buildTeamsConnectedAccountTenantKvKey('leaving'),
+ undefined,
+ );
+ });
+
+ it('should keep the tenant claimed while another connection in the workspace still holds it', async () => {
+ kvStore.set(buildTeamsConnectedAccountTenantKvKey('staying'), TENANT_ID);
+ listConnectionsMock.mockResolvedValue([
+ { id: 'leaving' },
+ { id: 'staying' },
+ ]);
+
+ expect(
+ await releaseTeamsConnectionTenant({ connectedAccountId: 'leaving' }),
+ ).toEqual({ releasedTenantId: null });
+ expect(kvDeleteMock).toHaveBeenCalledTimes(1);
+ expect(kvDeleteMock).toHaveBeenCalledWith(
+ buildTeamsConnectedAccountTenantKvKey('leaving'),
+ undefined,
+ );
+ });
+
+ it('should release the tenant when two connections holding it are released at the same time', async () => {
+ kvStore.set(
+ buildTeamsConnectedAccountTenantKvKey('also-leaving'),
+ TENANT_ID,
+ );
+ listConnectionsMock.mockResolvedValue([
+ { id: 'leaving' },
+ { id: 'also-leaving' },
+ ]);
+
+ const results = await Promise.all([
+ releaseTeamsConnectionTenant({ connectedAccountId: 'leaving' }),
+ releaseTeamsConnectionTenant({ connectedAccountId: 'also-leaving' }),
+ ]);
+
+ expect(kvDeleteMock).toHaveBeenCalledWith(
+ buildTeamsTenantKvKey(TENANT_ID),
+ { scope: 'SERVER' },
+ );
+ expect(results.map((result) => result.releasedTenantId)).toContain(
+ TENANT_ID,
+ );
+ });
+
+ it('should keep the connection tenant for a retry when listing connections fails', async () => {
+ listConnectionsMock.mockRejectedValue(new Error('refresh failed'));
+
+ await expect(
+ releaseTeamsConnectionTenant({ connectedAccountId: 'leaving' }),
+ ).rejects.toThrow('refresh failed');
+ expect(kvDeleteMock).not.toHaveBeenCalled();
+ });
+
+ it('should keep the connection tenant for a retry when reading other connections fails', async () => {
+ unreadableKvKeys.add(buildTeamsConnectedAccountTenantKvKey('staying'));
+ listConnectionsMock.mockResolvedValue([
+ { id: 'leaving' },
+ { id: 'staying' },
+ ]);
+
+ await expect(
+ releaseTeamsConnectionTenant({ connectedAccountId: 'leaving' }),
+ ).rejects.toThrow('kv unavailable');
+ expect(kvStore.get(buildTeamsConnectedAccountTenantKvKey('leaving'))).toBe(
+ TENANT_ID,
+ );
+ expect(kvStore.has(buildTeamsTenantKvKey(TENANT_ID))).toBe(true);
+ });
+
+ it('should claim the tenant again when a connection takes it while it is being released', async () => {
+ kvStore.set(buildTeamsConnectedAccountTenantKvKey('joining'), TENANT_ID);
+ listConnectionsMock
+ .mockResolvedValueOnce([{ id: 'leaving' }])
+ .mockResolvedValueOnce([{ id: 'leaving' }, { id: 'joining' }]);
+
+ expect(
+ await releaseTeamsConnectionTenant({ connectedAccountId: 'leaving' }),
+ ).toEqual({ releasedTenantId: null });
+ expect(kvStore.has(buildTeamsTenantKvKey(TENANT_ID))).toBe(true);
+ });
+});
diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/build-teams-connected-account-tenant-kv-key.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/build-teams-connected-account-tenant-kv-key.ts
new file mode 100644
index 00000000000..ac915072226
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/build-teams-connected-account-tenant-kv-key.ts
@@ -0,0 +1,6 @@
+import { TEAMS_CONNECTED_ACCOUNT_TENANT_KV_KEY_PREFIX } from 'src/features/chat/logic-functions/constants/teams-connected-account-tenant-kv-key-prefix';
+
+export const buildTeamsConnectedAccountTenantKvKey = (
+ connectedAccountId: string,
+): string =>
+ `${TEAMS_CONNECTED_ACCOUNT_TENANT_KV_KEY_PREFIX}:${connectedAccountId}`;
diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/build-teams-tenant-kv-key.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/build-teams-tenant-kv-key.ts
new file mode 100644
index 00000000000..3bb8324687a
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/build-teams-tenant-kv-key.ts
@@ -0,0 +1,4 @@
+import { TEAMS_TENANT_KV_KEY_PREFIX } from 'src/features/chat/logic-functions/constants/teams-tenant-kv-key-prefix';
+
+export const buildTeamsTenantKvKey = (tenantId: string): string =>
+ `${TEAMS_TENANT_KV_KEY_PREFIX}:${tenantId}`;
diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/fetch-teams-tenant-id.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/fetch-teams-tenant-id.ts
new file mode 100644
index 00000000000..339627eadf8
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/fetch-teams-tenant-id.ts
@@ -0,0 +1,20 @@
+import { isNonEmptyString } from '@sniptt/guards';
+
+import { type GraphCollectionPage } from 'src/features/transcripts/logic-functions/types/graph-collection-page.type';
+import { fetchGraphJson } from 'src/features/transcripts/logic-functions/utils/fetch-graph-json';
+
+export const fetchTeamsTenantId = async (
+ accessToken: string,
+): Promise<string> => {
+ const page = await fetchGraphJson<GraphCollectionPage<{ id?: unknown }>>({
+ accessToken,
+ url: 'organization?$select=id',
+ });
+ const tenantId = page.value?.[0]?.id;
+
+ if (!isNonEmptyString(tenantId)) {
+ throw new Error('Microsoft Graph returned no organization to claim');
+ }
+
+ return tenantId;
+};
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.ts
new file mode 100644
index 00000000000..688c5d0d28e
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/register-teams-connection.ts
@@ -0,0 +1,44 @@
+import { isNonEmptyString } from '@sniptt/guards';
+import { getConnection, kv } from 'twenty-sdk/logic-function';
+
+import { FEATURE_FLAGS } from 'src/constants/feature-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 { 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: FEATURE_FLAGS.IS_CHAT_ASSISTANT_ENABLED,
+ settingValue: process.env[CHAT_ENABLED_APPLICATION_VARIABLE_KEY],
+ });
+ const connection = isChatEnabled
+ ? await getConnection(connectedAccountId)
+ : undefined;
+
+ if (connection?.visibility !== 'workspace') {
+ await releaseTeamsConnectionTenant({ connectedAccountId });
+
+ return { claimedTenantId: null };
+ }
+
+ const tenantId = await fetchTeamsTenantId(connection.accessToken);
+ const connectedAccountTenantKvKey =
+ buildTeamsConnectedAccountTenantKvKey(connectedAccountId);
+ const previousTenantId = await kv.get<string>(connectedAccountTenantKvKey);
+
+ if (isNonEmptyString(previousTenantId) && previousTenantId !== tenantId) {
+ await releaseTeamsConnectionTenant({ connectedAccountId });
+ }
+
+ await kv.set(connectedAccountTenantKvKey, tenantId);
+ await kv.set(buildTeamsTenantKvKey(tenantId), null, { scope: 'SERVER' });
+
+ return { claimedTenantId: tenantId };
+};
diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/release-teams-connection-tenant.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/release-teams-connection-tenant.ts
new file mode 100644
index 00000000000..f3b392a61e0
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/release-teams-connection-tenant.ts
@@ -0,0 +1,73 @@
+import { isNonEmptyString } from '@sniptt/guards';
+import { kv, listConnections } from 'twenty-sdk/logic-function';
+
+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 { TEAMS_PROVIDER_NAME } from 'src/features/transcripts/constants/teams-provider-name';
+
+const listTeamsConnectionIds = async (): Promise<string[]> =>
+ (await listConnections({ providerName: TEAMS_PROVIDER_NAME })).map(
+ (connection) => connection.id,
+ );
+
+const isTenantHeldByConnections = async ({
+ tenantId,
+ connectionIds,
+}: {
+ tenantId: string;
+ connectionIds: string[];
+}): Promise<boolean> => {
+ const claimedTenantIds = await Promise.all(
+ connectionIds.map((connectionId) =>
+ kv.get<string>(buildTeamsConnectedAccountTenantKvKey(connectionId)),
+ ),
+ );
+
+ return claimedTenantIds.includes(tenantId);
+};
+
+export const releaseTeamsConnectionTenant = async ({
+ connectedAccountId,
+}: {
+ connectedAccountId: string;
+}): Promise<{ releasedTenantId: string | null }> => {
+ const connectedAccountTenantKvKey =
+ buildTeamsConnectedAccountTenantKvKey(connectedAccountId);
+ const tenantId = await kv.get<string>(connectedAccountTenantKvKey);
+
+ if (!isNonEmptyString(tenantId)) {
+ return { releasedTenantId: null };
+ }
+
+ const connectionIds = await listTeamsConnectionIds();
+
+ await kv.delete(connectedAccountTenantKvKey);
+
+ const isTenantHeldElsewhere = await isTenantHeldByConnections({
+ tenantId,
+ connectionIds,
+ }).catch(async (error: unknown) => {
+ await kv.set(connectedAccountTenantKvKey, tenantId);
+ throw error;
+ });
+
+ if (isTenantHeldElsewhere) {
+ return { releasedTenantId: null };
+ }
+
+ const tenantKvKey = buildTeamsTenantKvKey(tenantId);
+ const hasReleasedTenant = await kv.delete(tenantKvKey, { scope: 'SERVER' });
+
+ if (
+ await isTenantHeldByConnections({
+ tenantId,
+ connectionIds: await listTeamsConnectionIds(),
+ })
+ ) {
+ await kv.set(tenantKvKey, null, { scope: 'SERVER' });
+
+ return { releasedTenantId: null };
+ }
+
+ return { releasedTenantId: hasReleasedTenant ? tenantId : null };
+};
diff --git a/packages/twenty-apps/public/teams/src/features/transcripts/connection-providers/microsoft-teams-connection.ts b/packages/twenty-apps/public/teams/src/features/transcripts/connection-providers/microsoft-teams-connection.ts
index 0d9f81b0909..d5654c1c84b 100644
--- a/packages/twenty-apps/public/teams/src/features/transcripts/connection-providers/microsoft-teams-connection.ts
+++ b/packages/twenty-apps/public/teams/src/features/transcripts/connection-providers/microsoft-teams-connection.ts
@@ -1,5 +1,9 @@
import { defineConnectionProvider } from 'twenty-sdk/define';
+import {
+ TEAMS_REGISTER_CONNECTION_UNIVERSAL_IDENTIFIER,
+ TEAMS_TENANT_RELEASE_UNIVERSAL_IDENTIFIER,
+} from 'src/features/chat/constants/universal-identifiers';
import { TEAMS_PROVIDER_NAME } from 'src/features/transcripts/constants/teams-provider-name';
import { TEAMS_CONNECTION_PROVIDER_UNIVERSAL_IDENTIFIER } from 'src/features/transcripts/constants/universal-identifiers';
@@ -8,6 +12,12 @@ export default defineConnectionProvider({
name: TEAMS_PROVIDER_NAME,
displayName: 'Microsoft Teams',
type: 'oauth',
+ onConnectLogicFunction: {
+ universalIdentifier: TEAMS_REGISTER_CONNECTION_UNIVERSAL_IDENTIFIER,
+ },
+ onDisconnectLogicFunction: {
+ universalIdentifier: TEAMS_TENANT_RELEASE_UNIVERSAL_IDENTIFIER,
+ },
oauth: {
authorizationEndpoint:
'https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize',