diff --git a/__tests__/services/zendesk-api-service.spec.ts b/__tests__/services/zendesk-api-service.spec.ts index ccf51b4..357ef3b 100644 --- a/__tests__/services/zendesk-api-service.spec.ts +++ b/__tests__/services/zendesk-api-service.spec.ts @@ -697,4 +697,64 @@ describe("ZendeskService", () => { expect(requestMock).toHaveBeenCalledTimes(1); }); }); + + describe("createAccessToken", () => { + it("should create an access token with the correct data", async () => { + await service.createZendeskAccessToken(102, ["read", "write"]); + + expect(requestMock).toHaveBeenNthCalledWith(1, { + url: "/api/v2/oauth/tokens.json", + type: "POST", + contentType: "application/json", + data: JSON.stringify({ + token: { + client_id: 102, + scopes: ["read", "write"] + } + }) + }); + expect(requestMock).toHaveBeenCalledTimes(1); + }); + }); + + describe("createZisConnection", () => { + it("should create an access token with the correct data", async () => { + await service.createZisConnection("integrationName", "token", "connectionName", "zendesk.com"); + + expect(requestMock).toHaveBeenNthCalledWith(1, { + url: "/api/services/zis/integrations/integrationName/connections/bearer_token", + type: "POST", + contentType: "application/json", + headers: { + Authorization: "Bearer token" + }, + data: JSON.stringify({ + name: "connectionName", + token: "token", + allowed_domain: "zendesk.com" + }) + }); + expect(requestMock).toHaveBeenCalledTimes(1); + }); + }); + + describe("createZisInboundWebhook", () => { + it("should create an access token with the correct data", async () => { + await service.createZisInboundWebhook("integrationName", "token", "source", "eventType"); + + expect(requestMock).toHaveBeenNthCalledWith(1, { + url: "/api/services/zis/inbound_webhooks/generic/integrationName", + type: "POST", + contentType: "application/json", + headers: { + Authorization: "Bearer token" + }, + data: JSON.stringify({ + source_system: "source", + event_type: "eventType" + }) + }); + expect(requestMock).toHaveBeenCalledTimes(1); + }); + }); }); diff --git a/package.json b/package.json index 8f0119f..df5ca7b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@zendesk/zaf-toolbox", - "version": "0.7.0", + "version": "0.8.0", "description": "A toolbox for ZAF application built with 🩷 by Zendesk Labs", "main": "lib/src/index.js", "types": "lib/src/index.d.ts", diff --git a/src/models/zendesk-api.ts b/src/models/zendesk-api.ts index 4508704..37e1d06 100644 --- a/src/models/zendesk-api.ts +++ b/src/models/zendesk-api.ts @@ -153,3 +153,22 @@ export interface IMessage { export interface IMessagesResults extends IZendeskResponse { messages: IMessage[]; } + +export interface ICreateAccessTokenResponseToken { + url: string; + id: number; + user_id: number; + client_id: number; + token: string; + refresh_token: string | null; + created_at: string; + expires_at: string | null; + used_at: string | null; + scopes: string[]; + refresh_token_expires_at: string | null; + full_token: string; +} + +export interface ICreateAccessTokenResponse { + token: ICreateAccessTokenResponseToken; +} diff --git a/src/models/zendesk-integration-services.ts b/src/models/zendesk-integration-services.ts index f2a973e..76142c4 100644 --- a/src/models/zendesk-integration-services.ts +++ b/src/models/zendesk-integration-services.ts @@ -33,3 +33,27 @@ export interface IZisJobspecsResponse { has_more: boolean; }; } + +export interface IBearerTokenResponse { + name: string; + token: string; + allowed_domain: string; + created_at: string; + updated_at: string; +} + +export interface ICreateConnectionResponse { + bearer_token: IBearerTokenResponse; +} + +export interface ICreateInboundWebhookResponse { + id: string; + uuid: string; + zendesk_account_id: number; + path: string; + integration: string; + source_system: string; + event_type: string; + username: string; + password: string; +} diff --git a/src/services/zendesk-api-service.ts b/src/services/zendesk-api-service.ts index 3dfc877..840fd74 100644 --- a/src/services/zendesk-api-service.ts +++ b/src/services/zendesk-api-service.ts @@ -26,9 +26,12 @@ import { Line, IMessage, IMessagesResults, - IListFilter + IListFilter, + ICreateAccessTokenResponse } from "@models/index"; import { + ICreateConnectionResponse, + ICreateInboundWebhookResponse, IZisIntegration, IZisIntegrationResponse, IZisJobspec, @@ -398,4 +401,84 @@ export class ZendeskApiService { type: "DELETE" }); } + + /** + * Creates an access token for the integration. + * + * @param {number} clientId - The client ID of the integration. + * @param {string[]} scopes - The scopes for the access token, e.g., ["read", "write"]. More information: https://developer.zendesk.com/api-reference/ticketing/oauth/oauth_tokens/#scopes + * @returns {Promise} - The response from the API. + */ + public async createZendeskAccessToken(client_id: number, scopes: string[]): Promise { + return await this.client.request({ + url: "/api/v2/oauth/tokens.json", + type: "POST", + contentType: "application/json", + data: JSON.stringify({ + token: { + client_id, + scopes + } + }) + }); + } + + /** + * Creates a ZIS connection for the given integration. + * + * @param {string} integration_name - The name of the ZIS integration. + * @param {string} token - The token for the connection. + * @param {string} connection_name - The name of the connection. + * @param {string} allowed_domain - The allowed domain for the connection. + * @returns {Promise} - The response from the API. + */ + public async createZisConnection( + integration_name: string, + token: string, + connection_name: string, + allowed_domain: string + ): Promise { + return await this.client.request({ + url: `/api/services/zis/integrations/${integration_name}/connections/bearer_token`, + type: "POST", + contentType: "application/json", + headers: { + Authorization: `Bearer ${token}` + }, + data: JSON.stringify({ + name: connection_name, + token, + allowed_domain + }) + }); + } + + /** + * Creates a ZIS inbound webhook for the given integration. + * + * @param {string} integration_name - The name of the ZIS integration. + * @param {string} token - The token for the webhook. + * @param {string} source_system - The source system for the webhook. + * @param {string} event_type - The event type for the webhook. + * @returns {Promise} - The response from the API. + */ + public async createZisInboundWebhook( + integration_name: string, + token: string, + source_system: string, + event_type: string + ): Promise { + return await this.client.request({ + url: `/api/services/zis/inbound_webhooks/generic/${integration_name}`, + type: "POST", + contentType: "application/json", + headers: { + Authorization: `Bearer ${token}` + }, + data: JSON.stringify({ + source_system, + event_type + }) + }); + } }