lead.sync.factory.js 8.6 KB

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