Built motion from commit 3e059bc2.|2.5.32
[motion2.git] / server / migrations / 2.5.0.js
1 'use strict';
2
3 var BPromise = require('bluebird');
4 var util = require('util');
5
6 var logger = require('../config/logger')('migration');
7
8 var Sequence = function() {};
9
10 Sequence.prototype.enqueue = function(fn) {
11   this.tail = this.tail ? this.tail.finally(fn) : fn();
12 };
13
14 var Migration = function(queryInterface) {
15   this.queryInterface = queryInterface;
16   this.sequence = new Sequence();
17 };
18
19 Migration.prototype.changeColumn = function(table, column, type) {
20   var _this = this;
21   this.sequence.enqueue(function() {
22     return _this.queryInterface
23       .changeColumn(table, column, type)
24       .then(function() {
25         logger.info('Changed column %s in table %s', column, table);
26       })
27       .catch(function(err) {
28         logger.info(JSON.stringify(err));
29       });
30   });
31 };
32
33 Migration.prototype.addColumn = function(table, column, type) {
34   var _this = this;
35   this.sequence.enqueue(function() {
36     return _this.queryInterface
37       .addColumn(table, column, type)
38       .then(function() {
39         logger.info('Added column %s to %s', column, table);
40       })
41       .catch(function(err) {
42         logger.info(JSON.stringify(err));
43       });
44   });
45 };
46
47 Migration.prototype.dropTable = function(table) {
48   var _this = this;
49   this.sequence.enqueue(function() {
50     return _this.queryInterface
51       .dropTable(table, {
52         force: true
53       })
54       .then(function() {
55         logger.info('table dropped %s', table);
56       })
57       .catch(function(err) {
58         logger.info(JSON.stringify(err));
59       });
60   });
61 };
62
63 Migration.prototype.addIndex = function(table, column, options) {
64   var _this = this;
65   this.sequence.enqueue(function() {
66     return _this.queryInterface
67       .addIndex(table, column, {
68         indexName: options.indexName,
69         indicesType: options.indicesType
70       })
71       .then(function() {
72         logger.info('addIndex %s %s %s [%s]', table, column.join(','), options.indexName, options.indicesType);
73       })
74       .catch(function(err) {
75         logger.info(JSON.stringify(err));
76       });
77   });
78 };
79
80 Migration.prototype.removeIndex = function(table, indexName) {
81   var _this = this;
82   this.sequence.enqueue(function() {
83     return _this.queryInterface
84       .removeIndex(table, indexName)
85       .then(function() {
86         logger.info('removeIndex %s %s', table, indexName);
87       })
88       .catch(function(err) {
89         logger.info(JSON.stringify(err));
90       });
91   });
92 };
93
94 Migration.prototype.query = function(sql) {
95   var _this = this;
96   this.sequence.enqueue(function() {
97     return _this.queryInterface.sequelize
98       .query(sql)
99       .then(function() {
100         logger.info('query %s', sql);
101       })
102       .catch(function(err) {
103         logger.info(JSON.stringify(err));
104       });
105   });
106 };
107
108 Migration.prototype.removeColumn = function(table, column) {
109   var _this = this;
110   this.sequence.enqueue(function() {
111     return _this.queryInterface
112       .removeColumn(table, column)
113       .then(function() {
114         logger.info('Removed column %s from %s', column, table);
115       })
116       .catch(function(err) {
117         logger.info(
118           util.inspect(err, {
119             showHidden: false,
120             depth: null
121           })
122         );
123       });
124   });
125 };
126
127 Migration.prototype.renameColumn = function(table, oldColumn, newColumn) {
128   var _this = this;
129   this.sequence.enqueue(function() {
130     return _this.queryInterface
131       .renameColumn(table, oldColumn, newColumn)
132       .then(function() {
133         logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
134       })
135       .catch(function(err) {
136         logger.info(
137           util.inspect(err, {
138             showHidden: false,
139             depth: null
140           })
141         );
142       });
143   });
144 };
145
146 Migration.prototype.final = function(resolve) {
147   this.sequence.enqueue(function() {
148     return resolve();
149   });
150 };
151
152 module.exports = {
153   up: function(queryInterface, Sequelize) {
154     return new BPromise(function(resolve) {
155
156       var migration = new Migration(queryInterface);
157
158       // START whatsapp
159       migration.query('CREATE TABLE `whatsapp_accounts` (' +
160         ' `id` int(11) NOT NULL AUTO_INCREMENT,' +
161         ' `name` varchar(255) NOT NULL,' +
162         ' `key` varchar(255) NOT NULL,' +
163         ' `remote` varchar(255) NOT NULL,' +
164         ' `token` varchar(255) DEFAULT NULL,' +
165         ' `phone` varchar(255) DEFAULT NULL,' +
166         ' `type` enum(\'twilio\') DEFAULT NULL,' +
167         ' `accountSid` varchar(255) DEFAULT NULL,' +
168         ' `authToken` varchar(255) DEFAULT NULL,' +
169         ' `notificationTemplate` text,' +
170         ' `notificationSound` tinyint(1) DEFAULT \'0\',' +
171         ' `notificationShake` tinyint(1) DEFAULT \'0\',' +
172         ' `waitForTheAssignedAgent` int(11) DEFAULT \'10\',' +
173         ' `queueTransfer` tinyint(1) DEFAULT \'0\',' +
174         ' `queueTransferTimeout` int(11) DEFAULT \'300\',' +
175         ' `agentTransfer` tinyint(1) DEFAULT \'0\',' +
176         ' `agentTransferTimeout` int(11) DEFAULT \'300\',' +
177         ' `mandatoryDispositionPauseId` int(11) DEFAULT NULL,' +
178         ' `mandatoryDisposition` tinyint(1) DEFAULT \'0\',' +
179         ' `description` varchar(255) DEFAULT NULL,' +
180         ' `createdAt` datetime NOT NULL,' +
181         ' `updatedAt` datetime NOT NULL,' +
182         ' `ListId` int(11) DEFAULT NULL,' +
183         ' PRIMARY KEY (`id`),' +
184         ' UNIQUE KEY `name` (`name`),' +
185         ' KEY `mandatoryDispositionPauseId` (`mandatoryDispositionPauseId`),' +
186         ' KEY `ListId` (`ListId`),' +
187         ' CONSTRAINT `whatsapp_accounts_ibfk_1` FOREIGN KEY (`mandatoryDispositionPauseId`) REFERENCES `tools_pauses` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE,' +
188         ' CONSTRAINT `whatsapp_accounts_ibfk_2` FOREIGN KEY (`ListId`) REFERENCES `cm_lists` (`id`) ON DELETE SET NULL ON UPDATE CASCADE' +
189         ') ENGINE=InnoDB DEFAULT CHARSET=utf8;');
190
191       migration.addColumn('tools_dispositions', 'WhatsappAccountId', {
192         type: Sequelize.INTEGER
193       });
194
195       migration.addColumn('tools_canned_answers', 'WhatsappAccountId', {
196         type: Sequelize.INTEGER
197       });
198
199       migration.addColumn('users', 'whatsappPause', {
200         type: Sequelize.BOOLEAN,
201         defaultValue: false
202       });
203
204       migration.addColumn('users', 'whatsappCapacity', {
205         type: Sequelize.BOOLEAN,
206         defaultValue: false
207       });
208
209       migration.query('ALTER TABLE tools_dispositions ADD CONSTRAINT `tools_dispositions_ibfk_7` FOREIGN KEY (`WhatsappAccountId`) REFERENCES `whatsapp_accounts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE');
210       migration.query('ALTER TABLE tools_dispositions ADD CONSTRAINT `name_whatsapp` UNIQUE KEY (`name`,`WhatsappAccountId`)');
211       migration.query('ALTER TABLE tools_canned_answers ADD CONSTRAINT `tools_canned_answers_ibfk_6` FOREIGN KEY (`WhatsappAccountId`) REFERENCES `whatsapp_accounts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE');
212
213       migration.query('UPDATE users SET permissions=\'101,102,103,113,110,104,105,106,107,108,109,100,111,114\' WHERE role=\'agent\' AND permissions=\'101,102,103,110,104,105,106,107,108,109,100,111\'');
214       // END whatsapp
215
216       // START chat_interactions
217       migration.addColumn('chat_interactions', 'vidaooSessionId', {
218         type: Sequelize.TEXT
219       });
220
221       // START chat_websites
222       migration.addColumn('chat_websites', 'vidaooEscalation', {
223         type: Sequelize.BOOLEAN,
224         defaultValue: false
225       });
226
227       migration.addColumn('chat_websites', 'vidaooApiKey', {
228         type: Sequelize.STRING
229       });
230       // END chat_websites
231
232       // START voice_recordings
233       migration.addColumn('voice_recordings', 'location', {
234         type: Sequelize.TEXT
235       });
236       migration.addColumn('voice_recordings', 'transcribeName', {
237         type: Sequelize.STRING
238       });
239       migration.addColumn('voice_recordings', 'transcribeStatus', {
240         type: Sequelize.STRING,
241         defaultValue: 'NEW'
242       });
243       migration.addColumn('voice_recordings', 'fileUri', {
244         type: Sequelize.TEXT
245       });
246       migration.addColumn('voice_recordings', 'fileText', {
247         type: Sequelize.TEXT
248       });
249       migration.addColumn('voice_recordings', 'failureReason', {
250         type: Sequelize.TEXT
251       });
252       migration.addColumn('voice_recordings', 'sentiment', {
253         type: Sequelize.STRING
254       });
255       migration.addColumn('voice_recordings', 'sPositive', {
256         type: Sequelize.FLOAT
257       });
258       migration.addColumn('voice_recordings', 'sNegative', {
259         type: Sequelize.FLOAT
260       });
261       migration.addColumn('voice_recordings', 'sNeutral', {
262         type: Sequelize.FLOAT
263       });
264       migration.addColumn('voice_recordings', 'sMixed', {
265         type: Sequelize.FLOAT
266       });
267       migration.addColumn('voice_recordings', 'tempSentiment', {
268         type: Sequelize.BOOLEAN
269       });
270       // END voice_recordings
271
272       // START settings
273       migration.addColumn('settings', 'transcribe', {
274         type: Sequelize.BOOLEAN
275       });
276       migration.addColumn('settings', 'automaticTranscribe', {
277         type: Sequelize.BOOLEAN
278       });
279       migration.addColumn('settings', 'transcribeAccountId', {
280         type: Sequelize.INTEGER
281       });
282       migration.addColumn('settings', 'transcribeRegion', {
283         type: Sequelize.STRING
284       });
285       migration.addColumn('settings', 'sentiment', {
286         type: Sequelize.BOOLEAN
287       });
288       migration.addColumn('settings', 'automaticSentiment', {
289         type: Sequelize.BOOLEAN
290       });
291       migration.addColumn('settings', 'sentimentAccountId', {
292         type: Sequelize.INTEGER
293       });
294       migration.addColumn('settings', 'sentimentRegion', {
295         type: Sequelize.STRING
296       });
297       migration.addColumn('settings', 'language', {
298         type: Sequelize.STRING
299       });
300       migration.addColumn('settings', 'bucket', {
301         type: Sequelize.STRING
302       });
303       // END settings
304
305       // START FINAL
306       migration.final(resolve);
307       // END FINAL
308     });
309   },
310
311   down: function(queryInterface, Sequelize) {
312     // var migration = new Migration(queryInterface);
313   }
314 };