Woodshop
Search…
Display Zendesk users
This section shows you how to create a smart collection to list the users of your Zendesk account.

Declare the Smart Collection Zendesk Users

Zendesk API allows to access different data:
First, we need to declare the smart collection in your project based on the API documentation. As an example, here the smart collection definition for Users:
forest/zendesk-users.js
1
const { collection } = require('forest-express-sequelize');
2
3
// Search on users => https://support.zendesk.com/hc/en-us/articles/203663216-Searching-users-groups-and-organizations#topic_duj_sbb_vc
4
collection('zendesk_users', {
5
isSearchable: true,
6
actions: [],
7
fields: [{
8
field: 'id',
9
type: 'String',
10
isFilterable: false, // Zendesk API does not provide such capacity with the API
11
}, {
12
field: 'name',
13
type: 'String',
14
isFilterable: true,
15
}, {
16
field: 'alias',
17
type: 'String',
18
}, {
19
field: 'email',
20
type: 'String',
21
isFilterable: true,
22
}, {
23
field: 'role',
24
type: 'Enum',
25
enums: ['end-user', 'agent', 'admin'],
26
isFilterable: true,
27
}, {
28
field: 'role_type',
29
type: 'Number',
30
}, {
31
field: 'phone',
32
type: 'String',
33
isFilterable: true,
34
}, {
35
field: 'whatsapp',
36
type: 'String',
37
isFilterable: true,
38
}, {
39
field: 'last_login_at',
40
type: 'Date',
41
}, {
42
field: 'verified',
43
type: 'Boolean',
44
}, {
45
field: 'active',
46
type: 'Boolean',
47
}, {
48
field: 'suspended',
49
type: 'Boolean',
50
isFilterable: true,
51
}, {
52
field: 'created_at',
53
type: 'Date',
54
isSortable: true,
55
}, {
56
field: 'updated_at',
57
type: 'Date',
58
isSortable: true,
59
}, {
60
field: 'last_login_at',
61
type: 'Date',
62
}, {
63
field: 'notes',
64
type: 'String',
65
isFilterable: true,
66
}, {
67
field: 'details',
68
type: 'String',
69
isFilterable: true,
70
}, {
71
field: 'tags',
72
type: ['String'],
73
isFilterable: true,// is it possible? => no arrays are not yet filterable
74
}, {
75
field: 'time_zone',
76
type: 'String',
77
}, {
78
field: 'moderator',
79
type: 'Boolean',
80
}, {
81
field: 'external_id',
82
type: 'String',
83
isFilterable: true,
84
}, {
85
field: 'only_private_comments',
86
type: 'Boolean',
87
}, {
88
field: 'photo_url',
89
type: 'File',
90
get: (user) => {
91
return user.photo ? user.photo.content_url : null;
92
}
93
}, ],
94
segments: [],
95
});
96
Copied!
Some fields are available for filtering or sorting using the Zendesk API. To allow this on the Forest UI, simply add the keywords isFilterable and isSortable in your field definition.

Implement the Smart Collection route

In the file routes/zendesk-users.js, we’ve created a new route to implement the API behind the Smart Collection.
The logic here is to list all the users of your Zendesk account.
routes/zendesk-users.js
1
const express = require('express');
2
const { PermissionMiddlewareCreator } = require('forest-express-sequelize');
3
4
const router = express.Router();
5
const permissionMiddlewareCreator = new PermissionMiddlewareCreator('zendesk_tickets');
6
7
const {getUsers} = require('../services/zendesk-users-service');
8
9
// Get a list of Zendesk Users
10
router.get('/zendesk_users', permissionMiddlewareCreator.list(), (request, response, next) => {
11
getUsers(request, response, next);
12
});
Copied!

Implement the get Route

The section above help you display the list of all Zendesk users. But you'll need to implement also the logic to display the information of a specific user.
We just need to implement a new endpoint to get an individual user from the Zendesk API.
services/zendesk-users-services.js
1
async function getUser(request, response, next) {
2
return axios.get(`${ZENDESK_URL_PREFIX}/api/v2/users/${request.params.userId}?include=comment_count`, {
3
headers: {
4
'Authorization': `Basic ${getToken()}`
5
},
6
})
7
.then( async (resp) => {
8
let record = resp.data.user;
9
// Serialize the result using the Forest Admin format
10
const recordSerializer = new RecordSerializer({ name: 'zendesk_users' });
11
const recordSerialized = await recordSerializer.serialize(record);
12
response.send(recordSerialized);
13
})
14
.catch(next);
15
16
}
Copied!
routes/zendesk-users.js
1
2
const {getUsers, getUser} = require('../services/zendesk-tickets-service');
3
4
// Get a Zendesk Ticket
5
router.get('/zendesk_users/:userId', permissionMiddlewareCreator.details(), (request, response, next) => {
6
getUser(request, response, next);
7
});
Copied!