lead.sync.factory.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. angular.module('odoo')
  2. .factory('leadsRemoteFactory', function (
  3. leadsStorageFactory,
  4. odooInteroperabilityFactory,
  5. configFactory,
  6. sqlFactory,
  7. asyncLoopFactory
  8. ) {
  9. // Pull server leads data
  10. var pull = function (id, success, error) {
  11. configFactory(function (configuration) {
  12. if (id) {
  13. odooInteroperabilityFactory.read('crm.lead', [['id', '=', id]], configuration, function (response) {
  14. success(response);
  15. }, function (odooErr) {
  16. error(odooErr)
  17. });
  18. } else {
  19. odooInteroperabilityFactory.read('crm.lead', [['type', '=', 'lead']], configuration, function (response) {
  20. success(response);
  21. }, function (odooErr) {
  22. error(odooErr);
  23. });
  24. }
  25. }, function (configErr) {
  26. error(configErr);
  27. });
  28. }
  29. // Push leads data to server
  30. var push = function (id, data, success, error) {
  31. // Avoid odoo server warning message
  32. delete data.id;
  33. delete data.remote_id;
  34. delete data.modified;
  35. delete data.priority;
  36. configFactory(function (configuration) {
  37. if (id) {
  38. pull(id, function (response) {
  39. if (response.length == 1) {
  40. var remoteData = response[0];
  41. var remoteDate = new Date(remoteData.__last_update);
  42. var localDate = new Date(data.modifiedDate);
  43. delete data.modifiedDate;
  44. if (localDate > remoteDate) {
  45. odooInteroperabilityFactory.write('crm.lead', id, data, configuration, function (response) {
  46. success(response);
  47. }, function (odooErr) {
  48. error(odooErr);
  49. });
  50. } else {
  51. success(response);
  52. }
  53. } else {
  54. success(response);
  55. }
  56. }, function (pullErr) {
  57. error(pullErr);
  58. });
  59. } else {
  60. delete data.modifiedDate;
  61. odooInteroperabilityFactory.create('crm.lead', data, configuration, function (response) {
  62. success(response);
  63. }, function (odooErr) {
  64. error(odooErr);
  65. });
  66. }
  67. }, function (configErr) {
  68. error(configErr);
  69. });
  70. }
  71. // Destroy server lead data
  72. var destroy = function (id, success, error) {
  73. configFactory(function (configuration) {
  74. odooInteroperabilityFactory.unlink('crm.lead', id, configuration, function (response) {
  75. success(response);
  76. }, function (odooErr) {
  77. error(odooErr);
  78. });
  79. }, function (configErr) {
  80. error(configErr);
  81. });
  82. }
  83. // Get local lead data
  84. var get = function (contraint, success, error) {
  85. sqlFactory.selectByConstraint('lead', contraint, function (leads) {
  86. success(leads);
  87. }, function (err) {
  88. error(err);
  89. });
  90. }
  91. // Send all new data from local to remote server
  92. var syncNewData = function (success, error) {
  93. get('remote_id = 0', function (newLeads) {
  94. asyncLoopFactory(newLeads.length, function (loop) {
  95. var data = newLeads.item(loop.iteration());
  96. push(null, data, function (response) {
  97. loop.next();
  98. }, function (pushErr) {
  99. loop.next();
  100. });
  101. // End loop
  102. }, function () {
  103. success(newLeads);
  104. });
  105. }, function (getErr) {
  106. error(getErr);
  107. });
  108. }
  109. // Update all data from server
  110. var syncUpdatedData = function (success, error) {
  111. get("remote_id != 0 AND modified = 1", function (updatedLeads) {
  112. asyncLoopFactory(updatedLeads.length, function (loop) {
  113. var data = updatedLeads.item(loop.iteration());
  114. push(data.remote_id, data, function (response) {
  115. loop.next();
  116. }, function (pushErr) {
  117. loop.next();
  118. });
  119. // End loop
  120. }, function () {
  121. success(updatedLeads);
  122. });
  123. }, function (getErr) {
  124. error(getErr);
  125. });
  126. }
  127. // Delete all data
  128. var syncDeletedData = function (success, error) {
  129. get('remote_id != 0 AND modified = 2', function (deletedLeads) {
  130. asyncLoopFactory(deletedLeads.length, function (loop) {
  131. var id = deletedLeads.item(loop.iteration()).remote_id;
  132. destroy(id, function (response) {
  133. loop.next();
  134. }, function (destroyErr) {
  135. loop.next();
  136. });
  137. // End loop
  138. }, function () {
  139. success(deletedLeads);
  140. });
  141. }, function (getErr) {
  142. error(getErr);
  143. });
  144. }
  145. // Download all sync data from server
  146. var downloadSyncData = function (success, error) {
  147. pull(null, function (leads) {
  148. leadsStorageFactory.removeAll(function () {
  149. asyncLoopFactory(leads.length, function (loop) {
  150. var data = leads[loop.iteration()];
  151. data.remote_id = data.id;
  152. delete data.id;
  153. leadsStorageFactory.save(data, function (leadId) {
  154. loop.next();
  155. }, function (saveErr) {
  156. loop.next();
  157. });
  158. // End loop
  159. }, function () {
  160. success(leads);
  161. });
  162. }, function (removeAllErr) {
  163. error(removeAllErr);
  164. });
  165. }, function (pushErr) {
  166. error(pushErr);
  167. });
  168. }
  169. // Sync all leads data between local and remote data
  170. var sync = function (success, error) {
  171. syncNewData(function () {
  172. console.log("leads --> new data ok");
  173. syncUpdatedData(function () {
  174. console.log("leads --> updated data ok");
  175. syncDeletedData(function () {
  176. console.log("leads -> deleted data ok");
  177. downloadSyncData(function (data) {
  178. console.log("leads -> download data ok");
  179. success(data);
  180. }, function (downloadErr) {
  181. error(downloadErr);
  182. });
  183. }, function (syncErr) {
  184. error(syncErr);
  185. });
  186. }, function (syncErr) {
  187. error(syncErr);
  188. });
  189. }, function (syncErr) {
  190. error(syncErr);
  191. });
  192. }
  193. return {
  194. pull: pull,
  195. push: push,
  196. destroy: destroy,
  197. sync: sync
  198. }
  199. });