From c23a7e0c99ed93d4663092005a47c489989a71a4 Mon Sep 17 00:00:00 2001 From: Razvan Botea Date: Thu, 28 Apr 2016 14:07:54 +0300 Subject: [PATCH] operation on app models now use the right functions if the type is user or context --- lib/Model.js | 68 +++++++++++++++++++++------ lib/User.js | 6 +-- lib/database/elasticsearch_adapter.js | 10 +++- 3 files changed, 65 insertions(+), 19 deletions(-) diff --git a/lib/Model.js b/lib/Model.js index 6870ee5..0d77dc3 100644 --- a/lib/Model.js +++ b/lib/Model.js @@ -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'); @@ -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); @@ -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; @@ -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); }; @@ -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) { diff --git a/lib/User.js b/lib/User.js index 64f3e87..3942e1a 100644 --- a/lib/User.js +++ b/lib/User.js @@ -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; @@ -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); }); diff --git a/lib/database/elasticsearch_adapter.js b/lib/database/elasticsearch_adapter.js index 68d117e..32e706c 100644 --- a/lib/database/elasticsearch_adapter.js +++ b/lib/database/elasticsearch_adapter.js @@ -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) {