Skip to content

Commit

Permalink
operation on app models now use the right functions if the type is us…
Browse files Browse the repository at this point in the history
…er or context
  • Loading branch information
Razvan Botea committed Apr 28, 2016
1 parent 7d426b5 commit c23a7e0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
68 changes: 53 additions & 15 deletions lib/Model.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var Application = require('./Application');
var User = require('./User');
var Context = require('./Context');
var TelepatError = require('./TelepatError');
var async = require('async');
var utils = require('../utils/utils');
Expand All @@ -20,14 +22,25 @@ function Model(_id, callback) {
Model.delete = function(objects, callback) {
var childrenFilter = new FilterBuilder('or');

var appModels = {};

async.series([
function(callback1) {
Application.datasource.dataStorage.deleteObjects(objects, function(errs) {
async.forEachOfSeries(objects, function(modelName, id, c) {
if (modelName == 'context')
Context.delete(id, function() {});
else
appModels[id] = modelName;
c();
}, callback1);
},
function(callback1) {
Application.datasource.dataStorage.deleteObjects(appModels, function(errs) {
if (errs && errs.length >= 1) {
async.each(errs, function(error, c) {
if (error.status == 404) {
Application.logger.notice('Model "'+error.args[0]+'" with ID "'+error.args[1]+'" not found.');
delete objects[error.args[1]];
delete appModels[error.args[1]];
c();
} else {
c(error);
Expand All @@ -38,8 +51,8 @@ Model.delete = function(objects, callback) {
});
},
function(callback1) {
async.each(Object.keys(objects), function(id, c) {
var modelName = objects[id];
async.each(Object.keys(appModels), function(id, c) {
var modelName = appModels[id];
var filterObj = {};

filterObj[modelName+'_id'] = id;
Expand Down Expand Up @@ -201,10 +214,23 @@ Model.create = function(deltas, callback, returnedObjectsCb) {
dbItems.push(d.object);
});

if (dbItems.length > 0)
Application.datasource.dataStorage.createObjects(dbItems, callback1);
else
var appModelsObjects = [];

async.eachSeries(dbItems, function(item, c) {
if (item.type == 'user') {
User.create(item, item.application_id, function(){});
}
else if (item.type == 'context')
Context.create(item, function(){});
else
appModelsObjects.push(item);
c();
}, function() {
if (appModelsObjects.length > 0)
Application.datasource.dataStorage.createObjects(appModelsObjects, function(errs, result) {});

callback1();
});
}
], callback);
};
Expand All @@ -217,16 +243,28 @@ Model.create = function(deltas, callback, returnedObjectsCb) {
* @param cb
*/
Model.update = function(patches, callback) {
Application.datasource.dataStorage.updateObjects(patches, function(errs) {
if (errs && errs.length >= 1) {}
errs.forEach(function(error) {
if (error.status == 404)
Application.logger.notice(error.message);
else
Application.logger.error(error.toString());
var appModelsPatches = [];

async.eachSeries(patches, function(p, c) {
if (p.path.split('/')[0] == 'user')
User.update(p, function(){});
else if (p.path.split('/')[0] == 'context')
Context.update(p, function() {});
else
appModelsPatches.push(p);
c();
}, function() {
Application.datasource.dataStorage.updateObjects(appModelsPatches, function(errs) {
if (errs && errs.length >= 1) {}
errs.forEach(function(error) {
if (error.status == 404)
Application.logger.notice(error.message);
else
Application.logger.error(error.toString());
});
});
callback();
});
callback();
};

Model.getFilterFromChannel = function(channel) {
Expand Down
6 changes: 3 additions & 3 deletions lib/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ User.load = function() {
*/
User.create = function(props, appId, callback) {
var self = this;
props.id = guid.v4();
props.id = props.id || guid.v4();
props.application_id = appId;
props.created = Math.floor((new Date()).getTime()/1000);
props.modified = props.created;
Expand All @@ -66,10 +66,10 @@ User.create = function(props, appId, callback) {
type: 'user_metadata'
};

User(props.username, appId, function(err, result) {
User({username: props.username}, appId, function(err, result) {
if (err && err.status == 404) {
Application.datasource.dataStorage.createObjects([props, userMetadata], function(errs) {
if (errs.length) {
if (errs && errs.length) {
errs.forEach(function(error) {
Application.logger.error(error.message);
});
Expand Down
10 changes: 9 additions & 1 deletion lib/database/elasticsearch_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,15 @@ ElasticSearchDB.prototype.createObjects = function(objects, callback) {
index: this.config.index,
body: bulk,
refresh: builtinDetected
}, callback);
}, function(err, res) {
if (res.errors) {
res.items.forEach(function(error) {
Application.logger.error('Error creating '+error.index._type+': '+error.index.error);
});
}

callback(err, res);
});
};

ElasticSearchDB.prototype.updateObjects = function(patches, callback) {
Expand Down

0 comments on commit c23a7e0

Please sign in to comment.