Built motion from commit 4da8ce3.|0.0.143
authorAndrea Bianco <andrea.bianco@xcally.com>
Fri, 16 Sep 2016 14:24:19 +0000 (16:24 +0200)
committerAndrea Bianco <andrea.bianco@xcally.com>
Fri, 16 Sep 2016 14:24:19 +0000 (16:24 +0200)
14 files changed:
release-notes/changelog_0.0.143.txt
server/config/ami/cleaner.js
server/migrations-history/20160916135658-report-session.js [new file with mode: 0644]
server/migrations/20160916135648-report-session.js [new file with mode: 0644]
server/models/history/report_chat_session_history.js
server/models/history/report_fax_session_history.js
server/models/history/report_mail_session_history.js
server/models/history/report_openchannel_session_history.js
server/models/history/report_sms_session_history.js
server/models/report_chat_session.js
server/models/report_fax_session.js
server/models/report_mail_session.js
server/models/report_openchannel_session.js
server/models/report_sms_session.js

index 25e0c8e..614fe94 100644 (file)
@@ -1,5 +1,6 @@
 # xCALLY MOTION Changelog
 
+- * 1ba79fa - 2016-09-16: Added cleaner for all channels 
 - * 82c5e19 - 2016-09-16: remove custom label from mail selection (scheduler) 
 - * b1ef54a - 2016-09-16: fix service mail account 
 - * 6ae85eb - 2016-09-16: fixed enquiry forward after smtp changes (service) 
index 4470e4a..bf240f6 100644 (file)
@@ -4,18 +4,24 @@ var moment = require('moment');
 var _ = require('lodash');
 var Promise = require('bluebird');
 
-var ReportQueue = require('../../models').ReportQueue;
-var ReportSquare = require('../../models').ReportSquare;
-var ReportDial = require('../../models').ReportDial;
+var models = require('../../models');
 
 function Cleaner(ami) {
   console.log('Cleaner Initialization...');
-  removeInactiveCalls(ami);
+  var channels = ['Chat', 'Mail', 'Fax', 'Sms', 'Openchannel'];
+  clean(ami, channels);
   setInterval(function() {
-    removeInactiveCalls(ami);
+    clean(ami, channels);
   }, 10 * 60 * 1000);
 }
 
+function clean(ami, channels) {
+  removeInactiveCalls(ami);
+  _.forEach(channels, function(channel) {
+    removeExpiredApplications(channel);
+  });
+}
+
 function removeInactiveCalls(ami) {
   console.log('Removing inactive calls...');
   var _bulkClean = [],
@@ -36,7 +42,7 @@ function removeInactiveCalls(ami) {
           }
         });
       }
-      return ReportQueue
+      return models.ReportQueue
         .findAll({
           where: {
             $or: [{
@@ -63,7 +69,7 @@ function removeInactiveCalls(ami) {
           }))
         }
       });
