lead.sync.factory.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. angular.module('odoo')
  2. /**
  3. *
  4. */
  5. .factory('leadsRemoteFactory', function (
  6. leadsStorageFactory,
  7. odooFactory,
  8. sqlFactory,
  9. asyncLoopFactory
  10. ) {
  11. /**
  12. *
  13. */
  14. var pull = function (id, success, error) {
  15. if (id) {
  16. odooFactory.read('crm.lead', [['id', '=', id]], function (response) {
  17. success(response);
  18. }, function (odooErr) {
  19. error(odooErr)
  20. });
  21. } else {
  22. odooFactory.read('crm.lead', [['type', '=', 'lead']], function (response) {
  23. success(response);
  24. }, function (odooErr) {
  25. error(odooErr);
  26. });
  27. }
  28. }
  29. /**
  30. *
  31. */
  32. var push = function (id, data, success, error) {
  33. // Avoid odoo server warning message
  34. delete data.id;
  35. delete data.remote_id;
  36. delete data.modified;
  37. delete data.priority;
  38. if (id) {
  39. pull(id, function (response) {
  40. if (response.length == 1) {
  41. var remoteData = response[0];
  42. var remoteDate = new Date(remoteData.__last_update);
  43. var localDate = new Date(data.modifiedDate);
  44. delete data.modifiedDate;
  45. if (localDate > remoteDate) {
  46. odooFactory.write('crm.lead', id, data, function (response) {
  47. console.log(response);
  48. success(response);
  49. }, function (odooErr) {
  50. console.log(odooErr);
  51. error(odooErr);
  52. });
  53. } else {
  54. success(response);
  55. }
  56. } else {
  57. success(response);
  58. }
  59. }, function (pullErr) {
  60. error(pullErr);
  61. });
  62. } else {
  63. delete data.modifiedDate;
  64. odooFactory.create('crm.lead', data, function (response) {
  65. success(response);
  66. }, function (odooErr) {
  67. error(odooErr);
  68. });
  69. }
  70. }
  71. /**
  72. *
  73. */
  74. var destroy = function (id, success, error) {
  75. odooFactory.unlink('crm.lead', id, function (response) {
  76. success(response);
  77. }, function (odooErr) {
  78. error(odooErr);
  79. });
  80. }
  81. /**
  82. *
  83. */
  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. /**
  92. *
  93. */
  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. /**
  112. *
  113. */
  114. var syncUpdatedData = function (success, error) {
  115. get("remote_id != 0 AND modified = 1", function (updatedLeads) {
  116. asyncLoopFactory(updatedLeads.length, function (loop) {
  117. var data = updatedLeads.item(loop.iteration());
  118. console.log(data);
  119. push(data.remote_id, data, function (response) {
  120. loop.next();
  121. }, function (pushErr) {
  122. loop.next();
  123. });
  124. // end loop
  125. }, function () {
  126. success(updatedLeads);
  127. });
  128. }, function (getErr) {
  129. error(getErr);
  130. });
  131. }
  132. /**
  133. *
  134. */
  135. var syncDeletedData = function (success, error) {
  136. get('remote_id != 0 AND modified = 2', function (deletedLeads) {
  137. asyncLoopFactory(deletedLeads.length, function (loop) {
  138. var id = deletedLeads.item(loop.iteration()).remote_id;
  139. destroy(id, function (response) {
  140. loop.next();
  141. }, function (destroyErr) {
  142. loop.next();
  143. });
  144. // end loop
  145. }, function () {
  146. success(deletedLeads);
  147. });
  148. }, function (getErr) {
  149. error(getErr);
  150. });
  151. }
  152. /**
  153. *
  154. */
  155. var downloadSyncData = function (success, error) {
  156. pull(null, function (leads) {
  157. leadsStorageFactory.removeAll(function () {
  158. asyncLoopFactory(leads.length, function (loop) {
  159. var data = leads[loop.iteration()];
  160. data.remote_id = data.id;
  161. delete data.id;
  162. leadsStorageFactory.save(data, function (leadId) {
  163. loop.next();
  164. }, function (saveErr) {
  165. loop.next();
  166. });
  167. // end loop
  168. }, function () {
  169. success(leads);
  170. });
  171. }, function (removeAllErr) {
  172. error(removeAllErr);
  173. });
  174. }, function (pushErr) {
  175. error(pushErr);
  176. });
  177. }
  178. /**
  179. *
  180. */
  181. var sync = function (success, error) {
  182. syncNewData(function () {
  183. console.log("leads --> new data ok");
  184. syncUpdatedData(function () {
  185. console.log("leads --> updated data ok");
  186. syncDeletedData(function () {
  187. console.log("leads -> deleted data ok");
  188. downloadSyncData(function (data) {
  189. console.log("leads -> download data ok");
  190. success(data);
  191. }, function (downloadErr) {
  192. error(downloadErr);
  193. });
  194. }, function (syncErr) {
  195. error(syncErr);
  196. });
  197. }, function (syncErr) {
  198. error(syncErr);
  199. });
  200. }, function (syncErr) {
  201. error(syncErr);
  202. });
  203. }
  204. return {
  205. pull: pull,
  206. push: push,
  207. destroy: destroy,
  208. sync: sync
  209. }
  210. });