Woodshop
Search…
Authentication, Filtering & Sorting

Get authenticated to the Zendesk API

You first need to generate an authentication token to access the Zendesk API. We are going to use the basic authentication mechanism. More details provided here. The 2 parameters required are: a user email (agent) that is allowed to access Zendesk, and the API Key that you can retrieve from the Zendesk console:
These 2 parameters can be environment variables like this;
services/forest-smart-collection-helpers.js
1
function getToken () {
2
const authEmail = process.env.ZENDESK_AUTH_EMAIL;
3
const apiKey = process.env.ZENDESK_API_TOKEN;
4
return Buffer.from(`${authEmail}/token:${apiKey}`).toString('base64');
5
}
Copied!

Filtering and Sorting using the API

Zendesk API allows you to filter and sort the tickets/users, plus paginate the result.
What we need first, is to implement a way to transform the Forest Admin filtering, sorting and pagination convention to the Zendesk API format.

Filtering

services/forest-smart-collection-helpers.js
1
function getFilterConditons(params) {
2
3
let filters = [];
4
if (params.filters) {
5
let filtersJson = JSON.parse(params.filters)
6
if (filtersJson.aggregator) {
7
filters = filtersJson.conditions;
8
}
9
else {
10
filters = [filtersJson];
11
}
12
}
13
14
let filterConditions = [];
15
if (params.search) {
16
filterConditions.push(params.search)
17
}
18
for (let filter of filters) {
19
20
if (filter.field==='id') {
21
filterConditions.push(`${filter.value}`);
22
}
23
else {
24
// This example shows the equals, greater than and lower than conditions
25
// cf. Search operators => https://support.zendesk.com/hc/en-us/articles/203663226-Zendesk-Support-search-reference#topic_lhr_wsc_3v
26
let operator = ':';
27
switch (filter.operator) {
28
case 'before':
29
operator = '<';
30
break;
31
case 'after':
32
operator = '>';
33
break;
34
}
35
filterConditions.push(`${filter.field}${operator}${filter.value}`);
36
}
37
}
38
return filterConditions;
39
}
Copied!

Sorting

services/forest-smart-collection-helpers.js
1
function getSort(params, options) {
2
let sort_by = options.default_sort_by || '';
3
let sort_order = options.default_sort_order || '';
4
5
let sort = params.sort;
6
if (sort) {
7
let asc = true;
8
if (sort.startsWith('-')) {
9
asc = false;
10
sort = sort.substring(1);
11
}
12
13
const collectionName = options.collection_name;
14
const authorized_fields = Liana.Schemas.schemas[collectionName].fields
15
.filter(field=>field.isSortable)
16
.map(field=> field.field);
17
18
if (authorized_fields.includes(sort)) {
19
sort_by = sort;
20
sort_order = asc?'asc':'desc';
21
}
22
}
23
return {sort_by, sort_order};
24
}
Copied!