The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(slack): mark user links from disconnected Slack workspaces (#25269)
Abdul Rahman72ccdc1c1f9a
diff --git a/packages/twenty-apps/public/slack/README.md b/packages/twenty-apps/public/slack/README.mdindex 0a2def09445..cf954079113 100644--- a/packages/twenty-apps/public/slack/README.md+++ b/packages/twenty-apps/public/slack/README.md@@ -15,7 +15,7 @@ Mention the bot in a channel or DM it. It replies in the thread with your CRM daA Slack account with no link to a workspace member runs with the **Slack Assistant** role, which can read, create, update and soft-delete people, companies, opportunities, notes and tasks. Workspace members stay read-only and hard delete is off. Tighten the role in **Settings → Roles** if you want a narrower bot.-Everyone else runs with their own permissions. Connecting the Slack workspace sweeps the roster and links every full member whose confirmed Slack email matches a workspace member; anyone the sweep missed is matched the first time they mention the bot. Each pair is stored as a **Slack User Link**. Guests and Slack Connect users are never matched automatically, so an admin links them by hand from the app's **Settings** tab, which also lists who the sweep skipped. See [SETUP.md](https://github.com/twentyhq/twenty/blob/main/packages/twenty-apps/public/slack/SETUP.md) for how linking, consent and re-verification work.+Everyone else runs with their own permissions. Connecting the Slack workspace sweeps the roster and links every full member whose confirmed Slack email matches a workspace member; anyone the sweep missed is matched the first time they mention the bot. Each pair is stored as a **Slack User Link**. Guests and Slack Connect users are never matched automatically, so an admin links them by hand from the app's **Settings** tab, which also lists who the sweep skipped. Links made against a Slack workspace you have since disconnected stay in that list as history, marked **Slack workspace disconnected** rather than active, so they cannot be read as live access. See [SETUP.md](https://github.com/twentyhq/twenty/blob/main/packages/twenty-apps/public/slack/SETUP.md) for how linking, consent and re-verification work.When the bot is added to a channel it introduces itself once, with a short message in the channel and the details in a thread reply.diff --git a/packages/twenty-apps/public/slack/SETUP.md b/packages/twenty-apps/public/slack/SETUP.mdindex 4291e4cd6e8..65e2032ce4e 100644--- a/packages/twenty-apps/public/slack/SETUP.md+++ b/packages/twenty-apps/public/slack/SETUP.md@@ -245,6 +245,24 @@ status, lets a member with the permission remove a link or resend a pendingconsent request, and shows a read-only view to anyone without the permission.It renders nothing until Slack is connected.+Links made against a Slack workspace you are no longer connected to stay in+that list, because they are the audit trail of who could borrow whose access+and when. Disconnecting stops the app receiving that workspace's messages, so+these links normally go quiet, but it does not revoke them. An email-matched+link is inert either way, because email matching trusts only members of the+installed workspace. A consented manual link is not: it is keyed on the+sender's own Slack team, so it is still honoured if that person can reach the+bot another way, such as through Slack Connect in the workspace you are+connected to now. That is the same mechanism that makes manual links work for+Slack Connect users in the first place.++So the list marks these rows rather than hiding them, showing **Slack+workspace disconnected** instead of a live status and offering no consent+resend for them. Removing the row is what revokes the link, and disconnecting+a Slack workspace never does so on your behalf. If Slack does not confirm the+installed team when the section loads, every row keeps its stored status+rather than being marked wrongly.+The same section lists Slack users with no link yet, so you can see who thesweep skipped and finish the job in place: each row has its own member pickerand says whether linking activates immediately or asks the person first. Andiff --git a/packages/twenty-apps/public/slack/src/front-components/components/SlackUserLinksList.tsx b/packages/twenty-apps/public/slack/src/front-components/components/SlackUserLinksList.tsxindex 5517f2215cb..df54f437328 100644--- a/packages/twenty-apps/public/slack/src/front-components/components/SlackUserLinksList.tsx+++ b/packages/twenty-apps/public/slack/src/front-components/components/SlackUserLinksList.tsx@@ -20,7 +20,7 @@ import { type SlackUserLinkConsentState } from 'src/logic-functions/types/slack-import { isSlackUserLinkConsentState } from 'src/logic-functions/utils/is-slack-user-link-consent-state';import { type SlackUserLinkRecord } from 'src/front-components/types/slack-user-link-record.type';-const LINKS_GRID_TEMPLATE_COLUMNS = 'minmax(0, 2fr) minmax(0, 2fr) 252px 156px';+const LINKS_GRID_TEMPLATE_COLUMNS = 'minmax(0, 2fr) minmax(0, 2fr) 320px 156px';const REMOVAL_CONFIRM_TIMEOUT_MS = 4000;const StyledIdentity = styled.div`@@ -94,9 +94,23 @@ const CONSENT_COLORS: Record<SlackUserLinkConsentState, TagColor> = {[SLACK_USER_LINK_CONSENT_STATE.ADMIN_SET]: 'gray',};+const DISCONNECTED_WORKSPACE_LABEL = 'Slack workspace disconnected';++const isFromDisconnectedSlackWorkspace = ({+ slackUserLink,+ installedSlackTeamId,+}: {+ slackUserLink: SlackUserLinkRecord;+ installedSlackTeamId: string | undefined;+}): boolean =>+ isNonEmptyString(installedSlackTeamId) &&+ isNonEmptyString(slackUserLink.slackTeamId) &&+ slackUserLink.slackTeamId !== installedSlackTeamId;+type SlackUserLinksListProps = {slackUserLinks: SlackUserLinkRecord[];canManage: boolean;+ installedSlackTeamId: string | undefined;hasMore?: boolean;onRemove: (slackUserLink: SlackUserLinkRecord) => void;onResend: (slackUserLink: SlackUserLinkRecord) => void;@@ -107,6 +121,7 @@ type SlackUserLinksListProps = {export const SlackUserLinksList = ({slackUserLinks,canManage,+ installedSlackTeamId,hasMore = false,onRemove,onResend,@@ -147,10 +162,15 @@ export const SlackUserLinksList = ({</SlackTableRow><SlackTableBody>{slackUserLinks.map((slackUserLink) => {+ const isDisconnected = isFromDisconnectedSlackWorkspace({+ slackUserLink,+ installedSlackTeamId,+ });const consentState = toDisplayedConsentState(slackUserLink.consentState,);const isPending =+ !isDisconnected &&consentState === SLACK_USER_LINK_CONSENT_STATE.PENDING;const displayedName =slackUserLink.name ?? slackUserLink.slackUserId ?? 'Unnamed link';@@ -187,11 +207,15 @@ export const SlackUserLinksList = ({/></SlackTableCell><SlackTableCell>- {isDefined(consentState) && (- <Tag- color={CONSENT_COLORS[consentState]}- text={CONSENT_LABELS[consentState]}- />+ {isDisconnected ? (+ <Tag color="gray" text={DISCONNECTED_WORKSPACE_LABEL} />+ ) : (+ isDefined(consentState) && (+ <Tag+ color={CONSENT_COLORS[consentState]}+ text={CONSENT_LABELS[consentState]}+ />+ ))}<Tagcolor={getSourceColor(slackUserLink.source)}diff --git a/packages/twenty-apps/public/slack/src/front-components/components/SlackUserLinksSettings.tsx b/packages/twenty-apps/public/slack/src/front-components/components/SlackUserLinksSettings.tsxindex f80932b3a7f..c77cc1a4c47 100644--- a/packages/twenty-apps/public/slack/src/front-components/components/SlackUserLinksSettings.tsx+++ b/packages/twenty-apps/public/slack/src/front-components/components/SlackUserLinksSettings.tsx@@ -64,7 +64,7 @@ const StyledCenteredState = styled.div`export const SlackUserLinksSettings = () => {const { canManage, isPermissionLoading } = useCanManageSlackUserLinks();- const { isSlackConnected, isConnectionStatusLoading } =+ const { isSlackConnected, installedSlackTeamId, isConnectionStatusLoading } =useSlackConnectionStatus();const {slackUserLinks,@@ -221,6 +221,7 @@ export const SlackUserLinksSettings = () => {<SlackUserLinksListslackUserLinks={slackUserLinks}canManage={canManage}+ installedSlackTeamId={installedSlackTeamId}hasMore={hasMoreSlackUserLinks}onRemove={handleRemove}onResend={handleResend}diff --git a/packages/twenty-apps/public/slack/src/front-components/hooks/use-slack-connection-status.ts b/packages/twenty-apps/public/slack/src/front-components/hooks/use-slack-connection-status.tsindex 1624ff4be76..7d396eb5086 100644--- a/packages/twenty-apps/public/slack/src/front-components/hooks/use-slack-connection-status.ts+++ b/packages/twenty-apps/public/slack/src/front-components/hooks/use-slack-connection-status.ts@@ -2,15 +2,18 @@ import { useEffect, useState } from 'react';import { RestApiClient } from 'twenty-client-sdk/rest';import { SLACK_USER_LINKS_CONNECTION_STATUS_ROUTE_PATH } from 'src/constants/slack-user-links-route-path.constant';-import { asRecord } from 'src/logic-functions/utils/as-record.util';+import { type ParsedSlackConnectionStatus } from 'src/front-components/types/parsed-slack-connection-status.type';+import {+ DISCONNECTED_SLACK_CONNECTION_STATUS,+ parseSlackConnectionStatus,+} from 'src/front-components/utils/parse-slack-connection-status.util';-type SlackConnectionStatusState = {- isSlackConnected: boolean;+type SlackConnectionStatusState = ParsedSlackConnectionStatus & {isConnectionStatusLoading: boolean;};const LOADING_STATE: SlackConnectionStatusState = {- isSlackConnected: false,+ ...DISCONNECTED_SLACK_CONNECTION_STATUS,isConnectionStatusLoading: true,};@@ -21,7 +24,7 @@ export const useSlackConnectionStatus = (): SlackConnectionStatusState => {let cancelled = false;const fetchConnectionStatus = async () => {- let isSlackConnected = false;+ let connectionStatus: ParsedSlackConnectionStatus;try {const result = await new RestApiClient().post(@@ -29,13 +32,13 @@ export const useSlackConnectionStatus = (): SlackConnectionStatusState => {{},);- isSlackConnected = asRecord(result)?.isConnected === true;+ connectionStatus = parseSlackConnectionStatus(result);} catch {- isSlackConnected = false;+ connectionStatus = DISCONNECTED_SLACK_CONNECTION_STATUS;}if (!cancelled) {- setState({ isSlackConnected, isConnectionStatusLoading: false });+ setState({ ...connectionStatus, isConnectionStatusLoading: false });}};diff --git a/packages/twenty-apps/public/slack/src/front-components/types/parsed-slack-connection-status.type.ts b/packages/twenty-apps/public/slack/src/front-components/types/parsed-slack-connection-status.type.tsnew file mode 100644index 00000000000..d5cd813219b--- /dev/null+++ b/packages/twenty-apps/public/slack/src/front-components/types/parsed-slack-connection-status.type.ts@@ -0,0 +1,4 @@+export type ParsedSlackConnectionStatus = {+ isSlackConnected: boolean;+ installedSlackTeamId: string | undefined;+};diff --git a/packages/twenty-apps/public/slack/src/front-components/utils/__tests__/parse-slack-connection-status.util.test.ts b/packages/twenty-apps/public/slack/src/front-components/utils/__tests__/parse-slack-connection-status.util.test.tsnew file mode 100644index 00000000000..ffb7d0754ae--- /dev/null+++ b/packages/twenty-apps/public/slack/src/front-components/utils/__tests__/parse-slack-connection-status.util.test.ts@@ -0,0 +1,52 @@+import { describe, expect, it } from 'vitest';++import { parseSlackConnectionStatus } from 'src/front-components/utils/parse-slack-connection-status.util';++describe('parseSlackConnectionStatus', () => {+ it('should parse a connected status with its installed team id', () => {+ expect(+ parseSlackConnectionStatus({+ success: true,+ isConnected: true,+ installedSlackTeamId: 'T0INSTALLED',+ }),+ ).toEqual({ isSlackConnected: true, installedSlackTeamId: 'T0INSTALLED' });+ });++ it('should leave the team id undefined when the payload omits it', () => {+ expect(+ parseSlackConnectionStatus({ success: true, isConnected: true }),+ ).toEqual({ isSlackConnected: true, installedSlackTeamId: undefined });+ });++ it('should ignore a team id that is not a non-empty string', () => {+ for (const installedSlackTeamId of ['', 42, null, {}]) {+ expect(+ parseSlackConnectionStatus({+ success: true,+ isConnected: true,+ installedSlackTeamId,+ }),+ ).toEqual({ isSlackConnected: true, installedSlackTeamId: undefined });+ }+ });++ it('should report a disconnected status', () => {+ expect(+ parseSlackConnectionStatus({+ success: true,+ isConnected: false,+ installedSlackTeamId: 'T0INSTALLED',+ }),+ ).toEqual({ isSlackConnected: false, installedSlackTeamId: undefined });+ });++ it('should fall back on a malformed payload', () => {+ for (const value of [undefined, null, 'connected', []]) {+ expect(parseSlackConnectionStatus(value)).toEqual({+ isSlackConnected: false,+ installedSlackTeamId: undefined,+ });+ }+ });+});diff --git a/packages/twenty-apps/public/slack/src/front-components/utils/parse-slack-connection-status.util.ts b/packages/twenty-apps/public/slack/src/front-components/utils/parse-slack-connection-status.util.tsnew file mode 100644index 00000000000..6218c123b0c--- /dev/null+++ b/packages/twenty-apps/public/slack/src/front-components/utils/parse-slack-connection-status.util.ts@@ -0,0 +1,29 @@+import { isNonEmptyString } from '@sniptt/guards';++import { type ParsedSlackConnectionStatus } from 'src/front-components/types/parsed-slack-connection-status.type';+import { asRecord } from 'src/logic-functions/utils/as-record.util';++export const DISCONNECTED_SLACK_CONNECTION_STATUS: ParsedSlackConnectionStatus =+ {+ isSlackConnected: false,+ installedSlackTeamId: undefined,+ };++export const parseSlackConnectionStatus = (+ value: unknown,+): ParsedSlackConnectionStatus => {+ const record = asRecord(value);++ if (record?.isConnected !== true) {+ return DISCONNECTED_SLACK_CONNECTION_STATUS;+ }++ const installedSlackTeamId = record.installedSlackTeamId;++ return {+ isSlackConnected: true,+ installedSlackTeamId: isNonEmptyString(installedSlackTeamId)+ ? installedSlackTeamId+ : undefined,+ };+};diff --git a/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-connection-status-timeout-ms.ts b/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-connection-status-timeout-ms.tsnew file mode 100644index 00000000000..02db179e9c7--- /dev/null+++ b/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-connection-status-timeout-ms.ts@@ -0,0 +1 @@+export const SLACK_CONNECTION_STATUS_TIMEOUT_MS = 5 * 1000;diff --git a/packages/twenty-apps/public/slack/src/logic-functions/handlers/__tests__/slack-connection-status-handler.test.ts b/packages/twenty-apps/public/slack/src/logic-functions/handlers/__tests__/slack-connection-status-handler.test.tsindex 2453d074725..eb311450543 100644--- a/packages/twenty-apps/public/slack/src/logic-functions/handlers/__tests__/slack-connection-status-handler.test.ts+++ b/packages/twenty-apps/public/slack/src/logic-functions/handlers/__tests__/slack-connection-status-handler.test.ts@@ -1,27 +1,52 @@-import { describe, expect, it, vi } from 'vitest';+import { beforeEach, describe, expect, it, vi } from 'vitest';+import { SLACK_CONNECTION_STATUS_TIMEOUT_MS } from 'src/logic-functions/constants/slack-connection-status-timeout-ms';import { slackConnectionStatusHandler } from 'src/logic-functions/handlers/slack-connection-status-handler';-import { getSlackConnection } from 'src/logic-functions/utils/get-slack-connection';+import { getSlackClient } from 'src/logic-functions/utils/get-slack-client';-vi.mock('src/logic-functions/utils/get-slack-connection', () => ({- getSlackConnection: vi.fn(),+vi.mock('src/logic-functions/utils/get-slack-client', () => ({+ getSlackClient: vi.fn(),}));+const authTestMock = vi.fn();++const mockConnectedSlackClient = () => {+ vi.mocked(getSlackClient).mockResolvedValueOnce({+ success: true,+ client: { auth: { test: authTestMock } } as never,+ });+};+describe('slackConnectionStatusHandler', () => {- it('should report a connected workspace', async () => {- vi.mocked(getSlackConnection).mockResolvedValueOnce({- success: true,- accessToken: 'xoxb-token',- });+ beforeEach(() => {+ vi.clearAllMocks();+ });++ it('should report a connected workspace with its installed team id', async () => {+ mockConnectedSlackClient();+ authTestMock.mockResolvedValueOnce({ ok: true, team_id: 'T0INSTALLED' });await expect(slackConnectionStatusHandler()).resolves.toEqual({success: true,isConnected: true,+ installedSlackTeamId: 'T0INSTALLED',+ });+ });++ it('should bound the Slack call so a hanging Slack cannot exhaust the route budget', async () => {+ mockConnectedSlackClient();+ authTestMock.mockResolvedValueOnce({ ok: true, team_id: 'T0INSTALLED' });++ await slackConnectionStatusHandler();++ expect(getSlackClient).toHaveBeenCalledWith({+ timeout: SLACK_CONNECTION_STATUS_TIMEOUT_MS,+ retryConfig: { retries: 0 },});});it('should report a missing connection without failing', async () => {- vi.mocked(getSlackConnection).mockResolvedValueOnce({+ vi.mocked(getSlackClient).mockResolvedValueOnce({success: false,error: 'Slack is not connected.',});@@ -30,5 +55,29 @@ describe('slackConnectionStatusHandler', () => {success: true,isConnected: false,});++ expect(authTestMock).not.toHaveBeenCalled();+ });++ it('should stay connected without a team id when Slack does not answer', async () => {+ mockConnectedSlackClient();+ authTestMock.mockRejectedValueOnce(new Error('invalid_auth'));++ await expect(slackConnectionStatusHandler()).resolves.toEqual({+ success: true,+ isConnected: true,+ installedSlackTeamId: undefined,+ });+ });++ it('should ignore an empty team id', async () => {+ mockConnectedSlackClient();+ authTestMock.mockResolvedValueOnce({ ok: true, team_id: '' });++ await expect(slackConnectionStatusHandler()).resolves.toEqual({+ success: true,+ isConnected: true,+ installedSlackTeamId: undefined,+ });});});diff --git a/packages/twenty-apps/public/slack/src/logic-functions/handlers/slack-connection-status-handler.ts b/packages/twenty-apps/public/slack/src/logic-functions/handlers/slack-connection-status-handler.tsindex cd40e8884be..c380c2cd0fb 100644--- a/packages/twenty-apps/public/slack/src/logic-functions/handlers/slack-connection-status-handler.ts+++ b/packages/twenty-apps/public/slack/src/logic-functions/handlers/slack-connection-status-handler.ts@@ -1,13 +1,27 @@-import { getSlackConnection } from 'src/logic-functions/utils/get-slack-connection';+import { SLACK_CONNECTION_STATUS_TIMEOUT_MS } from 'src/logic-functions/constants/slack-connection-status-timeout-ms';+import { getInstalledSlackTeamId } from 'src/logic-functions/utils/get-installed-slack-team-id';+import { getSlackClient } from 'src/logic-functions/utils/get-slack-client';type SlackConnectionStatusResult = {success: true;isConnected: boolean;+ installedSlackTeamId?: string;};export const slackConnectionStatusHandler =async (): Promise<SlackConnectionStatusResult> => {- const connection = await getSlackConnection();+ const slackClientResult = await getSlackClient({+ timeout: SLACK_CONNECTION_STATUS_TIMEOUT_MS,+ retryConfig: { retries: 0 },+ });- return { success: true, isConnected: connection.success };+ if (!slackClientResult.success) {+ return { success: true, isConnected: false };+ }++ const installedSlackTeamId = await getInstalledSlackTeamId(+ slackClientResult.client,+ );++ return { success: true, isConnected: true, installedSlackTeamId };};