From 8244e7e9e55d78c6140f27931aadb65ebe7b498c Mon Sep 17 00:00:00 2001 From: Daniele Cinti Date: Wed, 14 Sep 2016 16:59:15 +0200 Subject: [PATCH] Built motion from commit 71daff1.|0.0.141 --- release-notes/changelog_0.0.141.txt | 2 + server/api/xchatty/xchatty.controller.js | 172 ++++++++++++++++++++++++++++- server/config/triggers/index.js | 116 ++++++++++---------- 3 files changed, 227 insertions(+), 63 deletions(-) diff --git a/release-notes/changelog_0.0.141.txt b/release-notes/changelog_0.0.141.txt index c1347f4..b2d6354 100644 --- a/release-notes/changelog_0.0.141.txt +++ b/release-notes/changelog_0.0.141.txt @@ -1,5 +1,7 @@ # xCALLY MOTION Changelog +- * 50850a2 - 2016-09-14: add app interval check in agent count for chat status +- * 4a62eea - 2016-09-14: Fixed triggers with forked ami - * 7c7b434 - 2016-09-14: server side app.js update - * 2856612 - 2016-09-14: server side config/triggers update process.on - * d56a499 - 2016-09-14: server side config ami/ami.js diff --git a/server/api/xchatty/xchatty.controller.js b/server/api/xchatty/xchatty.controller.js index 44ed89c..527adb7 100644 --- a/server/api/xchatty/xchatty.controller.js +++ b/server/api/xchatty/xchatty.controller.js @@ -53,7 +53,32 @@ var License = require('../../models').License; var Xchatty = require('../../models').Xchatty; var Setting = require('../../models').Setting; const Contact = require('../../models').Contact; +var Interval = require('../../models').Interval; + +const weekDaysCollection = { + mon: 1, + tue: 2, + wed: 3, + thu: 4, + fri: 5, + sat: 6, + sun: 7 +}; +const monthsCollection = { + jan: 1, + feb: 2, + mar: 3, + apr: 4, + may: 5, + jun: 6, + jul: 7, + aug: 8, + sep: 9, + oct: 10, + nov: 11, + dec: 12 +}; var id = 1; @@ -1034,18 +1059,25 @@ exports.getStatusByWebsite = function(req, res, next) { }] }, { model: Agent + }, { + model: Interval, + include: [{ + all: true + }] }] }); }) .then(function(chatApplications) { _.forEach(chatApplications, function(app) { - if (app.app === 'queue') { - if (app.ChatQueue.hasOwnProperty('Users') && app.ChatQueue.Users.length > 0) { - _users = _.uniq(_.merge(_users, _.map(app.ChatQueue.Users, 'id'))); + if (checkAppInterval(app)) { + if (app.app === 'queue') { + if (app.ChatQueue.hasOwnProperty('Users') && app.ChatQueue.Users.length > 0) { + _users = _.uniq(_.merge(_users, _.map(app.ChatQueue.Users, 'id'))); + } + } else if (app.app === 'agent') { + _users.push(app.User.id); + _users = _.uniq(_users); } - } else if (app.app === 'agent') { - _users.push(app.User.id); - _users = _.uniq(_users); } }); return Agent.findAndCountAll({ @@ -1124,3 +1156,131 @@ exports.getStatusByWebsite = function(req, res, next) { return res.status(500).jsonp({}); }); } + +function checkAppInterval(app) { + var valid = true; + + if (app.Interval) { + for (var j = 0; j < app.Interval.SubIntervals.length; j++) { + if (isIntervalValid(splitInterval(app.Interval.SubIntervals[j].interval))) { + break; + } else { + valid = false; + } + } + } else { + if (!isIntervalValid(splitInterval(app.interval))) { + valid = false; + } + } + + return valid; +} + +function splitInterval(interval) { + var finalInterval = {}; + var splittedInterval = interval.split(','); + var intValues; + splittedInterval.forEach(function(element, index) { + switch (index) { + case 0: + if (element !== '*') { + intValues = element.split('-'); + finalInterval.t_from = moment(intValues[0], 'HH:mm'); + finalInterval.t_to = moment(intValues[1], 'HH:mm'); + } else { + finalInterval.t_from = null; + finalInterval.t_to = null; + } + break; + case 1: + if (element !== '*') { + intValues = element.split('-'); + finalInterval.wd_from = weekDaysCollection[intValues[0]]; + finalInterval.wd_to = intValues[1] ? weekDaysCollection[intValues[1]] : null; + } else { + finalInterval.wd_from = null; + finalInterval.wd_to = null; + } + break; + case 2: + if (element !== '*') { + intValues = element.split('-'); + finalInterval.md_from = intValues[0]; + finalInterval.md_to = intValues[1] ? intValues[1] : null; + } else { + finalInterval.md_from = null; + finalInterval.md_to = null; + } + break; + case 3: + if (element !== '*') { + intValues = element.split('-'); + finalInterval.m_from = monthsCollection[intValues[0]]; + finalInterval.m_to = intValues[1] ? monthsCollection[intValues[1]] : null; + } else { + finalInterval.m_from = null; + finalInterval.m_to = null; + } + break; + default: + + } + + + }); + return finalInterval; +} + +function isIntervalValid(interval) { + var hour = moment().format("HH:mm"); + var day = moment().format("E"); + var monthDay = moment().format("D"); + var month = moment().format("M"); + + var tValid, wdValid, mdValid, mValid; + var monthsNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; + var weekDaysNumbers = [1, 2, 3, 4, 5, 6, 7]; + var daysOfMonthsNumbers = []; + for (var i = 1; i <= 31; i++) { + daysOfMonthsNumbers.push(i); + } + tValid = (interval.t_from && interval.t_to) ? moment(hour, "HH:mm").isBetween(moment(interval.t_from, "HH:mm"), moment(interval.t_to, "HH:mm")) : true; + if (interval.wd_from) { + if (interval.wd_to) { + var validWeekdays = _.filter(weekDaysNumbers, function(elem) { + return (elem >= interval.wd_from) && (elem <= interval.wd_to); + }); + wdValid = (validWeekdays.indexOf(parseInt(day)) !== -1) ? true : false; + } else { + wdValid = (parseInt(day) === interval.wd_from) ? true : false; + } + } else { + wdValid = true; + } + if (interval.md_from) { + if (interval.md_to) { + var validMonthsdays = _.filter(daysOfMonthsNumbers, function(elem) { + return (elem >= interval.md_from) && (elem <= interval.md_to); + }); + mdValid = (validMonthsdays.indexOf(parseInt(monthDay)) !== -1) ? true : false; + } else { + mdValid = (parseInt(monthDay) === interval.md_from) ? true : false; + } + } else { + mdValid = true; + } + if (interval.m_from) { + if (interval.m_to) { + var validMonths = _.filter(monthsNumbers, function(elem) { + return (elem >= interval.m_from) && (elem <= interval.m_to); + }); + mValid = (validMonths.indexOf(parseInt(month)) !== -1) ? true : false; + } else { + mValid = (parseInt(month) === interval.m_from) ? true : false; + } + } else { + mValid = true; + } + return tValid && wdValid && mdValid && mValid; +} diff --git a/server/config/triggers/index.js b/server/config/triggers/index.js index 572f74f..87f097b 100644 --- a/server/config/triggers/index.js +++ b/server/config/triggers/index.js @@ -53,10 +53,67 @@ module.exports = function(cpAmi) { return synchTriggers(_triggers); }) .then(function() { - return synchVars(cpAmi); + return synchVars(); }) .then(function() { console.log('Triggers are ', _triggers.length); + cpAmi.on('message', function(data) { + var actions = []; + switch (data.evt) { + case 'varset': + if (data.hasOwnProperty('variable') && data.hasOwnProperty('value')) { + var foundVar = _.find(localVariables, function(variable) { //search varset variable in local db variables + return (variable.name === _.head(_.keys(data.variable))) || (_.toLower(variable.name) === _.head(_.keys(data.variable))); + }); + if (foundVar) { //if local db variable exist + var callFound = _.find(callChannels, { + uniqueid: data.uniqueid + }); //check if there is already an object relative to the call, where the variable has been set, in the calls array + if (callFound) { //if the call exist, I add the new variable to the list of the call variables or update the old value + callFound.variables[foundVar.name] = data.value; + } else { //otherwise + if (callChannels.length >= MAX_CALLS) { //If I exceed the max lenght of the calls array I pop the oldest element (FIFO) + callChannels.shift(); + } + var call = { + uniqueid: data.uniqueid, + variables: {} + }; + call.variables[foundVar.name] = data.value; + callChannels.push(call); //Insert the new call in the calls array + } + } + } + break; + case 'ReportDial.afterUpdate': + if (data.channel) { + actions = util.getActionsByTriggers(data, _.filter(_triggers, { + channel: 'voice' + })); + data.dataValues.membername = data.channel.split(/\/|-/)[1]; + console.log('After dial update, Actions are ', actions.length); + execActions(actions, data, 'voice', 'outbound', util, voice, mail, cm, jscripty, integration, urlForward); + } + break; + case 'ReportAgent.afterCreate': + actions = util.getActionsByTriggers(data, _.filter(_triggers, { + channel: 'voice' + })); + console.log('After agent create, Actions are ', actions.length); + execActions(actions, data, 'voice', 'queue', util, voice, mail, cm, jscripty, integration, urlForward); + break; + case 'ReportAgent.afterUpdate': + if (data.changed('lastevent')) { + actions = util.getActionsByTriggers(data, _.filter(_triggers, { + channel: 'voice' + })); + console.log('After agent update, Actions are ', actions.length); + execActions(actions, data, 'voice', 'queue', util, voice, mail, cm, jscripty, integration, urlForward); + } + break; + default: + } + }); ChatRoom.afterCreate(function(chatRoom) { return chatRoom .getChatVisitor() @@ -78,32 +135,6 @@ module.exports = function(cpAmi) { execActions(actions, smsMessage.dataValues, 'sms', null, util, voice, mail, cm, jscripty, integration, urlForward, chat, sms); } }); - ReportDial.afterUpdate(function(doc) { - if (doc.channel) { - var actions = util.getActionsByTriggers(doc, _.filter(_triggers, { - channel: 'voice' - })); - doc.dataValues.membername = doc.channel.split(/\/|-/)[1]; - console.log('After dial update, Actions are ', actions.length); - execActions(actions, doc, 'voice', 'outbound', util, voice, mail, cm, jscripty, integration, urlForward); - } - }) - ReportAgent.afterCreate(function(doc) { - var actions = util.getActionsByTriggers(doc, _.filter(_triggers, { - channel: 'voice' - })); - console.log('After agent create, Actions are ', actions.length); - execActions(actions, doc, 'voice', 'queue', util, voice, mail, cm, jscripty, integration, urlForward); - }) - ReportAgent.afterUpdate(function(doc) { - if (doc.changed('lastevent')) { - var actions = util.getActionsByTriggers(doc, _.filter(_triggers, { - channel: 'voice' - })); - console.log('After agent update, Actions are ', actions.length); - execActions(actions, doc, 'voice', 'queue', util, voice, mail, cm, jscripty, integration, urlForward); - } - }) MailMessage.afterCreate(function(doc) { if (doc.status === 'RECEIVED') { var actions = util.getActionsByTriggers(doc, _.filter(_triggers, { @@ -120,42 +151,13 @@ module.exports = function(cpAmi) { }); } -function synchVars(cpAmi) { +function synchVars() { return Variable .findAll() .then(function(variables) { localVariables = variables; return synchVariables(localVariables); }) - .then(function() { - cpAmi.on('message', function(data) { - if (data.evt === 'varset') { - if (data.hasOwnProperty('variable') && data.hasOwnProperty('value')) { - var foundVar = _.find(localVariables, function(variable) { //search varset variable in local db variables - return (variable.name === _.head(_.keys(data.variable))) || (_.toLower(variable.name) === _.head(_.keys(data.variable))); - }); - if (foundVar) { //if local db variable exist - var callFound = _.find(callChannels, { - uniqueid: data.uniqueid - }); //check if there is already an object relative to the call, where the variable has been set, in the calls array - if (callFound) { //if the call exist, I add the new variable to the list of the call variables or update the old value - callFound.variables[foundVar.name] = data.value; - } else { //otherwise - if (callChannels.length >= MAX_CALLS) { //If I exceed the max lenght of the calls array I pop the oldest element (FIFO) - callChannels.shift(); - } - var call = { - uniqueid: data.uniqueid, - variables: {} - }; - call.variables[foundVar.name] = data.value; - callChannels.push(call); //Insert the new call in the calls array - } - } - } - } - }); - }) .catch(function(err) { console.log('Error initializing local variables for triggers:', err); }); -- 1.7.1