Built motion from commit 5e31ea4.|0.0.32
[motion.git] / server / api / freshdesk_account / freshdesk_account.controller.js
index 5bbcf5e..ae9ab8a 100644 (file)
@@ -1 +1,239 @@
-var _0x8d5c=["\x75\x73\x65\x20\x73\x74\x72\x69\x63\x74","\x6C\x6F\x64\x61\x73\x68","\x75\x74\x69\x6C","\x66\x72\x65\x73\x68\x64\x65\x73\x6B","\x46\x72\x65\x73\x68\x64\x65\x73\x6B\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","\x62\x75\x6C\x6B\x44\x65\x73\x74\x72\x6F\x79","\x73\x65\x6E\x64\x53\x74\x61\x74\x75\x73","\x63\x68\x65\x63\x6B\x41\x63\x63\x6F\x75\x6E\x74","\x61\x70\x69\x4B\x65\x79","\x70\x61\x72\x73\x65","\x72\x65\x71\x75\x69\x72\x65\x5F\x6C\x6F\x67\x69\x6E","\x6C\x69\x73\x74\x54\x69\x63\x6B\x65\x74\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","\x67\x65\x74\x46\x69\x65\x6C\x64\x73","\x2F\x74\x69\x63\x6B\x65\x74\x5F\x66\x69\x65\x6C\x64\x73\x2E\x6A\x73\x6F\x6E","\x67\x65\x74"];_0x8d5c[0];var _=require(_0x8d5c[1]);var util=require(_0x8d5c[2]);var _fd=require(_0x8d5c[3]);var FreshdeskAccount=require(_0x8d5c[5])[_0x8d5c[4]];function handleError(_0x33d4x6,_0x33d4x7){_0x33d4x7=_0x33d4x7||500;return function(_0x33d4x8){_0x33d4x6[_0x8d5c[7]](_0x33d4x7)[_0x8d5c[6]](_0x33d4x8)};}function responseWithResult(_0x33d4x6,_0x33d4x7){_0x33d4x7=_0x33d4x7||200;return function(_0x33d4xa){if(_0x33d4xa){_0x33d4x6[_0x8d5c[7]](_0x33d4x7)[_0x8d5c[8]](_0x33d4xa)}};}function handleEntityNotFound(_0x33d4x6){return function(_0x33d4xa){if(!_0x33d4xa){_0x33d4x6[_0x8d5c[7]](404)[_0x8d5c[9]]();return null;};return _0x33d4xa;}}function saveUpdates(_0x33d4xd){return function(_0x33d4xa){return _0x33d4xa[_0x8d5c[11]](_0x33d4xd)[_0x8d5c[10]](function(_0x33d4xe){return _0x33d4xe})}}function removeEntity(_0x33d4x6){return function(_0x33d4xa){if(_0x33d4xa){return _0x33d4xa[_0x8d5c[12]]()[_0x8d5c[10]](function(){_0x33d4x6[_0x8d5c[7]](204)[_0x8d5c[9]]()})}}}exports[_0x8d5c[13]]=function(_0x33d4x10,_0x33d4x6){var _0x33d4x11=[_0x8d5c[14],_0x8d5c[15],_0x8d5c[16],_0x8d5c[17]];var _0x33d4x12=_0x33d4x10[_0x8d5c[19]][_0x8d5c[18]]?parseInt(_0x33d4x10[_0x8d5c[19]][_0x8d5c[18]],10):100;var _0x33d4x13=_0x33d4x10[_0x8d5c[19]][_0x8d5c[20]]?parseInt(_0x33d4x10[_0x8d5c[19]][_0x8d5c[20]],10):0;var _0x33d4x14={where:{},limit:_0x33d4x12,offset:_0x33d4x13*_0x33d4x12};_[_0x8d5c[34]](_0x33d4x10[_0x8d5c[19]],function(_0x33d4x15,_0x33d4x16){switch(_0x33d4x16){case _0x8d5c[18]:;case _0x8d5c[20]:break ;;case _0x8d5c[23]:_0x33d4x14[_0x8d5c[21]]=util[_0x8d5c[26]](_0x8d5c[22],_0x33d4x10[_0x8d5c[19]][_0x8d5c[23]],_0x33d4x10[_0x8d5c[19]][_0x8d5c[24]]||_0x8d5c[25])||null;break ;;case _0x8d5c[24]:break ;;case _0x8d5c[32]:_0x33d4x14[_0x8d5c[28]][_0x8d5c[27]]=[];_0x33d4x11[_0x8d5c[31]](function(_0x33d4x17){var _0x33d4x18={};_0x33d4x18[_0x33d4x17]={$like:_0x8d5c[29]+_0x33d4x15+_0x8d5c[29]};_0x33d4x14[_0x8d5c[28]][_0x8d5c[27]][_0x8d5c[30]](_0x33d4x18);});break ;;default:_0x33d4x14[_0x8d5c[28]][_0x33d4x16]={$like:{}};_0x33d4x14[_0x8d5c[28]][_0x33d4x16][_0x8d5c[33]]=_0x8d5c[29]+_0x33d4x15+_0x8d5c[29];;}});FreshdeskAccount[_0x8d5c[46]](_0x33d4x14)[_0x8d5c[10]](function(_0x33d4x19){var _0x33d4x1a=Math[_0x8d5c[38]](_0x33d4x19[_0x8d5c[37]]/_0x33d4x12);var _0x33d4x1b=_0x33d4x1a>(_0x33d4x14[_0x8d5c[39]]+1)?util[_0x8d5c[26]](_0x8d5c[40],_0x33d4x10[_0x8d5c[41]],_0x33d4x10[_0x8d5c[43]][_0x8d5c[42]],_0x33d4x10[_0x8d5c[44]],_0x33d4x13+1):null;var _0x33d4x1c=_0x33d4x13>0?util[_0x8d5c[26]](_0x8d5c[40],_0x33d4x10[_0x8d5c[41]],_0x33d4x10[_0x8d5c[43]][_0x8d5c[42]],_0x33d4x10[_0x8d5c[44]],_0x33d4x13-1):null;return _0x33d4x6[_0x8d5c[7]](200)[_0x8d5c[6]]({count:_0x33d4x19[_0x8d5c[37]],rows:_0x33d4x19[_0x8d5c[45]],next_page:_0x33d4x1b,previous_page:_0x33d4x1c,total_pages:_0x33d4x1a});})[_0x8d5c[36]](function(_0x33d4x8){return _0x33d4x6[_0x8d5c[7]](500)[_0x8d5c[6]]({error:_0x8d5c[35]})});};exports[_0x8d5c[47]]=function(_0x33d4x10,_0x33d4x6){FreshdeskAccount[_0x8d5c[50]](_0x33d4x10[_0x8d5c[49]][_0x8d5c[48]])[_0x8d5c[10]](handleEntityNotFound(_0x33d4x6))[_0x8d5c[10]](responseWithResult(_0x33d4x6))[_0x8d5c[36]](handleError(_0x33d4x6))};exports[_0x8d5c[51]]=function(_0x33d4x10,_0x33d4x6){FreshdeskAccount[_0x8d5c[51]](_0x33d4x10[_0x8d5c[52]])[_0x8d5c[10]](responseWithResult(_0x33d4x6,201))[_0x8d5c[36]](handleError(_0x33d4x6))};exports[_0x8d5c[53]]=function(_0x33d4x10,_0x33d4x6){if(_0x33d4x10[_0x8d5c[52]][_0x8d5c[48]]){delete _0x33d4x10[_0x8d5c[52]][_0x8d5c[48]]};FreshdeskAccount[_0x8d5c[50]](_0x33d4x10[_0x8d5c[49]][_0x8d5c[48]])[_0x8d5c[10]](handleEntityNotFound(_0x33d4x6))[_0x8d5c[10]](saveUpdates(_0x33d4x10[_0x8d5c[52]]))[_0x8d5c[10]](responseWithResult(_0x33d4x6))[_0x8d5c[36]](handleError(_0x33d4x6));};exports[_0x8d5c[12]]=function(_0x33d4x10,_0x33d4x6){FreshdeskAccount[_0x8d5c[50]](_0x33d4x10[_0x8d5c[49]][_0x8d5c[48]])[_0x8d5c[10]](handleEntityNotFound(_0x33d4x6))[_0x8d5c[10]](removeEntity(_0x33d4x6))[_0x8d5c[36]](handleError(_0x33d4x6))};exports[_0x8d5c[54]]=function(_0x33d4x10,_0x33d4x6){FreshdeskAccount[_0x8d5c[12]]({where:{id:_0x33d4x10[_0x8d5c[19]][_0x8d5c[48]]},individualHooks:true})[_0x8d5c[10]](function(){return _0x33d4x6[_0x8d5c[55]](204)})[_0x8d5c[36]](function(_0x33d4x8){return handleError(_0x33d4x6,_0x33d4x8)})};exports[_0x8d5c[56]]=function(_0x33d4x10,_0x33d4x6){FreshdeskAccount[_0x8d5c[50]](_0x33d4x10[_0x8d5c[49]][_0x8d5c[48]])[_0x8d5c[10]](handleEntityNotFound(_0x33d4x6))[_0x8d5c[10]](function(_0x33d4x1d){var _0x33d4x1e= new _fd(_0x33d4x1d[_0x8d5c[17]],_0x33d4x1d[_0x8d5c[57]]);_0x33d4x1e[_0x8d5c[60]](function(_0x33d4x8,_0x33d4x1f,_0x33d4x20){if(_0x33d4x8){return _0x33d4x6[_0x8d5c[55]](400)};_0x33d4x20=JSON[_0x8d5c[58]](_0x33d4x20);if(_0x33d4x20[_0x8d5c[59]]){return _0x33d4x6[_0x8d5c[55]](400)};return _0x33d4x6[_0x8d5c[55]](200);});})[_0x8d5c[36]](handleError(_0x33d4x6))};exports[_0x8d5c[61]]=function(_0x33d4x10,_0x33d4x6){var _0x33d4x21={};_0x33d4x21[_0x33d4x10[_0x8d5c[49]][_0x8d5c[62]]]=_0x33d4x10[_0x8d5c[52]][_0x8d5c[63]];FreshdeskAccount[_0x8d5c[46]]({where:_0x33d4x21})[_0x8d5c[10]](function(_0x33d4x19){if(_0x33d4x19[_0x8d5c[37]]){return _0x33d4x6[_0x8d5c[7]](200)[_0x8d5c[6]]({isValid:false,value:_0x33d4x10[_0x8d5c[52]][_0x8d5c[63]]})};return _0x33d4x6[_0x8d5c[7]](200)[_0x8d5c[6]]({isValid:true,value:_0x33d4x10[_0x8d5c[52]][_0x8d5c[63]]});})[_0x8d5c[36]](function(_0x33d4x8){return handleError(_0x33d4x6,_0x33d4x8)});};exports[_0x8d5c[64]]=function(_0x33d4x10,_0x33d4x6,_0x33d4x22){FreshdeskAccount[_0x8d5c[50]](_0x33d4x10[_0x8d5c[49]][_0x8d5c[48]])[_0x8d5c[10]](handleEntityNotFound(_0x33d4x6))[_0x8d5c[10]](function(_0x33d4x1d){var _0x33d4x23= new _fd(_0x33d4x1d[_0x8d5c[17]],_0x33d4x1d[_0x8d5c[57]]);_0x33d4x23[_0x8d5c[66]](_0x8d5c[65],function(_0x33d4x8,_0x33d4x1f,_0x33d4x20){if(_0x33d4x8){return _0x33d4x22(_0x33d4x8)};_0x33d4x20=JSON[_0x8d5c[58]](_0x33d4x20);return _0x33d4x6[_0x8d5c[7]](200)[_0x8d5c[6]](_0x33d4x20);});})[_0x8d5c[36]](handleError(_0x33d4x6))};
\ No newline at end of file
+/**
+ * Using Rails-like standard naming convention for endpoints.
+ * GET     /api/freshdesk/accounts              ->  index
+ * POST    /api/freshdesk/accounts              ->  create
+ * GET     /api/freshdesk/accounts/:id          ->  show
+ * PUT     /api/freshdesk/accounts/:id          ->  update
+ * DELETE  /api/freshdesk/accounts/:id          ->  destroy
+ */
+
+'use strict';
+
+
+var _ = require('lodash');
+var util = require('util');
+var _fd = require('freshdesk');
+
+var FreshdeskAccount = require('../../models').FreshdeskAccount;
+
+
+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 FreshdeskAccounts
+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 + '%';
+    }
+  });
+
+  FreshdeskAccount
+    .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 FreshdeskAccount from the DB
+exports.show = function(req, res) {
+  FreshdeskAccount.findById(req.params.id)
+    .then(handleEntityNotFound(res))
+    .then(responseWithResult(res))
+    .catch(handleError(res));
+}
+
+// Creates a new FreshdeskAccount in the DB
+exports.create = function(req, res) {
+  FreshdeskAccount.create(req.body)
+    .then(responseWithResult(res, 201))
+    .catch(handleError(res));
+}
+
+// Updates an existing FreshdeskAccount in the DB
+exports.update = function(req, res) {
+  if (req.body.id) {
+    delete req.body.id;
+  }
+  FreshdeskAccount.findById(req.params.id)
+    .then(handleEntityNotFound(res))
+    .then(saveUpdates(req.body))
+    .then(responseWithResult(res))
+    .catch(handleError(res));
+}
+
+// Deletes a FreshdeskAccount from the DB
+exports.destroy = function(req, res) {
+  FreshdeskAccount.findById(req.params.id)
+    .then(handleEntityNotFound(res))
+    .then(removeEntity(res))
+    .catch(handleError(res));
+}
+
+exports.bulkDestroy = function(req, res) {
+  FreshdeskAccount
+    .destroy({
+      where: {
+        id: req.query.id
+      },
+      individualHooks: true
+    })
+    .then(function() {
+      return res.sendStatus(204);
+    })
+    .catch(function(err) {
+      return handleError(res, err);
+    });
+};
+
+exports.checkAccount = function(req, res) {
+  FreshdeskAccount.findById(req.params.id)
+    .then(handleEntityNotFound(res))
+    .then(function(account) {
+      var freshdesk = new _fd(account.remoteUri, account.apiKey);
+      freshdesk.listTickets(function(err, response, body) {
+        if (err) {
+          return res.sendStatus(400);
+        }
+        body = JSON.parse(body);
+        if (body.require_login) {
+          return res.sendStatus(400);
+        }
+        return res.sendStatus(200);
+      })
+    })
+    .catch(handleError(res));
+}
+
+exports.accountValidation = function(req, res) {
+  var where = {};
+  where[req.params.field] = req.body.value;
+  FreshdeskAccount
+    .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, next) {
+  FreshdeskAccount.findById(req.params.id)
+    .then(handleEntityNotFound(res))
+    .then(function(account) {
+      var Freshdesk = new _fd(account.remoteUri, account.apiKey);
+      Freshdesk.get('/ticket_fields.json', function(err, response, body) {
+        if (err) {
+          return next(err);
+        }
+        body = JSON.parse(body);
+        return res.status(200).send(body);
+      });
+    })
+    .catch(handleError(res));
+};