Skip to content
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
132 changes: 132 additions & 0 deletions __tests__/services/zendesk-api-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,138 @@ describe("ZendeskService", () => {
});
});

describe("getUsersByIds", () => {
const mockUsers: IZendeskUser[] = [
{
id: 1,
name: "Test User 1",
email: "test1@example.com",
created_at: "2023-01-01T00:00:00Z",
updated_at: "2023-01-01T00:00:00Z",
url: "https://example.zendesk.com/api/v2/users/1.json",
time_zone: "UTC",
iana_time_zone: "UTC",
phone: null,
photo: null,
locale_id: 1,
locale: "en-US",
organization_id: 100,
role: "end-user",
verified: true,
external_id: null,
tags: [],
alias: "",
active: true,
shared: false,
shared_agent: false,
shared_phone_number: null,
last_login_at: "2023-01-01T00:00:00Z",
two_factor_auth_enabled: null,
signature: "",
details: "",
notes: "",
role_type: 0,
custom_role_id: 0,
moderator: false,
ticket_restriction: null,
only_private_comments: false,
restricted_agent: false,
suspended: false,
default_group_id: 0,
report_csv: false,
user_fields: {}
},
{
id: 2,
name: "Test User 2",
email: "test2@example.com",
created_at: "2023-01-02T00:00:00Z",
updated_at: "2023-01-02T00:00:00Z",
url: "https://example.zendesk.com/api/v2/users/2.json",
time_zone: "UTC",
iana_time_zone: "UTC",
phone: null,
photo: null,
locale_id: 1,
locale: "en-US",
organization_id: 100,
role: "end-user",
verified: true,
external_id: null,
tags: [],
alias: "",
active: true,
shared: false,
shared_agent: false,
shared_phone_number: null,
last_login_at: "2023-01-02T00:00:00Z",
two_factor_auth_enabled: null,
signature: "",
details: "",
notes: "",
role_type: 0,
custom_role_id: 0,
moderator: false,
ticket_restriction: null,
only_private_comments: false,
restricted_agent: false,
suspended: false,
default_group_id: 0,
report_csv: false,
user_fields: {}
}
];

it("should retrieve multiple users by their IDs", async () => {
requestMock.mockResolvedValueOnce({ users: mockUsers });

const result = await service.getUsersByIds([1, 2]);

expect(requestMock).toHaveBeenCalledWith({
url: `/api/v2/users/show_many?ids=1,2`,
type: "GET",
contentType: "application/json"
});
expect(result).toEqual(mockUsers);
});

it("should handle single user ID", async () => {
requestMock.mockResolvedValueOnce({ users: [mockUsers[0]] });

const result = await service.getUsersByIds([1]);

expect(requestMock).toHaveBeenCalledWith({
url: `/api/v2/users/show_many?ids=1`,
type: "GET",
contentType: "application/json"
});
expect(result).toEqual([mockUsers[0]]);
});

it("should throw an error when trying to retrieve more than 100 users", async () => {
const manyUserIds = Array.from({ length: 101 }, (_, index) => index + 1);

await expect(service.getUsersByIds(manyUserIds)).rejects.toThrow(
"A limit of 100 users can be retrieved at a time."
);

expect(requestMock).not.toHaveBeenCalled();
});

it("should handle empty user array when user IDs don't exist", async () => {
requestMock.mockResolvedValueOnce({ users: [] });

const result = await service.getUsersByIds([99999]);

expect(requestMock).toHaveBeenCalledWith({
url: `/api/v2/users/show_many?ids=99999`,
type: "GET",
contentType: "application/json"
});
expect(result).toEqual([]);
});
});

describe("searchUsers", () => {
const searchQuery = "searchQuery";

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": "1.5.1",
"version": "1.6.0",
"description": "A toolbox for ZAF application built with 🩷 by Zendesk Labs",
"main": "lib/src/index.js",
"types": "lib/src/index.d.ts",
Expand Down
18 changes: 18 additions & 0 deletions src/services/zendesk-api-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,24 @@ export class ZendeskApiService {
return result.user;
}

/**
* Retrieve multiple zendesk users
* A limit of 100 users can be retrieved at a time.
*/
public async getUsersByIds<T = IZendeskUserFieldValue>(userIds: number[]): Promise<IZendeskUser<T>[]> {
if (userIds.length > MAX_TICKETS_PER_REQUEST) {
throw new Error("A limit of 100 users can be retrieved at a time.");
}

const { users } = await this.client.request<ISearchUserResults<T>>({
url: `/api/v2/users/show_many?ids=${userIds.join(",")}`,
type: "GET",
contentType: "application/json"
});

return users;
}

/**
* Searches the users corresponding to the given query
*
Expand Down
Loading