-
-
Notifications
You must be signed in to change notification settings - Fork 234
/
users.js
554 lines (508 loc) · 20.8 KB
/
users.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// File: users.js
const {Client} = require('../client');
/**
* @typedef {object} User
* @property {boolean} active - false if the user has been deleted
* @property {string} [alias] - An alias displayed to end users
* @property {boolean} chat_only - Whether or not the user is a chat-only agent
* @property {string} created_at - The time the user was created
* @property {number} [custom_role_id] - A custom role if the user is an agent on the Enterprise plan or above
* @property {number} [default_group_id] - The id of the user's default group
* @property {string} [details] - Any details you want to store about the user, such as an address
* @property {string} [email] - The user's primary email address. *Writeable on create only. On update, a secondary email is added. See Email Address
* @property {string} [external_id] - A unique identifier from another system. The API treats the id as case insensitive. Example: "ian1" and "IAN1" are the same value.
* @property {string} iana_time_zone - The time zone for the user
* @property {number} id - Automatically assigned when the user is created
* @property {string} last_login_at - Last time the user signed in to Zendesk Support or made an API request using an API token or basic authentication
* @property {string} [locale] - The user's locale. A BCP-47 compliant tag for the locale. If both "locale" and "locale_id" are present on create or update, "locale_id" is ignored and only "locale" is used.
* @property {number} [locale_id] - The user's language identifier
* @property {boolean} [moderator] - Designates whether the user has forum moderation capabilities
* @property {string} name - The user's name
* @property {string} [notes] - Any notes you want to store about the user
* @property {boolean} [only_private_comments] - true if the user can only create private comments
* @property {number} [organization_id] - The id of the user's organization. If the user has more than one organization memberships, the id of the user's default organization. If updating, see Organization ID
* @property {string} [phone] - The user's primary phone number. See Phone Number below
* @property {object} [photo] - The user's profile picture represented as an Attachment object
* @property {string} [remote_photo_url] - A URL pointing to the user's profile picture.
* @property {boolean} report_csv - This parameter is inert and has no effect. It may be deprecated in the future. Previously, this parameter determined whether a user could access a CSV report in a legacy Guide dashboard. This dashboard has been removed. See Announcing Guide legacy reporting upgrade to Explore
* @property {boolean} [restricted_agent] - If the agent has any restrictions; false for admins and unrestricted agents, true for other agents
* @property {string} [role] - The user's role. Possible values are "end-user", "agent", or "admin"
* @property {number} role_type - The user's role id. 0 for a custom agent, 1 for a light agent, 2 for a chat agent, 3 for a chat agent added to the Support account as a contributor (Chat Phase 4), 4 for an admin, and 5 for a billing admin
* @property {boolean} shared - If the user is shared from a different Zendesk Support instance. Ticket sharing accounts only
* @property {boolean} shared_agent - If the user is a shared agent from a different Zendesk Support instance. Ticket sharing accounts only
* @property {boolean} [shared_phone_number] - Whether the phone number is shared or not. See Phone Number below
* @property {string} [signature] - The user's signature. Only agents and admins can have signatures
* @property {boolean} [suspended] - If the agent is suspended. Tickets from suspended users are also suspended, and these users cannot sign in to the end user portal
* @property {string[]} [tags] - The user's tags. Only present if your account has user tagging enabled
* @property {string} [ticket_restriction] - Specifies which tickets the user has access to. Possible values are: "organization", "groups", "assigned", "requested", null. "groups" and "assigned" are valid only for agents. If you pass an invalid value to an end user (for example, "groups"), they will be assigned to "requested", regardless of their previous access
* @property {string} [time_zone] - The user's time zone. See Time Zone
* @property {boolean} two_factor_auth_enabled - If two factor authentication is enabled
* @property {string} updated_at - The time the user was last updated
* @property {string} url - The user's API url
* @property {object} [user_fields] - Values of custom fields in the user's profile. See User Fields
* @property {boolean} [verified] - Any of the user's identities is verified. See User Identities
*/
/**
* Client for the Zendesk Users API.
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/}
*/
class Users extends Client {
constructor(options) {
super(options);
this.jsonAPINames = ['users', 'user'];
this.sideLoadMap = [
{field: 'id', name: 'group', dataset: 'groups', all: true},
{
field: 'id',
name: 'identity',
dataset: 'identities',
array: true,
dataKey: 'user_id',
},
{field: 'custom_role_id', name: 'role', dataset: 'roles'},
{
field: 'organization_id',
name: 'organization',
dataset: 'organizations',
},
];
}
/**
* Authenticates the current user.
* @returns {Promise<object>} The authenticated user's details.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#show-the-currently-authenticated-user}
* @example
* const user = await client.users.auth();
*/
async auth() {
return this.get(['users', 'me']);
}
/**
* Lists all users.
* @returns {Promise<Array<User>>} An array of user objects.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#list-users}
* @example
* const users = await client.users.list();
*/
async list() {
return this.getAll(['users']);
}
/**
* Lists users with a specific filter.
* @param {string} type - The type of filter.
* @param {string|number} value - The value for the filter.
* @returns {Promise<Array<User>>} An array of user objects.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#list-users}
* @example
* const users = await client.users.listWithFilter('type', 'value');
*/
async listWithFilter(type, value) {
return this.getAll(['users', {[type]: value}]);
}
/**
* Lists users by group ID.
* @param {number} id - The ID of the group.
* @returns {Promise<Array<User>>} An array of user objects.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#list-users}
* @example
* const users = await client.users.listByGroup(12345);
*/
async listByGroup(id) {
return this.getAll(['groups', id, 'users']);
}
/**
* Lists users by organization ID.
* @param {number} id - The ID of the organization.
* @returns {Promise<Array<User>>} An array of user objects.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#list-users}
* @example
* const users = await client.users.listByOrganization(12345);
*/
async listByOrganization(id) {
return this.getAll(['organizations', id, 'users']);
}
/**
* Shows details of a user by ID.
* @param {number} id - The ID of the user.
* @returns {Promise<User>} The user's details.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#show-user}
* @example
* const user = await client.users.show(12345);
*/
async show(id) {
return this.get(['users', id]);
}
/**
* Shows details of multiple users by their IDs.
* @param {Array<number>} userIds - An array of user IDs.
* @returns {Promise<Array<User>>} An array of user details.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#show-many-users}
* @example
* const users = await client.users.showMany([12345, 67890]);
*/
async showMany(userIds) {
return this.get(['users', 'show_many', {ids: userIds}]);
}
/**
* Creates a new user.
* @param {object} user - The user details.
* @returns {Promise<User>} The created user's details.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#create-user}
* @example
* const newUser = await client.users.create({name: 'John Doe', email: 'john@example.com'});
*/
async create(user) {
return this.post(['users'], user);
}
/**
* Creates multiple users.
* @param {Array<object>} users - An array of user details.
* @returns {Promise<Array<User>>} An array of created user details.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#create-many-users}
* @example
* const newUsers = await client.users.createMany([{name: 'John Doe', email: 'john@example.com'}, {name: 'Jane Smith', email: 'jane@example.com'}]);
*/
async createMany(users) {
return this.post(['users', 'create_many'], users);
}
/**
* Creates or updates a user.
* @param {object} user - The user details.
* @returns {Promise<User>} The created or updated user's details.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#create-or-update-user}
* @example
* const user = await client.users.createOrUpdate({name: 'John Doe', email: 'john@example.com'});
*/
async createOrUpdate(user) {
return this.post(['users', 'create_or_update'], user);
}
/**
* Creates or updates multiple users.
* @param {Array<object>} users - An array of user details.
* @returns {Promise<Array<User>>} An array of created or updated user details.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#create-or-update-many-users}
* @example
* const users = await client.users.createOrUpdateMany([{name: 'John Doe', email: 'john@example.com'}, {name: 'Jane Smith', email: 'jane@example.com'}]);
*/
async createOrUpdateMany(users) {
return this.post(['users', 'create_or_update_many'], users);
}
/**
* Updates a user by ID.
* @param {number} id - The ID of the user.
* @param {object} user - The updated user details.
* @returns {Promise<User>} The updated user's details.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#update-user}
* @example
* const updatedUser = await client.users.update(12345, {name: 'Johnathan Doe'});
*/
async update(id, user) {
return this.put(['users', id], user);
}
/**
* Updates multiple users.
* @param {...*} args - Arguments including optional IDs and user details.
* @returns {Promise<Array<User>>} An array of updated user details.
* @async
* @throws {Error} Throws an error if not enough arguments are provided.
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#update-many-users}
* @example
* const updatedUsers = await client.users.updateMany([12345, 67890], [{name: 'John Doe'}, {name: 'Jane Smith'}]);
*/
async updateMany(...args /* Optional ids, users, cb */) {
if (args.length < 2) {
throw new Error('Not enough arguments; at least two expected.');
}
const ids = args[0];
const users = args.length === 2 ? args[0] : args[1];
if (args.length === 2) {
return this.put(['users', 'update_many'], users);
}
if (!ids) {
return this.put(['users', 'update_many'], users);
}
if (typeof ids === 'string') {
return this.put(
['users', 'update_many', '?ids=' + ids.toString()],
users,
);
}
if (typeof ids === 'object') {
if (Array.isArray(ids)) {
return this.put(
['users', 'update_many', '?ids=' + ids.join(',')],
users,
);
}
if (ids.hasOwn(ids, 'ids')) {
return this.put(
['users', 'update_many', '?ids=' + ids.ids.toString()],
users,
);
}
if (ids.hasOwn(ids, 'external_ids')) {
return this.put(
[
'users',
'update_many',
'?external_ids=' + ids.external_ids.toString(),
],
users,
);
}
}
}
/**
* Suspends a user by ID.
* @param {number} id - The ID of the user to suspend.
* @returns {Promise<User>} The suspended user's details.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#suspend-user}
* @example
* await client.users.suspend(12345);
*/
async suspend(id) {
return this.put(['users', id], {user: {suspended: true}});
}
/**
* Unsuspends a user by ID.
* @param {number} id - The ID of the user to unsuspend.
* @returns {Promise<User>} The unsuspended user's details.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#unsuspend-user}
* @example
* await client.users.unsuspend(12345);
*/
async unsuspend(id) {
return this.put(['users', id], {user: {suspended: false}});
}
/**
* Deletes a user by ID.
* @param {number} id - The ID of the user to delete.
* @returns {Promise<void>}
* @async
* @throws {Error} Throws an error if the user cannot be deleted.
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#delete-user}
* @example
* await client.users.delete(12345);
*/
async delete(id) {
return super.delete(['users', id]);
}
/**
* Deletes multiple users.
* @param {...*} args - Arguments including optional IDs and user details.
* @returns {Promise<void>}
* @async
* @throws {Error} Throws an error if not enough arguments are provided.
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#delete-many-users}
* @example
* await client.users.destroyMany([12345, 67890]);
*/
async destroyMany(...args) {
if (args.length < 2) {
throw new Error('Not enough arguments; at least two expected.');
}
const ids = args[0];
const users = args.length === 2 ? args[0] : args[1];
if (args.length === 2) {
return super.delete(['users', 'destroy_many'], users);
}
if (!ids) {
return super.delete(['users', 'destroy_many'], users);
}
if (typeof ids === 'string') {
return super.delete(
['users', 'destroy_many', '?ids=' + ids.toString()],
users,
);
}
if (typeof ids === 'object') {
if (Array.isArray(ids)) {
return super.delete(
['users', 'destroy_many', '?ids=' + ids.join(',')],
users,
);
}
if (ids.hasOwn(ids, 'ids')) {
return super.delete(
['users', 'destroy_many', '?ids=' + ids.ids.toString()],
users,
);
}
if (ids.hasOwn(ids, 'external_ids')) {
return super.delete(
[
'users',
'destroy_many',
'?external_ids=' + ids.external_ids.toString(),
],
users,
);
}
}
}
/**
* Searches for users based on specific parameters.
* @param {object} parameters - The search parameters.
* @returns {Promise<Array<User>>} An array of user objects that match the search criteria.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#search-users}
* @example
* const users = await client.users.search({query: 'john@example.com'});
*/
async search(parameters) {
return this.getAll(['users', 'search', parameters]);
}
/**
* Retrieves details of the currently authenticated user.
* @returns {Promise<{result: User}>} The user's details.The authenticated user's details.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#show-the-currently-authenticated-user}
* @example
* const user = await client.users.me();
*/
async me() {
return this.get(['users', 'me']);
}
/**
* Merges a user into another user.
* @param {number} id - The ID of the user to be merged.
* @param {number} targetId - The ID of the user into which the first user will be merged.
* @returns {Promise<object>} The details of the merged user.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#merge-user-into-another-user}
* @example
* await client.users.merge(12345, 67890);
*/
async merge(id, targetId) {
return this.put(['users', id, 'merge'], {user: {id: targetId}});
}
/**
* Changes the password of a user.
* @param {number} userId - The ID of the user whose password is to be changed.
* @param {string} oldPassword - The current password of the user.
* @param {string} newPassword - The new password for the user.
* @returns {Promise<object>} The user's details after the password change.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#change-password}
* @example
* await client.users.password(12345, 'oldPassword123', 'newPassword456');
*/
async password(userId, oldPassword, newPassword) {
return this.put(['users', userId, 'password'], {
previous_password: oldPassword,
password: newPassword,
});
}
/**
* Retrieves users incrementally with included related data.
* @param {number} startTime - The start time for the incremental export.
* @param {string} include - The related data to include.
* @returns {Promise<Array<object>>} An array of user objects with included data.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#incremental-export-users}
* @example
* const users = await client.users.incrementalInclude(1632614395, 'relatedData');
*/
async incrementalInclude(startTime, include) {
return this.getAll([
'incremental',
'users',
{start_time: startTime, include},
]);
}
/**
* Retrieves users incrementally.
* @param {number} startTime - The start time for the incremental export.
* @returns {Promise<Array<object>>} An array of user objects.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#incremental-export-users}
* @example
* const users = await client.users.incremental(1632614395);
*/
async incremental(startTime) {
return this.getAll(['incremental', 'users', {start_time: startTime}]);
}
/**
* Retrieves a sample of users incrementally.
* @param {number} startTime - The start time for the incremental export.
* @returns {Promise<Array<object>>} A sample array of user objects.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#incremental-sample-export-users}
* @example
* const usersSample = await client.users.incrementalSample(1632614395);
*/
async incrementalSample(startTime) {
return this.get([
'incremental',
'users',
'sample',
{start_time: startTime},
]);
}
/**
* Lists tags associated with a user.
* @param {number} userId - The ID of the user.
* @returns {Promise<Array<string>>} An array of tags associated with the user.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/user-tags/#list-tags}
* @example
* const tags = await client.users.listTags(12345);
*/
async listTags(userId) {
return this.getAll(['users', userId, 'tags']);
}
/**
* Sets tags for a user.
* @param {number} userId - The ID of the user.
* @param {Array<string>} tags - An array of tags to set for the user.
* @returns {Promise<object>} The user's details with the updated tags.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/user-tags/#set-tags}
* @example
* await client.users.setTags(12345, ['tag1', 'tag2']);
*/
async setTags(userId, tags) {
return this.post(['users', userId, 'tags'], tags);
}
/**
* Adds tags to a user.
* @param {number} userId - The ID of the user.
* @param {Array<string>} tags - An array of tags to add to the user.
* @returns {Promise<object>} The user's details with the added tags.
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/user-tags/#add-tags}
* @example
* await client.users.addTags(12345, ['tag3', 'tag4']);
*/
async addTags(userId, tags) {
return this.put(['users', userId, 'tags'], tags);
}
/**
* Removes tags from a user.
* @param {number} userId - The ID of the user.
* @param {Array<string>} tags - An array of tags to remove from the user.
* @returns {Promise<void>}
* @async
* @see {@link https://developer.zendesk.com/api-reference/ticketing/users/user-tags/#remove-tags}
* @example
* await client.users.removeTags(12345, ['tag3', 'tag4']);
*/
async removeTags(userId, tags) {
return super.delete(['users', userId, 'tags'], tags);
}
}
exports.Users = Users;