lead.sync.factory.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. angular.module('odoo')
  2. .factory('leadsRemoteFactory', function (config, odoo, sqlFactory, leadsStorageFactory, asyncLoop) {
  3. var server = {};
  4. var local = {};
  5. // Retrieve leads data from server
  6. server.retrieve = function (success, error) {
  7. config(function (configuration) {
  8. odoo.read('crm.lead', [['active', '=', true]], configuration, function (leads) {
  9. success(leads);
  10. }, function (odooErr) {
  11. error(odooErr);
  12. });
  13. }, function (configErr) {
  14. error(configErr);
  15. });
  16. }
  17. // Retrieve lead by id
  18. server.retrieveById = function (id, success, error) {
  19. config(function (configuration) {
  20. odoo.read('crm.lead', [['id', '=', id]], configuration, function (response) {
  21. success(response);
  22. }, function (odooReadErr) {
  23. error(odooReadErr)
  24. });
  25. }, function (configErr) {
  26. error(configErr);
  27. });
  28. }
  29. // Update leads remote data
  30. server.update = function (id, data, success, error) {
  31. config(function (configuration) {
  32. odoo.write('crm.lead', id, data, configuration, function (response) {
  33. success(response);
  34. }, function (odooWriteErr) {
  35. error(odooWriteErr);
  36. });
  37. }, function (configErr) {
  38. error(configErr);
  39. });
  40. }
  41. // Remove lead from server
  42. server.remove = function (id, success, error) {
  43. config(function (configuration) {
  44. odoo.unlink('crm.lead', id, configuration, function (response) {
  45. success(response);
  46. }, function (odooUnlinkErr) {
  47. error(odooUnlinkErr);
  48. });
  49. }, function (configErr) {
  50. error(configErr);
  51. });
  52. }
  53. // Create leads on remote data
  54. server.send = function (data, success, error) {
  55. config(function (configuration) {
  56. odoo.create('crm.lead', data, configuration, function (response) {
  57. success(response);
  58. }, function (odooCreateErr) {
  59. error(odooCreateErr);
  60. });
  61. }, function (configErr) {
  62. error(configErr);
  63. });
  64. }
  65. // Select leads from local storage
  66. local.retrieve = function (contraint, success, error) {
  67. sqlFactory.selectByConstraint('lead', contraint, function (leads) {
  68. success(leads);
  69. }, function (err) {
  70. error(err);
  71. });
  72. }
  73. // Sync leads data between local and remote storage
  74. var sync = function (success, error) {
  75. local.retrieve('remote_id = 0', function (newLeads) {
  76. console.log(newLeads);
  77. asyncLoop(newLeads.length, function (loop) {
  78. var data = newLeads.item(loop.iteration());
  79. // Avoid odoo server warning message
  80. delete data.id;
  81. delete data.remote_id;
  82. delete data.modified;
  83. delete data.modifiedDate;
  84. delete data.priority;
  85. server.send(data, function (response) {
  86. console.log(response);
  87. loop.next();
  88. }, function (sendErr) {
  89. loop.break();
  90. });
  91. // End loop
  92. }, function () {
  93. console.log('New data sended success');
  94. local.retrieve('remote_id != 0 AND modified = 1', function (modifiedLeads) {
  95. asyncLoop(modifiedLeads.length, function (loop) {
  96. var localData = modifiedLeads.item(loop.iteration());
  97. server.retrieveById(localData.remote_id, function (leads) {
  98. if (leads.length == 1) {
  99. var remoteData = leads[0];
  100. var remoteModifiedDate = new Date(remoteData.__last_update);
  101. var localModifiedDate = new Date(localData.modifiedDate);
  102. if (localModifiedDate > remoteModifiedDate) {
  103. var id = localData.remote_id;
  104. // Avoid odoo server warning message
  105. delete localData.id;
  106. delete localData.remote_id;
  107. delete localData.modified;
  108. delete localData.modifiedDate;
  109. delete localData.priority;
  110. server.update(id, localData, function (response) {
  111. loop.next();
  112. }, function (updateErr) {
  113. console.log(updateErr);
  114. loop.next();
  115. });
  116. } else {
  117. loop.next();
  118. }
  119. } {
  120. loop.next();
  121. }
  122. }, function (retrieveByIdErr) {
  123. console.log(retrieveByIdErr);
  124. loop.next();
  125. });
  126. // End loop
  127. }, function () {
  128. console.log('Modified data sended success');
  129. local.retrieve('remote_id != 0 AND modified = 2', function (deletedLeads) {
  130. asyncLoop(deletedLeads.length, function (loop) {
  131. var id = deletedLeads.item(loop.iteration()).remote_id;
  132. server.remove(id, function (response) {
  133. loop.next();
  134. }, function (removeErr) {
  135. console.log(removeErr);
  136. loop.next();
  137. });
  138. // End loop
  139. }, function () {
  140. console.log('Remote data deleted success');
  141. server.retrieve(function (updatedLeads) {
  142. console.log(updatedLeads);
  143. leadsStorageFactory.removeAll(function () {
  144. console.log('Removed All');
  145. asyncLoop(updatedLeads.length, function (loop) {
  146. var data = updatedLeads[loop.iteration()];
  147. // Set id for save on local database
  148. data.remote_id = data.id;
  149. delete data.id;
  150. leadsStorageFactory.save(data, function (leadId) {
  151. loop.next();
  152. }, function (saveLeadErr) {
  153. console.log(saveLeadErr);
  154. loop.next();
  155. });
  156. // End loop
  157. }, function () {
  158. console.log('Remote data downloaded success');
  159. success(updatedLeads);
  160. });
  161. }, function (removeLeadsErr) {
  162. error(removeLeadsErr);
  163. });
  164. }, function (retrieveErr) {
  165. error(retrieveErr);
  166. });
  167. });
  168. }, function (selectErr) {
  169. error(selectErr);
  170. });
  171. });
  172. }, function (selectErr) {
  173. error(selectErr);
  174. });
  175. });
  176. }, function (selectErr) {
  177. error(selectErr);
  178. });
  179. }
  180. return {
  181. retrieve: server.retrieve,
  182. update: server.update,
  183. send: server.send,
  184. sync: sync
  185. }
  186. });