2 * Using Rails-like standard naming convention for endpoints.
3 * GET /api/freshdesk/accounts -> index
4 * POST /api/freshdesk/accounts -> create
5 * GET /api/freshdesk/accounts/:id -> show
6 * PUT /api/freshdesk/accounts/:id -> update
7 * DELETE /api/freshdesk/accounts/:id -> destroy
13 var _ = require('lodash');
14 var util = require('util');
15 var _fd = require('freshdesk');
17 var FreshdeskAccount = require('../../models').FreshdeskAccount;
20 function handleError(res, statusCode) {
21 statusCode = statusCode || 500;
22 return function(err) {
23 res.status(statusCode).send(err);
27 function responseWithResult(res, statusCode) {
28 statusCode = statusCode || 200;
29 return function(entity) {
31 res.status(statusCode).json(entity);
36 function handleEntityNotFound(res) {
37 return function(entity) {
39 res.status(404).end();
46 function saveUpdates(updates) {
47 return function(entity) {
48 return entity.updateAttributes(updates)
49 .then(function(updated) {
55 function removeEntity(res) {
56 return function(entity) {
58 return entity.destroy()
60 res.status(204).end();
66 // Gets a list of FreshdeskAccounts
67 exports.index = function(req, res) {
68 var attributes = ['description', 'name', 'username', 'remoteUri'];
69 var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
70 var page = req.query.page ? parseInt(req.query.page, 10) : 0;
75 offset: page * per_page
78 _.forIn(req.query, function(value, key) {
84 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
90 attributes.forEach(function(attribute) {
93 $like: '%' + value + '%'
96 query.where.$or.push(tmp);
103 query.where[key].$like = '%' + value + '%';
108 .findAndCountAll(query)
109 .then(function(result) {
111 var total_pages = Math.ceil(result.count / per_page);
112 var next_page = total_pages > (query.offset + 1) ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page + 1) : null;
113 var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
115 return res.status(200).send({
118 next_page: next_page,
119 previous_page: previous_page,
120 total_pages: total_pages
124 .catch(function(err) {
125 return res.status(500).send({
126 error: 'Something blew up!'
131 // Gets a single FreshdeskAccount from the DB
132 exports.show = function(req, res) {
133 FreshdeskAccount.findById(req.params.id)
134 .then(handleEntityNotFound(res))
135 .then(responseWithResult(res))
136 .catch(handleError(res));
139 // Creates a new FreshdeskAccount in the DB
140 exports.create = function(req, res) {
141 FreshdeskAccount.create(req.body)
142 .then(responseWithResult(res, 201))
143 .catch(handleError(res));
146 // Updates an existing FreshdeskAccount in the DB
147 exports.update = function(req, res) {
151 FreshdeskAccount.findById(req.params.id)
152 .then(handleEntityNotFound(res))
153 .then(saveUpdates(req.body))
154 .then(responseWithResult(res))
155 .catch(handleError(res));
158 // Deletes a FreshdeskAccount from the DB
159 exports.destroy = function(req, res) {
160 FreshdeskAccount.findById(req.params.id)
161 .then(handleEntityNotFound(res))
162 .then(removeEntity(res))
163 .catch(handleError(res));
166 exports.bulkDestroy = function(req, res) {
172 individualHooks: true
175 return res.sendStatus(204);
177 .catch(function(err) {
178 return handleError(res, err);
182 exports.checkAccount = function(req, res) {
183 FreshdeskAccount.findById(req.params.id)
184 .then(handleEntityNotFound(res))
185 .then(function(account) {
186 var freshdesk = new _fd(account.remoteUri, account.apiKey);
187 freshdesk.listTickets(function(err, response, body) {
189 return res.sendStatus(400);
191 body = JSON.parse(body);
192 if (body.require_login) {
193 return res.sendStatus(400);
195 return res.sendStatus(200);
198 .catch(handleError(res));
201 exports.accountValidation = function(req, res) {
203 where[req.params.field] = req.body.value;
208 .then(function(result) {
210 return res.status(200).send({
212 value: req.body.value
215 return res.status(200).send({
217 value: req.body.value
220 .catch(function(err) {
221 return handleError(res, err);
225 exports.getFields = function(req, res, next) {
226 FreshdeskAccount.findById(req.params.id)
227 .then(handleEntityNotFound(res))
228 .then(function(account) {
229 var Freshdesk = new _fd(account.remoteUri, account.apiKey);
230 Freshdesk.get('/ticket_fields.json', function(err, response, body) {
234 body = JSON.parse(body);
235 return res.status(200).send(body);
238 .catch(handleError(res));