lead.sync.factory.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.modified_date);
  44. delete data.modified_date;
  45. if (localDate > remoteDate) {
  46. odooFactory.write('crm.lead', id, data, function (response) {
  47. console.log(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.modified_date;
  63. odooFactory.create('crm.lead', data, function (response) {
  64. success(response);
  65. }, function (odooErr) {
  66. error(odooErr);
  67. });
  68. }
  69. }
  70. /**
  71. *
  72. */
  73. var destroy = function (id, success, error) {
  74. odooFactory.unlink('crm.lead', id, function (response) {
  75. success(response);
  76. }, function (odooErr) {
  77. error(odooErr);
  78. });
  79. }
  80. /**
  81. *
  82. */
  83. var get = function (contraint, success, error) {
  84. sqlFactory.selectByConstraint('crm_lead', contraint, function (leads) {
  85. success(leads);
  86. }, function (err) {
  87. error(err);
  88. });
  89. }
  90. /**
  91. *
  92. */
  93. var syncNewData = function (success, error) {
  94. get('remote_id = 0', 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. console.log(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. push(data.remote_id, data, function (response) {
  119. loop.next();
  120. }, function (pushErr) {
  121. loop.next();
  122. });
  123. // end loop
  124. }, function () {
  125. success(updatedLeads);
  126. });
  127. }, function (getErr) {
  128. error(getErr);
  129. });
  130. }
  131. /**
  132. *
  133. */
  134. var syncDeletedData = function (success, error) {
  135. get('remote_id != 0 AND modified = 2', function (deletedLeads) {
  136. asyncLoopFactory(deletedLeads.length, function (loop) {
  137. var id = deletedLeads.item(loop.iteration()).remote_id;
  138. destroy(id, function (response) {
  139. loop.next();
  140. }, function (destroyErr) {
  141. loop.next();
  142. });
  143. // end loop
  144. }, function () {
  145. success(deletedLeads);
  146. });
  147. }, function (getErr) {
  148. error(getErr);
  149. });
  150. }
  151. /**
  152. *
  153. */
  154. var downloadSyncData = function (success, error) {
  155. pull(null, function (leads) {
  156. leadsStorageFactory.removeAll(function () {
  157. asyncLoopFactory(leads.length, function (loop) {
  158. var data = leads[loop.iteration()];
  159. data.remote_id = data.id;
  160. delete data.id;
  161. leadsStorageFactory.save(data, function (leadId) {
  162. loop.next();
  163. }, function (saveErr) {
  164. loop.next();
  165. });
  166. // end loop
  167. }, function () {
  168. success(leads);
  169. });
  170. }, function (removeAllErr) {
  171. error(removeAllErr);
  172. });
  173. }, function (pushErr) {
  174. error(pushErr);
  175. });
  176. }
  177. /**
  178. *
  179. */
  180. var sync = function (success, error) {
  181. syncNewData(function () {
  182. console.log("leads --> new data ok");
  183. syncUpdatedData(function () {
  184. console.log("leads --> updated data ok");
  185. syncDeletedData(function () {
  186. console.log("leads -> deleted data ok");
  187. downloadSyncData(function (data) {
  188. console.log("leads -> download data ok");
  189. success(data);
  190. }, function (downloadErr) {
  191. error(downloadErr);
  192. });
  193. }, function (syncErr) {
  194. error(syncErr);
  195. });
  196. }, function (syncErr) {
  197. error(syncErr);
  198. });
  199. }, function (syncErr) {
  200. error(syncErr);
  201. });
  202. }
  203. return {
  204. pull: pull,
  205. push: push,
  206. destroy: destroy,
  207. sync: sync
  208. }
  209. });