Skip to content

Commit

Permalink
[connectors] Add a CLI for Zendesk with a check-is-admin (#8881)
Browse files Browse the repository at this point in the history
* feat: add a CLI for Zendesk with a command check-is-admin

* feat: add Zendesk CLI to admin/cli
  • Loading branch information
aubin-tchoi authored Nov 25, 2024
1 parent 595b27a commit 693bbba
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
39 changes: 39 additions & 0 deletions connectors/src/connectors/zendesk/lib/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { ZendeskCommandType } from "@dust-tt/types";
import type { ZendeskCheckIsAdminResponseType } from "@dust-tt/types/src";

import { getZendeskSubdomainAndAccessToken } from "@connectors/connectors/zendesk/lib/zendesk_access_token";
import { fetchZendeskCurrentUser } from "@connectors/connectors/zendesk/lib/zendesk_api";
import { default as topLogger } from "@connectors/logger/logger";
import { ConnectorResource } from "@connectors/resources/connector_resource";

export const zendesk = async ({
command,
args,
}: ZendeskCommandType): Promise<ZendeskCheckIsAdminResponseType> => {
const logger = topLogger.child({ majorCommand: "zendesk", command, args });

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

switch (command) {
case "check-is-admin": {
if (!connector) {
throw new Error(`Connector ${connectorId} not found`);
}
const user = await fetchZendeskCurrentUser(
await getZendeskSubdomainAndAccessToken(connector.connectionId)
);
logger.info({ user }, "User returned by /users/me");
return {
userActive: user.active,
userRole: user.role,
userIsAdmin: user.role === "admin" && user.active,
};
}
}
};
19 changes: 19 additions & 0 deletions connectors/src/connectors/zendesk/lib/zendesk_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
ZendeskFetchedArticle,
ZendeskFetchedCategory,
ZendeskFetchedTicket,
ZendeskFetchedUser,
} from "@connectors/@types/node-zendesk";
import { ExternalOAuthTokenError } from "@connectors/lib/error";
import logger from "@connectors/logger/logger";
Expand Down Expand Up @@ -349,3 +350,21 @@ export async function fetchZendeskTicketsInBrand({
}
: { tickets: [], meta: { has_more: false, after_cursor: "" } };
}

/**
* Fetches the current user through a call to `/users/me`.
*/
export async function fetchZendeskCurrentUser({
subdomain,
accessToken,
}: {
subdomain: string;
accessToken: string;
}): Promise<ZendeskFetchedUser> {
const response = await fetch(
`https://${subdomain}.zendesk.com/api/v2/users/me`,
{ headers: { Authorization: `Bearer ${accessToken}` } }
);
const data = await response.json();
return data.user;
}
3 changes: 3 additions & 0 deletions connectors/src/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { microsoft } from "@connectors/connectors/microsoft/lib/cli";
import { notion } from "@connectors/connectors/notion/lib/cli";
import { slack } from "@connectors/connectors/slack/lib/cli";
import { launchCrawlWebsiteSchedulerWorkflow } from "@connectors/connectors/webcrawler/temporal/client";
import { zendesk } from "@connectors/connectors/zendesk/lib/cli";
import { getTemporalClient } from "@connectors/lib/temporal";
import { default as topLogger } from "@connectors/logger/logger";
import { ConnectorModel } from "@connectors/resources/storage/models/connector_model";
Expand Down Expand Up @@ -51,6 +52,8 @@ export async function runCommand(adminCommand: AdminCommandType) {
return intercom(adminCommand);
case "microsoft":
return microsoft(adminCommand);
case "zendesk":
return zendesk(adminCommand);
default:
assertNever(adminCommand);
}
Expand Down
25 changes: 25 additions & 0 deletions types/src/connectors/admin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,30 @@ export type IntercomForceResyncArticlesResponseType = t.TypeOf<
* </ Intercom>
*/

/**
* <Zendesk>
*/
export const ZendeskCommandSchema = t.type({
majorCommand: t.literal("zendesk"),
command: t.literal("check-is-admin"),
args: t.type({
connectorId: t.union([t.number, t.undefined]),
}),
});

export type ZendeskCommandType = t.TypeOf<typeof ZendeskCommandSchema>;
export const ZendeskCheckIsAdminResponseSchema = t.type({
userRole: t.string,
userActive: t.boolean,
userIsAdmin: t.boolean,
});
export type ZendeskCheckIsAdminResponseType = t.TypeOf<
typeof ZendeskCheckIsAdminResponseSchema
>;
/**
* </Zendesk>
*/

export const MicrosoftCommandSchema = t.type({
majorCommand: t.literal("microsoft"),
command: t.union([
Expand Down Expand Up @@ -243,6 +267,7 @@ export const AdminCommandSchema = t.union([
SlackCommandSchema,
TemporalCommandSchema,
WebcrawlerCommandSchema,
ZendeskCommandSchema,
]);

export type AdminCommandType = t.TypeOf<typeof AdminCommandSchema>;
Expand Down

0 comments on commit 693bbba

Please sign in to comment.