angular.module('odoo') .factory('leadsRemoteFactory', function (config, odoo, sqlFactory, asyncLoop) { // Retrieve leads data from server var 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); }); } // Update leads remote data var update = function (id, data, success, error) { config(function (configuration) { odoo.write('crm.lead', id, data, configuration, function (response) { success(response); }, function (odooWriteErr) { error(odooWriteErr); }); }, function (configErr) { error(configErr); }); } // Create leads on remote data var send = function (data, success, error) { config(function (configuration) {retrieve(function (leads) { console.log(leads); success(); }, function (retrieveError) { error(retrieveError); }); odoo.create('crm.lead', data, configuration, function (response) { success(response); }, function (odooCreateErr) { error(odooCreateErr); }); }, function (configErr) { error(configErr); }); } // Select leads from local storage var select = function (contraint, success, error) { sqlFactory.selectByConstraint('lead', contraint, function (leads) { success(leads); }, function (err) { error(err); }); } // Sync leads data between local and remote storage var sync = function (success, error) { select('remote_id = 0', function (newLeads) { asyncLoop(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; send(data, function (response) { console.log(response); loop.next(); }, function (sendErr) { loop.break(); }); }, function () { success(); }); }, function (selectErr) { error(selectErr); }); } return { retrieve: retrieve, update: update, send: send, sync: sync } });