Skip to content

Commit 8bf747f

Browse files
authored
Merge pull request #294 from zendesk/tristan-vu/users-by-id
users by ids
2 parents 9164f02 + bc38de1 commit 8bf747f

3 files changed

Lines changed: 151 additions & 1 deletion

File tree

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

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,138 @@ describe("ZendeskService", () => {
276276
});
277277
});
278278

279+
describe("getUsersByIds", () => {
280+
const mockUsers: IZendeskUser[] = [
281+
{
282+
id: 1,
283+
name: "Test User 1",
284+
email: "test1@example.com",
285+
created_at: "2023-01-01T00:00:00Z",
286+
updated_at: "2023-01-01T00:00:00Z",
287+
url: "https://example.zendesk.com/api/v2/users/1.json",
288+
time_zone: "UTC",
289+
iana_time_zone: "UTC",
290+
phone: null,
291+
photo: null,
292+
locale_id: 1,
293+
locale: "en-US",
294+
organization_id: 100,
295+
role: "end-user",
296+
verified: true,
297+
external_id: null,
298+
tags: [],
299+
alias: "",
300+
active: true,
301+
shared: false,
302+
shared_agent: false,
303+
shared_phone_number: null,
304+
last_login_at: "2023-01-01T00:00:00Z",
305+
two_factor_auth_enabled: null,
306+
signature: "",
307+
details: "",
308+
notes: "",
309+
role_type: 0,
310+
custom_role_id: 0,
311+
moderator: false,
312+
ticket_restriction: null,
313+
only_private_comments: false,
314+
restricted_agent: false,
315+
suspended: false,
316+
default_group_id: 0,
317+
report_csv: false,
318+
user_fields: {}
319+
},
320+
{
321+
id: 2,
322+
name: "Test User 2",
323+
email: "test2@example.com",
324+
created_at: "2023-01-02T00:00:00Z",
325+
updated_at: "2023-01-02T00:00:00Z",
326+
url: "https://example.zendesk.com/api/v2/users/2.json",
327+
time_zone: "UTC",
328+
iana_time_zone: "UTC",
329+
phone: null,
330+
photo: null,
331+
locale_id: 1,
332+
locale: "en-US",
333+
organization_id: 100,
334+
role: "end-user",
335+
verified: true,
336+
external_id: null,
337+
tags: [],
338+
alias: "",
339+
active: true,
340+
shared: false,
341+
shared_agent: false,
342+
shared_phone_number: null,
343+
last_login_at: "2023-01-02T00:00:00Z",
344+
two_factor_auth_enabled: null,
345+
signature: "",
346+
details: "",
347+
notes: "",
348+
role_type: 0,
349+
custom_role_id: 0,
350+
moderator: false,
351+
ticket_restriction: null,
352+
only_private_comments: false,
353+
restricted_agent: false,
354+
suspended: false,
355+
default_group_id: 0,
356+
report_csv: false,
357+
user_fields: {}
358+
}
359+
];
360+
361+
it("should retrieve multiple users by their IDs", async () => {
362+
requestMock.mockResolvedValueOnce({ users: mockUsers });
363+
364+
const result = await service.getUsersByIds([1, 2]);
365+
366+
expect(requestMock).toHaveBeenCalledWith({
367+
url: `/api/v2/users/show_many?ids=1,2`,
368+
type: "GET",
369+
contentType: "application/json"
370+
});
371+
expect(result).toEqual(mockUsers);
372+
});
373+
374+
it("should handle single user ID", async () => {
375+
requestMock.mockResolvedValueOnce({ users: [mockUsers[0]] });
376+
377+
const result = await service.getUsersByIds([1]);
378+
379+
expect(requestMock).toHaveBeenCalledWith({
380+
url: `/api/v2/users/show_many?ids=1`,
381+
type: "GET",
382+
contentType: "application/json"
383+
});
384+
expect(result).toEqual([mockUsers[0]]);
385+
});
386+
387+
it("should throw an error when trying to retrieve more than 100 users", async () => {
388+
const manyUserIds = Array.from({ length: 101 }, (_, index) => index + 1);
389+
390+
await expect(service.getUsersByIds(manyUserIds)).rejects.toThrow(
391+
"A limit of 100 users can be retrieved at a time."
392+
);
393+
394+
expect(requestMock).not.toHaveBeenCalled();
395+
});
396+
397+
it("should handle empty user array when user IDs don't exist", async () => {
398+
requestMock.mockResolvedValueOnce({ users: [] });
399+
400+
const result = await service.getUsersByIds([99999]);
401+
402+
expect(requestMock).toHaveBeenCalledWith({
403+
url: `/api/v2/users/show_many?ids=99999`,
404+
type: "GET",
405+
contentType: "application/json"
406+
});
407+
expect(result).toEqual([]);
408+
});
409+
});
410+
279411
describe("searchUsers", () => {
280412
const searchQuery = "searchQuery";
281413

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": "1.5.1",
3+
"version": "1.6.0",
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/services/zendesk-api-service.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,24 @@ export class ZendeskApiService {
180180
return result.user;
181181
}
182182

183+
/**
184+
* Retrieve multiple zendesk users
185+
* A limit of 100 users can be retrieved at a time.
186+
*/
187+
public async getUsersByIds<T = IZendeskUserFieldValue>(userIds: number[]): Promise<IZendeskUser<T>[]> {
188+
if (userIds.length > MAX_TICKETS_PER_REQUEST) {
189+
throw new Error("A limit of 100 users can be retrieved at a time.");
190+
}
191+
192+
const { users } = await this.client.request<ISearchUserResults<T>>({
193+
url: `/api/v2/users/show_many?ids=${userIds.join(",")}`,
194+
type: "GET",
195+
contentType: "application/json"
196+
});
197+
198+
return users;
199+
}
200+
183201
/**
184202
* Searches the users corresponding to the given query
185203
*

0 commit comments

Comments
 (0)