Built motion from commit 5e31ea4.|0.0.32
[motion.git] / server / api / trunk / trunk.controller.js
index 8aed4fc..2ccdd9a 100644 (file)
@@ -1 +1,298 @@
-var _0x8d03=["\x75\x73\x65\x20\x73\x74\x72\x69\x63\x74","\x6C\x6F\x64\x61\x73\x68","\x54\x72\x75\x6E\x6B","\x2E\x2E\x2F\x2E\x2E\x2F\x6D\x6F\x64\x65\x6C\x73","\x56\x6F\x69\x63\x65\x45\x78\x74\x65\x6E\x73\x69\x6F\x6E","\x73\x65\x71\x75\x65\x6C\x69\x7A\x65","\x75\x74\x69\x6C","\x69\x6E\x64\x65\x78","\x6E\x61\x6D\x65","\x68\x6F\x73\x74","\x63\x6F\x6E\x74\x65\x78\x74","\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","\x72\x6F\x6C\x65","\x73\x70\x6C\x69\x74","\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\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","\x74\x72\x75\x6E\x6B\x56\x61\x6C\x69\x64\x61\x74\x69\x6F\x6E","\x62\x6F\x64\x79","\x6C\x6F\x67","\x66\x69\x6E\x64\x41\x6C\x6C","\x63\x72\x65\x61\x74\x65","\x74\x72\x75\x6E\x6B\x73\x20\x66\x69\x6E\x64\x61\x6C\x6C","\x46\x69\x6E\x64\x69\x6E\x67\x20\x61\x70\x70\x20\x72\x6F\x77\x2E\x2E\x2E\x2E\x2E","\x6C\x65\x6E\x67\x74\x68","\x4D\x45\x53\x53\x41\x47\x45\x5F\x45\x58\x49\x53\x54\x5F\x54\x52\x55\x4E\x4B","\x74\x72\x75\x6E\x6B\x73\x20\x63\x72\x65\x61\x74\x65","\x75\x70\x64\x61\x74\x65","\x6D\x65\x72\x67\x65","\x63\x75\x74\x64\x69\x67\x69\x74\x73","\x6C\x69\x74\x65\x72\x61\x6C","\x73\x61\x76\x65","\x74\x72\x61\x6E\x73\x61\x63\x74\x69\x6F\x6E","\x64\x65\x73\x74\x72\x6F\x79","\x4D\x45\x53\x53\x41\x47\x45\x5F\x54\x52\x55\x4E\x4B\x5F\x4F\x55\x54\x42\x4F\x55\x4E\x44\x5F\x41\x53\x53\x4F\x43\x49\x41\x54\x45\x44","\x67\x65\x74\x56\x6F\x69\x63\x65\x45\x78\x74\x65\x6E\x73\x69\x6F\x6E\x73","\x62\x75\x6C\x6B\x44\x65\x73\x74\x72\x6F\x79","\x56\x6F\x69\x63\x65\x45\x78\x74\x65\x6E\x73\x69\x6F\x6E\x73"];_0x8d03[0];var _=require(_0x8d03[1]);var Trunk=require(_0x8d03[3])[_0x8d03[2]];var VoiceExtension=require(_0x8d03[3])[_0x8d03[4]];var sequelize=require(_0x8d03[3])[_0x8d03[5]];var util=require(_0x8d03[6]);exports[_0x8d03[7]]=function(_0x591fx6,_0x591fx7,_0x591fx8){var _0x591fx9=[_0x8d03[8],_0x8d03[9],_0x8d03[10],_0x8d03[11]];var _0x591fxa=_0x591fx6[_0x8d03[13]][_0x8d03[12]]?parseInt(_0x591fx6[_0x8d03[13]][_0x8d03[12]],10):100;var _0x591fxb=_0x591fx6[_0x8d03[13]][_0x8d03[14]]?parseInt(_0x591fx6[_0x8d03[13]][_0x8d03[14]],10):0;var _0x591fxc={where:{},limit:_0x591fxa,offset:_0x591fxb*_0x591fxa};_[_0x8d03[30]](_0x591fx6[_0x8d03[13]],function(_0x591fxd,_0x591fxe){switch(_0x591fxe){case _0x8d03[12]:;case _0x8d03[14]:break ;;case _0x8d03[17]:_0x591fxc[_0x8d03[15]]=util[_0x8d03[20]](_0x8d03[16],_0x591fx6[_0x8d03[13]][_0x8d03[17]],_0x591fx6[_0x8d03[13]][_0x8d03[18]]||_0x8d03[19])||null;break ;;case _0x8d03[18]:break ;;case _0x8d03[26]:_0x591fxc[_0x8d03[22]][_0x8d03[21]]=[];_0x591fx9[_0x8d03[25]](function(_0x591fxf){var _0x591fx10={};_0x591fx10[_0x591fxf]={$like:_0x8d03[23]+_0x591fxd+_0x8d03[23]};_0x591fxc[_0x8d03[22]][_0x8d03[21]][_0x8d03[24]](_0x591fx10);});break ;;case _0x8d03[27]:_0x591fxc[_0x8d03[22]][_0x8d03[27]]={$or:_0x591fxd[_0x8d03[28]](/[\s,]+/)};break ;;default:_0x591fxc[_0x8d03[22]][_0x591fxe]={$like:{}};_0x591fxc[_0x8d03[22]][_0x591fxe][_0x8d03[29]]=_0x8d03[23]+_0x591fxd+_0x8d03[23];;}});Trunk[_0x8d03[44]](_0x591fxc)[_0x8d03[43]](function(_0x591fx12){var _0x591fx13=Math[_0x8d03[36]](_0x591fx12[_0x8d03[35]]/_0x591fxa);var _0x591fx14=_0x591fx13>(_0x591fxc[_0x8d03[37]]+1)?util[_0x8d03[20]](_0x8d03[38],_0x591fx6[_0x8d03[39]],_0x591fx6[_0x8d03[40]][_0x8d03[9]],_0x591fx6[_0x8d03[41]],_0x591fxb+1):null;var _0x591fx15=_0x591fxb>0?util[_0x8d03[20]](_0x8d03[38],_0x591fx6[_0x8d03[39]],_0x591fx6[_0x8d03[40]][_0x8d03[9]],_0x591fx6[_0x8d03[41]],_0x591fxb-1):null;_0x591fx7[_0x8d03[33]](200)[_0x8d03[32]]({count:_0x591fx12[_0x8d03[35]],rows:_0x591fx12[_0x8d03[42]],next_page:_0x591fx14,previous_page:_0x591fx15,total_pages:_0x591fx13});})[_0x8d03[34]](function(_0x591fx11){_0x591fx7[_0x8d03[33]](500)[_0x8d03[32]]({error:_0x8d03[31]})});};exports[_0x8d03[45]]=function(_0x591fx6,_0x591fx7){Trunk[_0x8d03[49]](_0x591fx6[_0x8d03[48]][_0x8d03[47]])[_0x8d03[43]](function(_0x591fx16){if(!_0x591fx16){return _0x591fx7[_0x8d03[46]](404)};return _0x591fx7[_0x8d03[32]](_0x591fx16);})[_0x8d03[34]](function(_0x591fx11){return handleError(_0x591fx7,_0x591fx11)})};exports[_0x8d03[50]]=function(_0x591fx6,_0x591fx7){console[_0x8d03[52]](_0x591fx6[_0x8d03[51]]);Trunk[_0x8d03[53]]({where:{name:_0x591fx6[_0x8d03[51]][_0x8d03[8]]}})[_0x8d03[43]](function(_0x591fx17){if(!_0x591fx17){return _0x591fx7[_0x8d03[46]](404)};return _0x591fx7[_0x8d03[32]](_0x591fx17);})[_0x8d03[34]](function(_0x591fx11){return handleError(_0x591fx7,_0x591fx11)});};exports[_0x8d03[54]]=function(_0x591fx6,_0x591fx7){Trunk[_0x8d03[53]]({where:{name:_0x591fx6[_0x8d03[51]][_0x8d03[8]]}})[_0x8d03[43]](function(_0x591fx18){console[_0x8d03[52]](_0x591fx18);console[_0x8d03[52]](_0x8d03[56]);if(!_0x591fx18){return _0x591fx7[_0x8d03[46]](404)};if(_0x591fx18[_0x8d03[57]]>0){return _0x591fx7[_0x8d03[33]](500)[_0x8d03[32]]({message:_0x8d03[58]})};Trunk[_0x8d03[54]](_0x591fx6[_0x8d03[51]])[_0x8d03[43]](function(_0x591fx16){return _0x591fx7[_0x8d03[33]](201)[_0x8d03[32]](_0x591fx16)})[_0x8d03[34]](function(_0x591fx11){console[_0x8d03[52]](_0x8d03[59],_0x591fx11);return handleError(_0x591fx7,_0x591fx11);});})[_0x8d03[34]](function(_0x591fx11){console[_0x8d03[52]](_0x8d03[55],_0x591fx11);return handleError(_0x591fx7,_0x591fx11);})};exports[_0x8d03[60]]=function(_0x591fx6,_0x591fx7,_0x591fx8){Trunk[_0x8d03[53]]({where:{name:_0x591fx6[_0x8d03[51]][_0x8d03[8]],id:{$ne:_0x591fx6[_0x8d03[51]][_0x8d03[47]]}}})[_0x8d03[43]](function(_0x591fx18){console[_0x8d03[52]](_0x591fx18);console[_0x8d03[52]](_0x8d03[56]);if(!_0x591fx18){return _0x591fx7[_0x8d03[46]](404)};if(_0x591fx18[_0x8d03[57]]>0){return _0x591fx7[_0x8d03[33]](500)[_0x8d03[32]]({message:_0x8d03[58]})};if(_0x591fx6[_0x8d03[51]][_0x8d03[47]]){delete _0x591fx6[_0x8d03[51]][_0x8d03[47]]};Trunk[_0x8d03[49]](_0x591fx6[_0x8d03[48]][_0x8d03[47]])[_0x8d03[43]](function(_0x591fx16){if(!_0x591fx16){return _0x591fx7[_0x8d03[46]](404)};var _0x591fx19=_[_0x8d03[61]](_0x591fx16,_0x591fx6[_0x8d03[51]]);return sequelize[_0x8d03[65]](function(_0x591fx1a){return _0x591fx19[_0x8d03[64]]({transaction:_0x591fx1a})[_0x8d03[43]](function(_0x591fx16){return VoiceExtension[_0x8d03[60]]({trunk:_0x591fx16[_0x8d03[8]],cutdigits:sequelize[_0x8d03[63]](_0x8d03[62])},{where:{TrunkId:_0x591fx16[_0x8d03[47]]}},{transaction:_0x591fx1a})[_0x8d03[43]](function(){return _0x591fx7[_0x8d03[33]](200)[_0x8d03[32]](_0x591fx16)})})})[_0x8d03[34]](function(_0x591fx11){return handleError(_0x591fx7,_0x591fx11)});})[_0x8d03[34]](function(_0x591fx11){return _0x591fx8(_0x591fx11)});})[_0x8d03[34]](function(_0x591fx11){return _0x591fx8(_0x591fx11)})};exports[_0x8d03[66]]=function(_0x591fx6,_0x591fx7){Trunk[_0x8d03[49]](_0x591fx6[_0x8d03[48]][_0x8d03[47]])[_0x8d03[43]](function(_0x591fx16){if(!_0x591fx16){return _0x591fx7[_0x8d03[46]](404)};_0x591fx16[_0x8d03[68]]({where:{isApp:true}})[_0x8d03[43]](function(_0x591fx1b){console[_0x8d03[52]](_0x591fx1b[_0x8d03[57]]);if(_0x591fx1b[_0x8d03[57]]>0){return _0x591fx7[_0x8d03[33]](500)[_0x8d03[32]]({message:_0x8d03[67]})};_0x591fx16[_0x8d03[66]]()[_0x8d03[43]](function(){return _0x591fx7[_0x8d03[46]](204)})[_0x8d03[34]](function(_0x591fx11){return handleError(_0x591fx7,_0x591fx11)});})[_0x8d03[34]](function(_0x591fx11){return handleError(_0x591fx7,_0x591fx11)});})[_0x8d03[34]](function(_0x591fx11){return handleError(_0x591fx7,_0x591fx11)})};exports[_0x8d03[69]]=function(_0x591fx6,_0x591fx7){var _0x591fx1c=false;Trunk[_0x8d03[53]]({where:{id:_0x591fx6[_0x8d03[13]][_0x8d03[47]]},include:[{all:true}]})[_0x8d03[43]](function(_0x591fx17){if(!_0x591fx17){return _0x591fx7[_0x8d03[46]](404)};_0x591fx17[_0x8d03[25]](function(_0x591fx1d){if(_0x591fx1d[_0x8d03[70]][_0x8d03[57]]>0){_0x591fx1c=true}});if(_0x591fx1c){return _0x591fx7[_0x8d03[33]](500)[_0x8d03[32]]({message:_0x8d03[67]})};Trunk[_0x8d03[66]]({where:{id:_0x591fx6[_0x8d03[13]][_0x8d03[47]]},individualHooks:true})[_0x8d03[43]](function(){return _0x591fx7[_0x8d03[46]](204)})[_0x8d03[34]](function(_0x591fx11){return handleError(_0x591fx7,_0x591fx11)});})[_0x8d03[34]](function(_0x591fx11){return handleError(_0x591fx7,_0x591fx11)});};function handleError(_0x591fx7,_0x591fx11){return _0x591fx7[_0x8d03[33]](500)[_0x8d03[32]](_0x591fx11)}
\ No newline at end of file
+'use strict';
+
+var _ = require('lodash');
+var Trunk = require('../../models').Trunk;
+var VoiceExtension = require('../../models').VoiceExtension;
+var sequelize = require('../../models').sequelize;
+var util = require('util');
+
+// Get list of trunks
+exports.index = function(req, res, next) {
+
+  var attributes = ['name', 'host', 'context', '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;
+      case 'role':
+        query.where.role = {
+          $or: value.split(/[\s,]+/)
+        };
+        break;
+      default:
+        query.where[key] = {
+          $like: {}
+        };
+        query.where[key].$like = '%' + value + '%';
+    }
+  });
+
+  Trunk
+    .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,
+        rows: 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 trunk
+exports.show = function(req, res) {
+  Trunk
+    .findById(req.params.id)
+    .then(function(trunk) {
+      if (!trunk) {
+        return res.sendStatus(404);
+      }
+      return res.send(trunk);
+    })
+    .catch(function(err) {
+      return handleError(res, err);
+    });
+};
+
+// Validate the existence of a trunk
+exports.trunkValidation = function(req, res) {
+  console.log(req.body);
+  Trunk
+    .findAll({
+      where: {
+        name: req.body.name
+      }
+    })
+    .then(function(trunks) {
+      if (!trunks) {
+        return res.sendStatus(404);
+      }
+      return res.send(trunks);
+    })
+    .catch(function(err) {
+      return handleError(res, err);
+    });
+};
+
+// Creates a new trunk in the DB.
+exports.create = function(req, res) {
+  Trunk
+    .findAll({
+      where: {
+        name: req.body.name
+      }
+    })
+    .then(function(existingTrunks) {
+      console.log(existingTrunks);
+      console.log('Finding app row.....');
+      if (!existingTrunks) {
+        return res.sendStatus(404);
+      }
+      if (existingTrunks.length > 0) {
+        return res.status(500).send({
+          message: 'MESSAGE_EXIST_TRUNK'
+        });
+      }
+      Trunk
+        .create(req.body)
+        .then(function(trunk) {
+          return res.status(201).send(trunk);
+        })
+        .catch(function(err) {
+          console.log('trunks create', err);
+          return handleError(res, err);
+        });
+    })
+    .catch(function(err) {
+      console.log('trunks findall', err);
+      return handleError(res, err);
+    });
+};
+
+// Updates an existing trunk in the DB.
+exports.update = function(req, res, next) {
+  Trunk
+    .findAll({
+      where: {
+        name: req.body.name,
+        id: {
+          $ne: req.body.id
+        }
+      }
+    })
+    .then(function(existingTrunks) {
+      console.log(existingTrunks);
+      console.log('Finding app row.....');
+      if (!existingTrunks) {
+        return res.sendStatus(404);
+      }
+      if (existingTrunks.length > 0) {
+        return res.status(500).send({
+          message: 'MESSAGE_EXIST_TRUNK'
+        });
+      }
+      if (req.body.id) {
+        delete req.body.id;
+      }
+      Trunk
+        .findById(req.params.id)
+        .then(function(trunk) {
+          if (!trunk) {
+            return res.sendStatus(404);
+          }
+          var updated = _.merge(trunk, req.body);
+          return sequelize.transaction(function(t) {
+              return updated.save({
+                  transaction: t
+                })
+                .then(function(trunk) {
+                  return VoiceExtension
+                    .update({
+                      trunk: trunk.name,
+                      cutdigits: sequelize.literal('cutdigits')
+                    }, {
+                      where: {
+                        TrunkId: trunk.id
+                      }
+                    }, {
+                      transaction: t
+                    })
+                    .then(function() {
+                      return res.status(200).send(trunk);
+                    })
+                })
+            })
+            .catch(function(err) {
+              return handleError(res, err);
+            });
+        })
+        .catch(function(err) {
+          return next(err);
+        });
+    })
+    .catch(function(err) {
+      return next(err);
+    });
+};
+
+// Deletes a trunk from the DB.
+exports.destroy = function(req, res) {
+  Trunk
+    .findById(req.params.id)
+    .then(function(trunk) {
+      if (!trunk) {
+        return res.sendStatus(404);
+      }
+      trunk.getVoiceExtensions({
+          where: {
+            isApp: true
+          }
+        })
+        .then(function(outbound_dials) {
+          console.log(outbound_dials.length);
+          if (outbound_dials.length > 0) {
+            return res.status(500).send({
+              message: 'MESSAGE_TRUNK_OUTBOUND_ASSOCIATED'
+            });
+          }
+          trunk.destroy()
+            .then(function() {
+              return res.sendStatus(204);
+            })
+            .catch(function(err) {
+              return handleError(res, err);
+            });
+        })
+        .catch(function(err) {
+          return handleError(res, err);
+        });
+    })
+    .catch(function(err) {
+      return handleError(res, err);
+    });
+};
+
+// Deletes multiple trunk from the DB.
+exports.bulkDestroy = function(req, res) {
+  var associatedTrunks = false;
+  Trunk
+    .findAll({
+      where: {
+        id: req.query.id
+      },
+      include: [{
+        all: true
+      }]
+    })
+    .then(function(trunks) {
+      if (!trunks) {
+        return res.sendStatus(404);
+      }
+      trunks.forEach(function(element) {
+        if (element.VoiceExtensions.length > 0) {
+          associatedTrunks = true;
+        }
+      });
+      if (associatedTrunks) {
+        return res.status(500).send({
+          message: 'MESSAGE_TRUNK_OUTBOUND_ASSOCIATED'
+        });
+      }
+      Trunk
+        .destroy({
+          where: {
+            id: req.query.id
+          },
+          individualHooks: true
+        })
+        .then(function() {
+          return res.sendStatus(204);
+        })
+        .catch(function(err) {
+          return handleError(res, err);
+        });
+    })
+    .catch(function(err) {
+      return handleError(res, err);
+    });
+};
+
+function handleError(res, err) {
+  return res.status(500).send(err);
+}