customer.sync.factory.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. angular.module('odoo')
  2. /**
  3. * -----------------------------------------------------------------------------
  4. * Description: Customers data sync manager
  5. * -----------------------------------------------------------------------------
  6. */
  7. .factory('customersRemoteFactory', function (
  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('res.partner', [['id', '=', id]], function (response) {
  19. success(response);
  20. }, function (odooErr) {
  21. error(odooErr);
  22. });
  23. } else {
  24. odooFactory.read('res.partner', [['customer', '=', true]], 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. // Avoid odoo server warning message
  36. delete data.id;
  37. delete data.remote_id;
  38. delete data.modified;
  39. delete data.notify_email;
  40. if (id) {
  41. pull(id, function (response) {
  42. if (response.length == 1) {
  43. var remoteData = response[0];
  44. var remoteDate = new Date(remoteData.__last_update);
  45. var localDate = new Date(data.modified_date);
  46. delete data.modified_date;
  47. if (localDate > remoteDate) {
  48. odooFactory.write('res.partner', id, data, function (response) {
  49. success(response);
  50. }, function (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.modified_date;
  64. odooFactory.create('res.partner', 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('res.partner', id, function (response) {
  76. success(response);
  77. }, function (unlinkErr) {
  78. error(unlinkErr);
  79. });
  80. }
  81. /**
  82. *
  83. */
  84. var get = function (constraint, success, error) {
  85. sqlFactory.selectByConstraint('partner', constraint, function (customers) {
  86. success(customers);
  87. }, function (err) {
  88. error(err);
  89. });
  90. }
  91. /**
  92. *
  93. */
  94. var syncNewData = function (success, error) {
  95. get('remote_id = 0 AND customer = 1', function (newCustomers) {
  96. asyncLoopFactory(newCustomers.length, function (loop) {
  97. var data = newCustomers.item(loop.iteration());
  98. push(null, data, function (response) {
  99. loop.next();
  100. }, function (pushErr) {
  101. loop.break();
  102. });
  103. // end loop
  104. }, function () {
  105. success(newCustomers);
  106. });
  107. }, function (getErr) {
  108. error(getErr);
  109. });
  110. }
  111. /**
  112. *
  113. */
  114. var syncUpdatedData = function (success, error) {
  115. get('remote_id != 0 AND customer = 1 AND modified = 1', function (updatedCustomers) {
  116. asyncLoopFactory(updatedCustomers.length, function (loop) {
  117. var data = updatedCustomers.item(loop.iteration());
  118. push(data.remote_id, data, function (response) {
  119. loop.next();
  120. }, function () {
  121. loop.next();
  122. });
  123. // end loop
  124. }, function () {
  125. success(updatedCustomers);
  126. });
  127. }, function (getErr) {
  128. error(getErr);
  129. });
  130. }
  131. /**
  132. *
  133. */
  134. var syncDeletedData = function (success, error) {
  135. get('remote_id != 0 AND customer = 1 AND modified = 2', function (deletedCustomers) {
  136. asyncLoopFactory(deletedCustomers.length, function (loop) {
  137. var id = deletedCustomers.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(deletedCustomers);
  146. });
  147. }, function (getErr) {
  148. error(getErr);
  149. });
  150. }
  151. /**
  152. *
  153. */
  154. var downloadSyncData = function (success, error) {
  155. pull(null, function (customers) {
  156. customersStorageFactory.removeAll(function () {
  157. asyncLoopFactory(customers.length, function (loop) {
  158. var data = customers[loop.iteration()];
  159. data.remote_id = data.id;
  160. delete data.id;
  161. customersStorageFactory.save(data, function (customerId) {
  162. loop.next();
  163. }, function (saveErr) {
  164. loop.next();
  165. });
  166. // end loop
  167. }, function () {
  168. success(customers);
  169. });
  170. }, function (removeAllErr) {
  171. error(removeAllErr);
  172. });
  173. }, function (pullErr) {
  174. error(pullErr);
  175. });
  176. }
  177. /**
  178. *
  179. */
  180. var sync = function (success, error) {
  181. syncNewData(function () {
  182. console.log("customers --> new data ok");
  183. syncUpdatedData(function () {
  184. console.log("customers --> updated data ok");
  185. syncDeletedData(function () {
  186. console.log("customers -> deleted data ok");
  187. downloadSyncData(function (data) {
  188. console.log("customers -> 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. });