-      return ReportSquare
+      return models.ReportSquare
         .findAll({
           where: {
             leaveAt: null
@@ -80,7 +86,7 @@ function removeInactiveCalls(ami) {
         }
       });
 
-      return ReportDial
+      return models.ReportDial
         .findAll({
           where: {
             endtime: null
@@ -107,4 +113,38 @@ function removeInactiveCalls(ami) {
     });
 }
 
+function removeExpiredApplications(channel) {
+
+  return models['Report' + channel + 'Session']
+    .findAll({
+      where: {
+        leaveAt: null,
+        timeslot: { //If for any reason the timeslot is not populated I can't check if the app is expired
+          $ne: null
+        }
+      }
+    })
+    .then(function(sessions) {
+      var expiration, bulkFix = [];
+      _.forEach(sessions, function(session) {
+        expiration = moment(session.updatedAt).add(session.timeslot, 'seconds');
+        if (moment().isSameOrAfter(expiration)) {
+          bulkFix.push(session.updateAttributes({
+            leaveAt: moment().format("YYYY-MM-DD HH:mm:ss")
+          }));
+        }
+      });
+
+      return bulkFix;
+    })
+    .all()
+    .then(function() {
+      console.log('Inactive ' + channel + ' sessions cleaning completed!:');
+    })
+    .catch(function(err) {
+      console.log('Inactive ' + channel + ' sessions remove error:', err);
+    });
+
+}
+
 module.exports = Cleaner;
diff --git a/server/migrations-history/20160916135658-report-session.js b/server/migrations-history/20160916135658-report-session.js
new file mode 100644 (file)
index 0000000..300487f
--- /dev/null
@@ -0,0 +1,19 @@
+'use strict';
+
+module.exports = {
+  up: function(queryInterface, Sequelize) {
+    queryInterface.addColumn('report_chat_session_history', 'timeslot', Sequelize.INTEGER);
+    queryInterface.addColumn('report_mail_session_history', 'timeslot', Sequelize.INTEGER);
+    queryInterface.addColumn('report_fax_session_history', 'timeslot', Sequelize.INTEGER);
+    queryInterface.addColumn('report_sms_session_history', 'timeslot', Sequelize.INTEGER);
+    queryInterface.addColumn('report_openchannel_session_history', 'timeslot', Sequelize.INTEGER);
+  },
+
+  down: function(queryInterface, Sequelize) {
+    queryInterface.removeColumn('report_chat_session_history', 'timeslot');
+    queryInterface.removeColumn('report_mail_session_history', 'timeslot');
+    queryInterface.removeColumn('report_fax_session_history', 'timeslot');
+    queryInterface.removeColumn('report_sms_session_history', 'timeslot');
+    queryInterface.removeColumn('report_openchannel_session_history', 'timeslot');
+  }
+};
diff --git a/server/migrations/20160916135648-report-session.js b/server/migrations/20160916135648-report-session.js
new file mode 100644 (file)
index 0000000..c11771c
--- /dev/null
@@ -0,0 +1,19 @@
+'use strict';
+
+module.exports = {
+  up: function(queryInterface, Sequelize) {
+    queryInterface.addColumn('report_chat_session', 'timeslot', Sequelize.INTEGER);
+    queryInterface.addColumn('report_mail_session', 'timeslot', Sequelize.INTEGER);
+    queryInterface.addColumn('report_fax_session', 'timeslot', Sequelize.INTEGER);
+    queryInterface.addColumn('report_sms_session', 'timeslot', Sequelize.INTEGER);
+    queryInterface.addColumn('report_openchannel_session', 'timeslot', Sequelize.INTEGER);
+  },
+
+  down: function(queryInterface, Sequelize) {
+    queryInterface.removeColumn('report_chat_session', 'timeslot');
+    queryInterface.removeColumn('report_mail_session', 'timeslot');
+    queryInterface.removeColumn('report_fax_session', 'timeslot');
+    queryInterface.removeColumn('report_sms_session', 'timeslot');
+    queryInterface.removeColumn('report_openchannel_session', 'timeslot');
+  }
+};
index 5aeb953..5528ecb 100644 (file)
@@ -39,7 +39,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: 0
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_chat_session_history'
   });
index 9ad8745..a93a0ee 100644 (file)
@@ -27,7 +27,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_fax_session_history'
   });
index 24a84a1..9e890b1 100644 (file)
@@ -33,7 +33,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_mail_session_history'
   });
index 706cea7..213853a 100644 (file)
@@ -31,7 +31,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_openchannel_session_history'
   });
index ec149d3..85ffefb 100644 (file)
@@ -31,7 +31,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_sms_session_history'
   });
index 84520c9..c1cf610 100644 (file)
@@ -41,7 +41,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: 0
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_chat_session'
   });
index 4ab8547..a9dabfc 100644 (file)
@@ -27,7 +27,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_fax_session'
   });
index 68f3160..cb00a6a 100644 (file)
@@ -32,7 +32,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_mail_session'
   });
index 89ede8f..4d523a3 100644 (file)
@@ -30,7 +30,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_openchannel_session'
   });
index a67bad9..0b39820 100644 (file)
@@ -31,7 +31,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_sms_session'
   });