Skip to content

The open alternative to Salesforce, designed for AI.

Add a route to remove Teams user links (#27909)

Abdul Rahman03c8881efdef
diff --git a/packages/twenty-apps/public/teams/src/features/chat/constants/teams-user-links-route-path.ts b/packages/twenty-apps/public/teams/src/features/chat/constants/teams-user-links-route-path.ts
new file mode 100644
index 00000000000..6612107d3bd
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/constants/teams-user-links-route-path.ts
@@ -0,0 +1 @@
+export const TEAMS_USER_LINKS_REMOVE_ROUTE_PATH = '/teams-user-links/remove';
diff --git a/packages/twenty-apps/public/teams/src/features/chat/constants/universal-identifiers.ts b/packages/twenty-apps/public/teams/src/features/chat/constants/universal-identifiers.ts
index 6ea9f19483f..9a03da34fb0 100644
--- a/packages/twenty-apps/public/teams/src/features/chat/constants/universal-identifiers.ts
+++ b/packages/twenty-apps/public/teams/src/features/chat/constants/universal-identifiers.ts
@@ -120,3 +120,6 @@ export const TEAMS_USER_LINK_TEAMS_USER_INDEX_TENANT_ID_FIELD_UNIVERSAL_IDENTIFI
export const TEAMS_USER_LINK_TEAMS_USER_INDEX_USER_AAD_OBJECT_ID_FIELD_UNIVERSAL_IDENTIFIER =
'9bdc1528-8d6a-45e9-9b2d-cca99adf0ff2';
+
+export const TEAMS_REMOVE_USER_LINK_UNIVERSAL_IDENTIFIER =
+ 'f1217618-192a-4c6c-a502-eeed70d05736';
diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/__tests__/teams-remove-user-link.test.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/__tests__/teams-remove-user-link.test.ts
new file mode 100644
index 00000000000..f18eb3fdc65
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/__tests__/teams-remove-user-link.test.ts
@@ -0,0 +1,80 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { teamsRemoveUserLinkHandler } from 'src/features/chat/logic-functions/teams-remove-user-link';
+
+const { coreApiClientMock, destroyTeamsUserLinkMock, hasRolesPermissionMock } =
+ vi.hoisted(() => ({
+ coreApiClientMock: vi.fn(function () {
+ return {};
+ }),
+ destroyTeamsUserLinkMock: vi.fn(),
+ hasRolesPermissionMock: vi.fn(),
+ }));
+
+vi.mock('twenty-client-sdk/core', () => ({
+ CoreApiClient: coreApiClientMock,
+}));
+
+vi.mock(
+ 'src/features/chat/logic-functions/data/destroy-teams-user-link',
+ () => ({ destroyTeamsUserLink: destroyTeamsUserLinkMock }),
+);
+
+vi.mock(
+ 'src/features/chat/logic-functions/utils/current-user-has-roles-permission',
+ () => ({ currentUserHasRolesPermission: hasRolesPermissionMock }),
+);
+
+describe('teamsRemoveUserLinkHandler', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ hasRolesPermissionMock.mockResolvedValue(true);
+ destroyTeamsUserLinkMock.mockResolvedValue(undefined);
+ });
+
+ it('should remove the link as the application', async () => {
+ const result = await teamsRemoveUserLinkHandler({
+ body: { id: 'link-id' },
+ });
+
+ expect(result.success).toBe(true);
+ expect(coreApiClientMock).toHaveBeenCalledWith({ runAs: 'application' });
+ expect(destroyTeamsUserLinkMock).toHaveBeenCalledTimes(1);
+ expect(destroyTeamsUserLinkMock).toHaveBeenCalledWith({
+ client: expect.anything(),
+ id: 'link-id',
+ });
+ });
+
+ it('should reject a request without an id', async () => {
+ const result = await teamsRemoveUserLinkHandler({ body: {} });
+
+ expect(result.success).toBe(false);
+ expect(destroyTeamsUserLinkMock).not.toHaveBeenCalled();
+ });
+
+ it('should not remove the link for a member without the roles permission', async () => {
+ hasRolesPermissionMock.mockResolvedValue(false);
+
+ const result = await teamsRemoveUserLinkHandler({
+ body: { id: 'link-id' },
+ });
+
+ expect(result.success).toBe(false);
+ expect(destroyTeamsUserLinkMock).not.toHaveBeenCalled();
+ });
+
+ it('should report a failed removal', async () => {
+ destroyTeamsUserLinkMock.mockRejectedValue(new Error('not found'));
+
+ const result = await teamsRemoveUserLinkHandler({
+ body: { id: 'link-id' },
+ });
+
+ expect(result).toEqual({
+ success: false,
+ message: 'Could not remove the link',
+ error: 'not found',
+ });
+ });
+});
diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/data/destroy-teams-user-link.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/data/destroy-teams-user-link.ts
new file mode 100644
index 00000000000..1b552f897a0
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/data/destroy-teams-user-link.ts
@@ -0,0 +1,16 @@
+import { type CoreApiClient } from 'twenty-client-sdk/core';
+
+export const destroyTeamsUserLink = async ({
+ client,
+ id,
+}: {
+ client: Pick<CoreApiClient, 'mutation'>;
+ id: string;
+}): Promise<void> => {
+ await client.mutation({
+ destroyTeamsUserLink: {
+ __args: { id },
+ id: true,
+ },
+ });
+};
diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/teams-remove-user-link.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/teams-remove-user-link.ts
new file mode 100644
index 00000000000..276a81907d9
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/teams-remove-user-link.ts
@@ -0,0 +1,62 @@
+import { isNonEmptyString } from '@sniptt/guards';
+import { CoreApiClient } from 'twenty-client-sdk/core';
+import { defineLogicFunction, type RoutePayload } from 'twenty-sdk/define';
+
+import { TEAMS_USER_LINKS_REMOVE_ROUTE_PATH } from 'src/features/chat/constants/teams-user-links-route-path';
+import { TEAMS_REMOVE_USER_LINK_UNIVERSAL_IDENTIFIER } from 'src/features/chat/constants/universal-identifiers';
+import { destroyTeamsUserLink } from 'src/features/chat/logic-functions/data/destroy-teams-user-link';
+import { type TeamsUserLinkActionResult } from 'src/features/chat/logic-functions/types/teams-user-link-action-result.type';
+import { currentUserHasRolesPermission } from 'src/features/chat/logic-functions/utils/current-user-has-roles-permission';
+import { toErrorMessage } from 'src/utils/to-error-message';
+
+export const teamsRemoveUserLinkHandler = async (
+ payload: Pick<RoutePayload<{ id?: unknown }>, 'body'>,
+): Promise<TeamsUserLinkActionResult> => {
+ const id = payload.body?.id;
+
+ if (!isNonEmptyString(id)) {
+ return {
+ success: false,
+ message: 'Missing required fields',
+ error: 'id is required.',
+ };
+ }
+
+ if (!(await currentUserHasRolesPermission())) {
+ return {
+ success: false,
+ message: 'Not allowed',
+ error:
+ 'Only members with the roles permission can remove Teams user links.',
+ };
+ }
+
+ try {
+ await destroyTeamsUserLink({
+ client: new CoreApiClient({ runAs: 'application' }),
+ id,
+ });
+ } catch (error) {
+ return {
+ success: false,
+ message: 'Could not remove the link',
+ error: toErrorMessage(error),
+ };
+ }
+
+ return { success: true, message: 'Removed the Teams user link.' };
+};
+
+export default defineLogicFunction({
+ universalIdentifier: TEAMS_REMOVE_USER_LINK_UNIVERSAL_IDENTIFIER,
+ name: 'teams-remove-user-link',
+ description:
+ 'Removes a Teams user link. The account then falls back to email matching on its next message, so this undoes a manual link or clears a stale one. Restricted to members with the roles permission.',
+ timeoutSeconds: 30,
+ httpRouteTriggerSettings: {
+ path: TEAMS_USER_LINKS_REMOVE_ROUTE_PATH,
+ httpMethod: 'POST',
+ isAuthRequired: true,
+ },
+ handler: teamsRemoveUserLinkHandler,
+});
diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/types/teams-user-link-action-result.type.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/types/teams-user-link-action-result.type.ts
new file mode 100644
index 00000000000..8a8050951d8
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/types/teams-user-link-action-result.type.ts
@@ -0,0 +1,5 @@
+export type TeamsUserLinkActionResult = {
+ success: boolean;
+ message: string;
+ error?: string;
+};
diff --git a/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/current-user-has-roles-permission.ts b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/current-user-has-roles-permission.ts
new file mode 100644
index 00000000000..0deb6419085
--- /dev/null
+++ b/packages/twenty-apps/public/teams/src/features/chat/logic-functions/utils/current-user-has-roles-permission.ts
@@ -0,0 +1,21 @@
+import { MetadataApiClient } from 'twenty-client-sdk/metadata';
+
+const ROLES_PERMISSION_FLAG = 'ROLES';
+
+export const currentUserHasRolesPermission = async (): Promise<boolean> => {
+ try {
+ const { currentUser } = await new MetadataApiClient().query({
+ currentUser: {
+ currentUserWorkspace: {
+ permissionFlags: true,
+ },
+ },
+ });
+
+ return (currentUser.currentUserWorkspace?.permissionFlags ?? []).includes(
+ ROLES_PERMISSION_FLAG,
+ );
+ } catch {
+ return false;
+ }
+};
diff --git a/packages/twenty-apps/public/teams/src/roles/default-function.role.ts b/packages/twenty-apps/public/teams/src/roles/default-function.role.ts
index ad52b3615cc..6d638ce08ea 100644
--- a/packages/twenty-apps/public/teams/src/roles/default-function.role.ts
+++ b/packages/twenty-apps/public/teams/src/roles/default-function.role.ts
@@ -14,7 +14,7 @@ export default defineApplicationRole({
universalIdentifier: DEFAULT_ROLE_UNIVERSAL_IDENTIFIER,
label: 'Twenty Microsoft Teams tools role',
description:
- 'Reads calendar events and their calendar channel associations to match Teams meetings and writes imported transcripts to Call Recordings. Tracks assistant requests received from Teams, reads workspace members to match a Teams sender by email, and runs the Teams Assistant agent as that member, with the permissions of that member. Chat tools only forward requests to Microsoft Teams using the configured bot credentials.',
+ 'Reads calendar events and their calendar channel associations to match Teams meetings and writes imported transcripts to Call Recordings. Tracks assistant requests received from Teams, reads workspace members to match a Teams sender by email, keeps Teams user links so a matched sender is remembered and can be unlinked, and runs the Teams Assistant agent as that member, with the permissions of that member. Chat tools only forward requests to Microsoft Teams using the configured bot credentials.',
canReadAllObjectRecords: false,
canUpdateAllObjectRecords: false,
canSoftDeleteAllObjectRecords: false,
@@ -71,7 +71,7 @@ export default defineApplicationRole({
canReadObjectRecords: true,
canUpdateObjectRecords: true,
canSoftDeleteObjectRecords: false,
- canDestroyObjectRecords: false,
+ canDestroyObjectRecords: true,
},
],
fieldPermissions: [],