lead.sync.factory.js 8.0 KB

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