diff --git a/__tests__/services/zendesk-api-service.spec.ts b/__tests__/services/zendesk-api-service.spec.ts index d833656..9223e98 100644 --- a/__tests__/services/zendesk-api-service.spec.ts +++ b/__tests__/services/zendesk-api-service.spec.ts @@ -428,6 +428,43 @@ describe("ZendeskService", () => { expect(result).toEqual(organizations); }); }); + + describe("getRoles", () => { + it("should call the API and return the roles", async () => { + const custom_roles = [{ name: "role1" }]; + requestMock.mockResolvedValueOnce({ custom_roles }); + + const result = await service.getRoles(); + + expect(requestMock).toHaveBeenCalledWith(`/api/v2/custom_roles`); + expect(result).toEqual(custom_roles); + }); + + it("should continue calling the API until next_page disappears", async () => { + const custom_roles = [{ name: "role1" }]; + requestMock + .mockResolvedValueOnce({ custom_roles, next_page: "next_page" }) + .mockResolvedValueOnce({ custom_roles: [] }); + + const result = await service.getRoles(); + + expect(requestMock).toHaveBeenCalledTimes(2); + expect(requestMock).toHaveBeenNthCalledWith(1, `/api/v2/custom_roles`); + expect(requestMock).toHaveBeenNthCalledWith(2, "next_page"); + expect(result).toEqual(custom_roles); + }); + + it("should only call the API one time with fetchAllRoles set to false", async () => { + const custom_roles = [{ name: "role1" }]; + requestMock.mockResolvedValueOnce({ custom_roles, next_page: "next_page" }); + + const result = await service.getRoles(false); + + expect(requestMock).toHaveBeenCalledTimes(1); + expect(requestMock).toHaveBeenCalledWith(`/api/v2/custom_roles`); + expect(result).toEqual(custom_roles); + }); + }); describe("getLocales", () => { it("should fetch and return locales", async () => { const locales = [{ locale: "en-US" }]; diff --git a/package.json b/package.json index 2f390b3..fe211af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@zendesk/zaf-toolbox", - "version": "0.4.2", + "version": "0.4.3", "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 a6b757b..24358c6 100644 --- a/src/models/zendesk-api.ts +++ b/src/models/zendesk-api.ts @@ -33,6 +33,16 @@ export interface IZendeskOrganizations { url: string; } +export interface IZendeskRoles { + id: number; + name: string; + created_at: string; + updated_at: string; + description: string; + role_type: number; + team_member_count: number; +} + export interface IZendeskLocale { id: number; name: string; @@ -54,6 +64,14 @@ export interface IOrganizationsResults extends IZendeskResponse { organizations: IZendeskOrganizations[]; } +export interface IRolesResults extends IZendeskResponse { + custom_roles: IZendeskRoles[]; +} + +export interface ILocalesResults { + locales: IZendeskLocale[]; +} + export interface ILinesResults extends IZendeskResponse { lines: Line[]; } @@ -129,7 +147,3 @@ export interface IMessage { export interface IMessagesResults extends IZendeskResponse { messages: IMessage[]; } - -export interface ILocalesResults { - locales: IZendeskLocale[]; -} diff --git a/src/services/zendesk-api-service.ts b/src/services/zendesk-api-service.ts index f348153..9e0ac24 100644 --- a/src/services/zendesk-api-service.ts +++ b/src/services/zendesk-api-service.ts @@ -19,6 +19,8 @@ import { IZendeskLocale, IZendeskGroup, IZendeskOrganizations, + IZendeskRoles, + IRolesResults, ILinesResults, IZendeskResponse, Line, @@ -249,6 +251,16 @@ export class ZendeskApiService { (response) => response.organizations ); } + /** + * Fetch all user instance roles + */ + public async getRoles(fetchAllRoles = true): Promise { + return this.fetchAllPaginatedResults( + `/api/v2/custom_roles`, + fetchAllRoles, + (response) => response.custom_roles + ); + } /** * Fetch all user instance locales */