Built motion from commit 48095ee.|0.0.102
[motion.git] / server / api / sugarcrm_account / sugarcrm_account.controller.js
index cb7d78d..214ce45 100644 (file)
@@ -1,259 +1 @@
-/**
- * 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;
-}
+var _0xc8b7=["\x75\x73\x65\x20\x73\x74\x72\x69\x63\x74","\x6C\x6F\x64\x61\x73\x68","\x2E\x2E\x2F\x2E\x2E\x2F\x63\x6F\x6E\x66\x69\x67\x2F\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","\x73\x65\x6E\x64\x53\x74\x61\x74\x75\x73","\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","\x63\x61\x74\x63\x68","\x71\x75\x65\x72\x79","\x67\x65\x74\x51\x75\x65\x72\x79","\x66\x69\x6E\x64\x41\x6E\x64\x43\x6F\x75\x6E\x74\x41\x6C\x6C","\x63\x6F\x6E\x66\x69\x67\x75\x72\x61\x74\x69\x6F\x6E\x73","\x73\x63\x6F\x70\x65","\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","\x72\x65\x6D\x6F\x74\x65\x55\x72\x69","\x73\x74\x72\x69\x70\x54\x72\x61\x69\x6C\x69\x6E\x67\x53\x6C\x61\x73\x68","\x2F\x73\x65\x72\x76\x69\x63\x65\x2F\x76\x34\x5F\x31\x2F\x72\x65\x73\x74\x2E\x70\x68\x70","\x75\x73\x65\x72\x6E\x61\x6D\x65","\x70\x61\x73\x73\x77\x6F\x72\x64","\x69\x6E\x69\x74","\x6C\x6F\x67\x69\x6E","\x62\x75\x6C\x6B\x44\x65\x73\x74\x72\x6F\x79","\x69\x64\x73","\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","\x63\x6F\x75\x6E\x74","\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"];_0xc8b7[0];var _=require(_0xc8b7[1]);var Util=require(_0xc8b7[2]);var SugarcrmAccount=require(_0xc8b7[4])[_0xc8b7[3]];function handleError(_0xcffcx5,_0xcffcx6){_0xcffcx6=_0xcffcx6||500;return function(_0xcffcx7){_0xcffcx5[_0xc8b7[6]](_0xcffcx6)[_0xc8b7[5]](_0xcffcx7)}}function responseWithResult(_0xcffcx5,_0xcffcx6){_0xcffcx6=_0xcffcx6||200;return function(_0xcffcx9){if(_0xcffcx9){_0xcffcx5[_0xc8b7[6]](_0xcffcx6)[_0xc8b7[7]](_0xcffcx9)}}}function responseWithoutResult(_0xcffcx5,_0xcffcx6){_0xcffcx6=_0xcffcx6||200;return function(){_0xcffcx5[_0xc8b7[8]](_0xcffcx6)}}function handleEntityNotFound(_0xcffcx5){return function(_0xcffcx9){if(!_0xcffcx9){_0xcffcx5[_0xc8b7[6]](404)[_0xc8b7[9]]();return null};return _0xcffcx9}}function saveUpdates(_0xcffcxd){return function(_0xcffcx9){return _0xcffcx9[_0xc8b7[11]](_0xcffcxd)[_0xc8b7[10]](function(_0xcffcxe){return _0xcffcxe})}}function removeEntity(_0xcffcx5){return function(_0xcffcx9){if(_0xcffcx9){return _0xcffcx9[_0xc8b7[12]]()[_0xc8b7[10]](function(){_0xcffcx5[_0xc8b7[6]](204)[_0xc8b7[9]]()})}}}exports[_0xc8b7[13]]=function(_0xcffcx10,_0xcffcx5){return SugarcrmAccount[_0xc8b7[19]](_0xc8b7[18])[_0xc8b7[17]](Util[_0xc8b7[16]](_0xcffcx10[_0xc8b7[15]]))[_0xc8b7[10]](responseWithResult(_0xcffcx5))[_0xc8b7[14]](handleError(_0xcffcx5))};exports[_0xc8b7[20]]=function(_0xcffcx10,_0xcffcx5){return SugarcrmAccount[_0xc8b7[23]](_0xcffcx10[_0xc8b7[22]][_0xc8b7[21]])[_0xc8b7[10]](handleEntityNotFound(_0xcffcx5))[_0xc8b7[10]](responseWithResult(_0xcffcx5))[_0xc8b7[14]](handleError(_0xcffcx5))};exports[_0xc8b7[24]]=function(_0xcffcx10,_0xcffcx5){return SugarcrmAccount[_0xc8b7[24]](_0xcffcx10[_0xc8b7[25]])[_0xc8b7[10]](responseWithResult(_0xcffcx5,201))[_0xc8b7[14]](handleError(_0xcffcx5))};exports[_0xc8b7[26]]=function(_0xcffcx10,_0xcffcx5){if(_0xcffcx10[_0xc8b7[25]][_0xc8b7[21]]){delete _0xcffcx10[_0xc8b7[25]][_0xc8b7[21]]};return SugarcrmAccount[_0xc8b7[23]](_0xcffcx10[_0xc8b7[22]][_0xc8b7[21]])[_0xc8b7[10]](handleEntityNotFound(_0xcffcx5))[_0xc8b7[10]](saveUpdates(_0xcffcx10[_0xc8b7[25]]))[_0xc8b7[10]](responseWithResult(_0xcffcx5))[_0xc8b7[14]](handleError(_0xcffcx5))};exports[_0xc8b7[12]]=function(_0xcffcx10,_0xcffcx5){return SugarcrmAccount[_0xc8b7[23]](_0xcffcx10[_0xc8b7[22]][_0xc8b7[21]])[_0xc8b7[10]](handleEntityNotFound(_0xcffcx5))[_0xc8b7[10]](removeEntity(_0xcffcx5))[_0xc8b7[14]](handleError(_0xcffcx5))};exports[_0xc8b7[27]]=function(_0xcffcx10,_0xcffcx5){return SugarcrmAccount[_0xc8b7[23]](_0xcffcx10[_0xc8b7[22]][_0xc8b7[21]])[_0xc8b7[10]](handleEntityNotFound(_0xcffcx5))[_0xc8b7[10]](function(_0xcffcx11){var _0xcffcx12=require(_0xc8b7[28]);_0xcffcx12[_0xc8b7[34]]({apiURL:Util[_0xc8b7[30]](_0xcffcx11[_0xc8b7[29]])+_0xc8b7[31],login:_0xcffcx11[_0xc8b7[32]],passwd:_0xcffcx11[_0xc8b7[33]]});_0xcffcx12[_0xc8b7[35]](function(_0xcffcx13){if(!_0xcffcx13){return _0xcffcx5[_0xc8b7[8]](400)};return _0xcffcx5[_0xc8b7[8]](200)})})[_0xc8b7[14]](handleError(_0xcffcx5))};exports[_0xc8b7[36]]=function(_0xcffcx10,_0xcffcx5){SugarcrmAccount[_0xc8b7[12]]({where:{id:_0xcffcx10[_0xc8b7[15]][_0xc8b7[37]]},individualHooks:true})[_0xc8b7[10]](function(){return _0xcffcx5[_0xc8b7[8]](204)})[_0xc8b7[14]](handleError(_0xcffcx5))};exports[_0xc8b7[38]]=function(_0xcffcx10,_0xcffcx5){var _0xcffcx14={};_0xcffcx14[_0xcffcx10[_0xc8b7[22]][_0xc8b7[39]]]=_0xcffcx10[_0xc8b7[25]][_0xc8b7[40]];return SugarcrmAccount[_0xc8b7[17]]({where:_0xcffcx14})[_0xc8b7[10]](function(_0xcffcx15){if(_0xcffcx15[_0xc8b7[41]]){return _0xcffcx5[_0xc8b7[6]](200)[_0xc8b7[5]]({isValid:false,value:_0xcffcx10[_0xc8b7[25]][_0xc8b7[40]]})};return _0xcffcx5[_0xc8b7[6]](200)[_0xc8b7[5]]({isValid:true,value:_0xcffcx10[_0xc8b7[25]][_0xc8b7[40]]})})[_0xc8b7[14]](function(_0xcffcx7){return handleError(_0xcffcx5,_0xcffcx7)})};exports[_0xc8b7[42]]=function(_0xcffcx10,_0xcffcx5){var _0xcffcx12=require(_0xc8b7[28]);return SugarcrmAccount[_0xc8b7[23]](_0xcffcx10[_0xc8b7[22]][_0xc8b7[21]])[_0xc8b7[10]](handleEntityNotFound(_0xcffcx5))[_0xc8b7[10]](function(_0xcffcx11){_0xcffcx12[_0xc8b7[34]]({apiURL:Util[_0xc8b7[30]](_0xcffcx11[_0xc8b7[29]])+_0xc8b7[31],login:_0xcffcx11[_0xc8b7[32]],passwd:_0xcffcx11[_0xc8b7[33]]});_0xcffcx12[_0xc8b7[35]](function(_0xcffcx13){if(!_0xcffcx13){return _0xcffcx5[_0xc8b7[8]](400)};var _0xcffcx16={session:_0xcffcx13,module_name:_0xc8b7[43]};_0xcffcx12[_0xc8b7[46]](_0xc8b7[44],_0xcffcx16,function(_0xcffcx15,_0xcffcx7){if(_0xcffcx7){return _0xcffcx5[_0xc8b7[8]](400)};return _0xcffcx5[_0xc8b7[6]](200)[_0xc8b7[5]](_0xcffcx15[_0xc8b7[45]])})})})[_0xc8b7[14]](handleError(_0xcffcx5))}
\ No newline at end of file