Skip to content

Commit

Permalink
Add Zendesk api wrapper functions (#8157)
Browse files Browse the repository at this point in the history
* feat: add a function getZendeskAccessToken

* feat: add an API wrapper to handle data fetching

* fix: remove the await .json since it's already handled in callZendeskApi

* feat: widen the OAuth scope to read to allow getting brands

* feat: replace our wrapper with the lib node-zendesk
  • Loading branch information
aubin-tchoi authored Oct 22, 2024
1 parent 9b98f26 commit 83daae4
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 5 deletions.
45 changes: 42 additions & 3 deletions connectors/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions connectors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@types/express": "^4.17.17",
"@types/fs-extra": "^11.0.1",
"@types/minimist": "^1.2.2",
"@types/node-zendesk": "^2.0.15",
"@types/remove-markdown": "^0.3.4",
"@types/uuid": "^9.0.2",
"axios": "^1.5.1",
Expand All @@ -48,6 +49,7 @@
"micromark-extension-gfm": "^3.0.0",
"minimist": "^1.2.8",
"morgan": "^1.10.0",
"node-zendesk": "^5.0.13",
"octokit": "^3.1.2",
"p-queue": "^7.3.4",
"pg": "^8.8.0",
Expand Down
101 changes: 101 additions & 0 deletions connectors/src/connectors/zendesk/lib/node-zendesk-types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import "node-zendesk";

interface Brand {
url: string;
id: number;
name: string;
brand_url: string;
subdomain: string;
host_mapping: string | null;
has_help_center: boolean;
help_center_state: string;
active: boolean;
default: boolean;
is_deleted: boolean;
logo: object | null;
ticket_form_ids: number[];
signature_template: string;
created_at: string;
updated_at: string;
}

interface Response {
status: number;
headers: object;
statusText: string;
}

interface Category {
id: number;
url: string;
html_url: string;
position: number;
created_at: string;
updated_at: string;
name: string;
description: string;
locale: string;
source_locale: string;
outdated: boolean;
}

interface Article {
id: number;
url: string;
html_url: string;
author_id: number;
comments_disabled: boolean;
draft: boolean;
promoted: boolean;
position: number;
vote_sum: number;
vote_count: number;
section_id: number;
created_at: string;
updated_at: string;
name: string;
title: string;
source_locale: string;
locale: string;
outdated: boolean;
outdated_locales: string[];
edited_at: string;
user_segment_id: number;
permission_group_id: number;
content_tag_ids: number[];
label_names: string[];
body: string;
user_segment_ids: number[];
}

declare module "node-zendesk" {
interface Client {
brand: {
list: () => Promise<{
response: Response;
result: Brand[];
}>;
show: (
brandId: number
) => Promise<{ response: Response; result: { brand: Brand } }>;
};
helpcenter: {
categories: {
list: () => Promise<Category[]>;
show: (
categoryId: number
) => Promise<{ response: Response; result: Category }>;
};
articles: {
list: () => Promise<Article[]>;
show: (
articleId: number
) => Promise<{ response: Response; result: Article }>;
listByCategory: (categoryId: number) => Promise<Article[]>;
listSinceStartTime: (startTime: number) => Promise<Article[]>;
};
};
}

export function createClient(options: object): Client;
}
13 changes: 13 additions & 0 deletions connectors/src/connectors/zendesk/lib/zendesk_access_token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getOAuthConnectionAccessTokenWithThrow } from "@connectors/lib/oauth";
import logger from "@connectors/logger/logger";

export async function getZendeskAccessToken(
connectionId: string
): Promise<string> {
const token = await getOAuthConnectionAccessTokenWithThrow({
logger,
provider: "zendesk",
connectionId,
});
return token.access_token;
}
11 changes: 11 additions & 0 deletions connectors/src/connectors/zendesk/lib/zendesk_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createClient } from "node-zendesk";

export function createZendeskClient({
token,
subdomain = "d3v-dust",
}: {
token: string;
subdomain?: string;
}) {
return createClient({ oauth: true, token, subdomain });
}
2 changes: 1 addition & 1 deletion core/src/oauth/providers/zendesk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Provider for ZendeskConnectionProvider {
"client_id": *OAUTH_ZENDESK_CLIENT_ID,
"client_secret": *OAUTH_ZENDESK_CLIENT_SECRET,
"redirect_uri": redirect_uri,
"scope": "tickets:read hc:read"
"scope": "read"
});

let req = reqwest::Client::new()
Expand Down
2 changes: 1 addition & 1 deletion front/lib/api/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ const PROVIDER_STRATEGIES: Record<
},
zendesk: {
setupUri: (connection) => {
const scopes = ["tickets:read", "hc:read"];
const scopes = ["read"];
return (
`https://d3v-dust.zendesk.com/oauth/authorizations/new?` +
`client_id=${config.getOAuthZendeskClientId()}` +
Expand Down

0 comments on commit 83daae4

Please sign in to comment.