Built motion from commit 5e31ea4.|0.0.32
[motion.git] / server / api / mail_business_automation / mail_business_automation.controller.js
index 9eb4d5e..5c49502 100644 (file)
@@ -1 +1,218 @@
-var _0x118f=["\x75\x73\x65\x20\x73\x74\x72\x69\x63\x74","\x6C\x6F\x64\x61\x73\x68","\x75\x74\x69\x6C","\x4D\x61\x69\x6C\x42\x75\x73\x69\x6E\x65\x73\x73\x41\x75\x74\x6F\x6D\x61\x74\x69\x6F\x6E","\x2E\x2E\x2F\x2E\x2E\x2F\x6D\x6F\x64\x65\x6C\x73","\x42\x75\x73\x69\x6E\x65\x73\x73\x43\x6F\x6E\x64\x69\x74\x69\x6F\x6E","\x42\x75\x73\x69\x6E\x65\x73\x73\x41\x63\x74\x69\x6F\x6E","\x73\x65\x71\x75\x65\x6C\x69\x7A\x65","\x69\x6E\x64\x65\x78","\x6E\x61\x6D\x65","\x64\x65\x73\x63\x72\x69\x70\x74\x69\x6F\x6E","\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","\x73\x65\x6E\x64","\x73\x74\x61\x74\x75\x73","\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","\x74\x68\x65\x6E","\x66\x69\x6E\x64\x41\x6E\x64\x43\x6F\x75\x6E\x74\x41\x6C\x6C","\x73\x68\x6F\x77","\x73\x65\x6E\x64\x53\x74\x61\x74\x75\x73","\x69\x64","\x70\x61\x72\x61\x6D\x73","\x66\x69\x6E\x64\x42\x79\x49\x64","\x63\x72\x65\x61\x74\x65","\x6F\x72","\x62\x6F\x64\x79","\x4D\x61\x69\x6C\x42\x75\x73\x69\x6E\x65\x73\x73\x41\x75\x74\x6F\x6D\x61\x74\x69\x6F\x6E\x49\x64","\x63\x6F\x6E\x63\x61\x74","\x61\x6E\x64","\x61\x63\x74\x69\x6F\x6E\x73","\x62\x75\x6C\x6B\x43\x72\x65\x61\x74\x65","\x74\x72\x61\x6E\x73\x61\x63\x74\x69\x6F\x6E","\x75\x70\x64\x61\x74\x65","\x6D\x65\x72\x67\x65","\x73\x61\x76\x65","\x66\x69\x6E\x64","\x64\x65\x73\x74\x72\x6F\x79","\x62\x75\x6C\x6B\x44\x65\x73\x74\x72\x6F\x79"];_0x118f[0];var _=require(_0x118f[1]);var util=require(_0x118f[2]);var MailBusinessAutomation=require(_0x118f[4])[_0x118f[3]];var BusinessCondition=require(_0x118f[4])[_0x118f[5]];var BusinessAction=require(_0x118f[4])[_0x118f[6]];var sequelize=require(_0x118f[4])[_0x118f[7]];exports[_0x118f[8]]=function(_0xa58dx7,_0xa58dx8,_0xa58dx9){var _0xa58dxa=[_0x118f[9],_0x118f[10]];var _0xa58dxb=_0xa58dx7[_0x118f[12]][_0x118f[11]]?parseInt(_0xa58dx7[_0x118f[12]][_0x118f[11]],10):100;var _0xa58dxc=_0xa58dx7[_0x118f[12]][_0x118f[13]]?parseInt(_0xa58dx7[_0x118f[12]][_0x118f[13]],10):0;var _0xa58dxd={where:{},limit:_0xa58dxb,offset:_0xa58dxc*_0xa58dxb};_[_0x118f[27]](_0xa58dx7[_0x118f[12]],function(_0xa58dxe,_0xa58dxf){switch(_0xa58dxf){case _0x118f[11]:;case _0x118f[13]:break ;;case _0x118f[16]:_0xa58dxd[_0x118f[14]]=util[_0x118f[19]](_0x118f[15],_0xa58dx7[_0x118f[12]][_0x118f[16]],_0xa58dx7[_0x118f[12]][_0x118f[17]]||_0x118f[18])||null;break ;;case _0x118f[17]:break ;;case _0x118f[25]:_0xa58dxd[_0x118f[21]][_0x118f[20]]=[];_0xa58dxa[_0x118f[24]](function(_0xa58dx10){var _0xa58dx11={};_0xa58dx11[_0xa58dx10]={$like:_0x118f[22]+_0xa58dxe+_0x118f[22]};_0xa58dxd[_0x118f[21]][_0x118f[20]][_0x118f[23]](_0xa58dx11);});break ;;default:_0xa58dxd[_0x118f[21]][_0xa58dxf]={$like:{}};_0xa58dxd[_0x118f[21]][_0xa58dxf][_0x118f[26]]=_0x118f[22]+_0xa58dxe+_0x118f[22];;}});MailBusinessAutomation[_0x118f[42]](_0xa58dxd)[_0x118f[41]](function(_0xa58dx13){var _0xa58dx14=Math[_0x118f[33]](_0xa58dx13[_0x118f[32]]/_0xa58dxb);var _0xa58dx15=_0xa58dx14>(_0xa58dxd[_0x118f[34]]+1)?util[_0x118f[19]](_0x118f[35],_0xa58dx7[_0x118f[36]],_0xa58dx7[_0x118f[38]][_0x118f[37]],_0xa58dx7[_0x118f[39]],_0xa58dxc+1):null;var _0xa58dx16=_0xa58dxc>0?util[_0x118f[19]](_0x118f[35],_0xa58dx7[_0x118f[36]],_0xa58dx7[_0x118f[38]][_0x118f[37]],_0xa58dx7[_0x118f[39]],_0xa58dxc-1):null;_0xa58dx8[_0x118f[30]](200)[_0x118f[29]]({count:_0xa58dx13[_0x118f[32]],mailAutomations:_0xa58dx13[_0x118f[40]],next_page:_0xa58dx15,previous_page:_0xa58dx16,total_pages:_0xa58dx14});})[_0x118f[31]](function(_0xa58dx12){_0xa58dx8[_0x118f[30]](500)[_0x118f[29]]({error:_0x118f[28]})});};exports[_0x118f[43]]=function(_0xa58dx7,_0xa58dx8){MailBusinessAutomation[_0x118f[47]](_0xa58dx7[_0x118f[46]][_0x118f[45]],{include:[{all:true}]})[_0x118f[41]](function(_0xa58dx17){if(!_0xa58dx17){return _0xa58dx8[_0x118f[44]](404)};return _0xa58dx8[_0x118f[29]](_0xa58dx17);})[_0x118f[31]](function(_0xa58dx12){return handleError(_0xa58dx8,_0xa58dx12)})};exports[_0x118f[48]]=function(_0xa58dx7,_0xa58dx8,_0xa58dx9){return sequelize[_0x118f[56]](function(_0xa58dx18){return MailBusinessAutomation[_0x118f[48]](_0xa58dx7[_0x118f[50]],{transaction:_0xa58dx18})[_0x118f[41]](function(_0xa58dx17){var _0xa58dx19=[];if(_0xa58dx7[_0x118f[50]][_0x118f[49]]){_0xa58dx7[_0x118f[50]][_0x118f[49]][_0x118f[24]](function(_0xa58dx1a){_0xa58dx1a[_0x118f[51]]=_0xa58dx17[_0x118f[45]]});_0xa58dx19=_0xa58dx19[_0x118f[52]](_0xa58dx7[_0x118f[50]][_0x118f[49]]);};if(_0xa58dx7[_0x118f[50]][_0x118f[53]]){_0xa58dx7[_0x118f[50]][_0x118f[53]][_0x118f[24]](function(_0xa58dx1a){_0xa58dx1a[_0x118f[51]]=_0xa58dx17[_0x118f[45]]});_0xa58dx19=_0xa58dx19[_0x118f[52]](_0xa58dx7[_0x118f[50]][_0x118f[53]]);};return BusinessCondition[_0x118f[55]](_0xa58dx19,{transaction:_0xa58dx18})[_0x118f[41]](function(_0xa58dx1b){if(_0xa58dx7[_0x118f[50]][_0x118f[54]]){_0xa58dx7[_0x118f[50]][_0x118f[54]][_0x118f[24]](function(_0xa58dx1a){_0xa58dx1a[_0x118f[51]]=_0xa58dx17[_0x118f[45]]})};return BusinessAction[_0x118f[55]](_0xa58dx7[_0x118f[50]][_0x118f[54]],{transaction:_0xa58dx18});});})})[_0x118f[41]](function(){return _0xa58dx8[_0x118f[44]](201)})[_0x118f[31]](function(_0xa58dx12){return _0xa58dx9(_0xa58dx12)})};exports[_0x118f[57]]=function(_0xa58dx7,_0xa58dx8){if(_0xa58dx7[_0x118f[50]][_0x118f[45]]){delete _0xa58dx7[_0x118f[50]][_0x118f[45]]};MailBusinessAutomation[_0x118f[60]]({where:{id:_0xa58dx7[_0x118f[46]][_0x118f[45]]}})[_0x118f[41]](function(_0xa58dx17){if(!_0xa58dx17){return _0xa58dx8[_0x118f[44]](404)};var _0xa58dx1c=_[_0x118f[58]](_0xa58dx17,_0xa58dx7[_0x118f[50]]);_0xa58dx1c[_0x118f[59]]()[_0x118f[41]](function(){return _0xa58dx8[_0x118f[30]](200)[_0x118f[29]](_0xa58dx17)})[_0x118f[31]](function(_0xa58dx12){return handleError(_0xa58dx8,_0xa58dx12)});})[_0x118f[31]](function(_0xa58dx12){return handleError(_0xa58dx8,_0xa58dx12)});};exports[_0x118f[61]]=function(_0xa58dx7,_0xa58dx8){MailBusinessAutomation[_0x118f[47]](_0xa58dx7[_0x118f[46]][_0x118f[45]])[_0x118f[41]](function(_0xa58dx17){if(!_0xa58dx17){return _0xa58dx8[_0x118f[44]](404)};_0xa58dx17[_0x118f[61]]()[_0x118f[41]](function(){return _0xa58dx8[_0x118f[44]](204)})[_0x118f[31]](function(_0xa58dx12){return handleError(_0xa58dx8,_0xa58dx12)});})[_0x118f[31]](function(_0xa58dx12){return handleError(_0xa58dx8,_0xa58dx12)})};exports[_0x118f[62]]=function(_0xa58dx7,_0xa58dx8){MailBusinessAutomation[_0x118f[61]]({where:{id:_0xa58dx7[_0x118f[12]][_0x118f[45]]},individualHooks:true})[_0x118f[41]](function(){return _0xa58dx8[_0x118f[44]](204)})[_0x118f[31]](function(_0xa58dx12){return handleError(_0xa58dx8,_0xa58dx12)})};function handleError(_0xa58dx8,_0xa58dx12){return _0xa58dx8[_0x118f[30]](500)[_0x118f[29]](_0xa58dx12)}
\ No newline at end of file
+'use strict';
+
+var _ = require('lodash');
+var util = require('util');
+
+var MailBusinessAutomation = require('../../models').MailBusinessAutomation;
+var BusinessCondition = require('../../models').BusinessCondition;
+var BusinessAction = require('../../models').BusinessAction;
+var sequelize = require('../../models').sequelize;
+
+// Get list of agents
+exports.index = function(req, res, next) {
+
+  var attributes = ['name', 'description'];
+  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 + '%';
+    }
+  });
+
+  MailBusinessAutomation
+    .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;
+
+      res.status(200).send({
+        count: result.count,
+        mailAutomations: result.rows,
+        next_page: next_page,
+        previous_page: previous_page,
+        total_pages: total_pages
+      });
+
+    })
+    .catch(function(err) {
+      res.status(500).send({
+        error: 'Something blew up!'
+      });
+    });
+};
+// Get a single mailBusinessAutomation
+exports.show = function(req, res) {
+  MailBusinessAutomation
+    .findById(req.params.id, {
+      include: [{
+        all: true
+      }]
+    })
+    .then(function(mailBusinessAutomation) {
+      if (!mailBusinessAutomation) {
+        return res.sendStatus(404);
+      }
+      return res.send(mailBusinessAutomation);
+    })
+    .catch(function(err) {
+      return handleError(res, err);
+    });
+};
+
+// Creates a new mailBusinessAutomation in the DB.
+exports.create = function(req, res, next) {
+
+  return sequelize.transaction(function(t) {
+      return MailBusinessAutomation
+        .create(req.body, {
+          transaction: t
+        })
+        .then(function(mailBusinessAutomation) {
+          var conditions = [];
+          if (req.body.or) {
+            req.body.or.forEach(function(elm) {
+              elm.MailBusinessAutomationId = mailBusinessAutomation.id;
+            });
+            conditions = conditions.concat(req.body.or);
+          }
+
+          if (req.body.and) {
+            req.body.and.forEach(function(elm) {
+              elm.MailBusinessAutomationId = mailBusinessAutomation.id;
+            });
+            conditions = conditions.concat(req.body.and);
+          }
+
+          return BusinessCondition
+            .bulkCreate(conditions, {
+              transaction: t
+            })
+            .then(function(businessCondtions) {
+              if (req.body.actions) {
+                req.body.actions.forEach(function(elm) {
+                  elm.MailBusinessAutomationId = mailBusinessAutomation.id;
+                });
+              }
+
+              return BusinessAction
+                .bulkCreate(req.body.actions, {
+                  transaction: t
+                });
+            });
+        })
+    })
+    .then(function() {
+      // Transaction has been committed
+      // result is whatever the result of the promise chain returned to the transaction callback
+      return res.sendStatus(201);
+    }).catch(function(err) {
+      // Transaction has been rolled back
+      // err is whatever rejected the promise chain returned to the transaction callback
+      return next(err);
+    });
+};
+
+// Updates an existing mailBusinessAutomation in the DB.
+exports.update = function(req, res) {
+  if (req.body.id) {
+    delete req.body.id;
+  }
+  MailBusinessAutomation
+    .find({
+      where: {
+        id: req.params.id
+      }
+    })
+    .then(function(mailBusinessAutomation) {
+      if (!mailBusinessAutomation) {
+        return res.sendStatus(404);
+      }
+      var updated = _.merge(mailBusinessAutomation, req.body);
+      updated.save()
+        .then(function() {
+          return res.status(200).send(mailBusinessAutomation);
+        })
+        .catch(function(err) {
+          return handleError(res, err);
+        });
+    })
+    .catch(function(err) {
+      return handleError(res, err);
+    });
+};
+
+// Deletes a mailBusinessAutomation from the DB.
+exports.destroy = function(req, res) {
+  MailBusinessAutomation
+    .findById(req.params.id)
+    .then(function(mailBusinessAutomation) {
+      if (!mailBusinessAutomation) {
+        return res.sendStatus(404);
+      }
+      mailBusinessAutomation
+        .destroy()
+        .then(function() {
+          return res.sendStatus(204);
+        })
+        .catch(function(err) {
+          return handleError(res, err);
+        });
+    })
+    .catch(function(err) {
+      return handleError(res, err);
+    });
+};
+
+// Deletes a agent from the DB.
+exports.bulkDestroy = function(req, res) {
+  MailBusinessAutomation
+    .destroy({
+      where: {
+        id: req.query.id
+      },
+      individualHooks: true
+    })
+    .then(function() {
+      return res.sendStatus(204);
+    })
+    .catch(function(err) {
+      return handleError(res, err);
+    });
+};
+
+function handleError(res, err) {
+  return res.status(500).send(err);
+}