3 var BPromise = require('bluebird');
4 var util = require('util');
6 var logger = require('../config/logger')('migration');
8 var Sequence = function () {};
10 Sequence.prototype.enqueue = function (fn) {
11 this.tail = this.tail ? this.tail.finally(fn) : fn();
14 var Migration = function (queryInterface) {
15 this.queryInterface = queryInterface;
16 this.sequence = new Sequence();
19 Migration.prototype.changeColumn = function (table, column, type) {
21 this.sequence.enqueue(function () {
22 return _this.queryInterface
23 .changeColumn(table, column, type)
25 logger.info('Changed column %s in table %s', column, table);
27 .catch(function (err) {
28 logger.info(JSON.stringify(err));
33 Migration.prototype.addColumn = function (table, column, type) {
35 this.sequence.enqueue(function () {
36 return _this.queryInterface
37 .addColumn(table, column, type)
39 logger.info('Added column %s to %s', column, table);
41 .catch(function (err) {
42 logger.info(JSON.stringify(err));
47 Migration.prototype.dropTable = function (table) {
49 this.sequence.enqueue(function () {
50 return _this.queryInterface
55 logger.info('table dropped %s', table);
57 .catch(function (err) {
58 logger.info(JSON.stringify(err));
63 Migration.prototype.addIndex = function (table, column, options) {
65 this.sequence.enqueue(function () {
66 return _this.queryInterface
67 .addIndex(table, column, {
68 indexName: options.indexName,
69 indicesType: options.indicesType
72 logger.info('addIndex %s %s %s [%s]', table, column.join(','), options.indexName, options.indicesType);
74 .catch(function (err) {
75 logger.info(JSON.stringify(err));
80 Migration.prototype.removeIndex = function (table, indexName) {
82 this.sequence.enqueue(function () {
83 return _this.queryInterface
84 .removeIndex(table, indexName)
86 logger.info('removeIndex %s %s', table, indexName);
88 .catch(function (err) {
89 logger.info(JSON.stringify(err));
94 Migration.prototype.query = function (sql) {
96 this.sequence.enqueue(function () {
97 return _this.queryInterface.sequelize
100 logger.info('query %s', sql);
102 .catch(function (err) {
103 logger.info(JSON.stringify(err));
108 Migration.prototype.removeColumn = function (table, column) {
110 this.sequence.enqueue(function () {
111 return _this.queryInterface
112 .removeColumn(table, column)
114 logger.info('Removed column %s from %s', column, table);
116 .catch(function (err) {
127 Migration.prototype.renameColumn = function (table, oldColumn, newColumn) {
129 this.sequence.enqueue(function () {
130 return _this.queryInterface
131 .renameColumn(table, oldColumn, newColumn)
133 logger.info('Renamed column from %s to %s on %s', oldColumn, newColumn, table);
135 .catch(function (err) {
146 Migration.prototype.final = function (resolve) {
147 this.sequence.enqueue(function () {
153 up: function (queryInterface, Sequelize) {
154 return new BPromise(function (resolve) {
156 var migration = new Migration(queryInterface);
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;');
191 migration.addColumn('tools_dispositions', 'WhatsappAccountId', {
192 type: Sequelize.INTEGER
195 migration.addColumn('tools_canned_answers', 'WhatsappAccountId', {
196 type: Sequelize.INTEGER
199 migration.addColumn('users', 'whatsappPause', {
200 type: Sequelize.BOOLEAN,
204 migration.addColumn('users', 'whatsappCapacity', {
205 type: Sequelize.BOOLEAN,
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');
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\'');
216 // START chat_interactions
217 migration.addColumn('chat_interactions', 'vidaooSessionId', {
221 // START chat_websites
222 migration.addColumn('chat_websites', 'vidaooEscalation', {
223 type: Sequelize.BOOLEAN,
227 migration.addColumn('chat_websites', 'vidaooApiKey', {
228 type: Sequelize.STRING
232 // START voice_recordings
233 migration.addColumn('voice_recordings', 'location', {
236 migration.addColumn('voice_recordings', 'transcribeName', {
237 type: Sequelize.STRING
239 migration.addColumn('voice_recordings', 'transcribeStatus', {
240 type: Sequelize.STRING,
243 migration.addColumn('voice_recordings', 'fileUri', {
246 migration.addColumn('voice_recordings', 'fileText', {
249 migration.addColumn('voice_recordings', 'failureReason', {
252 migration.addColumn('voice_recordings', 'sentiment', {
253 type: Sequelize.STRING
255 migration.addColumn('voice_recordings', 'sPositive', {
256 type: Sequelize.FLOAT
258 migration.addColumn('voice_recordings', 'sNegative', {
259 type: Sequelize.FLOAT
261 migration.addColumn('voice_recordings', 'sNeutral', {
262 type: Sequelize.FLOAT
264 migration.addColumn('voice_recordings', 'sMixed', {
265 type: Sequelize.FLOAT
267 migration.addColumn('voice_recordings', 'tempSentiment', {
268 type: Sequelize.BOOLEAN
270 // END voice_recordings
273 migration.addColumn('settings', 'transcribe', {
274 type: Sequelize.BOOLEAN
276 migration.addColumn('settings', 'automaticTranscribe', {
277 type: Sequelize.BOOLEAN
279 migration.addColumn('settings', 'transcribeAccountId', {
280 type: Sequelize.INTEGER
282 migration.addColumn('settings', 'transcribeRegion', {
283 type: Sequelize.STRING
285 migration.addColumn('settings', 'sentiment', {
286 type: Sequelize.BOOLEAN
288 migration.addColumn('settings', 'automaticSentiment', {
289 type: Sequelize.BOOLEAN
291 migration.addColumn('settings', 'sentimentAccountId', {
292 type: Sequelize.INTEGER
294 migration.addColumn('settings', 'sentimentRegion', {
295 type: Sequelize.STRING
297 migration.addColumn('settings', 'language', {
298 type: Sequelize.STRING
300 migration.addColumn('settings', 'bucket', {
301 type: Sequelize.STRING
306 migration.final(resolve);
311 down: function (queryInterface, Sequelize) {
312 // var migration = new Migration(queryInterface);