diff --git a/connectors/src/connectors/intercom/index.ts b/connectors/src/connectors/intercom/index.ts index b2918debf3b4..f22e9869c896 100644 --- a/connectors/src/connectors/intercom/index.ts +++ b/connectors/src/connectors/intercom/index.ts @@ -67,8 +67,9 @@ export class IntercomConnectorManager extends BaseConnectorManager { let connector = null; try { - const intercomWorkspace = - await fetchIntercomWorkspace(intercomAccessToken); + const intercomWorkspace = await fetchIntercomWorkspace({ + accessToken: intercomAccessToken, + }); if (!intercomWorkspace) { return new Err( new Error( @@ -158,8 +159,10 @@ export class IntercomConnectorManager extends BaseConnectorManager { if (connectionId) { const newConnectionId = connectionId; - const newIntercomWorkspace = - await fetchIntercomWorkspace(newConnectionId); + const accessToken = await getIntercomAccessToken(newConnectionId); + const newIntercomWorkspace = await fetchIntercomWorkspace({ + accessToken, + }); if (!newIntercomWorkspace) { return new Err({ diff --git a/connectors/src/connectors/intercom/lib/conversation_permissions.ts b/connectors/src/connectors/intercom/lib/conversation_permissions.ts index d465911a6222..59a1dbb343fe 100644 --- a/connectors/src/connectors/intercom/lib/conversation_permissions.ts +++ b/connectors/src/connectors/intercom/lib/conversation_permissions.ts @@ -5,6 +5,7 @@ import type { ModelId, } from "@dust-tt/types"; +import { getIntercomAccessToken } from "@connectors/connectors/intercom/lib/intercom_access_token"; import { fetchIntercomTeam, fetchIntercomTeams, @@ -41,7 +42,8 @@ export async function allowSyncTeam({ }); } if (!team) { - const teamOnIntercom = await fetchIntercomTeam(connectionId, teamId); + const accessToken = await getIntercomAccessToken(connectionId); + const teamOnIntercom = await fetchIntercomTeam({ accessToken, teamId }); if (teamOnIntercom) { team = await IntercomTeam.create({ connectorId, @@ -177,7 +179,8 @@ export async function retrieveIntercomConversationsPermissions({ }); } } else { - const teams = await fetchIntercomTeams(connector.connectionId); + const accessToken = await getIntercomAccessToken(connector.connectionId); + const teams = await fetchIntercomTeams({ accessToken }); if (isRootLevel) { nodes.push({ provider: "intercom", diff --git a/connectors/src/connectors/intercom/lib/help_center_permissions.ts b/connectors/src/connectors/intercom/lib/help_center_permissions.ts index b35febdb5814..ce647ab7b5e1 100644 --- a/connectors/src/connectors/intercom/lib/help_center_permissions.ts +++ b/connectors/src/connectors/intercom/lib/help_center_permissions.ts @@ -6,6 +6,7 @@ import type { } from "@dust-tt/types"; import { Op } from "sequelize"; +import { getIntercomAccessToken } from "@connectors/connectors/intercom/lib/intercom_access_token"; import { fetchIntercomCollection, fetchIntercomCollections, @@ -59,17 +60,17 @@ export async function allowSyncHelpCenter({ helpCenterId, }, }); - + const accessToken = await getIntercomAccessToken(connectionId); if (helpCenter?.permission === "none") { await helpCenter.update({ permission: "read", }); } if (!helpCenter) { - const helpCenterOnIntercom = await fetchIntercomHelpCenter( - connectionId, - helpCenterId - ); + const helpCenterOnIntercom = await fetchIntercomHelpCenter({ + accessToken, + helpCenterId, + }); if (helpCenterOnIntercom) { helpCenter = await IntercomHelpCenter.create({ connectorId: connectorId, @@ -90,11 +91,11 @@ export async function allowSyncHelpCenter({ // If withChildren we are allowing the full Help Center. if (withChildren) { - const level1Collections = await fetchIntercomCollections( - connectionId, - helpCenter.helpCenterId, - null - ); + const level1Collections = await fetchIntercomCollections({ + accessToken, + helpCenterId: helpCenter.helpCenterId, + parentId: null, + }); const permissionUpdatePromises = level1Collections.map((c1) => allowSyncCollection({ connectorId, @@ -191,16 +192,17 @@ export async function allowSyncCollection({ collectionId, }, }); + const accessToken = await getIntercomAccessToken(connectionId); if (collection?.permission === "none") { await collection.update({ permission: "read", }); } else if (!collection) { - const intercomCollection = await fetchIntercomCollection( - connectionId, - collectionId - ); + const intercomCollection = await fetchIntercomCollection({ + accessToken, + collectionId, + }); const hpId = helpCenterId || intercomCollection?.help_center_id; @@ -237,11 +239,11 @@ export async function allowSyncCollection({ helpCenterId: collection.helpCenterId, region, }), - fetchIntercomCollections( - connectionId, - collection.helpCenterId, - collection.collectionId - ), + fetchIntercomCollections({ + accessToken, + helpCenterId: collection.helpCenterId, + parentId: collection.collectionId, + }), ]); const collectionPermissionPromises = childrenCollections.map((c) => @@ -362,6 +364,8 @@ export async function retrieveIntercomHelpCentersPermissions({ throw new Error("Connector not found"); } + const accessToken = await getIntercomAccessToken(connector.connectionId); + const isReadPermissionsOnly = filterPermission === "read"; const isRootLevel = !parentInternalId; let nodes: ContentNode[] = []; @@ -393,9 +397,7 @@ export async function retrieveIntercomHelpCentersPermissions({ lastUpdatedAt: helpCenter.updatedAt.getTime(), })); } else { - const helpCenters = await fetchIntercomHelpCenters( - connector.connectionId - ); + const helpCenters = await fetchIntercomHelpCenters({ accessToken }); nodes = helpCenters.map((helpCenter) => ({ provider: connector.type, internalId: getHelpCenterInternalId(connectorId, helpCenter.id), @@ -458,11 +460,11 @@ export async function retrieveIntercomHelpCentersPermissions({ lastUpdatedAt: collection.updatedAt.getTime() || null, })); } else { - const collectionsInIntercom = await fetchIntercomCollections( - connector.connectionId, - helpCenterParentId, - parentId - ); + const collectionsInIntercom = await fetchIntercomCollections({ + accessToken, + helpCenterId: helpCenterParentId, + parentId, + }); nodes = collectionsInIntercom.map((collection) => { const matchingCollectionInDb = collectionsInDb.find( (c) => c.collectionId === collection.id diff --git a/connectors/src/connectors/intercom/lib/intercom_api.ts b/connectors/src/connectors/intercom/lib/intercom_api.ts index cb784414b4d6..7785844f5809 100644 --- a/connectors/src/connectors/intercom/lib/intercom_api.ts +++ b/connectors/src/connectors/intercom/lib/intercom_api.ts @@ -101,7 +101,11 @@ async function queryIntercomAPI({ /** * Return the Intercom Workspace. */ -export async function fetchIntercomWorkspace(accessToken: string): Promise<{ +export async function fetchIntercomWorkspace({ + accessToken, +}: { + accessToken: string; +}): Promise<{ id: string; name: string; region: string; @@ -130,9 +134,11 @@ export async function fetchIntercomWorkspace(accessToken: string): Promise<{ /** * Return the list of Help Centers of the Intercom workspace */ -export async function fetchIntercomHelpCenters( - accessToken: string -): Promise { +export async function fetchIntercomHelpCenters({ + accessToken, +}: { + accessToken: string; +}): Promise { const response: { type: "list"; data: IntercomHelpCenterType[]; @@ -148,10 +154,13 @@ export async function fetchIntercomHelpCenters( /** * Return the detail of Help Center */ -export async function fetchIntercomHelpCenter( - accessToken: string, - helpCenterId: string -): Promise { +export async function fetchIntercomHelpCenter({ + accessToken, + helpCenterId, +}: { + accessToken: string; + helpCenterId: string; +}): Promise { const response = await queryIntercomAPI({ accessToken, path: `help_center/help_centers/${helpCenterId}`, @@ -164,11 +173,15 @@ export async function fetchIntercomHelpCenter( /** * Return the list of Collections filtered by Help Center and parent Collection. */ -export async function fetchIntercomCollections( - accessToken: string, - helpCenterId: string, - parentId: string | null -): Promise { +export async function fetchIntercomCollections({ + accessToken, + helpCenterId, + parentId, +}: { + accessToken: string; + helpCenterId: string; + parentId: string | null; +}): Promise { let response, hasMore; let page = 1; const collections: IntercomCollectionType[] = []; @@ -204,10 +217,13 @@ export async function fetchIntercomCollections( /** * Return the detail of a Collection. */ -export async function fetchIntercomCollection( - accessToken: string, - collectionId: string -): Promise { +export async function fetchIntercomCollection({ + accessToken, + collectionId, +}: { + accessToken: string; + collectionId: string; +}): Promise { const response = await queryIntercomAPI({ accessToken, path: `help_center/collections/${collectionId}`, @@ -255,9 +271,11 @@ export async function fetchIntercomArticles({ /** * Return the list of Teams. */ -export async function fetchIntercomTeams( - accessToken: string -): Promise { +export async function fetchIntercomTeams({ + accessToken, +}: { + accessToken: string; +}): Promise { const response = await queryIntercomAPI({ accessToken, path: `teams`, @@ -270,10 +288,13 @@ export async function fetchIntercomTeams( /** * Return the detail of a Team. */ -export async function fetchIntercomTeam( - accessToken: string, - teamId: string -): Promise { +export async function fetchIntercomTeam({ + accessToken, + teamId, +}: { + accessToken: string; + teamId: string; +}): Promise { const response = await queryIntercomAPI({ accessToken, path: `teams/${teamId}`, diff --git a/connectors/src/connectors/intercom/temporal/activities.ts b/connectors/src/connectors/intercom/temporal/activities.ts index 394a119ee1dc..83cab612914b 100644 --- a/connectors/src/connectors/intercom/temporal/activities.ts +++ b/connectors/src/connectors/intercom/temporal/activities.ts @@ -145,10 +145,11 @@ export async function syncHelpCenterOnlyActivity({ } // If the help center is not on intercom anymore we delete the Help Center data - const helpCenterOnIntercom = await fetchIntercomHelpCenter( - connector.connectionId, - helpCenterOnDb.helpCenterId - ); + const accessToken = await getIntercomAccessToken(connector.connectionId); + const helpCenterOnIntercom = await fetchIntercomHelpCenter({ + accessToken, + helpCenterId: helpCenterOnDb.helpCenterId, + }); if (!helpCenterOnIntercom) { await removeHelpCenter({ connectorId, @@ -289,10 +290,11 @@ export async function syncLevel1CollectionWithChildrenActivity({ } // If the collection is not present on Intercom anymore we delete the collection and its children - const collectionOnIntercom = await fetchIntercomCollection( - connector.connectionId, - collectionOnDB.collectionId - ); + const accessToken = await getIntercomAccessToken(connector.connectionId); + const collectionOnIntercom = await fetchIntercomCollection({ + accessToken, + collectionId: collectionOnDB.collectionId, + }); if (collectionOnIntercom === null) { await deleteCollectionWithChildren({ connectorId, @@ -463,10 +465,8 @@ export async function syncTeamOnlyActivity({ } // If the team does not exists on Intercom we delete the team and its conversations - const teamOnIntercom = await fetchIntercomTeam( - connector.connectionId, - teamId - ); + const accessToken = await getIntercomAccessToken(connector.connectionId); + const teamOnIntercom = await fetchIntercomTeam({ accessToken, teamId }); if (!teamOnIntercom) { await deleteTeamAndConversations({ connectorId, diff --git a/connectors/src/connectors/intercom/temporal/sync_help_center.ts b/connectors/src/connectors/intercom/temporal/sync_help_center.ts index 8d4c1f02106e..aa3ecd9929a6 100644 --- a/connectors/src/connectors/intercom/temporal/sync_help_center.ts +++ b/connectors/src/connectors/intercom/temporal/sync_help_center.ts @@ -1,6 +1,7 @@ import type { ModelId } from "@dust-tt/types"; import TurndownService from "turndown"; +import { getIntercomAccessToken } from "@connectors/connectors/intercom/lib/intercom_access_token"; import { fetchIntercomCollections } from "@connectors/connectors/intercom/lib/intercom_api"; import type { IntercomArticleType, @@ -201,11 +202,12 @@ export async function upsertCollectionWithChildren({ } // Then we call ourself recursively on the children collections - const childrenCollectionsOnIntercom = await fetchIntercomCollections( - connectionId, + const accessToken = await getIntercomAccessToken(connectionId); + const childrenCollectionsOnIntercom = await fetchIntercomCollections({ + accessToken, helpCenterId, - collection.id - ); + parentId: collection.id, + }); await Promise.all( childrenCollectionsOnIntercom.map(async (collectionOnIntercom) => { diff --git a/connectors/src/lib/cli.ts b/connectors/src/lib/cli.ts index 5c65a4974405..8d1ff156c94a 100644 --- a/connectors/src/lib/cli.ts +++ b/connectors/src/lib/cli.ts @@ -1392,8 +1392,8 @@ export const intercom = async ({ } case "check-teams": { logger.info("[Admin] Checking teams"); - - const teamsOnIntercom = await fetchIntercomTeams(connector.connectionId); + const accessToken = await getIntercomAccessToken(connector.connectionId); + const teamsOnIntercom = await fetchIntercomTeams({ accessToken }); const teamsOnDb = await IntercomTeam.findAll({ where: { connectorId,