2014-08-17 01:17:23 -05:00
|
|
|
var Promise = require('bluebird'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
models = require('../../models'),
|
|
|
|
utils = require('./utils'),
|
2014-04-27 18:28:50 -05:00
|
|
|
|
2014-07-31 14:53:55 -05:00
|
|
|
Importer000;
|
2014-07-27 17:22:00 -05:00
|
|
|
|
2013-09-17 10:55:55 -05:00
|
|
|
Importer000 = function () {
|
2014-08-09 14:16:54 -05:00
|
|
|
_.bindAll(this, 'doImport');
|
2013-09-17 10:55:55 -05:00
|
|
|
|
2013-09-24 05:46:30 -05:00
|
|
|
this.version = '000';
|
2013-09-17 10:55:55 -05:00
|
|
|
|
|
|
|
this.importFrom = {
|
2014-08-09 14:16:54 -05:00
|
|
|
'000': this.doImport,
|
|
|
|
'001': this.doImport,
|
|
|
|
'002': this.doImport,
|
|
|
|
'003': this.doImport
|
2013-09-17 10:55:55 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
Importer000.prototype.importData = function (data) {
|
|
|
|
return this.canImport(data)
|
|
|
|
.then(function (importerFunc) {
|
|
|
|
return importerFunc(data);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Importer000.prototype.canImport = function (data) {
|
|
|
|
if (data.meta && data.meta.version && this.importFrom[data.meta.version]) {
|
2014-08-17 01:17:23 -05:00
|
|
|
return Promise.resolve(this.importFrom[data.meta.version]);
|
2013-09-17 10:55:55 -05:00
|
|
|
}
|
|
|
|
|
2014-08-17 01:17:23 -05:00
|
|
|
return Promise.reject('Unsupported version of data: ' + data.meta.version);
|
2013-09-17 10:55:55 -05:00
|
|
|
};
|
|
|
|
|
2014-08-09 14:16:54 -05:00
|
|
|
Importer000.prototype.loadUsers = function () {
|
|
|
|
var users = {all: {}};
|
2013-09-17 10:55:55 -05:00
|
|
|
|
2014-11-26 19:28:29 -05:00
|
|
|
return models.User.findAll({include: ['roles']}).then(function (_users) {
|
2014-08-09 14:16:54 -05:00
|
|
|
_users.forEach(function (user) {
|
2014-09-09 23:06:24 -05:00
|
|
|
users.all[user.get('email')] = {realId: user.get('id')};
|
2014-08-09 14:16:54 -05:00
|
|
|
if (user.related('roles').toJSON()[0] && user.related('roles').toJSON()[0].name === 'Owner') {
|
|
|
|
users.owner = user.toJSON();
|
2014-07-31 14:53:55 -05:00
|
|
|
}
|
2014-08-09 14:16:54 -05:00
|
|
|
});
|
2014-07-31 14:53:55 -05:00
|
|
|
|
2014-08-09 14:16:54 -05:00
|
|
|
if (!users.owner) {
|
2014-08-17 01:17:23 -05:00
|
|
|
return Promise.reject('Unable to find an owner');
|
2013-11-20 15:36:02 -05:00
|
|
|
}
|
2013-09-17 10:55:55 -05:00
|
|
|
|
2014-08-17 01:17:23 -05:00
|
|
|
return users;
|
2014-08-09 14:16:54 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-09-09 23:06:24 -05:00
|
|
|
// Importer000.prototype.importerFunction = function (t) {
|
2014-08-09 14:16:54 -05:00
|
|
|
//
|
2014-09-09 23:06:24 -05:00
|
|
|
// };
|
2013-09-17 10:55:55 -05:00
|
|
|
|
2014-08-09 14:16:54 -05:00
|
|
|
Importer000.prototype.doUserImport = function (t, tableData, users, errors) {
|
|
|
|
var userOps = [],
|
|
|
|
imported = [];
|
2014-02-24 15:28:18 -05:00
|
|
|
|
2014-08-09 14:16:54 -05:00
|
|
|
if (tableData.users && tableData.users.length) {
|
|
|
|
if (tableData.roles_users && tableData.roles_users.length) {
|
|
|
|
tableData = utils.preProcessRolesUsers(tableData);
|
|
|
|
}
|
2013-11-20 15:36:02 -05:00
|
|
|
|
2014-08-09 14:16:54 -05:00
|
|
|
// Import users, deduplicating with already present users
|
|
|
|
userOps = utils.importUsers(tableData.users, users, t);
|
2014-05-05 08:51:21 -05:00
|
|
|
|
2014-08-17 01:17:23 -05:00
|
|
|
return Promise.settle(userOps).then(function (descriptors) {
|
2013-11-20 15:36:02 -05:00
|
|
|
descriptors.forEach(function (d) {
|
2014-08-17 01:17:23 -05:00
|
|
|
if (d.isRejected()) {
|
|
|
|
errors = errors.concat(d.reason());
|
2014-08-09 14:16:54 -05:00
|
|
|
} else {
|
2014-08-17 01:17:23 -05:00
|
|
|
imported.push(d.value().toJSON());
|
2013-11-20 15:36:02 -05:00
|
|
|
}
|
|
|
|
});
|
2014-05-05 08:51:21 -05:00
|
|
|
|
2014-08-09 14:16:54 -05:00
|
|
|
// If adding the users fails,
|
|
|
|
if (errors.length > 0) {
|
2014-05-05 08:51:21 -05:00
|
|
|
t.rollback(errors);
|
2014-08-09 14:16:54 -05:00
|
|
|
} else {
|
2014-08-17 01:17:23 -05:00
|
|
|
return imported;
|
2013-11-20 15:36:02 -05:00
|
|
|
}
|
|
|
|
});
|
2014-08-09 14:16:54 -05:00
|
|
|
}
|
|
|
|
|
2014-08-17 01:17:23 -05:00
|
|
|
return Promise.resolve({});
|
2014-08-09 14:16:54 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
Importer000.prototype.doImport = function (data) {
|
|
|
|
var self = this,
|
|
|
|
tableData = data.data,
|
|
|
|
imported = {},
|
2014-08-26 15:41:11 -05:00
|
|
|
errors = [],
|
2014-08-09 14:16:54 -05:00
|
|
|
users = {},
|
|
|
|
owner = {};
|
|
|
|
|
|
|
|
return self.loadUsers().then(function (result) {
|
|
|
|
owner = result.owner;
|
|
|
|
users = result.all;
|
|
|
|
|
|
|
|
return models.Base.transaction(function (t) {
|
|
|
|
// Step 1: Attempt to handle adding new users
|
|
|
|
self.doUserImport(t, tableData, users, errors).then(function (result) {
|
2014-08-26 15:41:11 -05:00
|
|
|
var importResults = [];
|
|
|
|
|
2014-08-09 14:16:54 -05:00
|
|
|
imported.users = result;
|
|
|
|
|
|
|
|
_.each(imported.users, function (user) {
|
|
|
|
users[user.email] = {realId: user.id};
|
|
|
|
});
|
|
|
|
|
|
|
|
// process user data - need to figure out what users we have available for assigning stuff to etc
|
|
|
|
try {
|
|
|
|
tableData = utils.processUsers(tableData, owner, users, ['posts', 'tags']);
|
|
|
|
} catch (error) {
|
|
|
|
return t.rollback([error]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do any pre-processing of relationships (we can't depend on ids)
|
|
|
|
if (tableData.posts_tags && tableData.posts && tableData.tags) {
|
|
|
|
tableData = utils.preProcessPostTags(tableData);
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:41:11 -05:00
|
|
|
// Import things in the right order
|
2014-08-09 14:16:54 -05:00
|
|
|
|
2014-08-26 15:41:11 -05:00
|
|
|
return utils.importTags(tableData.tags, t).then(function (results) {
|
|
|
|
if (results) {
|
|
|
|
importResults = importResults.concat(results);
|
|
|
|
}
|
2014-08-09 14:16:54 -05:00
|
|
|
|
2014-08-26 15:41:11 -05:00
|
|
|
return utils.importPosts(tableData.posts, t);
|
|
|
|
}).then(function (results) {
|
|
|
|
if (results) {
|
|
|
|
importResults = importResults.concat(results);
|
|
|
|
}
|
2014-08-09 14:16:54 -05:00
|
|
|
|
2014-08-26 15:41:11 -05:00
|
|
|
return utils.importSettings(tableData.settings, t);
|
|
|
|
}).then(function (results) {
|
|
|
|
if (results) {
|
|
|
|
importResults = importResults.concat(results);
|
|
|
|
}
|
|
|
|
}).then(function () {
|
|
|
|
importResults.forEach(function (p) {
|
|
|
|
if (p.isRejected()) {
|
|
|
|
errors = errors.concat(p.reason());
|
2014-08-09 14:16:54 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (errors.length === 0) {
|
|
|
|
t.commit();
|
|
|
|
} else {
|
|
|
|
t.rollback(errors);
|
|
|
|
}
|
|
|
|
});
|
2014-08-26 15:41:11 -05:00
|
|
|
|
|
|
|
/** do nothing with these tables, the data shouldn't have changed from the fixtures
|
|
|
|
* permissions
|
|
|
|
* roles
|
|
|
|
* permissions_roles
|
|
|
|
* permissions_users
|
|
|
|
*/
|
2014-08-09 14:16:54 -05:00
|
|
|
});
|
|
|
|
}).then(function () {
|
2014-09-09 23:06:24 -05:00
|
|
|
// TODO: could return statistics of imported items
|
2014-08-17 01:17:23 -05:00
|
|
|
return Promise.resolve();
|
2014-08-09 14:16:54 -05:00
|
|
|
});
|
2013-09-17 10:55:55 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Importer000: Importer000,
|
|
|
|
importData: function (data) {
|
|
|
|
return new Importer000().importData(data);
|
|
|
|
}
|
2014-02-26 21:44:09 -05:00
|
|
|
};
|