Subject: [PATCH] Built motion from commit 4da8ce3.|0.0.143

From: Andrea Bianco <[email-masked]>
Date: Fri, 16 Sep 2016 16:24:19 +0200

---
 release-notes/changelog_0.0.143.txt                |    1 +
 server/config/ami/cleaner.js                       |   56 +++++++++++++++++---
 .../20160916135658-report-session.js               |   19 +++++++
 server/migrations/20160916135648-report-session.js |   19 +++++++
 .../models/history/report_chat_session_history.js  |    3 +-
 .../models/history/report_fax_session_history.js   |    3 +-
 .../models/history/report_mail_session_history.js  |    3 +-
 .../history/report_openchannel_session_history.js  |    3 +-
 .../models/history/report_sms_session_history.js   |    3 +-
 server/models/report_chat_session.js               |    3 +-
 server/models/report_fax_session.js                |    3 +-
 server/models/report_mail_session.js               |    3 +-
 server/models/report_openchannel_session.js        |    3 +-
 server/models/report_sms_session.js                |    3 +-
 14 files changed, 107 insertions(+), 18 deletions(-)
 create mode 100644 server/migrations-history/20160916135658-report-session.js
 create mode 100644 server/migrations/20160916135648-report-session.js

diff --git a/release-notes/changelog_0.0.143.txt b/release-notes/changelog_0.0.143.txt
index 25e0c8e..614fe94 100644
--- a/release-notes/changelog_0.0.143.txt
+++ b/release-notes/changelog_0.0.143.txt
@@ -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) 
diff --git a/server/config/ami/cleaner.js b/server/config/ami/cleaner.js
index 4470e4a..bf240f6 100644
--- a/server/config/ami/cleaner.js
+++ b/server/config/ami/cleaner.js
@@ -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
index 0000000..300487f
--- /dev/null
+++ b/server/migrations-history/20160916135658-report-session.js
@@ -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
index 0000000..c11771c
--- /dev/null
+++ b/server/migrations/20160916135648-report-session.js
@@ -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');
+  }
+};
diff --git a/server/models/history/report_chat_session_history.js b/server/models/history/report_chat_session_history.js
index 5aeb953..5528ecb 100644
--- a/server/models/history/report_chat_session_history.js
+++ b/server/models/history/report_chat_session_history.js
@@ -39,7 +39,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: 0
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_chat_session_history'
   });
diff --git a/server/models/history/report_fax_session_history.js b/server/models/history/report_fax_session_history.js
index 9ad8745..a93a0ee 100644
--- a/server/models/history/report_fax_session_history.js
+++ b/server/models/history/report_fax_session_history.js
@@ -27,7 +27,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_fax_session_history'
   });
diff --git a/server/models/history/report_mail_session_history.js b/server/models/history/report_mail_session_history.js
index 24a84a1..9e890b1 100644
--- a/server/models/history/report_mail_session_history.js
+++ b/server/models/history/report_mail_session_history.js
@@ -33,7 +33,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_mail_session_history'
   });
diff --git a/server/models/history/report_openchannel_session_history.js b/server/models/history/report_openchannel_session_history.js
index 706cea7..213853a 100644
--- a/server/models/history/report_openchannel_session_history.js
+++ b/server/models/history/report_openchannel_session_history.js
@@ -31,7 +31,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_openchannel_session_history'
   });
diff --git a/server/models/history/report_sms_session_history.js b/server/models/history/report_sms_session_history.js
index ec149d3..85ffefb 100644
--- a/server/models/history/report_sms_session_history.js
+++ b/server/models/history/report_sms_session_history.js
@@ -31,7 +31,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_sms_session_history'
   });
diff --git a/server/models/report_chat_session.js b/server/models/report_chat_session.js
index 84520c9..c1cf610 100644
--- a/server/models/report_chat_session.js
+++ b/server/models/report_chat_session.js
@@ -41,7 +41,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: 0
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_chat_session'
   });
diff --git a/server/models/report_fax_session.js b/server/models/report_fax_session.js
index 4ab8547..a9dabfc 100644
--- a/server/models/report_fax_session.js
+++ b/server/models/report_fax_session.js
@@ -27,7 +27,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_fax_session'
   });
diff --git a/server/models/report_mail_session.js b/server/models/report_mail_session.js
index 68f3160..cb00a6a 100644
--- a/server/models/report_mail_session.js
+++ b/server/models/report_mail_session.js
@@ -32,7 +32,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_mail_session'
   });
diff --git a/server/models/report_openchannel_session.js b/server/models/report_openchannel_session.js
index 89ede8f..4d523a3 100644
--- a/server/models/report_openchannel_session.js
+++ b/server/models/report_openchannel_session.js
@@ -30,7 +30,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_openchannel_session'
   });
diff --git a/server/models/report_sms_session.js b/server/models/report_sms_session.js
index a67bad9..0b39820 100644
--- a/server/models/report_sms_session.js
+++ b/server/models/report_sms_session.js
@@ -31,7 +31,8 @@ module.exports = function(sequelize, DataTypes) {
     timeout: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
-    }
+    },
+    timeslot: DataTypes.INTEGER
   }, {
     tableName: 'report_sms_session'
   });
-- 
1.7.1