Built motion from commit 06df96e on branch develop.
[motion.git] / server / config / automations / mail.js
1 'use strict';
2
3 var _ = require('lodash');
4 var config = require('../environment');
5 var moment = require('moment');
6 // var stringifyObject = require('stringify-object');
7
8 var sequelize = require('../../models').sequelize;
9 var Automation = require('../../models').Automation;
10 var MailRoom = require('../../models').MailRoom;
11 var Settings = require('../../models').Settings;
12 var automationTimeout;
13 var activeAutomations = {};
14
15 function getTimeout() {
16   Settings
17     .findOne()
18     .then(function(result) {
19       automationTimeout = result.automation_timeout;
20     })
21     .catch(function(err) {
22       // console.log(err);
23     });
24 }
25
26 function executeAutomation(mailAutomation) {
27   var query = {
28     where: {}
29   };
30   var actions = {};
31   var conditionsGroup = JSON.parse(mailAutomation.conditions).group;
32   var actionsGroup = JSON.parse(mailAutomation.actions).group;
33   var subConditions = {};
34   var mainOp = conditionsGroup.boolean === 'AND' ? '$and' : '$or';
35   query.where[mainOp] = [];
36   conditionsGroup.rules.forEach(function(rule) {
37     if (rule.operator) {
38       // console.log('normal rule');
39       query.where[mainOp].push(getCondition(rule));
40     } else if (rule.group) {
41       // console.log('subgroup');
42       var subOp = rule.group.boolean === 'AND' ? '$and' : '$or';
43       var tempGroup = {};
44       tempGroup[subOp] = [];
45       rule.group.rules.forEach(function(subRule) {
46         // console.log('subgroup rule');
47         tempGroup[subOp].push(getCondition(subRule));
48       });
49       query.where[mainOp].push(tempGroup);
50     }
51   });
52
53   //
54   MailRoom
55     .findAll(query)
56     .then(function(res) {
57       res.forEach(function(mail) {
58         actionsGroup.rules.forEach(function(rule) {
59           switch (rule.field.value) {
60             case 'status':
61               setStatus(mail.id, rule.data);
62               break;
63             default:
64           }
65         });
66       });
67     })
68     .catch(function(err) {
69       console.log(err);
70     });
71 }
72
73 function setStatus(ticketId, newStatus) {
74   MailRoom.update({
75       status: newStatus,
76     }, {
77       where: {
78         id: ticketId
79       }
80     })
81     .catch(function(err) {
82       console.log(err);
83     });
84 }
85
86 function getCondition(rule) {
87   var result = {};
88   switch (rule.field.value) {
89     case 'status':
90       switch (rule.operator) {
91         case '=':
92           result[rule.field.value] = rule.data;
93           break;
94         case '<>':
95           result[rule.field.value] = {
96             $ne: rule.data
97           };
98           break;
99         default:
100       }
101       break;
102     case 'createdAt':
103       var compareDate = moment().subtract(parseInt(rule.data, 10), 'hours').format("YYYY-MM-DD HH:mm:ss");
104       switch (rule.operator) {
105         case '>':
106           result[rule.field.value] = {
107             lt: compareDate
108           };
109           break;
110         case '>=':
111           result[rule.field.value] = {
112             $lte: compareDate
113           };
114           break;
115         case '<':
116           result[rule.field.value] = {
117             $gt: compareDate
118           };
119           break;
120         case '<=':
121           result[rule.field.value] = {
122             $gte: compareDate
123           };
124           break;
125         default:
126       }
127       break;
128     default:
129   }
130   return result;
131 }
132
133 function createInterval(mailAutomation) {
134   activeAutomations['interval' + mailAutomation.id] = setInterval(function() {
135     executeAutomation(mailAutomation);
136   }, automationTimeout * 1000);
137 }
138
139 module.exports = function() {
140   getTimeout();
141
142   Automation.afterCreate(function(doc, options) {
143     createInterval(doc);
144   });
145
146   Automation.afterUpdate(function(doc, options) {
147     clearInterval(activeAutomations['interval' + doc.id]);
148     createInterval(doc);
149   });
150
151   Automation.afterDelete(function(doc, options) {
152     clearInterval(activeAutomations['interval' + doc.id]);
153   });
154
155   Automation
156     .findAll({
157       where: {
158         status: true,
159         channel: 'mail'
160       }
161     })
162     .then(function(mailAutomations) {
163
164       mailAutomations.forEach(function(mailAutomation) {
165         createInterval(mailAutomation);
166       });
167     })
168     .catch(function(err) {
169       console.error(err);
170     });
171 };