-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding folder nodes mapping the Intercom hierarchy (#9469)
* Handling most folder node creations, need to figure out what to do on revoke and strategy.detroy * rebased on main, now with mimetypes and latest api * Created backfill script * oversights * slightly faster team sync * Moved db calls after core calls, refactored util function * moved upserts to activities * linter * fixed activity import * Fixed backfill collection parents, added dry run option * Teams renamed into Conversation, catching null connector * revert microsoft renaming --------- Co-authored-by: Lucas <[email protected]>
- Loading branch information
Showing
6 changed files
with
337 additions
and
53 deletions.
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
connectors/migrations/20241219_backfill_intercom_data_source_folders.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { makeScript } from "scripts/helpers"; | ||
|
||
import { | ||
getDataSourceNodeMimeType, | ||
getHelpCenterCollectionInternalId, | ||
getHelpCenterInternalId, | ||
getParentIdsForCollection, | ||
getTeamInternalId, | ||
getTeamsInternalId, | ||
} from "@connectors/connectors/intercom/lib/utils"; | ||
import { dataSourceConfigFromConnector } from "@connectors/lib/api/data_source_config"; | ||
import { concurrentExecutor } from "@connectors/lib/async_utils"; | ||
import { upsertDataSourceFolder } from "@connectors/lib/data_sources"; | ||
import { | ||
IntercomCollection, | ||
IntercomHelpCenter, | ||
IntercomTeam, | ||
IntercomWorkspace, | ||
} from "@connectors/lib/models/intercom"; | ||
import { ConnectorResource } from "@connectors/resources/connector_resource"; | ||
|
||
async function createFolderNodes(execute: boolean) { | ||
const connectors = await ConnectorResource.listByType("intercom", {}); | ||
|
||
for (const connector of connectors) { | ||
const dataSourceConfig = dataSourceConfigFromConnector(connector); | ||
|
||
// Create Teams folder | ||
console.log( | ||
`[${connector.id}] -> ${JSON.stringify({ folderId: getTeamsInternalId(connector.id), parents: [getTeamsInternalId(connector.id)] })}` | ||
); | ||
if (execute) { | ||
await upsertDataSourceFolder({ | ||
dataSourceConfig, | ||
folderId: getTeamsInternalId(connector.id), | ||
parents: [getTeamsInternalId(connector.id)], | ||
title: "Conversations", | ||
mimeType: getDataSourceNodeMimeType("CONVERSATIONS_FOLDER"), | ||
}); | ||
} | ||
|
||
const teams = await IntercomTeam.findAll({ | ||
where: { | ||
connectorId: connector.id, | ||
}, | ||
}); | ||
// Create a team folder for each team | ||
await concurrentExecutor( | ||
teams, | ||
async (team) => { | ||
const teamInternalId = getTeamInternalId(connector.id, team.teamId); | ||
console.log( | ||
`[${connector.id}] -> ${JSON.stringify({ folderId: teamInternalId, parents: [teamInternalId, getTeamsInternalId(connector.id)] })}` | ||
); | ||
if (execute) { | ||
await upsertDataSourceFolder({ | ||
dataSourceConfig, | ||
folderId: teamInternalId, | ||
parents: [teamInternalId, getTeamsInternalId(connector.id)], | ||
title: team.name, | ||
mimeType: getDataSourceNodeMimeType("TEAM"), | ||
}); | ||
} | ||
}, | ||
{ concurrency: 16 } | ||
); | ||
|
||
// Length = 1, for loop just in case | ||
const workspaces = await IntercomWorkspace.findAll({ | ||
where: { | ||
connectorId: connector.id, | ||
}, | ||
}); | ||
|
||
for (const workspace of workspaces) { | ||
// Length mostly 1 | ||
const helpCenters = await IntercomHelpCenter.findAll({ | ||
where: { | ||
connectorId: connector.id, | ||
intercomWorkspaceId: workspace.intercomWorkspaceId, | ||
}, | ||
}); | ||
|
||
for (const helpCenter of helpCenters) { | ||
// Create Help Center folder | ||
const helpCenterInternalId = getHelpCenterInternalId( | ||
connector.id, | ||
helpCenter.helpCenterId | ||
); | ||
console.log( | ||
`[${connector.id}] -> ${JSON.stringify({ folderId: helpCenterInternalId, parents: [helpCenterInternalId] })}` | ||
); | ||
if (execute) { | ||
await upsertDataSourceFolder({ | ||
dataSourceConfig, | ||
folderId: helpCenterInternalId, | ||
parents: [helpCenterInternalId], | ||
title: helpCenter.name, | ||
mimeType: getDataSourceNodeMimeType("HELP_CENTER"), | ||
}); | ||
} | ||
|
||
const collections = await IntercomCollection.findAll({ | ||
where: { | ||
connectorId: connector.id, | ||
helpCenterId: helpCenter.helpCenterId, | ||
}, | ||
}); | ||
|
||
// Create a collection folder for each collection | ||
await concurrentExecutor( | ||
collections, | ||
async (collection) => { | ||
const collectionInternalId = getHelpCenterCollectionInternalId( | ||
connector.id, | ||
collection.collectionId | ||
); | ||
const collectionParents = await getParentIdsForCollection({ | ||
connectorId: connector.id, | ||
collectionId: collection.collectionId, | ||
helpCenterId: helpCenter.helpCenterId, | ||
}); | ||
console.log( | ||
`[${connector.id}] -> ${JSON.stringify({ folderId: collectionInternalId, parents: collectionParents })}` | ||
); | ||
if (execute) { | ||
await upsertDataSourceFolder({ | ||
dataSourceConfig, | ||
folderId: collectionInternalId, | ||
parents: collectionParents, | ||
title: collection.name, | ||
mimeType: getDataSourceNodeMimeType("COLLECTION"), | ||
}); | ||
} | ||
}, | ||
{ concurrency: 16 } | ||
); | ||
} | ||
} | ||
} | ||
} | ||
makeScript({}, async ({ execute }) => { | ||
await createFolderNodes(execute); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.