Skip to content

Commit 5ad9aba

Browse files
authored
Merge pull request #134 from zendesk/pol-mampey/add-get-roles-call
Add call to get roles from user instance
2 parents 9889d99 + 115a04d commit 5ad9aba

4 files changed

Lines changed: 68 additions & 5 deletions

File tree

__tests__/services/zendesk-api-service.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,43 @@ describe("ZendeskService", () => {
428428
expect(result).toEqual(organizations);
429429
});
430430
});
431+
432+
describe("getRoles", () => {
433+
it("should call the API and return the roles", async () => {
434+
const custom_roles = [{ name: "role1" }];
435+
requestMock.mockResolvedValueOnce({ custom_roles });
436+
437+
const result = await service.getRoles();
438+
439+
expect(requestMock).toHaveBeenCalledWith(`/api/v2/custom_roles`);
440+
expect(result).toEqual(custom_roles);
441+
});
442+
443+
it("should continue calling the API until next_page disappears", async () => {
444+
const custom_roles = [{ name: "role1" }];
445+
requestMock
446+
.mockResolvedValueOnce({ custom_roles, next_page: "next_page" })
447+
.mockResolvedValueOnce({ custom_roles: [] });
448+
449+
const result = await service.getRoles();
450+
451+
expect(requestMock).toHaveBeenCalledTimes(2);
452+
expect(requestMock).toHaveBeenNthCalledWith(1, `/api/v2/custom_roles`);
453+
expect(requestMock).toHaveBeenNthCalledWith(2, "next_page");
454+
expect(result).toEqual(custom_roles);
455+
});
456+
457+
it("should only call the API one time with fetchAllRoles set to false", async () => {
458+
const custom_roles = [{ name: "role1" }];
459+
requestMock.mockResolvedValueOnce({ custom_roles, next_page: "next_page" });
460+
461+
const result = await service.getRoles(false);
462+
463+
expect(requestMock).toHaveBeenCalledTimes(1);
464+
expect(requestMock).toHaveBeenCalledWith(`/api/v2/custom_roles`);
465+
expect(result).toEqual(custom_roles);
466+
});
467+
});
431468
describe("getLocales", () => {
432469
it("should fetch and return locales", async () => {
433470
const locales = [{ locale: "en-US" }];

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zendesk/zaf-toolbox",
3-
"version": "0.4.2",
3+
"version": "0.4.3",
44
"description": "A toolbox for ZAF application built with 🩷 by Zendesk Labs",
55
"main": "lib/src/index.js",
66
"types": "lib/src/index.d.ts",

src/models/zendesk-api.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ export interface IZendeskOrganizations {
3333
url: string;
3434
}
3535

36+
export interface IZendeskRoles {
37+
id: number;
38+
name: string;
39+
created_at: string;
40+
updated_at: string;
41+
description: string;
42+
role_type: number;
43+
team_member_count: number;
44+
}
45+
3646
export interface IZendeskLocale {
3747
id: number;
3848
name: string;
@@ -54,6 +64,14 @@ export interface IOrganizationsResults extends IZendeskResponse {
5464
organizations: IZendeskOrganizations[];
5565
}
5666

67+
export interface IRolesResults extends IZendeskResponse {
68+
custom_roles: IZendeskRoles[];
69+
}
70+
71+
export interface ILocalesResults {
72+
locales: IZendeskLocale[];
73+
}
74+
5775
export interface ILinesResults extends IZendeskResponse {
5876
lines: Line[];
5977
}
@@ -129,7 +147,3 @@ export interface IMessage {
129147
export interface IMessagesResults extends IZendeskResponse {
130148
messages: IMessage[];
131149
}
132-
133-
export interface ILocalesResults {
134-
locales: IZendeskLocale[];
135-
}

src/services/zendesk-api-service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
IZendeskLocale,
2020
IZendeskGroup,
2121
IZendeskOrganizations,
22+
IZendeskRoles,
23+
IRolesResults,
2224
ILinesResults,
2325
IZendeskResponse,
2426
Line,
@@ -249,6 +251,16 @@ export class ZendeskApiService {
249251
(response) => response.organizations
250252
);
251253
}
254+
/**
255+
* Fetch all user instance roles
256+
*/
257+
public async getRoles(fetchAllRoles = true): Promise<IZendeskRoles[]> {
258+
return this.fetchAllPaginatedResults<IRolesResults, IZendeskRoles>(
259+
`/api/v2/custom_roles`,
260+
fetchAllRoles,
261+
(response) => response.custom_roles
262+
);
263+
}
252264
/**
253265
* Fetch all user instance locales
254266
*/

0 commit comments

Comments
 (0)