lead.sync.factory.js 7.2 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', [['active', '=', true]], 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. console.log('No Actualizar');
  52. success(response);
  53. }
  54. } else {
  55. success(response);
  56. }
  57. }, function (pullErr) {
  58. error(pullErr);
  59. });
  60. } else {
  61. delete data.modifiedDate;
  62. odooInteroperabilityFactory.create('crm.lead', data, configuration, function (response) {
  63. success(response);
  64. }, function (odooErr) {
  65. error(odooErr);
  66. });
  67. }
  68. }, function (configErr) {
  69. error(configErr);
  70. });
  71. }
  72. // Destroy server lead data
  73. var destroy = function (id, success, error) {
  74. configFactory(function (configuration) {
  75. odooInteroperabilityFactory.unlink('crm.lead', id, configuration, function (response) {
  76. success(response);
  77. }, function (odooErr) {
  78. error(odooErr);
  79. });
  80. }, function (configErr) {
  81. error(configErr);
  82. });
  83. }
  84. // Get local lead data
  85. var get = function (contraint, success, error) {
  86. sqlFactory.selectByConstraint('lead', contraint, function (leads) {
  87. success(leads);
  88. }, function (err) {
  89. error(err);
  90. });
  91. }
  92. // Send all new data from local to remote server
  93. var syncNewData = function (success, error) {
  94. get("remote_id = 0 AND type = 'lead'", function (newLeads) {
  95. asyncLoopFactory(newLeads.length, function (loop) {
  96. var data = newLeads.item(loop.iteration());
  97. push(null, data, function (response) {
  98. loop.next();
  99. }, function (pushErr) {
  100. loop.next();
  101. });
  102. // End loop
  103. }, function () {
  104. success(newLeads);
  105. });
  106. }, function (getErr) {
  107. error(getErr);
  108. });
  109. }
  110. // Update all data from server
  111. var syncUpdatedData = function (success, error) {
  112. get("remote_id != 0 AND modified = 1 AND type = 'lead'", function (updatedLeads) {
  113. asyncLoopFactory(updatedLeads.length, function (loop) {
  114. var data = updatedLeads.item(loop.iteration());
  115. push(data.remote_id, data, function (response) {
  116. loop.next();
  117. }, function (pushErr) {
  118. loop.next();
  119. });
  120. // End loop
  121. }, function () {
  122. success(updatedLeads);
  123. });
  124. }, function(getErr) {
  125. error(getErr);
  126. });
  127. }
  128. // Delete all data
  129. var syncDeletedData = function (success, error) {
  130. get("remote_id != 0 AND modified = 2 AND type = 'lead'", function (deletedLeads) {
  131. asyncLoopFactory(deletedLeads.length, function (loop) {
  132. var id = deletedLeads.item(loop.iteration()).remote_id;
  133. destroy(id, function (response) {
  134. success(response);
  135. }, function (destroyErr) {
  136. error(destroyErr);
  137. });
  138. // End loop
  139. }, function () {
  140. success(deletedLeads);
  141. });
  142. }, function (getErr) {
  143. error(getErr);
  144. });
  145. }
  146. // Download all sync data from server
  147. var downloadSyncData = function (success, error) {
  148. pull(null, function (leads) {
  149. leadsStorageFactory.removeAll(function () {
  150. asyncLoopFactory(leads.length, function (loop) {
  151. var data = leads[loop.iteration()];
  152. data.remote_id = data.id;
  153. delete data.id;
  154. leadsStorageFactory.save(data, function (leadId) {
  155. loop.next();
  156. }, function (saveErr) {
  157. loop.next();
  158. });
  159. // End loop
  160. }, function () {
  161. success(leads);
  162. });
  163. }, function (removeAllErr) {
  164. error(removeAllErr);
  165. });
  166. }, function (pushErr) {
  167. error(pushErr);
  168. });
  169. }
  170. // Sync all leads data between local and remote data
  171. var sync = function (success, error) {
  172. syncNewData(function () {
  173. // console.log("New data ok");
  174. syncUpdatedData(function () {
  175. // console.log("Updated data ok");
  176. syncDeletedData(function () {
  177. // console.log("Deleted data ok");
  178. downloadSyncData(function (data) {
  179. // console.log("Download data ok");
  180. success(data);
  181. }, function (downloadErr) {
  182. error(downloadErr);
  183. });
  184. }, function (syncErr) {
  185. error(syncErr);
  186. });
  187. }, function (syncErr) {
  188. error(syncErr);
  189. });
  190. }, function (syncErr) {
  191. error(syncErr);
  192. });
  193. }
  194. return {
  195. pull: pull,
  196. push: push,
  197. destroy: destroy,
  198. sync: sync
  199. }
  200. });