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:
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.
constexpress=require('express');const { PermissionMiddlewareCreator } =require('forest-express-sequelize');constrouter=express.Router();constpermissionMiddlewareCreator=newPermissionMiddlewareCreator('zendesk_tickets');const {getUsers} =require('../services/zendesk-users-service');// Get a list of Zendesk Usersrouter.get('/zendesk_users',permissionMiddlewareCreator.list(), (request, response, next) => {getUsers(request, response, next);});
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
asyncfunctiongetUser(request, response, next) {returnaxios.get(`${ZENDESK_URL_PREFIX}/api/v2/users/${request.params.userId}?include=comment_count`, { headers: {'Authorization':`Basic ${getToken()}` }, }).then( async (resp) => {let record =resp.data.user;// Serialize the result using the Forest Admin formatconstrecordSerializer=newRecordSerializer({ name:'zendesk_users' });constrecordSerialized=awaitrecordSerializer.serialize(record);response.send(recordSerialized); }).catch(next); }
routes/zendesk-users.js
const {getUsers,getUser} =require('../services/zendesk-tickets-service');// Get a Zendesk Ticketrouter.get('/zendesk_users/:userId',permissionMiddlewareCreator.details(), (request, response, next) => {getUser(request, response, next);});