customer.sync.factory.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. angular.module('odoo')
  2. /**
  3. *
  4. */
  5. .factory('customersRemoteFactory', function (
  6. customersStorageFactory,
  7. odooFactory,
  8. sqlFactory,
  9. asyncLoopFactory
  10. ) {
  11. /**
  12. *
  13. */
  14. var pull = function (id, success, error) {
  15. if (id) {
  16. odooFactory.read('res.partner', [['id', '=', id]], function (response) {
  17. success(response);
  18. }, function (odooErr) {
  19. error(odooErr);
  20. });
  21. } else {
  22. odooFactory.read('res.partner', [['customer', '=', true]], 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. if (id) {
  38. pull(id, function (response) {
  39. if (response.length == 1) {
  40. var remoteData = response[0];
  41. var remoteDate = new Date(remoteData.__last_update);
  42. var localDate = new Date(data.modifiedDate);
  43. delete data.modifiedDate;
  44. if (localDate > remoteDate) {
  45. odooFactory.write('res.partner', id, data, function (response) {
  46. success(response);
  47. }, function (odooErr) {
  48. error(odooErr);
  49. });
  50. } else {
  51. success(response);
  52. }
  53. } else {
  54. success(response);
  55. }
  56. }, function (pullErr) {
  57. error(pullErr);
  58. });
  59. } else {
  60. delete data.modifiedDate;
  61. odooFactory.create('res.partner', data, function (response) {
  62. success(response);
  63. }, function (odooErr) {
  64. error(odooErr);
  65. });
  66. }
  67. }
  68. /**
  69. *
  70. */
  71. var destroy = function (id, success, error) {
  72. odooFactory.unlink('res.partner', id, function (response) {
  73. success(response);
  74. }, function (odooInteroperabilityFactoryErr) {
  75. error(odooInteroperabilityFactoryErr);
  76. });
  77. }
  78. /**
  79. *
  80. */
  81. var get = function (constraint, success, error) {
  82. sqlFactory.selectByConstraint('partner', constraint, function (customers) {
  83. success(customers);
  84. }, function (err) {
  85. error(err);
  86. });
  87. }
  88. /**
  89. *
  90. */
  91. var syncNewData = function (success, error) {
  92. get('remote_id = 0 AND customer = 1', function (newCustomers) {
  93. asyncLoopFactory(newCustomers.length, function (loop) {
  94. var data = newCustomers.item(loop.iteration());
  95. push(null, data, function (response) {
  96. loop.next();
  97. }, function (pushErr) {
  98. loop.break();
  99. });
  100. // end loop
  101. }, function () {
  102. success(newCustomers);
  103. });
  104. }, function (getErr) {
  105. error(getErr);
  106. });
  107. }
  108. /**
  109. *
  110. */
  111. var syncUpdatedData = function (success, error) {
  112. get('remote_id != 0 AND customer = 1 AND modified = 1', function (updatedCustomers) {
  113. asyncLoopFactory(updatedCustomers.length, function (loop) {
  114. var data = updatedCustomers.item(loop.iteration());
  115. push(data.remote_id, data, function (response) {
  116. loop.next();
  117. }, function () {
  118. loop.next();
  119. });
  120. // End loop
  121. }, function () {
  122. success(updatedCustomers);
  123. });
  124. }, function (getErr) {
  125. error(getErr);
  126. });
  127. }
  128. /**
  129. *
  130. */
  131. var syncDeletedData = function (success, error) {
  132. get('remote_id != 0 AND customer = 1 AND modified = 2', function (deletedCustomers) {
  133. asyncLoopFactory(deletedCustomers.length, function (loop) {
  134. var id = deletedCustomers.item(loop.iteration()).remote_id;
  135. destroy(id, function (response) {
  136. loop.next();
  137. }, function (destroyErr) {
  138. loop.next();
  139. });
  140. // end loop
  141. }, function () {
  142. success(deletedCustomers);
  143. });
  144. }, function (getErr) {
  145. error(getErr);
  146. });
  147. }
  148. /**
  149. *
  150. */
  151. var downloadSyncData = function (success, error) {
  152. pull(null, function (customers) {
  153. customersStorageFactory.removeAll(function () {
  154. asyncLoopFactory(customers.length, function (loop) {
  155. var data = customers[loop.iteration()];
  156. data.remote_id = data.id;
  157. delete data.id;
  158. customersStorageFactory.save(data, function (customerId) {
  159. loop.next();
  160. }, function (saveErr) {
  161. loop.next();
  162. });
  163. // End loop
  164. }, function () {
  165. success(customers);
  166. });
  167. }, function (removeAllErr) {
  168. error(removeAllErr);
  169. });
  170. }, function (pullErr) {
  171. error(pullErr);
  172. });
  173. }
  174. /**
  175. *
  176. */
  177. var sync = function (success, error) {
  178. syncNewData(function () {
  179. syncUpdatedData(function () {
  180. syncDeletedData(function () {
  181. downloadSyncData(function (data) {
  182. success(data);
  183. }, function (downloadErr) {
  184. error(downloadErr);
  185. });
  186. }, function (syncErr) {
  187. error(syncErr);
  188. });
  189. }, function (syncErr) {
  190. error(syncErr);
  191. });
  192. }, function (syncErr) {
  193. error(syncErr);
  194. });
  195. }
  196. return {
  197. pull: pull,
  198. push: push,
  199. destroy: destroy,
  200. sync: sync
  201. }
  202. });