Skip to content

Commit

Permalink
more refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Fontanier committed Jul 19, 2024
1 parent 6e65dbb commit 106e427
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 75 deletions.
11 changes: 7 additions & 4 deletions connectors/src/connectors/intercom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ export class IntercomConnectorManager extends BaseConnectorManager<null> {
let connector = null;

try {
const intercomWorkspace =
await fetchIntercomWorkspace(intercomAccessToken);
const intercomWorkspace = await fetchIntercomWorkspace({
accessToken: intercomAccessToken,
});
if (!intercomWorkspace) {
return new Err(
new Error(
Expand Down Expand Up @@ -158,8 +159,10 @@ export class IntercomConnectorManager extends BaseConnectorManager<null> {

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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ModelId,
} from "@dust-tt/types";

import { getIntercomAccessToken } from "@connectors/connectors/intercom/lib/intercom_access_token";
import {
fetchIntercomTeam,
fetchIntercomTeams,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
56 changes: 29 additions & 27 deletions connectors/src/connectors/intercom/lib/help_center_permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
69 changes: 45 additions & 24 deletions connectors/src/connectors/intercom/lib/intercom_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IntercomHelpCenterType[]> {
export async function fetchIntercomHelpCenters({
accessToken,
}: {
accessToken: string;
}): Promise<IntercomHelpCenterType[]> {
const response: {
type: "list";
data: IntercomHelpCenterType[];
Expand All @@ -148,10 +154,13 @@ export async function fetchIntercomHelpCenters(
/**
* Return the detail of Help Center
*/
export async function fetchIntercomHelpCenter(
accessToken: string,
helpCenterId: string
): Promise<IntercomHelpCenterType | null> {
export async function fetchIntercomHelpCenter({
accessToken,
helpCenterId,
}: {
accessToken: string;
helpCenterId: string;
}): Promise<IntercomHelpCenterType | null> {
const response = await queryIntercomAPI({
accessToken,
path: `help_center/help_centers/${helpCenterId}`,
Expand All @@ -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<IntercomCollectionType[]> {
export async function fetchIntercomCollections({
accessToken,
helpCenterId,
parentId,
}: {
accessToken: string;
helpCenterId: string;
parentId: string | null;
}): Promise<IntercomCollectionType[]> {
let response, hasMore;
let page = 1;
const collections: IntercomCollectionType[] = [];
Expand Down Expand Up @@ -204,10 +217,13 @@ export async function fetchIntercomCollections(
/**
* Return the detail of a Collection.
*/
export async function fetchIntercomCollection(
accessToken: string,
collectionId: string
): Promise<IntercomCollectionType | null> {
export async function fetchIntercomCollection({
accessToken,
collectionId,
}: {
accessToken: string;
collectionId: string;
}): Promise<IntercomCollectionType | null> {
const response = await queryIntercomAPI({
accessToken,
path: `help_center/collections/${collectionId}`,
Expand Down Expand Up @@ -255,9 +271,11 @@ export async function fetchIntercomArticles({
/**
* Return the list of Teams.
*/
export async function fetchIntercomTeams(
accessToken: string
): Promise<IntercomTeamType[]> {
export async function fetchIntercomTeams({
accessToken,
}: {
accessToken: string;
}): Promise<IntercomTeamType[]> {
const response = await queryIntercomAPI({
accessToken,
path: `teams`,
Expand All @@ -270,10 +288,13 @@ export async function fetchIntercomTeams(
/**
* Return the detail of a Team.
*/
export async function fetchIntercomTeam(
accessToken: string,
teamId: string
): Promise<IntercomTeamType | null> {
export async function fetchIntercomTeam({
accessToken,
teamId,
}: {
accessToken: string;
teamId: string;
}): Promise<IntercomTeamType | null> {
const response = await queryIntercomAPI({
accessToken,
path: `teams/${teamId}`,
Expand Down
24 changes: 12 additions & 12 deletions connectors/src/connectors/intercom/temporal/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions connectors/src/connectors/intercom/temporal/sync_help_center.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions connectors/src/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 106e427

Please sign in to comment.