lead.sync.factory.js 8.0 KB

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