2 * Using Rails-like standard naming convention for endpoints.
3 * GET /api/sugarcrm/accounts -> index
4 * POST /api/sugarcrm/accounts -> create
5 * GET /api/sugarcrm/accounts/:id -> show
6 * PUT /api/sugarcrm/accounts/:id -> update
7 * DELETE /api/sugarcrm/accounts/:id -> destroy
13 var _ = require('lodash');
14 var util = require('util');
16 var SugarcrmAccount = require('../../models').SugarcrmAccount;
19 function handleError(res, statusCode) {
20 statusCode = statusCode || 500;
21 return function(err) {
22 res.status(statusCode).send(err);
26 function responseWithResult(res, statusCode) {
27 statusCode = statusCode || 200;
28 return function(entity) {
30 res.status(statusCode).json(entity);
35 function handleEntityNotFound(res) {
36 return function(entity) {
38 res.status(404).end();
45 function saveUpdates(updates) {
46 return function(entity) {
47 return entity.updateAttributes(updates)
48 .then(function(updated) {
54 function removeEntity(res) {
55 return function(entity) {
57 return entity.destroy()
59 res.status(204).end();
65 // Gets a list of SugarcrmAccounts
66 exports.index = function(req, res) {
67 var attributes = ['description', 'name', 'username', 'remoteUri'];
68 var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
69 var page = req.query.page ? parseInt(req.query.page, 10) : 0;
74 offset: page * per_page
77 _.forIn(req.query, function(value, key) {
83 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
89 attributes.forEach(function(attribute) {
92 $like: '%' + value + '%'
95 query.where.$or.push(tmp);
102 query.where[key].$like = '%' + value + '%';
107 .findAndCountAll(query)
108 .then(function(result) {
110 var total_pages = Math.ceil(result.count / per_page);
111 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;
112 var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
114 return res.status(200).send({
117 next_page: next_page,
118 previous_page: previous_page,
119 total_pages: total_pages
123 .catch(function(err) {
124 return res.status(500).send({
125 error: 'Something blew up!'
130 // Gets a single SugarcrmAccount from the DB
131 exports.show = function(req, res) {
132 SugarcrmAccount.findById(req.params.id)
133 .then(handleEntityNotFound(res))
134 .then(responseWithResult(res))
135 .catch(handleError(res));
138 // Creates a new SugarcrmAccount in the DB
139 exports.create = function(req, res) {
140 SugarcrmAccount.create(req.body)
141 .then(responseWithResult(res, 201))
142 .catch(handleError(res));
145 // Updates an existing SugarcrmAccount in the DB
146 exports.update = function(req, res) {
150 SugarcrmAccount.findById(req.params.id)
151 .then(handleEntityNotFound(res))
152 .then(saveUpdates(req.body))
153 .then(responseWithResult(res))
154 .catch(handleError(res));
157 // Deletes a SugarcrmAccount from the DB
158 exports.destroy = function(req, res) {
159 SugarcrmAccount.findById(req.params.id)
160 .then(handleEntityNotFound(res))
161 .then(removeEntity(res))
162 .catch(handleError(res));
165 exports.checkAccount = function(req, res) {
166 SugarcrmAccount.findById(req.params.id)
167 .then(handleEntityNotFound(res))
168 .then(function(account) {
169 var sugar = require('node-sugarcrm-client');
171 apiURL: stripTrailingSlash(account.remoteUri) + "/service/v4_1/rest.php",
172 login: account.username,
173 passwd: account.password
175 sugar.login(function(sessionId) {
177 return res.sendStatus(400);
179 return res.sendStatus(200);
182 .catch(handleError(res));
185 exports.bulkDestroy = function(req, res) {
191 individualHooks: true
194 return res.sendStatus(204);
196 .catch(function(err) {
197 return handleError(res, err);
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) {
226 var sugar = require('node-sugarcrm-client');
227 SugarcrmAccount.findById(req.params.id)
228 .then(handleEntityNotFound(res))
229 .then(function(account) {
231 apiURL: stripTrailingSlash(account.remoteUri) + "/service/v4_1/rest.php",
232 login: account.username,
233 passwd: account.password
235 sugar.login(function(sessionId) {
237 return res.sendStatus(400);
243 sugar.call("get_module_fields", params, function(result, err) {
245 return res.sendStatus(400);
247 return res.status(200).send(result.module_fields);
251 .catch(handleError(res));
254 function stripTrailingSlash(str) {
255 if (str.substr(-1) === '/') {
256 return str.substr(0, str.length - 1);