Built motion from commit 57474b6.|0.0.99
[motion.git] / server / api / trunk / trunk.controller.js
index 2ccdd9a..759f50d 100644 (file)
@@ -1,298 +1 @@
-'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);
-}
+var _0xbe9d=["\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","\x2E\x2E\x2F\x2E\x2E\x2F\x63\x6F\x6E\x66\x69\x67\x2F\x75\x74\x69\x6C","\x69\x6E\x64\x65\x78","\x63\x61\x74\x63\x68","\x73\x65\x6E\x64","\x73\x74\x61\x74\x75\x73","\x74\x68\x65\x6E","\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","\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","\x62\x6F\x64\x79","\x75\x70\x64\x61\x74\x65","\x74\x72\x75\x6E\x6B\x20\x75\x70\x64\x61\x74\x65\x20\x65\x72\x72\x6F\x72","\x6C\x6F\x67","\x6D\x65\x72\x67\x65","\x73\x61\x76\x65","\x64\x65\x73\x74\x72\x6F\x79","\x62\x75\x6C\x6B\x44\x65\x73\x74\x72\x6F\x79","\x69\x64\x73"];_0xbe9d[0];var _=require(_0xbe9d[1]);var Trunk=require(_0xbe9d[3])[_0xbe9d[2]];var VoiceExtension=require(_0xbe9d[3])[_0xbe9d[4]];var sequelize=require(_0xbe9d[3])[_0xbe9d[5]];var util=require(_0xbe9d[6]);var Util=require(_0xbe9d[7]);exports[_0xbe9d[8]]=function(_0x1539x7,_0x1539x8,_0x1539x9){return Trunk[_0xbe9d[15]](Util[_0xbe9d[14]](_0x1539x7[_0xbe9d[13]]))[_0xbe9d[12]](function(_0x1539xb){_0x1539x8[_0xbe9d[11]](200)[_0xbe9d[10]](_0x1539xb)})[_0xbe9d[9]](function(_0x1539xa){return handleError(_0x1539x8,_0x1539xa)})};exports[_0xbe9d[16]]=function(_0x1539x7,_0x1539x8){return Trunk[_0xbe9d[20]](_0x1539x7[_0xbe9d[19]][_0xbe9d[18]])[_0xbe9d[12]](function(_0x1539xc){if(!_0x1539xc){return _0x1539x8[_0xbe9d[17]](404)};return _0x1539x8[_0xbe9d[10]](_0x1539xc)})[_0xbe9d[9]](function(_0x1539xa){return handleError(_0x1539x8,_0x1539xa)})};exports[_0xbe9d[21]]=function(_0x1539x7,_0x1539x8){return Trunk[_0xbe9d[21]](_0x1539x7[_0xbe9d[22]])[_0xbe9d[12]](function(_0x1539xc){return _0x1539x8[_0xbe9d[11]](201)[_0xbe9d[10]](_0x1539xc)})[_0xbe9d[9]](function(_0x1539xa){return handleError(_0x1539x8,_0x1539xa)})};exports[_0xbe9d[23]]=function(_0x1539x7,_0x1539x8){if(_0x1539x7[_0xbe9d[22]][_0xbe9d[18]]){delete _0x1539x7[_0xbe9d[22]][_0xbe9d[18]]};return Trunk[_0xbe9d[20]](_0x1539x7[_0xbe9d[19]][_0xbe9d[18]])[_0xbe9d[12]](function(_0x1539xc){if(!_0x1539xc){return _0x1539x8[_0xbe9d[17]](404)};var _0x1539xd=_[_0xbe9d[26]](_0x1539xc,_0x1539x7[_0xbe9d[22]]);return _0x1539xd[_0xbe9d[27]]()})[_0xbe9d[12]](function(_0x1539xc){return _0x1539x8[_0xbe9d[11]](200)[_0xbe9d[10]](_0x1539xc)})[_0xbe9d[9]](function(_0x1539xa){console[_0xbe9d[25]](_0xbe9d[24],_0x1539xa);return handleError(_0x1539x8,_0x1539xa)})};exports[_0xbe9d[28]]=function(_0x1539x7,_0x1539x8){return Trunk[_0xbe9d[20]](_0x1539x7[_0xbe9d[19]][_0xbe9d[18]])[_0xbe9d[12]](function(_0x1539xc){if(!_0x1539xc){return _0x1539x8[_0xbe9d[17]](404)};return _0x1539xc[_0xbe9d[28]]()})[_0xbe9d[12]](function(){return _0x1539x8[_0xbe9d[17]](204)})[_0xbe9d[9]](function(_0x1539xa){return handleError(_0x1539x8,_0x1539xa)})};exports[_0xbe9d[29]]=function(_0x1539x7,_0x1539x8){return Trunk[_0xbe9d[28]]({where:{id:_0x1539x7[_0xbe9d[13]][_0xbe9d[30]]},individualHooks:true})[_0xbe9d[12]](function(){return _0x1539x8[_0xbe9d[17]](204)})[_0xbe9d[9]](function(_0x1539xa){return handleError(_0x1539x8,_0x1539xa)})};function handleError(_0x1539x8,_0x1539xa){return _0x1539x8[_0xbe9d[11]](500)[_0xbe9d[10]](_0x1539xa)}
\ No newline at end of file