Skip to content

Add call to get roles from user instance #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions __tests__/services/zendesk-api-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" }];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
22 changes: 18 additions & 4 deletions src/models/zendesk-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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[];
}
Expand Down Expand Up @@ -129,7 +147,3 @@ export interface IMessage {
export interface IMessagesResults extends IZendeskResponse {
messages: IMessage[];
}

export interface ILocalesResults {
locales: IZendeskLocale[];
}
12 changes: 12 additions & 0 deletions src/services/zendesk-api-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
IZendeskLocale,
IZendeskGroup,
IZendeskOrganizations,
IZendeskRoles,
IRolesResults,
ILinesResults,
IZendeskResponse,
Line,
Expand Down Expand Up @@ -249,6 +251,16 @@ export class ZendeskApiService {
(response) => response.organizations
);
}
/**
* Fetch all user instance roles
*/
public async getRoles(fetchAllRoles = true): Promise<IZendeskRoles[]> {
return this.fetchAllPaginatedResults<IRolesResults, IZendeskRoles>(
`/api/v2/custom_roles`,
fetchAllRoles,
(response) => response.custom_roles
);
}
/**
* Fetch all user instance locales
*/
Expand Down