The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Add user-scoped Fathom connection provider (#29433)
nitin23d0d31478b3
diff --git a/packages/twenty-apps/public/fathom/SETUP.md b/packages/twenty-apps/public/fathom/SETUP.mdindex 0b0369febf5..e5855498880 100644--- a/packages/twenty-apps/public/fathom/SETUP.md+++ b/packages/twenty-apps/public/fathom/SETUP.md@@ -58,6 +58,23 @@ to the workspace that made the connection, so no per-workspace domain is needed.The import runs through the requesting user's Fathom connection.+## Workspace-shared and personal connections++Connecting Fathom from **Settings > Applications > Fathom** adds a+workspace-shared connection. Its webhook receives the connected account's own+recordings and the team recordings it can access.++Members can also add a personal connection from **Settings > App preferences**,+which is behind the `IS_APP_PREFERENCES_ENABLED` workspace feature flag. A+personal connection's webhook receives only that member's own recordings+(`my_recordings` and `my_shared_with_team_recordings`), so teammates who each+connect do not all import the same team recordings. Its history imports, the+31-day one on connect and manual backfills, include only calls recorded by the+member's email. Fathom has no endpoint that names the account behind a token,+so a member whose Fathom email differs from their Twenty email gets new calls+but no history. Actions run by a member use their personal connection first and+fall back to the workspace-shared one.+## Media import behaviourFathom prepares the downloadable file after the transcript is ready, so a Calldiff --git a/packages/twenty-apps/public/fathom/src/connection-providers/fathom-connection.ts b/packages/twenty-apps/public/fathom/src/connection-providers/fathom-connection.tsindex 59cefd74d2c..ec29495c6c3 100644--- a/packages/twenty-apps/public/fathom/src/connection-providers/fathom-connection.ts+++ b/packages/twenty-apps/public/fathom/src/connection-providers/fathom-connection.ts@@ -1,6 +1,7 @@import { defineConnectionProvider } from 'twenty-sdk/define';import { FATHOM_PROVIDER_NAME } from 'src/constants/fathom.constant';+import { FATHOM_OAUTH_CONFIG } from 'src/constants/fathom-oauth-config.constant';import {FATHOM_CONNECTION_PROVIDER_UNIVERSAL_IDENTIFIER,FATHOM_DISCONNECT_UNIVERSAL_IDENTIFIER,@@ -12,19 +13,12 @@ export default defineConnectionProvider({name: FATHOM_PROVIDER_NAME,displayName: 'Fathom',type: 'oauth',+ scope: 'WORKSPACE',onConnectLogicFunction: {universalIdentifier: FATHOM_REGISTER_CONNECTION_UNIVERSAL_IDENTIFIER,},onDisconnectLogicFunction: {universalIdentifier: FATHOM_DISCONNECT_UNIVERSAL_IDENTIFIER,},- oauth: {- authorizationEndpoint: 'https://fathom.video/external/v1/oauth2/authorize',- tokenEndpoint: 'https://api.fathom.ai/external/v1/oauth2/token',- scopes: ['public_api'],- clientIdVariable: 'FATHOM_CLIENT_ID',- clientSecretVariable: 'FATHOM_CLIENT_SECRET',- tokenRequestContentType: 'form-urlencoded',- usePkce: false,- },+ oauth: FATHOM_OAUTH_CONFIG,});diff --git a/packages/twenty-apps/public/fathom/src/connection-providers/fathom-user-connection.ts b/packages/twenty-apps/public/fathom/src/connection-providers/fathom-user-connection.tsnew file mode 100644index 00000000000..0c65d411c38--- /dev/null+++ b/packages/twenty-apps/public/fathom/src/connection-providers/fathom-user-connection.ts@@ -0,0 +1,24 @@+import { defineConnectionProvider } from 'twenty-sdk/define';++import { FATHOM_USER_PROVIDER_NAME } from 'src/constants/fathom.constant';+import { FATHOM_OAUTH_CONFIG } from 'src/constants/fathom-oauth-config.constant';+import {+ FATHOM_DISCONNECT_UNIVERSAL_IDENTIFIER,+ FATHOM_REGISTER_CONNECTION_UNIVERSAL_IDENTIFIER,+ FATHOM_USER_CONNECTION_PROVIDER_UNIVERSAL_IDENTIFIER,+} from 'src/constants/universal-identifiers';++export default defineConnectionProvider({+ universalIdentifier: FATHOM_USER_CONNECTION_PROVIDER_UNIVERSAL_IDENTIFIER,+ name: FATHOM_USER_PROVIDER_NAME,+ displayName: 'Fathom',+ type: 'oauth',+ scope: 'USER',+ onConnectLogicFunction: {+ universalIdentifier: FATHOM_REGISTER_CONNECTION_UNIVERSAL_IDENTIFIER,+ },+ onDisconnectLogicFunction: {+ universalIdentifier: FATHOM_DISCONNECT_UNIVERSAL_IDENTIFIER,+ },+ oauth: FATHOM_OAUTH_CONFIG,+});diff --git a/packages/twenty-apps/public/fathom/src/constants/fathom-oauth-config.constant.ts b/packages/twenty-apps/public/fathom/src/constants/fathom-oauth-config.constant.tsnew file mode 100644index 00000000000..0c6a4619737--- /dev/null+++ b/packages/twenty-apps/public/fathom/src/constants/fathom-oauth-config.constant.ts@@ -0,0 +1,16 @@+import { type defineConnectionProvider } from 'twenty-sdk/define';++type OAuthConnectionProviderConfig = Extract<+ Parameters<typeof defineConnectionProvider>[0],+ { type: 'oauth' }+>['oauth'];++export const FATHOM_OAUTH_CONFIG: OAuthConnectionProviderConfig = {+ authorizationEndpoint: 'https://fathom.video/external/v1/oauth2/authorize',+ tokenEndpoint: 'https://api.fathom.ai/external/v1/oauth2/token',+ scopes: ['public_api'],+ clientIdVariable: 'FATHOM_CLIENT_ID',+ clientSecretVariable: 'FATHOM_CLIENT_SECRET',+ tokenRequestContentType: 'form-urlencoded',+ usePkce: false,+};diff --git a/packages/twenty-apps/public/fathom/src/constants/fathom-webhook-triggers.constant.ts b/packages/twenty-apps/public/fathom/src/constants/fathom-webhook-triggers.constant.tsnew file mode 100644index 00000000000..81af42a5889--- /dev/null+++ b/packages/twenty-apps/public/fathom/src/constants/fathom-webhook-triggers.constant.ts@@ -0,0 +1,13 @@+import { type TriggeredFor } from 'fathom-typescript/sdk/models/operations';++// Personal connections exclude recordings owned by other team members to avoid duplicate imports.+export const FATHOM_USER_WEBHOOK_TRIGGERS: TriggeredFor[] = [+ 'my_recordings',+ 'my_shared_with_team_recordings',+];++export const FATHOM_WORKSPACE_WEBHOOK_TRIGGERS: TriggeredFor[] = [+ 'my_recordings',+ 'my_shared_with_team_recordings',+ 'shared_team_recordings',+];diff --git a/packages/twenty-apps/public/fathom/src/constants/fathom.constant.ts b/packages/twenty-apps/public/fathom/src/constants/fathom.constant.tsindex 480af24b88a..8b382203409 100644--- a/packages/twenty-apps/public/fathom/src/constants/fathom.constant.ts+++ b/packages/twenty-apps/public/fathom/src/constants/fathom.constant.ts@@ -1,4 +1,9 @@export const FATHOM_PROVIDER_NAME = 'fathom';+export const FATHOM_USER_PROVIDER_NAME = 'fathom-user';+export const FATHOM_PROVIDER_NAMES: readonly string[] = [+ FATHOM_PROVIDER_NAME,+ FATHOM_USER_PROVIDER_NAME,+];export const FATHOM_WEBHOOK_CONNECTION_QUERY_PARAMETER = 'connectionId';export const CALENDAR_EVENT_PAGE_SIZE = 200;export const MAX_CALENDAR_EVENT_PAGES = 10;diff --git a/packages/twenty-apps/public/fathom/src/constants/universal-identifiers.ts b/packages/twenty-apps/public/fathom/src/constants/universal-identifiers.tsindex 6e7387f0a0d..22cc0da6d23 100644--- a/packages/twenty-apps/public/fathom/src/constants/universal-identifiers.ts+++ b/packages/twenty-apps/public/fathom/src/constants/universal-identifiers.ts@@ -7,6 +7,8 @@ export const DEFAULT_ROLE_UNIVERSAL_IDENTIFIER ='66c9ec61-dd73-4b1c-b3c5-ee54dffd8f05';export const FATHOM_CONNECTION_PROVIDER_UNIVERSAL_IDENTIFIER ='eec1a27d-7678-43df-81e9-9f245ada8fe4';+export const FATHOM_USER_CONNECTION_PROVIDER_UNIVERSAL_IDENTIFIER =+ '5f25b793-9b6a-4825-809b-b91aa2e1fef4';export const FATHOM_REGISTER_CONNECTION_UNIVERSAL_IDENTIFIER ='d6cb329f-8d0d-48f0-af30-e3ce2e11ddc6';export const FATHOM_DISCONNECT_UNIVERSAL_IDENTIFIER =diff --git a/packages/twenty-apps/public/fathom/src/logic-functions/__tests__/fathom-backfill-worker.test.ts b/packages/twenty-apps/public/fathom/src/logic-functions/__tests__/fathom-backfill-worker.test.tsindex 5fdc5f0f620..e571db5ed0a 100644--- a/packages/twenty-apps/public/fathom/src/logic-functions/__tests__/fathom-backfill-worker.test.ts+++ b/packages/twenty-apps/public/fathom/src/logic-functions/__tests__/fathom-backfill-worker.test.ts@@ -137,4 +137,37 @@ describe('fathomBackfillWorkerHandler', () => {notFoundError,);});++ it('lists every recording a workspace-shared connection can access', async () => {+ mocks.getConnection.mockResolvedValue({+ id: 'connection-1',+ scope: 'WORKSPACE',+ handle: '[email protected]',+ accessToken: 'token-1',+ });+ mocks.listMeetings.mockRejectedValue(buildFathomNotFoundError());++ await expect(fathomBackfillWorkerHandler(PAYLOAD)).rejects.toThrow();++ expect(mocks.listMeetings).toHaveBeenCalledOnce();+ expect(mocks.listMeetings.mock.calls[0][0]).not.toHaveProperty(+ 'recordedBy',+ );+ });++ it('lists only the recordings of the member behind a personal connection', async () => {+ mocks.getConnection.mockResolvedValue({+ id: 'connection-1',+ scope: 'USER',+ handle: '[email protected]',+ accessToken: 'token-1',+ });+ mocks.listMeetings.mockRejectedValue(buildFathomNotFoundError());++ await expect(fathomBackfillWorkerHandler(PAYLOAD)).rejects.toThrow();++ expect(mocks.listMeetings).toHaveBeenCalledExactlyOnceWith(+ expect.objectContaining({ recordedBy: ['[email protected]'] }),+ );+ });});diff --git a/packages/twenty-apps/public/fathom/src/logic-functions/__tests__/fathom-register-connection.test.ts b/packages/twenty-apps/public/fathom/src/logic-functions/__tests__/fathom-register-connection.test.tsindex f4c17f9cd5d..f2bd75d55c2 100644--- a/packages/twenty-apps/public/fathom/src/logic-functions/__tests__/fathom-register-connection.test.ts+++ b/packages/twenty-apps/public/fathom/src/logic-functions/__tests__/fathom-register-connection.test.ts@@ -113,6 +113,40 @@ describe('fathomRegisterConnectionHandler', () => {});});+ it('subscribes a workspace-shared connection to the team recordings it can access', async () => {+ sdkMocks.getConnection.mockResolvedValue({+ scope: 'WORKSPACE',+ accessToken: 'token',+ });++ await fathomRegisterConnectionHandler(HOOK_PAYLOAD);++ expect(sdkMocks.createWebhook).toHaveBeenCalledExactlyOnceWith(+ expect.objectContaining({+ triggeredFor: [+ 'my_recordings',+ 'my_shared_with_team_recordings',+ 'shared_team_recordings',+ ],+ }),+ );+ });++ it('subscribes a personal connection to its own recordings only', async () => {+ sdkMocks.getConnection.mockResolvedValue({+ scope: 'USER',+ accessToken: 'token',+ });++ await fathomRegisterConnectionHandler(HOOK_PAYLOAD);++ expect(sdkMocks.createWebhook).toHaveBeenCalledExactlyOnceWith(+ expect.objectContaining({+ triggeredFor: ['my_recordings', 'my_shared_with_team_recordings'],+ }),+ );+ });+it('starts the initial import once the registration is stored', async () => {await fathomRegisterConnectionHandler(HOOK_PAYLOAD);diff --git a/packages/twenty-apps/public/fathom/src/logic-functions/__tests__/fathom-uninstall.test.ts b/packages/twenty-apps/public/fathom/src/logic-functions/__tests__/fathom-uninstall.test.tsindex c854cf1a807..5819da45b7e 100644--- a/packages/twenty-apps/public/fathom/src/logic-functions/__tests__/fathom-uninstall.test.ts+++ b/packages/twenty-apps/public/fathom/src/logic-functions/__tests__/fathom-uninstall.test.ts@@ -27,7 +27,11 @@ vi.mock('fathom-typescript', () => ({const { fathomUninstallHandler } =await import('src/logic-functions/fathom-uninstall');-const buildConnection = (id: string) => ({ id, accessToken: `token-${id}` });+const buildConnection = (id: string, providerName: string) => ({+ id,+ providerName,+ accessToken: `token-${id}`,+});const buildRegistration = (registrationKey: string) => ({webhookId: `webhook-for-${registrationKey}`,@@ -40,8 +44,8 @@ describe('fathomUninstallHandler', () => {vi.resetAllMocks();vi.spyOn(console, 'error').mockImplementation(() => undefined);sdkMocks.listConnections.mockResolvedValue([- buildConnection('connection-1'),- buildConnection('connection-2'),+ buildConnection('connection-1', 'fathom'),+ buildConnection('connection-2', 'fathom-user'),]);sdkMocks.kvGet.mockImplementation((key: string) =>Promise.resolve(buildRegistration(key)),diff --git a/packages/twenty-apps/public/fathom/src/logic-functions/fathom-backfill-worker.ts b/packages/twenty-apps/public/fathom/src/logic-functions/fathom-backfill-worker.tsindex aa7fed4fdc5..7adacbe8352 100644--- a/packages/twenty-apps/public/fathom/src/logic-functions/fathom-backfill-worker.ts+++ b/packages/twenty-apps/public/fathom/src/logic-functions/fathom-backfill-worker.ts@@ -66,6 +66,8 @@ export const fathomBackfillWorkerHandler = async (),createdAfter,cursor: payload.cursor,+ // Fathom can't name a token's account, so the handle (member's email) stands in for it.+ recordedBy: connection.scope === 'USER' ? [connection.handle] : undefined,});} catch (error) {const delay = getFathomRequeueDelay({ error, now: new Date() });diff --git a/packages/twenty-apps/public/fathom/src/logic-functions/fathom-register-connection.ts b/packages/twenty-apps/public/fathom/src/logic-functions/fathom-register-connection.tsindex a438b64b6dc..13f54bfc09a 100644--- a/packages/twenty-apps/public/fathom/src/logic-functions/fathom-register-connection.ts+++ b/packages/twenty-apps/public/fathom/src/logic-functions/fathom-register-connection.ts@@ -4,6 +4,10 @@ import { getConnection, kv } from 'twenty-sdk/logic-function';import { isDefined } from 'src/utils/is-defined';import { FATHOM_INITIAL_BACKFILL_DAYS } from 'src/constants/fathom.constant';+import {+ FATHOM_USER_WEBHOOK_TRIGGERS,+ FATHOM_WORKSPACE_WEBHOOK_TRIGGERS,+} from 'src/constants/fathom-webhook-triggers.constant';import { FATHOM_REGISTER_CONNECTION_UNIVERSAL_IDENTIFIER } from 'src/constants/universal-identifiers';import { type FathomConnectionHookPayload } from 'src/logic-functions/types/fathom-connection-hook-payload.type';import { type FathomWebhookRegistration } from 'src/logic-functions/types/fathom-webhook-registration.type';@@ -95,11 +99,10 @@ export const fathomRegisterConnectionHandler = async (apiUrl,connectedAccountId: payload.connectedAccountId,}),- triggeredFor: [- 'my_recordings',- 'my_shared_with_team_recordings',- 'shared_team_recordings',- ],+ triggeredFor:+ connection.scope === 'USER'+ ? FATHOM_USER_WEBHOOK_TRIGGERS+ : FATHOM_WORKSPACE_WEBHOOK_TRIGGERS,includeTranscript: true,includeSummary: true,includeActionItems: true,diff --git a/packages/twenty-apps/public/fathom/src/logic-functions/fathom-uninstall.ts b/packages/twenty-apps/public/fathom/src/logic-functions/fathom-uninstall.tsindex 138251128c9..d8b38fb20e8 100644--- a/packages/twenty-apps/public/fathom/src/logic-functions/fathom-uninstall.ts+++ b/packages/twenty-apps/public/fathom/src/logic-functions/fathom-uninstall.ts@@ -1,12 +1,7 @@import { defineUninstallLogicFunction } from 'twenty-sdk/define';-import {- type AppConnection,- kv,- listConnections,-} from 'twenty-sdk/logic-function';+import { type AppConnection, kv } from 'twenty-sdk/logic-function';import { isDefined } from 'src/utils/is-defined';-import { FATHOM_PROVIDER_NAME } from 'src/constants/fathom.constant';import { FATHOM_UNINSTALL_UNIVERSAL_IDENTIFIER } from 'src/constants/universal-identifiers';import { type FathomWebhookRegistration } from 'src/logic-functions/types/fathom-webhook-registration.type';import { getConnectionAccessTokenOrThrow } from 'src/logic-functions/utils/get-connection-access-token-or-throw.util';@@ -14,6 +9,7 @@ import { getFathomConnectionClaimKey } from 'src/logic-functions/utils/get-fathoimport { createFathomClient } from 'src/logic-functions/utils/create-fathom-client.util';import { getFathomWebhookRegistrationKey } from 'src/logic-functions/utils/get-fathom-webhook-registration-key.util';import { isFathomNotFoundError } from 'src/logic-functions/utils/is-fathom-not-found-error.util';+import { listFathomConnections } from 'src/logic-functions/utils/list-fathom-connections.util';import { toErrorMessage } from 'src/logic-functions/utils/to-error-message.util';const deleteWebhookForConnection = async (@@ -58,9 +54,7 @@ const deleteWebhookForConnection = async (export const fathomUninstallHandler = async (): Promise<{deletedWebhookCount: number;}> => {- const connections = await listConnections({- providerName: FATHOM_PROVIDER_NAME,- });+ const connections = await listFathomConnections();let deletedWebhookCount = 0;for (const connection of connections) {diff --git a/packages/twenty-apps/public/fathom/src/logic-functions/utils/__tests__/list-fathom-connections-for-request.test.ts b/packages/twenty-apps/public/fathom/src/logic-functions/utils/__tests__/list-fathom-connections-for-request.test.tsindex 5cd161fd266..905a1044f95 100644--- a/packages/twenty-apps/public/fathom/src/logic-functions/utils/__tests__/list-fathom-connections-for-request.test.ts+++ b/packages/twenty-apps/public/fathom/src/logic-functions/utils/__tests__/list-fathom-connections-for-request.test.ts@@ -13,11 +13,18 @@ vi.mock('twenty-sdk/logic-function', async (importOriginal) => ({const buildConnection = (id: string, userWorkspaceId: string) => ({id,- visibility: 'user',+ providerName: 'fathom-user',+ scope: 'USER',userWorkspaceId,accessToken: `token-${id}`,});+const SHARED_CONNECTION = {+ ...buildConnection('connection-shared', 'user-workspace-1'),+ providerName: 'fathom',+ scope: 'WORKSPACE',+};+const CONNECTIONS = [buildConnection('connection-1', 'user-workspace-1'),buildConnection('connection-2', 'user-workspace-2'),@@ -26,22 +33,41 @@ const CONNECTIONS = [describe('listFathomConnectionsForRequest', () => {beforeEach(() => {vi.resetAllMocks();- sdkMocks.listConnections.mockResolvedValue(CONNECTIONS);+ sdkMocks.listConnections.mockResolvedValue([+ SHARED_CONNECTION,+ ...CONNECTIONS,+ ]);});it('returns every connected account for a run with nobody behind it', async () => {- const sharedConnection = {- ...buildConnection('connection-shared', 'user-workspace-1'),- visibility: 'workspace',- };+ expect(+ await listFathomConnectionsForRequest({ userWorkspaceId: null }),+ ).toEqual([SHARED_CONNECTION, ...CONNECTIONS]);+ });+ it('returns only the personal connection of the requesting member', async () => {+ expect(+ await listFathomConnectionsForRequest({+ userWorkspaceId: 'user-workspace-2',+ }),+ ).toEqual([CONNECTIONS[1]]);+ });++ it('falls back to the workspace-shared connection for a member without one', async () => {+ expect(+ await listFathomConnectionsForRequest({+ userWorkspaceId: 'user-workspace-3',+ }),+ ).toEqual([SHARED_CONNECTION]);+ });++ it('ignores connections of other providers', async () => {sdkMocks.listConnections.mockResolvedValue([- sharedConnection,- ...CONNECTIONS,+ { ...SHARED_CONNECTION, providerName: 'other-provider' },]);expect(await listFathomConnectionsForRequest({ userWorkspaceId: null }),- ).toEqual([sharedConnection, ...CONNECTIONS]);+ ).toEqual([]);});});diff --git a/packages/twenty-apps/public/fathom/src/logic-functions/utils/list-fathom-connections.util.ts b/packages/twenty-apps/public/fathom/src/logic-functions/utils/list-fathom-connections.util.tsindex f4ba824a5db..d6438c45dd0 100644--- a/packages/twenty-apps/public/fathom/src/logic-functions/utils/list-fathom-connections.util.ts+++ b/packages/twenty-apps/public/fathom/src/logic-functions/utils/list-fathom-connections.util.ts@@ -1,6 +1,8 @@import { type AppConnection, listConnections } from 'twenty-sdk/logic-function';-import { FATHOM_PROVIDER_NAME } from 'src/constants/fathom.constant';+import { FATHOM_PROVIDER_NAMES } from 'src/constants/fathom.constant';-export const listFathomConnections = (): Promise<AppConnection[]> =>- listConnections({ providerName: FATHOM_PROVIDER_NAME });+export const listFathomConnections = async (): Promise<AppConnection[]> =>+ (await listConnections()).filter((connection) =>+ FATHOM_PROVIDER_NAMES.includes(connection.providerName),+ );diff --git a/packages/twenty-apps/public/fathom/src/logic-functions/utils/list-fathom-meeting-page.util.ts b/packages/twenty-apps/public/fathom/src/logic-functions/utils/list-fathom-meeting-page.util.tsindex 86d2961f47c..512f4b27dc5 100644--- a/packages/twenty-apps/public/fathom/src/logic-functions/utils/list-fathom-meeting-page.util.ts+++ b/packages/twenty-apps/public/fathom/src/logic-functions/utils/list-fathom-meeting-page.util.ts@@ -12,14 +12,17 @@ export const listFathomMeetingPage = async ({fathomClient,createdAfter,cursor,+ recordedBy,}: {fathomClient: FathomMeetingLister;createdAfter?: string;cursor?: string;+ recordedBy?: string[];}): Promise<FathomMeetingPage> => {const { result } = await fathomClient.listMeetings({...(isDefined(createdAfter) ? { createdAfter } : {}),...(isDefined(cursor) ? { cursor } : {}),+ ...(isDefined(recordedBy) ? { recordedBy } : {}),includeActionItems: true,});