-'use strict';
-
-var _ = require('lodash');
-var VoiceExtension = require('../../models').VoiceExtension;
-var Interval = require('../../models').Interval;
-var sequelize = require('../../models').sequelize;
-var Sequelize = require('../../models').Sequelize;
-var util = require('util');
-var async = require('async');
-
-var Applications = {
- Dial: [{
- app: 'GotoIfTime',
- appdata: '%s?%s,${EXTEN},%s:%s,${EXTEN},%s',
- }, {
- app: 'Set',
- appdata: 'CALLERID(all)=%s'
- }, {
- app: 'Dial',
- appdata: '%s,%s,%s,%s',
- isApp: true
- }, {
- app: 'NoOp',
- appdata: 'Dial Application End'
- }],
- Queue: [{
- app: 'GotoIfTime',
- appdata: '%s?%s,${EXTEN},%s:%s,${EXTEN},%s',
- }, {
- app: 'Answer',
- appdata: ''
- }, {
- app: 'Queue',
- appdata: '%s,%s,%s,%s,%s',
- isApp: true
- }, {
- app: 'NoOp',
- appdata: 'Queue Application End'
- }],
- Playback: [{
- app: 'GotoIfTime',
- appdata: '%s?%s,${EXTEN},%s:%s,${EXTEN},%s',
- }, {
- app: 'Playback',
- appdata: '%s,%s',
- isApp: true
- }, {
- app: 'NoOp',
- appdata: 'Playback Application End'
- }],
- AGI: [{
- app: 'GotoIfTime',
- appdata: '%s?%s,${EXTEN},%s:%s,${EXTEN},%s',
- }, {
- app: 'AGI',
- appdata: 'agi://127.0.0.1/square,%s',
- isApp: true
- }, {
- app: 'NoOp',
- appdata: 'AGI Application End'
- }],
- Goto: [{
- app: 'GotoIfTime',
- appdata: '%s?%s,${EXTEN},%s:%s,${EXTEN},%s',
- }, {
- app: 'Goto',
- appdata: '%s,%s,%d',
- isApp: true
- }, {
- app: 'NoOp',
- appdata: 'Goto Application End'
- }],
- Hangup: [{
- app: 'GotoIfTime',
- appdata: '%s?%s,${EXTEN},%s:%s,${EXTEN},%s',
- }, {
- app: 'Hangup',
- appdata: '%s',
- isApp: true
- }, {
- app: 'NoOp',
- appdata: 'Hangup Application End'
- }],
- Set: [{
- app: 'GotoIfTime',
- appdata: '%s?%s,${EXTEN},%s:%s,${EXTEN},%s',
- }, {
- app: 'Set',
- appdata: '%s=%s',
- isApp: true
- }, {
- app: 'NoOp',
- appdata: 'Set Application End'
- }],
- custom: [{
- app: 'GotoIfTime',
- appdata: '%s?%s,${EXTEN},%s:%s,${EXTEN},%s',
- }, {
- app: 'custom',
- appdata: '%s',
- isApp: true
- }, {
- app: 'NoOp',
- appdata: 'Custom Application End'
- }],
- Voicemail: [{
- app: 'GotoIfTime',
- appdata: '%s?%s,${EXTEN},%s:%s,${EXTEN},%s',
- }, {
- app: 'Voicemail',
- appdata: '%s',
- isApp: true
- }, {
- app: 'NoOp',
- appdata: 'Voicemail Application End'
- }]
-};
-
-// Get list of voice_extensions
-exports.index = function(req, res) {
- VoiceExtension
- .findAll()
- .then(function(voice_extensions) {
- return res.status(200).send(voice_extensions);
- })
- .catch(function(err) {
- return handleError(res, err);
- });
-};
-
-// Get list of agents
-exports.showByRoute = function(req, res, next) {
-
- var attributes = ['context', 'exten', 'app', 'appdata'];
- 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: {
- type: req.params.route,
- VoiceExtensionId: null,
- app: 'NoOp'
- },
- 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 + '%';
- }
- });
-
- VoiceExtension
- .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 list of voice_extensions by route
-exports.showAppsByRoute = function(req, res) {
- VoiceExtension
- .findAll({
- where: {
- VoiceExtensionId: req.params.id,
- isApp: true
- }
- })
- .then(function(voice_extensions) {
- return res.status(200).send(voice_extensions);
- })
- .catch(function(err) {
- return handleError(res, err);
- });
-};
-
-// Get a single voice_extension or application
-exports.show = function(req, res) {
- VoiceExtension
- .findById(req.params.id)
- .then(function(voice_extension) {
- if (!voice_extension) {
- return res.sendStatus(404);
- }
- return res.send(voice_extension);
- })
- .catch(function(err) {
- return handleError(res, err);
- });
-};
-
-// Validate the existence of a route
-exports.routeValidation = function(req, res) {
- VoiceExtension
- .findAll({
- where: {
- exten: req.body.exten,
- context: req.body.context,
- type: req.body.type
- }
- })
- .then(function(voice_extension) {
- if (!voice_extension) {
- return res.sendStatus(404);
- }
- return res.send(voice_extension);
- })
- .catch(function(err) {
- return handleError(res, err);
- });
-};
-
-// exports.sortAppsByRoute = function(req, res, next) {
-// return sequelize
-// .transaction()
-// .then(function(t) {
-// async.waterfall([
-// function(callback) {
-// VoiceExtension
-// .findAll({
-// where: {
-// appGroup: req.body.applications
-// }
-// })
-// .then(function(voiceApplications) {
-// callback(null, voiceApplications);
-// })
-// .catch(function(err) {
-// callback(err);
-// });
-// },
-// function(voiceApplications, callback) {
-// console.log('found ad saved applications');
-// console.log('destroying old applications from db');
-// return VoiceExtension.destroy({
-// where: {
-// appGroup: req.body.applications
-// }
-// }, {
-// transaction: t
-// })
-// .then(function() {
-// callback(null, voiceApplications);
-// })
-// .catch(function(err) {
-// callback(err);
-// });
-// },
-// function(voiceApplications, callback) {
-// console.log('destroyed old applications from db');
-// var priority = 2;
-// console.log('iterating sorted applications ids');
-// req.body.applications.forEach(function(sortedElem) {
-// var tmpElem = _.find(voiceApplications, function(elem) {
-// return (elem.appGroup == sortedElem && elem.isApp === true);
-// });
-// var application = _.cloneDeep(Applications[tmpElem.app]);
-// if (!application || tmpElem.customApp) {
-// application = [{
-// app: 'GotoIfTime',
-// appdata: '%s?%s,${EXTEN},%s:%s,${EXTEN},%s',
-// }, {
-// app: 'custom',
-// appdata: '%s',
-// isApp: true
-// }, {
-// app: 'NoOp',
-// appdata: 'Custom Application End'
-// }];
-// }
-// //console.log('PRIORITY');
-// //console.log(priority);
-// var appGroup = util.format('%s%s%s', tmpElem.context, tmpElem.exten, priority);
-// tmpElem.interval = tmpElem.interval || '*,*,*,*';
-// var tmpIntervals = _.filter(voiceApplications, function(elem) {
-// return (elem.appGroup == sortedElem && elem.isInterval === true);
-// });
-// if (tmpIntervals && tmpIntervals.length > 0) {
-// console.log('found custom interval, adding to app');
-// application.splice(0, 1);
-// tmpIntervals.forEach(function(elem, index) {
-//
-// application.unshift({
-// app: 'GotoIfTimeFromArray',
-// appdata: elem.interval + '?%s,${EXTEN},%s:%s,${EXTEN},%s',
-// IntervalId: elem.IntervalId ? elem.IntervalId : null,
-// isInterval: true,
-// interval: elem.interval,
-// });
-// })
-// }
-// priority = appCreate(application, tmpElem, appGroup, tmpIntervals || [], priority);
-// //console.log('returned priority');
-// //console.log(priority);
-// console.log('creating group of extensions');
-// return VoiceExtension
-// .bulkCreate(application, {
-// transaction: t
-// })
-// });
-// callback();
-// }
-// ],
-// function(err, result) {
-// if (err) {
-// console.log(err);
-// t.rollback();
-// } else {
-// console.log('commit');
-// t.commit();
-// console.log('finished creating, now searching the sorted and saved apps');
-// VoiceExtension
-// .findAll({
-// where: {
-// VoiceExtensionId: req.params.id,
-// isApp: true
-// }
-// })
-// .then(function(voiceExtensions) {
-// console.log('found sorted applications, returning');
-// //console.log(_.pluck(voiceExtensions, 'dataValues'));
-// return res.status(201).send(voiceExtensions);
-// })
-// }
-// });
-// });
-// }
-
-exports.sortAppsByRoute = function(req, res, next) {
- // console.log('finding and saving old applications');
- VoiceExtension
- .findAll({
- where: {
- appGroup: req.body.applications
- }
- })
- .then(function(voiceApplications) {
- // console.log('found ad saved applications');
- return sequelize.transaction(function(t) {
- // console.log('destroying old applications from db');
- return VoiceExtension.destroy({
- where: {
- appGroup: req.body.applications
- }
- }, {
- transaction: t
- })
- .then(function() {
- var priority = 2;
- req.body.applications.forEach(function(sortedElem) {
- var tmpElem = _.find(voiceApplications, function(elem) {
- return (elem.appGroup == sortedElem && elem.isApp === true);
- });
- var application = _.cloneDeep(Applications[tmpElem.app]);
- if (!application || tmpElem.customApp) {
- application = [{
- app: 'GotoIfTime',
- appdata: '%s?%s,${EXTEN},%s:%s,${EXTEN},%s',
- }, {
- app: 'custom',
- appdata: '%s',
- isApp: true
- }, {
- app: 'NoOp',
- appdata: 'Custom Application End'
- }];
- }
- var appGroup = util.format('%s%s%s', tmpElem.context, tmpElem.exten, priority);
- tmpElem.interval = tmpElem.interval || '*,*,*,*';
- var tmpIntervals = _.filter(voiceApplications, function(elem) {
- return (elem.appGroup == sortedElem && elem.isInterval === true);
- });
- if (tmpIntervals && tmpIntervals.length > 0) {
- // console.log('found custom interval, adding to app');
- application.splice(0, 1);
- tmpIntervals.forEach(function(elem, index) {
-
- application.unshift({
- app: 'GotoIfTimeFromArray',
- appdata: elem.interval + '?%s,${EXTEN},%s:%s,${EXTEN},%s',
- IntervalId: elem.IntervalId ? elem.IntervalId : null,
- isInterval: true,
- interval: elem.interval,
- });
- })
- }
- priority = appCreate(application, tmpElem, appGroup, tmpIntervals || [], priority);
- //console.log('returned priority');
- //console.log(priority);
- // console.log('creating group of extensions');
- return VoiceExtension
- .bulkCreate(application, {
- transaction: t
- })
- });
- })
- })
- .then(function() {
- // console.log('finished creating, now searching the sorted and saved apps');
- // VoiceExtension
- // .findAll({
- // where: {
- // VoiceExtensionId: req.params.id,
- // isApp: true
- // }
- // })
- // .then(function(voiceExtensions) {
- // console.log('found sorted applications, returning');
- // //console.log(_.pluck(voiceExtensions, 'dataValues'));
- // return res.status(201).send(voiceExtensions);
- // })
- return res.sendStatus(201);
- })
- .catch(function(err) {
- return handleError(res, err);
- });
- })
- .catch(function(err) {
- return next(err);
- });
-};
-
-// // Creates a new voice_extension in the DB.
-// exports.create = function(req, res) {
-// VoiceExtension
-// .create(req.body)
-// .then(function(voice_extension) {
-// return res.status(201).send(voice_extension);
-// })
-// .catch(function(err) {
-// return handleError(res, err);
-// });
-// };
-
-// Creates a new voiceApplication in the DB.
-exports.create = function(req, res, next) {
- // //console.log(req.body);
- var application;
- var appGroup;
- var interval;
- VoiceExtension
- .max('priority', {
- where: {
- VoiceExtensionId: req.body.VoiceExtensionId
- }
- }).then(function(max) {
- // SET PRIORITY
- if (req.body.VoiceExtensionId) {
- req.body.priority = max ? ++max : 2;
- application = _.cloneDeep(Applications[req.body.app]);
- if (!application || req.body.customApp) {
- application = [{
- app: 'GotoIfTime',
- appdata: '%s?%s,${EXTEN},%s:%s,${EXTEN},%s',
- }, {
- app: 'custom',
- appdata: '%s',
- isApp: true
- }, {
- app: 'NoOp',
- appdata: 'Custom Application End'
- }];
- }
- appGroup = util.format('%s%s%s', req.body.context, req.body.exten, req.body.priority);
- // interval = createInterval(req.body);
-
-
- req.body.interval = req.body.interval || '*,*,*,*';
- Interval
- .findAll({
- where: {
- IntervalId: (req.body.IntervalId !== null) ? req.body.IntervalId : 0
- }
- })
- .then(function(Intervals) {
- var intervals = [];
- var gotoInterval;
- if (Intervals && Intervals.length > 0) {
- application.splice(0, 1);
- intervals = _.pluck(Intervals, 'dataValues');
- intervals.forEach(function(elem, index) {
- // gotoInterval = createInterval(elem);
- application.unshift({
- app: 'GotoIfTimeFromArray',
- appdata: elem.interval + '?%s,${EXTEN},%s:%s,${EXTEN},%s',
- IntervalId: elem.id,
- interval: gotoInterval,
- isInterval: true
- });
- })
- }
- appCreate(application, req.body, appGroup, intervals);
- VoiceExtension
- .bulkCreate(application)
- .then(function(voiceExtensions) {
- return res.status(201).send(voiceExtensions);
- })
- })
- } else {
- VoiceExtension
- .findAll({
- where: {
- exten: req.body.exten,
- context: req.body.context,
- type: req.body.type,
- }
- })
- .then(function(existingExtensions) {
- //console.log(existingExtensions);
- //console.log('Finding app row.....');
- if (!existingExtensions) {
- return res.sendStatus(404);
- }
- if (existingExtensions.length > 0) {
- return res.status(500).send({
- message: 'MESSAGE_EXIST_ROUTE'
- });
- }
- req.body.priority = 1;
- VoiceExtension
- .create(req.body)
- .then(function(voiceExtensions) {
- return res.status(201).send(voiceExtensions);
- })
- .catch(function(err) {
- return handleError(res, err);
- })
- })
- .catch(function(err) {
- return handleError(res, err);
- });
- }
- // //console.log(application);
-
- })
- .catch(function(err) {
- next(err);
- });
-};
-
-// function createInterval(element) {
-// var interval = [];
-// if (element.t_from && element.t_from !== '') {
-// interval[0] = element.t_from;
-// if (element.t_to && element.t_to !== '') {
-// interval[0] += '-' + element.t_to;
-// }
-// } else {
-// interval[0] = '*';
-// }
-//
-// if (element.wd_from && element.wd_from !== '') {
-// interval[1] = element.wd_from;
-// if (element.wd_to && element.wd_to !== '') {
-// interval[1] += '-' + element.wd_to;
-// }
-// } else {
-// interval[1] = '*';
-// }
-//
-// if (element.md_from && element.md_from !== '') {
-// interval[2] = element.md_from;
-// if (element.md_to && element.md_to !== '') {
-// interval[2] += '-' + element.md_to;
-// }
-// } else {
-// interval[2] = '*';
-// }
-//
-// if (element.m_from && element.m_from !== '') {
-// interval[3] = element.m_from;
-// if (element.m_to && element.m_to !== '') {
-// interval[3] += '-' + element.m_to;
-// }
-// } else {
-// interval[3] = '*';
-// }
-//
-// return interval.join(',');
-// }
-
-function appCreate(application, element, appGroup, intervals, priority) {
- var intLength = intervals.length;
- if (element.customApp) {
- element.app = 'custom'; //in questo modo posso rimuovere il default dello switch, ma lo lascio per completezza
- }
- switch (element.app) {
- case 'Dial':
- if (!element.callerID) {
- _.remove(application, {
- app: 'Set'
- });
- }
- application.forEach(function(elem, index) {
- elem.exten = element.exten;
- elem.type = element.type;
- elem.context = element.context;
- elem.VoiceExtensionId = element.VoiceExtensionId;
- elem.priority = priority ? priority++ : element.priority++;
- elem.appGroup = appGroup;
- switch (elem.app) {
- case 'GotoIfTime':
- elem.appdata = util.format(elem.appdata, element.IntervalId ? '*,*,*,*' : element.interval, elem.context, elem.priority + 1, elem.context, (elem.priority + 2));
- break;
- case 'GotoIfTimeFromArray':
- elem.app = 'GotoIfTime';
- elem.appdata = util.format(elem.appdata, elem.context, elem.priority + intLength - index, elem.context, (index + 1) < intLength ? (elem.priority + 1) : (elem.priority + 2));
- break;
- case 'Dial':
- if (element.type === 'outbound') {
- elem.IntervalId = element.IntervalId;
- elem.interval = element.interval;
- elem.trunk = element.trunk;
- elem.TrunkId = element.TrunkId;
- elem.cutdigits = element.cutdigits || 0;
- elem.callerID = element.callerID || null;
- // elem.appdata = util.format('SIP/${EXTEN:%d}@%s', elem.cutdigits, elem.trunk);
- } else {
- elem.IntervalId = element.IntervalId;
- elem.interval = element.interval;
- elem.interface = element.interface;
- elem.app_options = element.app_options || '';
- elem.timeout = element.timeout || '';
- elem.url = element.url || '';
- elem.appdata = util.format(elem.appdata, element.interface, elem.timeout, elem.app_options ? elem.app_options.replace(',', '') : '', elem.url);
- }
- break;
- case 'Set':
- elem.appdata = util.format(elem.appdata, element.callerID);
- break;
- case 'NoOp':
- if (element.type === 'outbound') {
- elem.appdata = 'Outbound Dial Application End';
- }
- break;
- default:
-
- }
- });
- break;
- case 'Queue':
- application.forEach(function(elem, index) {
- elem.exten = element.exten;
- elem.type = element.type;
- elem.context = element.context;
- elem.VoiceExtensionId = element.VoiceExtensionId;
- elem.priority = priority ? priority++ : element.priority++;
- elem.appGroup = appGroup;
- switch (elem.app) {
- case 'GotoIfTime':
- elem.appdata = util.format(elem.appdata, element.IntervalId ? '*,*,*,*' : element.interval, elem.context, elem.priority + 1, elem.context, (elem.priority + 2));
- break;
- case 'GotoIfTimeFromArray':
- elem.app = 'GotoIfTime';
- elem.appdata = util.format(elem.appdata, elem.context, elem.priority + intLength - index, elem.context, (index + 1) < intLength ? (elem.priority + 1) : (elem.priority + 2));
- break;
- case 'Queue':
- elem.IntervalId = element.IntervalId;
- elem.app_options = element.app_options || '';
- elem.audiofile = element.audiofile || '';
- elem.queue = element.queue;
- elem.interval = element.interval;
- elem.interface = element.interface;
- elem.app_options = element.app_options;
- elem.timeout = element.timeout || '';
- elem.url = element.url || '';
- elem.appdata = util.format(elem.appdata, elem.queue, elem.app_options ? elem.app_options.replace(',', '') : '', elem.url, elem.audiofile, elem.timeout);
- break;
- default:
-
- }
- });
- break;
- case 'Playback':
- application.forEach(function(elem, index) {
- elem.exten = element.exten;
- elem.type = element.type;
- elem.context = element.context;
- elem.VoiceExtensionId = element.VoiceExtensionId;
- elem.priority = priority ? priority++ : element.priority++;
- elem.appGroup = appGroup;
- switch (elem.app) {
- case 'GotoIfTime':
- elem.appdata = util.format(elem.appdata, element.IntervalId ? '*,*,*,*' : element.interval, elem.context, elem.priority + 1, elem.context, (elem.priority + 2));
- break;
- case 'GotoIfTimeFromArray':
- elem.app = 'GotoIfTime';
- elem.appdata = util.format(elem.appdata, elem.context, elem.priority + intLength - index, elem.context, (index + 1) < intLength ? (elem.priority + 1) : (elem.priority + 2));
- break;
- case 'Playback':
- elem.IntervalId = element.IntervalId;
- elem.interval = element.interval;
- elem.audiofile = element.audiofile || '';
- elem.app_options = element.app_options || '';
- elem.appdata = util.format(elem.appdata, elem.audiofile, elem.app_options ? elem.app_options.replace(',', '') : '');
- break;
- default:
-
- }
- });
- break;
- case 'AGI':
- application.forEach(function(elem, index) {
- elem.exten = element.exten;
- elem.type = element.type;
- elem.context = element.context;
- elem.VoiceExtensionId = element.VoiceExtensionId;
- elem.priority = priority ? priority++ : element.priority++;
- elem.appGroup = appGroup;
- switch (elem.app) {
- case 'GotoIfTime':
- elem.appdata = util.format(elem.appdata, element.IntervalId ? '*,*,*,*' : element.interval, elem.context, elem.priority + 1, elem.context, (elem.priority + 2));
- break;
- case 'GotoIfTimeFromArray':
- elem.app = 'GotoIfTime';
- elem.appdata = util.format(elem.appdata, elem.context, elem.priority + intLength - index, elem.context, (index + 1) < intLength ? (elem.priority + 1) : (elem.priority + 2));
- break;
- case 'AGI':
- elem.IntervalId = element.IntervalId;
- elem.interval = element.interval;
- elem.project = element.project;
- elem.appdata = util.format(elem.appdata, elem.project);
- break;
- default:
-
- }
- });
- break;
- case 'Goto':
- application.forEach(function(elem, index) {
- elem.exten = element.exten;
- elem.type = element.type;
- elem.context = element.context;
- elem.VoiceExtensionId = element.VoiceExtensionId;
- elem.priority = priority ? priority++ : element.priority++;
- elem.appGroup = appGroup;
- switch (elem.app) {
- case 'GotoIfTime':
- elem.appdata = util.format(elem.appdata, element.IntervalId ? '*,*,*,*' : element.interval, elem.context, elem.priority + 1, elem.context, (elem.priority + 2));
- break;
- case 'GotoIfTimeFromArray':
- elem.app = 'GotoIfTime';
- elem.appdata = util.format(elem.appdata, elem.context, elem.priority + intLength - index, elem.context, (index + 1) < intLength ? (elem.priority + 1) : (elem.priority + 2));
- break;
- case 'Goto':
- elem.IntervalId = element.IntervalId;
- elem.interval = element.interval;
- elem.alt_priority = element.alt_priority;
- elem.alt_extension = element.alt_extension;
- elem.alt_context = element.alt_context;
- elem.appdata = util.format(elem.appdata, elem.alt_context, elem.alt_extension, elem.alt_priority);
- break;
- default:
-
- }
- });
- break;
- case 'Hangup':
- application.forEach(function(elem, index) {
- elem.exten = element.exten;
- elem.type = element.type;
- elem.context = element.context;
- elem.VoiceExtensionId = element.VoiceExtensionId;
- elem.priority = priority ? priority++ : element.priority++;
- elem.appGroup = appGroup;
- switch (elem.app) {
- case 'GotoIfTime':
- elem.appdata = util.format(elem.appdata, element.IntervalId ? '*,*,*,*' : element.interval, elem.context, elem.priority + 1, elem.context, (elem.priority + 2));
- break;
- case 'GotoIfTimeFromArray':
- elem.app = 'GotoIfTime';
- elem.appdata = util.format(elem.appdata, elem.context, elem.priority + intLength - index, elem.context, (index + 1) < intLength ? (elem.priority + 1) : (elem.priority + 2));
- break;
- case 'Hangup':
- elem.IntervalId = element.IntervalId;
- elem.interval = element.interval;
- elem.causecode = element.causecode;
- elem.appdata = util.format(elem.appdata, elem.causecode);
- break;
- default:
-
- }
- });
- break;
- case 'Set':
- application.forEach(function(elem, index) {
- elem.exten = element.exten;
- elem.type = element.type;
- elem.context = element.context;
- elem.VoiceExtensionId = element.VoiceExtensionId;
- elem.priority = priority ? priority++ : element.priority++;
- elem.appGroup = appGroup;
- switch (elem.app) {
- case 'GotoIfTime':
- elem.appdata = util.format(elem.appdata, element.IntervalId ? '*,*,*,*' : element.interval, elem.context, elem.priority + 1, elem.context, (elem.priority + 2));
- break;
- case 'GotoIfTimeFromArray':
- elem.app = 'GotoIfTime';
- elem.appdata = util.format(elem.appdata, elem.context, elem.priority + intLength - index, elem.context, (index + 1) < intLength ? (elem.priority + 1) : (elem.priority + 2));
- break;
- case 'Set':
- elem.IntervalId = element.IntervalId;
- elem.interval = element.interval;
- elem.variable = element.variable;
- elem.value = element.value;
- elem.appdata = util.format(elem.appdata, elem.variable, elem.value);
- break;
- default:
-
- }
- });
- break;
- case 'custom':
- application.forEach(function(elem, index) {
- elem.exten = element.exten;
- elem.type = element.type;
- elem.context = element.context;
- elem.VoiceExtensionId = element.VoiceExtensionId;
- elem.priority = priority ? priority++ : element.priority++;
- elem.appGroup = appGroup;
- switch (elem.app) {
- case 'GotoIfTime':
- elem.appdata = util.format(elem.appdata, element.IntervalId ? '*,*,*,*' : element.interval, elem.context, elem.priority + 1, elem.context, (elem.priority + 2));
- break;
- case 'GotoIfTimeFromArray':
- elem.app = 'GotoIfTime';
- elem.appdata = util.format(elem.appdata, elem.context, elem.priority + intLength - index, elem.context, (index + 1) < intLength ? (elem.priority + 1) : (elem.priority + 2));
- break;
- case 'custom':
- elem.IntervalId = element.IntervalId;
- elem.interval = element.interval;
- elem.app = elem.customApp = element.customApp;
- elem.appdata = util.format(elem.appdata, element.appdata);
- break;
- default:
-
- }
- });
- break;
- case 'Voicemail':
- application.forEach(function(elem, index) {
- elem.exten = element.exten;
- elem.type = element.type;
- elem.context = element.context;
- elem.VoiceExtensionId = element.VoiceExtensionId;
- elem.priority = priority ? priority++ : element.priority++;
- elem.appGroup = appGroup;
- switch (elem.app) {
- case 'GotoIfTime':
- elem.appdata = util.format(elem.appdata, element.IntervalId ? '*,*,*,*' : element.interval, elem.context, elem.priority + 1, elem.context, (elem.priority + 2));
- break;
- case 'GotoIfTimeFromArray':
- elem.app = 'GotoIfTime';
- elem.appdata = util.format(elem.appdata, elem.context, elem.priority + intLength - index, elem.context, (index + 1) < intLength ? (elem.priority + 1) : (elem.priority + 2));
- break;
- case 'Voicemail':
- elem.IntervalId = element.IntervalId;
- elem.interval = element.interval;
- elem.mailbox = element.mailbox;
- elem.appdata = util.format(elem.appdata, elem.mailbox);
- break;
- default:
-
- }
- });
- break;
- default:
- application.forEach(function(elem, index) {
- elem.exten = element.exten;
- elem.type = element.type;
- elem.context = element.context;
- elem.VoiceExtensionId = element.VoiceExtensionId;
- elem.priority = priority ? priority++ : element.priority++;
- elem.appGroup = appGroup;
- switch (elem.app) {
- case 'GotoIfTime':
- elem.appdata = util.format(elem.appdata, element.IntervalId ? '*,*,*,*' : element.interval, elem.context, elem.priority + 1, elem.context, (elem.priority + 2));
- break;
- case 'GotoIfTimeFromArray':
- elem.app = 'GotoIfTime';
- elem.appdata = util.format(elem.appdata, elem.context, elem.priority + intLength - index, elem.context, (index + 1) < intLength ? (elem.priority + 1) : (elem.priority + 2));
- break;
- case 'custom':
- elem.IntervalId = element.IntervalId;
- elem.interval = element.interval;
- elem.app = elem.customApp = element.customApp;
- elem.appdata = util.format(elem.appdata, element.appdata);
- break;
- default:
-
- }
- });
-
- }
- if (priority) {
- return priority;
- }
-}
-
-// Updates an existing voice_extension in the DB.
-exports.update = function(req, res) {
- VoiceExtension
- .findAll({
- where: {
- exten: req.body.exten,
- context: req.body.context,
- type: req.body.type,
- VoiceExtensionId: null,
- id: {
- $ne: req.body.id
- }
- }
- })
- .then(function(existingExtensions) {
- //console.log(existingExtensions);
- //console.log('Finding app row.....');
- if (!existingExtensions) {
- return res.sendStatus(404);
- }
- if (existingExtensions.length > 0) {
- return res.status(500).send({
- message: 'MESSAGE_EXIST_ROUTE'
- })
- }
- if (req.body.id) {
- delete req.body.id;
- }
- VoiceExtension
- .find({
- where: {
- id: req.params.id
- }
- })
- .then(function(voice_extension) {
- if (!voice_extension) {
- return res.sendStatus(404);
- }
- var updated = _.merge(voice_extension, req.body);
- return sequelize.transaction(function(t) {
- return updated.save({
- transaction: t
- })
- .then(function(updatedRoute) {
- VoiceExtension
- .findAll({
- where: {
- VoiceExtensionId: updatedRoute.id,
- isApp: true
- }
- })
- .then(function(voiceExtensions) {
- return VoiceExtension.destroy({
- where: {
- VoiceExtensionId: updatedRoute.id
- }
- }, {
- transaction: t
- })
- .then(function() {
- voiceExtensions = _.pluck(voiceExtensions, 'dataValues');
- var intervalIds = _.filter(_.pluck(voiceExtensions, 'IntervalId'), function(elem) {
- return elem !== null;
- });
- Interval
- .findAll({
- where: {
- IntervalId: intervalIds
- }
- })
- .then(function(intervals) {
- var priority = 2;
- var interval;
- voiceExtensions.forEach(function(tmpElem) {
- var application = _.cloneDeep(Applications[tmpElem.app]);
- if (!application || tmpElem.customApp) {
- application = [{
- app: 'GotoIfTime',
- appdata: '%s?%s,${EXTEN},%s:%s,${EXTEN},%s',
- }, {
- app: 'custom',
- appdata: '%s',
- isApp: true
- }, {
- app: 'NoOp',
- appdata: 'Custom Application End'
- }];
- }
- tmpElem.context = updatedRoute.context;
- tmpElem.exten = updatedRoute.exten;
- var appGroup = util.format('%s%s%s', tmpElem.context, tmpElem.exten, priority);
- tmpElem.interval = tmpElem.interval || '*,*,*,*';
- var tmpIntervals = _.filter(intervals, function(elem) {
- return (elem.IntervalId == tmpElem.IntervalId);
- });
- if (tmpIntervals && tmpIntervals.length > 0) {
- application.splice(0, 1);
- tmpIntervals.forEach(function(elem, index) {
- application.unshift({
- app: 'GotoIfTimeFromArray',
- appdata: elem.interval + '?%s,${EXTEN},%s:%s,${EXTEN},%s',
- IntervalId: elem.id,
- isInterval: true,
- interval: elem.interval
- });
- })
- }
- priority = appCreate(application, tmpElem, appGroup, tmpIntervals || [], priority);
- VoiceExtension
- .bulkCreate(application)
- });
- return res.sendStatus(200);
- })
- })
- })
- })
- })
- .catch(function(err) {
- return handleError(res, err);
- });
- })
- .catch(function(err) {
- return handleError(res, err);
- });
- })
- .catch(function(err) {
- return handleError(res, err);
- });
-};
-
-// Updates an existing voice_extension in the DB.
-exports.updateApplication = function(req, res, next) {
- var interval;
- if (req.body.appdata) {
- delete req.body.appdata;
- }
-
- //console.log('Finding app row.....');
- VoiceExtension
- .findById(req.body.id)
- .then(function(voiceExtension) {
- if (!voiceExtension) {
- return res.sendStatus(404);
- }
- delete req.body.id;
- var updated = _.merge(voiceExtension, req.body);
- //console.log('Updating app row.....');
- return sequelize.transaction(function(t) {
- return updated.save({
- transaction: t
- })
- .then(function() {
- //console.log('Finding extension apps..');
- VoiceExtension
- .findAll({
- where: {
- VoiceExtensionId: req.body.VoiceExtensionId,
- isApp: true
- }
- })
- .then(function(voiceExtensions) {
- //console.log('Extensions found');
- //console.log('Destroying extension rows...');
- return VoiceExtension.destroy({
- where: {
- VoiceExtensionId: req.body.VoiceExtensionId
- }
- }, {
- transaction: t
- })
- .then(function() {
- voiceExtensions = _.pluck(voiceExtensions, 'dataValues');
- var intervalIds = _.filter(_.pluck(voiceExtensions, 'IntervalId'), function(elem) {
- return elem !== null;
- });
- return Interval
- .findAll({
- where: {
- IntervalId: intervalIds
- }
- })
- .then(function(intervals) {
- //console.log('Creating extension rows...');
- var priority = 2;
- var interval;
- voiceExtensions.forEach(function(tmpElem) {
- //console.log('Creating app..');
- var application = _.cloneDeep(Applications[tmpElem.app]);
- if (!application || tmpElem.customApp) {
- application = [{
- app: 'GotoIfTime',
- appdata: '%s?%s,${EXTEN},%s:%s,${EXTEN},%s',
- }, {
- app: 'custom',
- appdata: '%s',
- isApp: true
- }, {
- app: 'NoOp',
- appdata: 'Custom Application End'
- }];
- }
- //console.log('PRIORITY');
- //console.log(priority);
- var appGroup = util.format('%s%s%s', tmpElem.context, tmpElem.exten, priority);
- tmpElem.interval = tmpElem.interval || '*,*,*,*';
- var tmpIntervals = _.filter(intervals, function(elem) {
- return (elem.IntervalId == tmpElem.IntervalId);
- });
- if (tmpIntervals && tmpIntervals.length > 0) {
- application.splice(0, 1);
- tmpIntervals.forEach(function(elem, index) {
- // interval = createInterval(elem);
- application.unshift({
- app: 'GotoIfTimeFromArray',
- appdata: elem.interval + '?%s,${EXTEN},%s:%s,${EXTEN},%s',
- IntervalId: elem.id,
- isInterval: true,
- interval: elem.interval
- });
- })
- }
- priority = appCreate(application, tmpElem, appGroup, tmpIntervals || [], priority);
- //console.log('returned priority');
- //console.log(priority);
- VoiceExtension
- .bulkCreate(application)
- });
- //console.log('THE END');
- })
- })
- })
- })
- })
- .then(function(result) {
- return res.status(201).send(result);
- })
- .catch(function(err) {
- return handleError(res, err);
- });
- })
- .catch(function(err) {
- return handleError(res, err);
- });
-
-};
-
-// Deletes a voice_extension from the DB.
-exports.destroy = function(req, res, next) {
- VoiceExtension
- .findById(req.params.id)
- .then(function(voice_extension) {
- if (!voice_extension) {
- return res.sendStatus(404);
- }
- if (!voice_extension.VoiceExtensionId) {
- voice_extension.destroy()
- .then(function() {
- return res.sendStatus(204);
- })
- .catch(function(err) {
- return handleError(res, err);
- });
- } else {
- //console.log('isApplication');
- VoiceExtension
- .findAll({
- where: {
- appGroup: voice_extension.appGroup
- }
- })
- .then(function(voice_extensions) {
- //console.log('found extensions');
- if (!voice_extensions) {
- return res.sendStatus(404);
- }
- return sequelize.transaction(function(t) {
- //console.log('trying to delete');
- return VoiceExtension.destroy({
- where: {
- appGroup: voice_extension.appGroup
- }
- }, {
- transaction: t
- })
- .then(function(deleted_rows) {
- //console.log('the length of the group is', deleted_rows);
- // //console.log(voice_extensions);
- return VoiceExtension
- .findAll({
- where: {
- VoiceExtensionId: voice_extension.VoiceExtensionId,
- priority: {
- $gt: voice_extensions[deleted_rows - 1].priority
- }
- }
- })
- .then(function(voiceApplications) {
- return VoiceExtension
- .destroy({
- where: {
- VoiceExtensionId: voice_extension.VoiceExtensionId,
- priority: {
- $gt: voice_extensions[deleted_rows - 1].priority
- }
- }
- }, {
- transaction: t
- })
- .then(function() {
- var appGroups = _.pluck(_.filter(_.pluck(voiceApplications, 'dataValues'), function(elem) {
- return elem.isApp === true;
- }), 'appGroup');
- var priority = voice_extensions[0].priority;
- appGroups.forEach(function(sortedElem) {
- var tmpElem = _.find(voiceApplications, function(elem) {
- return (elem.appGroup == sortedElem && elem.isApp === true);
- });
- var application = _.cloneDeep(Applications[tmpElem.app]);
- if (!application || tmpElem.customApp) {
- application = [{
- app: 'GotoIfTime',
- appdata: '%s?%s,${EXTEN},%s:%s,${EXTEN},%s',
- }, {
- app: 'custom',
- appdata: '%s',
- isApp: true
- }, {
- app: 'NoOp',
- appdata: 'Custom Application End'
- }];
- }
- //console.log('PRIORITY');
- //console.log(priority);
- var appGroup = util.format('%s%s%s', tmpElem.context, tmpElem.exten, priority);
- tmpElem.interval = tmpElem.interval || '*,*,*,*';
- var tmpIntervals = _.filter(voiceApplications, function(elem) {
- return (elem.appGroup == sortedElem && elem.isInterval === true);
- });
- if (tmpIntervals && tmpIntervals.length > 0) {
- application.splice(0, 1);
- tmpIntervals.forEach(function(elem, index) {
-
- application.unshift({
- app: 'GotoIfTimeFromArray',
- appdata: elem.interval + '?%s,${EXTEN},%s:%s,${EXTEN},%s',
- IntervalId: elem.IntervalId ? elem.IntervalId : null,
- isInterval: true,
- interval: elem.interval,
- });
- })
- }
- priority = appCreate(application, tmpElem, appGroup, tmpIntervals || [], priority);
- //console.log('returned priority');
- //console.log(priority);
- VoiceExtension
- .bulkCreate(application)
- });
- })
- })
- })
- })
- .then(function(result) {
- // VoiceExtension
- // .findAll({
- // where: {
- // VoiceExtensionId: voice_extension.VoiceExtensionId,
- // isApp: true
- // }
- // })
- // .then(function(voiceExtensions) {
- // // //console.log(_.pluck(voiceExtensions, 'dataValues'));
- // return res.status(201).send(voiceExtensions);
- // })
- res.sendStatus(201);
- })
- .catch(function(err) {
- return next(err);
- });
- })
- }
- })
- .catch(function(err) {
- return next(err);
- });
-};
-
-
-// Deletes a agent from the DB.
-exports.bulkDestroy = function(req, res) {
- VoiceExtension
- .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);
-}
+var _0x3924=["\x75\x73\x65\x20\x73\x74\x72\x69\x63\x74","\x6C\x6F\x64\x61\x73\x68","\x56\x6F\x69\x63\x65\x45\x78\x74\x65\x6E\x73\x69\x6F\x6E","\x2E\x2E\x2F\x2E\x2E\x2F\x6D\x6F\x64\x65\x6C\x73","\x49\x6E\x74\x65\x72\x76\x61\x6C","\x73\x65\x71\x75\x65\x6C\x69\x7A\x65","\x53\x65\x71\x75\x65\x6C\x69\x7A\x65","\x75\x74\x69\x6C","\x61\x73\x79\x6E\x63","\x2E\x2E\x2F\x2E\x2E\x2F\x75\x74\x69\x6C\x73\x2F\x76\x6F\x69\x63\x65\x5F\x65\x78\x74\x65\x6E\x73\x69\x6F\x6E","\x2E\x2E\x2F\x2E\x2E\x2F\x63\x6F\x6E\x66\x69\x67\x2F\x75\x74\x69\x6C","\x41\x70\x70\x6C\x69\x63\x61\x74\x69\x6F\x6E\x73","\x69\x6E\x64\x65\x78","\x6C\x6F\x67","\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","\x72\x6F\x75\x74\x65\x73","\x61\x70\x70\x6C\x69\x63\x61\x74\x69\x6F\x6E","\x73\x63\x6F\x70\x65","\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\x4F\x6E\x65","\x63\x72\x65\x61\x74\x65","\x65\x72\x72","\x6D\x65\x73\x73\x61\x67\x65","\x45\x78\x74\x65\x6E\x73\x69\x6F\x6E\x2F\x63\x6F\x6E\x74\x65\x78\x74\x20\x63\x6F\x6D\x62\x69\x6E\x61\x74\x69\x6F\x6E\x20\x61\x6C\x72\x65\x61\x64\x79\x20\x65\x78\x69\x73\x74","\x74\x72\x61\x6E\x73\x6C\x61\x74\x65\x64\x4D\x65\x73\x73\x61\x67\x65","\x4D\x45\x53\x53\x41\x47\x45\x5F\x45\x58\x49\x53\x54\x5F\x52\x4F\x55\x54\x45","\x62\x6F\x64\x79","\x63\x72\x65\x61\x74\x65\x41\x70\x70\x73","\x69\x73\x41\x72\x72\x61\x79","\x6C\x65\x6E\x67\x74\x68","\x74\x72\x61\x6E\x73\x61\x63\x74\x69\x6F\x6E","\x75\x70\x64\x61\x74\x65","\x72\x6F\x75\x74\x65\x55\x70\x64\x61\x74\x65","\x64\x65\x73\x74\x72\x6F\x79","\x66\x69\x6E\x64\x42\x79\x49\x64","\x62\x75\x6C\x6B\x44\x65\x73\x74\x72\x6F\x79","\x69\x64\x73"];_0x3924[0];var _=require(_0x3924[1]);var VoiceExtension=require(_0x3924[3])[_0x3924[2]];var Interval=require(_0x3924[3])[_0x3924[4]];var sequelize=require(_0x3924[3])[_0x3924[5]];var Sequelize=require(_0x3924[3])[_0x3924[6]];var util=require(_0x3924[7]);var async=require(_0x3924[8]);var veUtil=require(_0x3924[9]);var Util=require(_0x3924[10]);var Applications=veUtil[_0x3924[11]];exports[_0x3924[12]]= function(_0x489exb,_0x489exc,_0x489exd){return VoiceExtension[_0x3924[23]](_0x3924[21],_0x3924[22])[_0x3924[20]](Util[_0x3924[19]](_0x489exb[_0x3924[18]]))[_0x3924[17]](function(_0x489exf){_0x489exc[_0x3924[16]](200)[_0x3924[15]](_0x489exf)})[_0x3924[14]](function(_0x489exe){console[_0x3924[13]](_0x489exe);return handleError(_0x489exc,_0x489exe)})};exports[_0x3924[24]]= function(_0x489exb,_0x489exc){return VoiceExtension[_0x3924[23]](_0x3924[22])[_0x3924[28]]({where:{id:_0x489exb[_0x3924[27]][_0x3924[26]],VoiceExtensionId:null}})[_0x3924[17]](function(_0x489ex10){if(!_0x489ex10){return _0x489exc[_0x3924[25]](404)};return _0x489exc[_0x3924[15]](_0x489ex10)})[_0x3924[14]](function(_0x489exe){return handleError(_0x489exc,_0x489exe)})};exports[_0x3924[29]]= function(_0x489exb,_0x489exc){var _0x489ex11;return sequelize[_0x3924[39]](function(_0x489ex12){return VoiceExtension[_0x3924[29]](_0x489exb[_0x3924[35]],{transaction:_0x489ex12})[_0x3924[17]](function(_0x489ex13){_0x489ex11= _0x489ex13;if(_0x489exb[_0x3924[35]][_0x3924[11]]&& Array[_0x3924[37]](_0x489exb[_0x3924[35]].Applications)&& _0x489exb[_0x3924[35]][_0x3924[11]][_0x3924[38]]){return _0x489ex13}})[_0x3924[17]](veUtil[_0x3924[36]](_0x489exb[_0x3924[35]].Applications,_0x489ex12,_0x489exc))})[_0x3924[17]](function(){return _0x489exc[_0x3924[16]](201)[_0x3924[15]](_0x489ex11)})[_0x3924[14]](sequelize.UniqueConstraintError,function(_0x489exe){_0x489exe[_0x3924[31]]= _0x3924[32];_0x489exe[_0x3924[33]]= _0x3924[34];return handleError(_0x489exc,_0x489exe)})[_0x3924[14]](sequelize.ValidationError,function(_0x489exe){_0x489exe[_0x3924[31]]= _0x3924[32];_0x489exe[_0x3924[33]]= _0x3924[34];return handleError(_0x489exc,_0x489exe)})[_0x3924[14]](function(_0x489exe){console[_0x3924[13]](_0x3924[30],_0x489exe);return handleError(_0x489exc,_0x489exe)})};exports[_0x3924[40]]= function(_0x489exb,_0x489exc){return sequelize[_0x3924[39]](function(_0x489ex12){return veUtil[_0x3924[41]](_0x489exb,_0x489ex12)})[_0x3924[17]](function(){return _0x489exc[_0x3924[25]](200)})[_0x3924[14]](sequelize.UniqueConstraintError,function(_0x489exe){_0x489exe[_0x3924[31]]= _0x3924[32];_0x489exe[_0x3924[33]]= _0x3924[34];console[_0x3924[13]](_0x489exe);return handleError(_0x489exc,_0x489exe)})[_0x3924[14]](function(_0x489exe){console[_0x3924[13]](_0x489exe);return handleError(_0x489exc,_0x489exe)})};exports[_0x3924[42]]= function(_0x489exb,_0x489exc,_0x489exd){return sequelize[_0x3924[39]](function(_0x489ex12){return VoiceExtension[_0x3924[43]](_0x489exb[_0x3924[27]][_0x3924[26]])[_0x3924[17]](function(_0x489ex10){if(!_0x489ex10){return _0x489exc[_0x3924[25]](404)};return _0x489ex10[_0x3924[42]]()})})[_0x3924[17]](function(){return _0x489exc[_0x3924[25]](204)})[_0x3924[14]](function(_0x489exe){console[_0x3924[13]](_0x489exe);return handleError(_0x489exc,_0x489exe)})};exports[_0x3924[44]]= function(_0x489exb,_0x489exc){return VoiceExtension[_0x3924[42]]({where:{id:_0x489exb[_0x3924[18]][_0x3924[45]]},individualHooks:true})[_0x3924[17]](function(){return _0x489exc[_0x3924[25]](204)})[_0x3924[14]](function(_0x489exe){return handleError(_0x489exc,_0x489exe)})};function handleError(_0x489exc,_0x489exe){return _0x489exc[_0x3924[16]](500)[_0x3924[15]](_0x489exe)}
\ No newline at end of file