Skip to content

Commit

Permalink
Intercom cli to force resync all articles, and articles only (#6960)
Browse files Browse the repository at this point in the history
  • Loading branch information
PopDaph authored Aug 29, 2024
1 parent de6b30d commit 87cce11
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
50 changes: 37 additions & 13 deletions connectors/src/connectors/intercom/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
IntercomCheckTeamsResponseType,
IntercomCommandType,
IntercomFetchConversationResponseType,
IntercomForceResyncArticlesResponseType,
} from "@dust-tt/types";
import { Op } from "sequelize";

Expand All @@ -14,6 +15,7 @@ import {
fetchIntercomTeams,
} from "@connectors/connectors/intercom/lib/intercom_api";
import {
IntercomArticle,
IntercomConversation,
IntercomTeam,
} from "@connectors/lib/models/intercom";
Expand All @@ -28,24 +30,37 @@ export const intercom = async ({
| IntercomFetchConversationResponseType
| IntercomCheckTeamsResponseType
| IntercomCheckMissingConversationsResponseType
| IntercomForceResyncArticlesResponseType
> => {
const logger = topLogger.child({ majorCommand: "intercom", command, args });

if (!args.connectorId) {
throw new Error("Missing --connectorId argument");
}
const connectorId = args.connectorId.toString();

const connector = await ConnectorResource.fetchById(connectorId);
if (!connector) {
throw new Error(`Connector ${connectorId} not found`);
}
if (connector.type !== "intercom") {
const connectorId = args.connectorId ? args.connectorId.toString() : null;
const connector = connectorId
? await ConnectorResource.fetchById(connectorId)
: null;
if (connector && connector.type !== "intercom") {
throw new Error(`Connector ${args.connectorId} is not of type intercom`);
}

switch (command) {
case "force-resync-articles": {
const force = args.force === "true";
if (!force) {
throw new Error("[Admin] Need to pass --force=true to force resync");
}
logger.info("[Admin] Forcing resync of articles");
const updated = await IntercomArticle.update(
{ lastUpsertedTs: null },
{ where: {} } // Targets all records
);
return {
affectedCount: updated[0],
};
}
case "check-conversation": {
if (!connector) {
throw new Error(`Connector ${connectorId} not found`);
}
if (!args.conversationId) {
throw new Error("Missing --conversationId argument");
}
Expand All @@ -66,7 +81,7 @@ export const intercom = async ({
const conversationOnDB = await IntercomConversation.findOne({
where: {
conversationId,
connectorId,
connectorId: connector.id,
},
});

Expand All @@ -78,6 +93,9 @@ export const intercom = async ({
};
}
case "fetch-conversation": {
if (!connector) {
throw new Error(`Connector ${connectorId} not found`);
}
if (!args.conversationId) {
throw new Error("Missing --conversationId argument");
}
Expand All @@ -96,6 +114,9 @@ export const intercom = async ({
};
}
case "check-missing-conversations": {
if (!connector) {
throw new Error(`Connector ${connectorId} not found`);
}
if (!args.day) {
throw new Error("Missing --day argument");
}
Expand Down Expand Up @@ -133,7 +154,7 @@ export const intercom = async ({
// Fetch all conversations for the day from DB
const convosOnDB = await IntercomConversation.findAll({
where: {
connectorId,
connectorId: connector.id,
conversationCreatedAt: {
[Op.gte]: startOfDay,
[Op.lte]: endOfDay,
Expand All @@ -157,12 +178,15 @@ export const intercom = async ({
};
}
case "check-teams": {
if (!connector) {
throw new Error(`Connector ${connectorId} not found`);
}
logger.info("[Admin] Checking teams");
const accessToken = await getIntercomAccessToken(connector.connectionId);
const teamsOnIntercom = await fetchIntercomTeams({ accessToken });
const teamsOnDb = await IntercomTeam.findAll({
where: {
connectorId,
connectorId: connector.id,
},
});

Expand Down
11 changes: 10 additions & 1 deletion types/src/connectors/admin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,15 @@ export type TemporalCommandType = t.TypeOf<typeof TemporalCommandSchema>;
export const IntercomCommandSchema = t.type({
majorCommand: t.literal("intercom"),
command: t.union([
t.literal("force-resync-articles"),
t.literal("check-conversation"),
t.literal("fetch-conversation"),
t.literal("check-missing-conversations"),
t.literal("check-teams"),
]),
args: t.type({
connectorId: t.number,
force: t.union([t.literal("true"), t.undefined]),
connectorId: t.union([t.number, t.undefined]),
conversationId: t.union([t.number, t.undefined]),
day: t.union([t.string, t.undefined]),
}),
Expand Down Expand Up @@ -198,6 +200,12 @@ export const IntercomCheckMissingConversationsResponseSchema = t.type({
export type IntercomCheckMissingConversationsResponseType = t.TypeOf<
typeof IntercomCheckMissingConversationsResponseSchema
>;
export const IntercomForceResyncArticlesResponseSchema = t.type({
affectedCount: t.number,
});
export type IntercomForceResyncArticlesResponseType = t.TypeOf<
typeof IntercomForceResyncArticlesResponseSchema
>;
/**
* </ Intercom>
*/
Expand Down Expand Up @@ -351,6 +359,7 @@ export const AdminResponseSchema = t.union([
NotionUpsertResponseSchema,
TemporalCheckQueueResponseSchema,
TemporalUnprocessedWorkflowsResponseSchema,
IntercomForceResyncArticlesResponseSchema,
]);

export type AdminResponseType = t.TypeOf<typeof AdminResponseSchema>;

0 comments on commit 87cce11

Please sign in to comment.