|
@@ -1,76 +1,115 @@
|
|
|
angular.module('odoo')
|
|
|
|
|
|
-.factory('leadsRemoteFactory', function (config, odoo, sqlFactory, leadsStorageFactory, asyncLoop) {
|
|
|
- var server = {};
|
|
|
- var local = {};
|
|
|
-
|
|
|
- // Retrieve leads data from server
|
|
|
- server.retrieve = function (success, error) {
|
|
|
- config(function (configuration) {
|
|
|
- odoo.read('crm.lead', [['active', '=', true]], configuration, function (leads) {
|
|
|
- success(leads);
|
|
|
- }, function (odooErr) {
|
|
|
- error(odooErr);
|
|
|
- });
|
|
|
- }, function (configErr) {
|
|
|
- error(configErr);
|
|
|
- });
|
|
|
- }
|
|
|
+.factory('leadsRemoteFactory', function (
|
|
|
+ leadsStorageFactory,
|
|
|
+ odooInteroperabilityFactory,
|
|
|
+ configFactory,
|
|
|
+ sqlFactory,
|
|
|
+ asyncLoopFactory
|
|
|
+) {
|
|
|
+
|
|
|
+ // Pull server leads data
|
|
|
+ var pull = function (id, success, error) {
|
|
|
+ configFactory(function (configuration) {
|
|
|
+ if (id) {
|
|
|
+
|
|
|
+ odooInteroperabilityFactory.read('crm.lead', [['id', '=', id]], configuration, function (response) {
|
|
|
+ success(response);
|
|
|
+ }, function (odooErr) {
|
|
|
+ error(odooErr)
|
|
|
+ });
|
|
|
|
|
|
- // Retrieve lead by id
|
|
|
- server.retrieveById = function (id, success, error) {
|
|
|
- config(function (configuration) {
|
|
|
- odoo.read('crm.lead', [['id', '=', id]], configuration, function (response) {
|
|
|
- success(response);
|
|
|
- }, function (odooReadErr) {
|
|
|
- error(odooReadErr)
|
|
|
- });
|
|
|
- }, function (configErr) {
|
|
|
- error(configErr);
|
|
|
- });
|
|
|
- }
|
|
|
+ } else {
|
|
|
|
|
|
- // Update leads remote data
|
|
|
- server.update = function (id, data, success, error) {
|
|
|
- config(function (configuration) {
|
|
|
- odoo.write('crm.lead', id, data, configuration, function (response) {
|
|
|
- success(response);
|
|
|
- }, function (odooWriteErr) {
|
|
|
- error(odooWriteErr);
|
|
|
- });
|
|
|
+ odooInteroperabilityFactory.read('crm.lead', [['active', '=', true]], configuration, function (response) {
|
|
|
+ success(response);
|
|
|
+ }, function (odooErr) {
|
|
|
+ error(odooErr);
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
}, function (configErr) {
|
|
|
error(configErr);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- // Remove lead from server
|
|
|
- server.remove = function (id, success, error) {
|
|
|
- config(function (configuration) {
|
|
|
- odoo.unlink('crm.lead', id, configuration, function (response) {
|
|
|
- success(response);
|
|
|
- }, function (odooUnlinkErr) {
|
|
|
- error(odooUnlinkErr);
|
|
|
- });
|
|
|
+ // Push leads data to server
|
|
|
+ var push = function (id, data, success, error) {
|
|
|
+
|
|
|
+ // Avoid odoo server warning message
|
|
|
+ delete data.id;
|
|
|
+ delete data.remote_id;
|
|
|
+ delete data.modified;
|
|
|
+ delete data.priority;
|
|
|
+
|
|
|
+ configFactory(function (configuration) {
|
|
|
+
|
|
|
+ if (id) {
|
|
|
+
|
|
|
+ pull(id, function (response) {
|
|
|
+ if (response.length == 1) {
|
|
|
+ var remoteData = response[0];
|
|
|
+
|
|
|
+ var remoteDate = new Date(remoteData.__last_update);
|
|
|
+ var localDate = new Date(data.modifiedDate);
|
|
|
+
|
|
|
+ delete data.modifiedDate;
|
|
|
+
|
|
|
+ if (localDate > remoteDate) {
|
|
|
+
|
|
|
+ odooInteroperabilityFactory.write('crm.lead', id, data, configuration, function (response) {
|
|
|
+ success(response);
|
|
|
+ }, function (odooErr) {
|
|
|
+ error(odooErr);
|
|
|
+ });
|
|
|
+
|
|
|
+ } else {
|
|
|
+ console.log('No Actualizar');
|
|
|
+ success(response);
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ success(response);
|
|
|
+ }
|
|
|
+
|
|
|
+ }, function (pullErr) {
|
|
|
+ error(pullErr);
|
|
|
+ });
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ delete data.modifiedDate;
|
|
|
+
|
|
|
+ odooInteroperabilityFactory.create('crm.lead', data, configuration, function (response) {
|
|
|
+ success(response);
|
|
|
+ }, function (odooErr) {
|
|
|
+ error(odooErr);
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}, function (configErr) {
|
|
|
error(configErr);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- // Create leads on remote data
|
|
|
- server.send = function (data, success, error) {
|
|
|
- config(function (configuration) {
|
|
|
- odoo.create('crm.lead', data, configuration, function (response) {
|
|
|
+ // Destroy server lead data
|
|
|
+ var destroy = function (id, success, error) {
|
|
|
+ configFactory(function (configuration) {
|
|
|
+
|
|
|
+ odooInteroperabilityFactory.unlink('crm.lead', id, configuration, function (response) {
|
|
|
success(response);
|
|
|
- }, function (odooCreateErr) {
|
|
|
- error(odooCreateErr);
|
|
|
+ }, function (odooErr) {
|
|
|
+ error(odooErr);
|
|
|
});
|
|
|
+
|
|
|
}, function (configErr) {
|
|
|
error(configErr);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- // Select leads from local storage
|
|
|
- local.retrieve = function (contraint, success, error) {
|
|
|
+ // Get local lead data
|
|
|
+ var get = function (contraint, success, error) {
|
|
|
sqlFactory.selectByConstraint('lead', contraint, function (leads) {
|
|
|
success(leads);
|
|
|
}, function (err) {
|
|
@@ -78,138 +117,132 @@ angular.module('odoo')
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- // Sync leads data between local and remote storage
|
|
|
- var sync = function (success, error) {
|
|
|
- local.retrieve("remote_id = 0 AND type = 'lead'", function (newLeads) {
|
|
|
- console.log(newLeads);
|
|
|
- asyncLoop(newLeads.length, function (loop) {
|
|
|
+ // Send all new data from local to remote server
|
|
|
+ var syncNewData = function (success, error) {
|
|
|
+ get("remote_id = 0 AND type = 'lead'", function (newLeads) {
|
|
|
+ asyncLoopFactory(newLeads.length, function (loop) {
|
|
|
var data = newLeads.item(loop.iteration());
|
|
|
|
|
|
- // Avoid odoo server warning message
|
|
|
- delete data.id;
|
|
|
- delete data.remote_id;
|
|
|
- delete data.modified;
|
|
|
- delete data.modifiedDate;
|
|
|
- delete data.priority;
|
|
|
+ push(null, data, function (response) {
|
|
|
+ loop.next();
|
|
|
+ }, function (pushErr) {
|
|
|
+ loop.next();
|
|
|
+ });
|
|
|
+
|
|
|
+ // End loop
|
|
|
+ }, function () {
|
|
|
+ success(newLeads);
|
|
|
+ });
|
|
|
+ }, function (getErr) {
|
|
|
+ error(getErr);
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
- server.send(data, function (response) {
|
|
|
- console.log(response);
|
|
|
+ // Update all data from server
|
|
|
+ var syncUpdatedData = function (success, error) {
|
|
|
+ get("remote_id != 0 AND modified = 1 AND type = 'lead'", function (updatedLeads) {
|
|
|
+ asyncLoopFactory(updatedLeads.length, function (loop) {
|
|
|
+ var data = updatedLeads.item(loop.iteration());
|
|
|
+
|
|
|
+ push(data.remote_id, data, function (response) {
|
|
|
+ loop.next();
|
|
|
+ }, function (pushErr) {
|
|
|
loop.next();
|
|
|
- }, function (sendErr) {
|
|
|
- loop.break();
|
|
|
});
|
|
|
+
|
|
|
// End loop
|
|
|
}, function () {
|
|
|
- console.log('New data sended success');
|
|
|
-
|
|
|
- local.retrieve("remote_id != 0 AND modified = 1 AND type = 'lead'", function (modifiedLeads) {
|
|
|
- asyncLoop(modifiedLeads.length, function (loop) {
|
|
|
- var localData = modifiedLeads.item(loop.iteration());
|
|
|
-
|
|
|
- server.retrieveById(localData.remote_id, function (leads) {
|
|
|
- if (leads.length == 1) {
|
|
|
- var remoteData = leads[0];
|
|
|
-
|
|
|
- var remoteModifiedDate = new Date(remoteData.__last_update);
|
|
|
- var localModifiedDate = new Date(localData.modifiedDate);
|
|
|
-
|
|
|
-
|
|
|
- if (localModifiedDate > remoteModifiedDate) {
|
|
|
- var id = localData.remote_id;
|
|
|
-
|
|
|
- // Avoid odoo server warning message
|
|
|
- delete localData.id;
|
|
|
- delete localData.remote_id;
|
|
|
- delete localData.modified;
|
|
|
- delete localData.modifiedDate;
|
|
|
- delete localData.priority;
|
|
|
-
|
|
|
- server.update(id, localData, function (response) {
|
|
|
- loop.next();
|
|
|
- }, function (updateErr) {
|
|
|
- console.log(updateErr);
|
|
|
- loop.next();
|
|
|
- });
|
|
|
- } else {
|
|
|
- loop.next();
|
|
|
- }
|
|
|
- } {
|
|
|
- loop.next();
|
|
|
- }
|
|
|
- }, function (retrieveByIdErr) {
|
|
|
- console.log(retrieveByIdErr);
|
|
|
- loop.next();
|
|
|
- });
|
|
|
-
|
|
|
- // End loop
|
|
|
- }, function () {
|
|
|
- console.log('Modified data sended success');
|
|
|
-
|
|
|
- local.retrieve("remote_id != 0 AND modified = 2 AND type = 'lead'", function (deletedLeads) {
|
|
|
- asyncLoop(deletedLeads.length, function (loop) {
|
|
|
- var id = deletedLeads.item(loop.iteration()).remote_id;
|
|
|
-
|
|
|
- server.remove(id, function (response) {
|
|
|
- loop.next();
|
|
|
- }, function (removeErr) {
|
|
|
- console.log(removeErr);
|
|
|
- loop.next();
|
|
|
- });
|
|
|
-
|
|
|
- // End loop
|
|
|
- }, function () {
|
|
|
-
|
|
|
- console.log('Remote data deleted success');
|
|
|
- server.retrieve(function (updatedLeads) {
|
|
|
- console.log(updatedLeads);
|
|
|
-
|
|
|
- leadsStorageFactory.removeAll(function () {
|
|
|
- console.log('Removed All');
|
|
|
-
|
|
|
- asyncLoop(updatedLeads.length, function (loop) {
|
|
|
- var data = updatedLeads[loop.iteration()];
|
|
|
-
|
|
|
- // Set id for save on local database
|
|
|
- data.remote_id = data.id;
|
|
|
- delete data.id;
|
|
|
-
|
|
|
- leadsStorageFactory.save(data, function (leadId) {
|
|
|
- loop.next();
|
|
|
- }, function (saveLeadErr) {
|
|
|
- console.log(saveLeadErr);
|
|
|
- loop.next();
|
|
|
- });
|
|
|
-
|
|
|
- // End loop
|
|
|
- }, function () {
|
|
|
-
|
|
|
- console.log('Remote data downloaded success');
|
|
|
- success(updatedLeads);
|
|
|
- });
|
|
|
- }, function (removeLeadsErr) {
|
|
|
- error(removeLeadsErr);
|
|
|
- });
|
|
|
- }, function (retrieveErr) {
|
|
|
- error(retrieveErr);
|
|
|
- });
|
|
|
- });
|
|
|
- }, function (selectErr) {
|
|
|
- error(selectErr);
|
|
|
- });
|
|
|
+ success(updatedLeads);
|
|
|
+ });
|
|
|
+ }, function(getErr) {
|
|
|
+ error(getErr);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // Delete all data
|
|
|
+ var syncDeletedData = function (success, error) {
|
|
|
+ get("remote_id != 0 AND modified = 2 AND type = 'lead'", function (deletedLeads) {
|
|
|
+ asyncLoopFactory(deletedLeads.length, function (loop) {
|
|
|
+ var id = deletedLeads.item(loop.iteration()).remote_id;
|
|
|
+
|
|
|
+ destroy(id, function (response) {
|
|
|
+ success(response);
|
|
|
+ }, function (destroyErr) {
|
|
|
+ error(destroyErr);
|
|
|
+ });
|
|
|
+
|
|
|
+ // End loop
|
|
|
+ }, function () {
|
|
|
+ success(deletedLeads);
|
|
|
+ });
|
|
|
+ }, function (getErr) {
|
|
|
+ error(getErr);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // Download all sync data from server
|
|
|
+ var downloadSyncData = function (success, error) {
|
|
|
+ pull(null, function (leads) {
|
|
|
+
|
|
|
+ leadsStorageFactory.removeAll(function () {
|
|
|
+ asyncLoopFactory(leads.length, function (loop) {
|
|
|
+ var data = leads[loop.iteration()];
|
|
|
+
|
|
|
+ data.remote_id = data.id;
|
|
|
+ delete data.id;
|
|
|
+
|
|
|
+ leadsStorageFactory.save(data, function (leadId) {
|
|
|
+ loop.next();
|
|
|
+ }, function (saveErr) {
|
|
|
+ loop.next();
|
|
|
+ });
|
|
|
+
|
|
|
+ // End loop
|
|
|
+ }, function () {
|
|
|
+ success(leads);
|
|
|
+ });
|
|
|
+
|
|
|
+ }, function (removeAllErr) {
|
|
|
+ error(removeAllErr);
|
|
|
+ });
|
|
|
+
|
|
|
+ }, function (pushErr) {
|
|
|
+ error(pushErr);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // Sync all leads data between local and remote data
|
|
|
+ var sync = function (success, error) {
|
|
|
+ syncNewData(function () {
|
|
|
+
|
|
|
+ // console.log("New data ok");
|
|
|
+ syncUpdatedData(function () {
|
|
|
+
|
|
|
+ // console.log("Updated data ok");
|
|
|
+ syncDeletedData(function () {
|
|
|
+
|
|
|
+ // console.log("Deleted data ok");
|
|
|
+ downloadSyncData(function (data) {
|
|
|
+ // console.log("Download data ok");
|
|
|
+ success(data);
|
|
|
+ }, function (downloadErr) {
|
|
|
+ error(downloadErr);
|
|
|
});
|
|
|
- }, function (selectErr) {
|
|
|
- error(selectErr);
|
|
|
+ }, function (syncErr) {
|
|
|
+ error(syncErr);
|
|
|
});
|
|
|
+ }, function (syncErr) {
|
|
|
+ error(syncErr);
|
|
|
});
|
|
|
- }, function (selectErr) {
|
|
|
- error(selectErr);
|
|
|
+ }, function (syncErr) {
|
|
|
+ error(syncErr);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
- retrieve: server.retrieve,
|
|
|
- update: server.update,
|
|
|
- send: server.send,
|
|
|
+ pull: pull,
|
|
|
+ push: push,
|
|
|
+ destroy: destroy,
|
|
|
sync: sync
|
|
|
}
|
|
|
});
|