From d6d200f51e65eebcf7914689227fb98f3ad2571a Mon Sep 17 00:00:00 2001 From: Victor Piolin Date: Wed, 28 May 2025 15:29:37 -0400 Subject: [PATCH 1/3] feat: Add support for Zis Connection, Zis Token and zis webhook --- .../services/zendesk-api-service.spec.ts | 60 +++++++++++++ src/models/zendesk-api.ts | 19 +++++ src/models/zendesk-integration-services.ts | 24 ++++++ src/services/zendesk-api-service.ts | 85 ++++++++++++++++++- 4 files changed, 187 insertions(+), 1 deletion(-) diff --git a/__tests__/services/zendesk-api-service.spec.ts b/__tests__/services/zendesk-api-service.spec.ts index 71b59c1..23244bd 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/src/models/zendesk-api.ts b/src/models/zendesk-api.ts index 24358c6..280e110 100644 --- a/src/models/zendesk-api.ts +++ b/src/models/zendesk-api.ts @@ -147,3 +147,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 4294f0a..68be2e6 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 + }) + }); + } } From 6cf43e3b43fb9c8847ea0c2f690d13406f28b185 Mon Sep 17 00:00:00 2001 From: Vico1993 Date: Wed, 28 May 2025 19:31:13 +0000 Subject: [PATCH 2/3] [BOT] Bump version from 0.4.3 to 0.5.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a3bb1f5..978802a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@zendesk/zaf-toolbox", - "version": "0.4.3", + "version": "0.5.0", "description": "A toolbox for ZAF application built with 🩷 by Zendesk Labs", "main": "lib/src/index.js", "types": "lib/src/index.d.ts", From 845b21358953df4b38a21eca0b6d2a44c205c3b9 Mon Sep 17 00:00:00 2001 From: Vico1993 Date: Fri, 30 May 2025 18:40:46 +0000 Subject: [PATCH 3/3] [BOT] Bump version from 0.7.0 to 0.8.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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",