Built motion from commit 5e31ea4.|0.0.32
[motion.git] / server / api / sugarcrm_account / sugarcrm_account.controller.js
index 6062588..cb7d78d 100644 (file)
@@ -1 +1,259 @@
-var _0x636a=["\x75\x73\x65\x20\x73\x74\x72\x69\x63\x74","\x6C\x6F\x64\x61\x73\x68","\x75\x74\x69\x6C","\x53\x75\x67\x61\x72\x63\x72\x6D\x41\x63\x63\x6F\x75\x6E\x74","\x2E\x2E\x2F\x2E\x2E\x2F\x6D\x6F\x64\x65\x6C\x73","\x73\x65\x6E\x64","\x73\x74\x61\x74\x75\x73","\x6A\x73\x6F\x6E","\x65\x6E\x64","\x74\x68\x65\x6E","\x75\x70\x64\x61\x74\x65\x41\x74\x74\x72\x69\x62\x75\x74\x65\x73","\x64\x65\x73\x74\x72\x6F\x79","\x69\x6E\x64\x65\x78","\x64\x65\x73\x63\x72\x69\x70\x74\x69\x6F\x6E","\x6E\x61\x6D\x65","\x75\x73\x65\x72\x6E\x61\x6D\x65","\x72\x65\x6D\x6F\x74\x65\x55\x72\x69","\x70\x65\x72\x5F\x70\x61\x67\x65","\x71\x75\x65\x72\x79","\x70\x61\x67\x65","\x6F\x72\x64\x65\x72","\x25\x73\x20\x25\x73","\x73\x6F\x72\x74\x5F\x62\x79","\x73\x6F\x72\x74\x5F\x6F\x72\x64\x65\x72","\x41\x53\x43","\x66\x6F\x72\x6D\x61\x74","\x24\x6F\x72","\x77\x68\x65\x72\x65","\x25","\x70\x75\x73\x68","\x66\x6F\x72\x45\x61\x63\x68","\x24","\x24\x6C\x69\x6B\x65","\x66\x6F\x72\x49\x6E","\x53\x6F\x6D\x65\x74\x68\x69\x6E\x67\x20\x62\x6C\x65\x77\x20\x75\x70\x21","\x63\x61\x74\x63\x68","\x63\x6F\x75\x6E\x74","\x63\x65\x69\x6C","\x6F\x66\x66\x73\x65\x74","\x25\x73\x3A\x2F\x2F\x25\x73\x25\x73\x3F\x70\x61\x67\x65\x3D\x25\x64","\x70\x72\x6F\x74\x6F\x63\x6F\x6C","\x68\x6F\x73\x74","\x68\x65\x61\x64\x65\x72\x73","\x62\x61\x73\x65\x55\x72\x6C","\x72\x6F\x77\x73","\x66\x69\x6E\x64\x41\x6E\x64\x43\x6F\x75\x6E\x74\x41\x6C\x6C","\x73\x68\x6F\x77","\x69\x64","\x70\x61\x72\x61\x6D\x73","\x66\x69\x6E\x64\x42\x79\x49\x64","\x63\x72\x65\x61\x74\x65","\x62\x6F\x64\x79","\x75\x70\x64\x61\x74\x65","\x63\x68\x65\x63\x6B\x41\x63\x63\x6F\x75\x6E\x74","\x6E\x6F\x64\x65\x2D\x73\x75\x67\x61\x72\x63\x72\x6D\x2D\x63\x6C\x69\x65\x6E\x74","\x2F\x73\x65\x72\x76\x69\x63\x65\x2F\x76\x34\x5F\x31\x2F\x72\x65\x73\x74\x2E\x70\x68\x70","\x70\x61\x73\x73\x77\x6F\x72\x64","\x69\x6E\x69\x74","\x73\x65\x6E\x64\x53\x74\x61\x74\x75\x73","\x6C\x6F\x67\x69\x6E","\x62\x75\x6C\x6B\x44\x65\x73\x74\x72\x6F\x79","\x61\x63\x63\x6F\x75\x6E\x74\x56\x61\x6C\x69\x64\x61\x74\x69\x6F\x6E","\x66\x69\x65\x6C\x64","\x76\x61\x6C\x75\x65","\x67\x65\x74\x46\x69\x65\x6C\x64\x73","\x43\x61\x73\x65\x73","\x67\x65\x74\x5F\x6D\x6F\x64\x75\x6C\x65\x5F\x66\x69\x65\x6C\x64\x73","\x6D\x6F\x64\x75\x6C\x65\x5F\x66\x69\x65\x6C\x64\x73","\x63\x61\x6C\x6C","\x73\x75\x62\x73\x74\x72","\x2F","\x6C\x65\x6E\x67\x74\x68"];_0x636a[0];var _=require(_0x636a[1]);var util=require(_0x636a[2]);var SugarcrmAccount=require(_0x636a[4])[_0x636a[3]];function handleError(_0x6c9fx5,_0x6c9fx6){_0x6c9fx6=_0x6c9fx6||500;return function(_0x6c9fx7){_0x6c9fx5[_0x636a[6]](_0x6c9fx6)[_0x636a[5]](_0x6c9fx7)};}function responseWithResult(_0x6c9fx5,_0x6c9fx6){_0x6c9fx6=_0x6c9fx6||200;return function(_0x6c9fx9){if(_0x6c9fx9){_0x6c9fx5[_0x636a[6]](_0x6c9fx6)[_0x636a[7]](_0x6c9fx9)}};}function handleEntityNotFound(_0x6c9fx5){return function(_0x6c9fx9){if(!_0x6c9fx9){_0x6c9fx5[_0x636a[6]](404)[_0x636a[8]]();return null;};return _0x6c9fx9;}}function saveUpdates(_0x6c9fxc){return function(_0x6c9fx9){return _0x6c9fx9[_0x636a[10]](_0x6c9fxc)[_0x636a[9]](function(_0x6c9fxd){return _0x6c9fxd})}}function removeEntity(_0x6c9fx5){return function(_0x6c9fx9){if(_0x6c9fx9){return _0x6c9fx9[_0x636a[11]]()[_0x636a[9]](function(){_0x6c9fx5[_0x636a[6]](204)[_0x636a[8]]()})}}}exports[_0x636a[12]]=function(_0x6c9fxf,_0x6c9fx5){var _0x6c9fx10=[_0x636a[13],_0x636a[14],_0x636a[15],_0x636a[16]];var _0x6c9fx11=_0x6c9fxf[_0x636a[18]][_0x636a[17]]?parseInt(_0x6c9fxf[_0x636a[18]][_0x636a[17]],10):100;var _0x6c9fx12=_0x6c9fxf[_0x636a[18]][_0x636a[19]]?parseInt(_0x6c9fxf[_0x636a[18]][_0x636a[19]],10):0;var _0x6c9fx13={where:{},limit:_0x6c9fx11,offset:_0x6c9fx12*_0x6c9fx11};_[_0x636a[33]](_0x6c9fxf[_0x636a[18]],function(_0x6c9fx14,_0x6c9fx15){switch(_0x6c9fx15){case _0x636a[17]:;case _0x636a[19]:break ;;case _0x636a[22]:_0x6c9fx13[_0x636a[20]]=util[_0x636a[25]](_0x636a[21],_0x6c9fxf[_0x636a[18]][_0x636a[22]],_0x6c9fxf[_0x636a[18]][_0x636a[23]]||_0x636a[24])||null;break ;;case _0x636a[23]:break ;;case _0x636a[31]:_0x6c9fx13[_0x636a[27]][_0x636a[26]]=[];_0x6c9fx10[_0x636a[30]](function(_0x6c9fx16){var _0x6c9fx17={};_0x6c9fx17[_0x6c9fx16]={$like:_0x636a[28]+_0x6c9fx14+_0x636a[28]};_0x6c9fx13[_0x636a[27]][_0x636a[26]][_0x636a[29]](_0x6c9fx17);});break ;;default:_0x6c9fx13[_0x636a[27]][_0x6c9fx15]={$like:{}};_0x6c9fx13[_0x636a[27]][_0x6c9fx15][_0x636a[32]]=_0x636a[28]+_0x6c9fx14+_0x636a[28];;}});SugarcrmAccount[_0x636a[45]](_0x6c9fx13)[_0x636a[9]](function(_0x6c9fx18){var _0x6c9fx19=Math[_0x636a[37]](_0x6c9fx18[_0x636a[36]]/_0x6c9fx11);var _0x6c9fx1a=_0x6c9fx19>(_0x6c9fx13[_0x636a[38]]+1)?util[_0x636a[25]](_0x636a[39],_0x6c9fxf[_0x636a[40]],_0x6c9fxf[_0x636a[42]][_0x636a[41]],_0x6c9fxf[_0x636a[43]],_0x6c9fx12+1):null;var _0x6c9fx1b=_0x6c9fx12>0?util[_0x636a[25]](_0x636a[39],_0x6c9fxf[_0x636a[40]],_0x6c9fxf[_0x636a[42]][_0x636a[41]],_0x6c9fxf[_0x636a[43]],_0x6c9fx12-1):null;return _0x6c9fx5[_0x636a[6]](200)[_0x636a[5]]({count:_0x6c9fx18[_0x636a[36]],rows:_0x6c9fx18[_0x636a[44]],next_page:_0x6c9fx1a,previous_page:_0x6c9fx1b,total_pages:_0x6c9fx19});})[_0x636a[35]](function(_0x6c9fx7){return _0x6c9fx5[_0x636a[6]](500)[_0x636a[5]]({error:_0x636a[34]})});};exports[_0x636a[46]]=function(_0x6c9fxf,_0x6c9fx5){SugarcrmAccount[_0x636a[49]](_0x6c9fxf[_0x636a[48]][_0x636a[47]])[_0x636a[9]](handleEntityNotFound(_0x6c9fx5))[_0x636a[9]](responseWithResult(_0x6c9fx5))[_0x636a[35]](handleError(_0x6c9fx5))};exports[_0x636a[50]]=function(_0x6c9fxf,_0x6c9fx5){SugarcrmAccount[_0x636a[50]](_0x6c9fxf[_0x636a[51]])[_0x636a[9]](responseWithResult(_0x6c9fx5,201))[_0x636a[35]](handleError(_0x6c9fx5))};exports[_0x636a[52]]=function(_0x6c9fxf,_0x6c9fx5){if(_0x6c9fxf[_0x636a[51]][_0x636a[47]]){delete _0x6c9fxf[_0x636a[51]][_0x636a[47]]};SugarcrmAccount[_0x636a[49]](_0x6c9fxf[_0x636a[48]][_0x636a[47]])[_0x636a[9]](handleEntityNotFound(_0x6c9fx5))[_0x636a[9]](saveUpdates(_0x6c9fxf[_0x636a[51]]))[_0x636a[9]](responseWithResult(_0x6c9fx5))[_0x636a[35]](handleError(_0x6c9fx5));};exports[_0x636a[11]]=function(_0x6c9fxf,_0x6c9fx5){SugarcrmAccount[_0x636a[49]](_0x6c9fxf[_0x636a[48]][_0x636a[47]])[_0x636a[9]](handleEntityNotFound(_0x6c9fx5))[_0x636a[9]](removeEntity(_0x6c9fx5))[_0x636a[35]](handleError(_0x6c9fx5))};exports[_0x636a[53]]=function(_0x6c9fxf,_0x6c9fx5){SugarcrmAccount[_0x636a[49]](_0x6c9fxf[_0x636a[48]][_0x636a[47]])[_0x636a[9]](handleEntityNotFound(_0x6c9fx5))[_0x636a[9]](function(_0x6c9fx1c){var _0x6c9fx1d=require(_0x636a[54]);_0x6c9fx1d[_0x636a[57]]({apiURL:stripTrailingSlash(_0x6c9fx1c[_0x636a[16]])+_0x636a[55],login:_0x6c9fx1c[_0x636a[15]],passwd:_0x6c9fx1c[_0x636a[56]]});_0x6c9fx1d[_0x636a[59]](function(_0x6c9fx1e){if(!_0x6c9fx1e){return _0x6c9fx5[_0x636a[58]](400)};return _0x6c9fx5[_0x636a[58]](200);});})[_0x636a[35]](handleError(_0x6c9fx5))};exports[_0x636a[60]]=function(_0x6c9fxf,_0x6c9fx5){SugarcrmAccount[_0x636a[11]]({where:{id:_0x6c9fxf[_0x636a[18]][_0x636a[47]]},individualHooks:true})[_0x636a[9]](function(){return _0x6c9fx5[_0x636a[58]](204)})[_0x636a[35]](function(_0x6c9fx7){return handleError(_0x6c9fx5,_0x6c9fx7)})};exports[_0x636a[61]]=function(_0x6c9fxf,_0x6c9fx5){var _0x6c9fx1f={};_0x6c9fx1f[_0x6c9fxf[_0x636a[48]][_0x636a[62]]]=_0x6c9fxf[_0x636a[51]][_0x636a[63]];SugarcrmAccount[_0x636a[45]]({where:_0x6c9fx1f})[_0x636a[9]](function(_0x6c9fx18){if(_0x6c9fx18[_0x636a[36]]){return _0x6c9fx5[_0x636a[6]](200)[_0x636a[5]]({isValid:false,value:_0x6c9fxf[_0x636a[51]][_0x636a[63]]})};return _0x6c9fx5[_0x636a[6]](200)[_0x636a[5]]({isValid:true,value:_0x6c9fxf[_0x636a[51]][_0x636a[63]]});})[_0x636a[35]](function(_0x6c9fx7){return handleError(_0x6c9fx5,_0x6c9fx7)});};exports[_0x636a[64]]=function(_0x6c9fxf,_0x6c9fx5){var _0x6c9fx1d=require(_0x636a[54]);SugarcrmAccount[_0x636a[49]](_0x6c9fxf[_0x636a[48]][_0x636a[47]])[_0x636a[9]](handleEntityNotFound(_0x6c9fx5))[_0x636a[9]](function(_0x6c9fx1c){_0x6c9fx1d[_0x636a[57]]({apiURL:stripTrailingSlash(_0x6c9fx1c[_0x636a[16]])+_0x636a[55],login:_0x6c9fx1c[_0x636a[15]],passwd:_0x6c9fx1c[_0x636a[56]]});_0x6c9fx1d[_0x636a[59]](function(_0x6c9fx1e){if(!_0x6c9fx1e){return _0x6c9fx5[_0x636a[58]](400)};var _0x6c9fx20={session:_0x6c9fx1e,module_name:_0x636a[65]};_0x6c9fx1d[_0x636a[68]](_0x636a[66],_0x6c9fx20,function(_0x6c9fx18,_0x6c9fx7){if(_0x6c9fx7){return _0x6c9fx5[_0x636a[58]](400)};return _0x6c9fx5[_0x636a[6]](200)[_0x636a[5]](_0x6c9fx18[_0x636a[67]]);});});})[_0x636a[35]](handleError(_0x6c9fx5));};function stripTrailingSlash(_0x6c9fx22){if(_0x6c9fx22[_0x636a[69]](-1)===_0x636a[70]){return _0x6c9fx22[_0x636a[69]](0,_0x6c9fx22[_0x636a[71]]-1)};return _0x6c9fx22;}
\ No newline at end of file
+/**
+ * Using Rails-like standard naming convention for endpoints.
+ * GET     /api/sugarcrm/accounts              ->  index
+ * POST    /api/sugarcrm/accounts              ->  create
+ * GET     /api/sugarcrm/accounts/:id          ->  show
+ * PUT     /api/sugarcrm/accounts/:id          ->  update
+ * DELETE  /api/sugarcrm/accounts/:id          ->  destroy
+ */
+
+'use strict';
+
+
+var _ = require('lodash');
+var util = require('util');
+
+var SugarcrmAccount = require('../../models').SugarcrmAccount;
+
+
+function handleError(res, statusCode) {
+  statusCode = statusCode || 500;
+  return function(err) {
+    res.status(statusCode).send(err);
+  };
+}
+
+function responseWithResult(res, statusCode) {
+  statusCode = statusCode || 200;
+  return function(entity) {
+    if (entity) {
+      res.status(statusCode).json(entity);
+    }
+  };
+}
+
+function handleEntityNotFound(res) {
+  return function(entity) {
+    if (!entity) {
+      res.status(404).end();
+      return null;
+    }
+    return entity;
+  };
+}
+
+function saveUpdates(updates) {
+  return function(entity) {
+    return entity.updateAttributes(updates)
+      .then(function(updated) {
+        return updated;
+      });
+  };
+}
+
+function removeEntity(res) {
+  return function(entity) {
+    if (entity) {
+      return entity.destroy()
+        .then(function() {
+          res.status(204).end();
+        });
+    }
+  };
+}
+
+// Gets a list of SugarcrmAccounts
+exports.index = function(req, res) {
+  var attributes = ['description', 'name', 'username', 'remoteUri'];
+  var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
+  var page = req.query.page ? parseInt(req.query.page, 10) : 0;
+
+  var query = {
+    where: {},
+    limit: per_page,
+    offset: page * per_page
+  };
+
+  _.forIn(req.query, function(value, key) {
+    switch (key) {
+      case 'per_page':
+      case 'page':
+        break;
+      case 'sort_by':
+        query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
+        break;
+      case 'sort_order':
+        break;
+      case '$':
+        query.where.$or = [];
+        attributes.forEach(function(attribute) {
+          var tmp = {};
+          tmp[attribute] = {
+            $like: '%' + value + '%'
+          };
+
+          query.where.$or.push(tmp);
+        });
+        break;
+      default:
+        query.where[key] = {
+          $like: {}
+        };
+        query.where[key].$like = '%' + value + '%';
+    }
+  });
+
+  SugarcrmAccount
+    .findAndCountAll(query)
+    .then(function(result) {
+
+      var total_pages = Math.ceil(result.count / per_page);
+      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;
+      var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
+
+      return res.status(200).send({
+        count: result.count,
+        rows: result.rows,
+        next_page: next_page,
+        previous_page: previous_page,
+        total_pages: total_pages
+      });
+
+    })
+    .catch(function(err) {
+      return res.status(500).send({
+        error: 'Something blew up!'
+      });
+    });
+}
+
+// Gets a single SugarcrmAccount from the DB
+exports.show = function(req, res) {
+  SugarcrmAccount.findById(req.params.id)
+    .then(handleEntityNotFound(res))
+    .then(responseWithResult(res))
+    .catch(handleError(res));
+}
+
+// Creates a new SugarcrmAccount in the DB
+exports.create = function(req, res) {
+  SugarcrmAccount.create(req.body)
+    .then(responseWithResult(res, 201))
+    .catch(handleError(res));
+}
+
+// Updates an existing SugarcrmAccount in the DB
+exports.update = function(req, res) {
+  if (req.body.id) {
+    delete req.body.id;
+  }
+  SugarcrmAccount.findById(req.params.id)
+    .then(handleEntityNotFound(res))
+    .then(saveUpdates(req.body))
+    .then(responseWithResult(res))
+    .catch(handleError(res));
+}
+
+// Deletes a SugarcrmAccount from the DB
+exports.destroy = function(req, res) {
+  SugarcrmAccount.findById(req.params.id)
+    .then(handleEntityNotFound(res))
+    .then(removeEntity(res))
+    .catch(handleError(res));
+}
+
+exports.checkAccount = function(req, res) {
+  SugarcrmAccount.findById(req.params.id)
+    .then(handleEntityNotFound(res))
+    .then(function(account) {
+      var sugar = require('node-sugarcrm-client');
+      sugar.init({
+        apiURL: stripTrailingSlash(account.remoteUri) + "/service/v4_1/rest.php",
+        login: account.username,
+        passwd: account.password
+      });
+      sugar.login(function(sessionId) {
+        if (!sessionId) {
+          return res.sendStatus(400);
+        }
+        return res.sendStatus(200);
+      });
+    })
+    .catch(handleError(res));
+}
+
+exports.bulkDestroy = function(req, res) {
+  SugarcrmAccount
+    .destroy({
+      where: {
+        id: req.query.id
+      },
+      individualHooks: true
+    })
+    .then(function() {
+      return res.sendStatus(204);
+    })
+    .catch(function(err) {
+      return handleError(res, err);
+    });
+};
+
+exports.accountValidation = function(req, res) {
+  var where = {};
+  where[req.params.field] = req.body.value;
+  SugarcrmAccount
+    .findAndCountAll({
+      where: where
+    })
+    .then(function(result) {
+      if (result.count) {
+        return res.status(200).send({
+          isValid: false,
+          value: req.body.value
+        });
+      }
+      return res.status(200).send({
+        isValid: true,
+        value: req.body.value
+      });
+    })
+    .catch(function(err) {
+      return handleError(res, err);
+    });
+};
+
+exports.getFields = function(req, res) {
+  var sugar = require('node-sugarcrm-client');
+  SugarcrmAccount.findById(req.params.id)
+    .then(handleEntityNotFound(res))
+    .then(function(account) {
+      sugar.init({
+        apiURL: stripTrailingSlash(account.remoteUri) + "/service/v4_1/rest.php",
+        login: account.username,
+        passwd: account.password
+      });
+      sugar.login(function(sessionId) {
+        if (!sessionId) {
+          return res.sendStatus(400);
+        }
+        var params = {
+          session: sessionId,
+          module_name: "Cases"
+        };
+        sugar.call("get_module_fields", params, function(result, err) {
+          if (err) {
+            return res.sendStatus(400);
+          }
+          return res.status(200).send(result.module_fields);
+        });
+      });
+    })
+    .catch(handleError(res));
+};
+
+function stripTrailingSlash(str) {
+  if (str.substr(-1) === '/') {
+    return str.substr(0, str.length - 1);
+  }
+  return str;
+}