customer.sync.factory.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. angular.module('odoo')
  2. .factory('customersRemoteFactory', function (
  3. customersStorageFactory,
  4. odooFactory,
  5. configFactory,
  6. sqlFactory,
  7. asyncLoopFactory
  8. ) {
  9. // Pull server customers data
  10. var pull = function (id, success, error) {
  11. configFactory(function (configuration) {
  12. if (id) {
  13. odooFactory.read('res.partner', [['id', '=', id]], configuration, function (response) {
  14. success(response);
  15. }, function (odooErr) {
  16. error(odooErr);
  17. });
  18. } else {
  19. odooFactory.read('res.partner', [['customer', '=', true]], configuration, function (response) {
  20. success(response);
  21. }, function (odooErr) {
  22. error(odooErr)
  23. });
  24. }
  25. }, function (configErr) {
  26. error(configErr);
  27. });
  28. }
  29. // Push customers data to server
  30. var push = function (id, data, success, error) {
  31. // Avoid odoo server warning message
  32. delete data.id;
  33. delete data.remote_id;
  34. delete data.modified;
  35. configFactory(function (configuration) {
  36. if (id) {
  37. pull(id, function (response) {
  38. if (response.length == 1) {
  39. var remoteData = response[0];
  40. var remoteDate = new Date(remoteData.__last_update);
  41. var localDate = new Date(data.modifiedDate);
  42. delete data.modifiedDate;
  43. if (localDate > remoteDate) {
  44. odooFactory.write('res.partner', id, data, configuration, function (response) {
  45. success(response);
  46. }, function (odooErr) {
  47. error(odooErr);
  48. });
  49. } else {
  50. success(response);
  51. }
  52. } else {
  53. success(response);
  54. }
  55. }, function (pullErr) {
  56. error(pullErr);
  57. });
  58. } else {
  59. delete data.modifiedDate;
  60. odooFactory.create('res.partner', data, configuration, function (response) {
  61. success(response);
  62. }, function (odooErr) {
  63. error(odooErr);
  64. });
  65. }
  66. }, function (configFactoryErr) {
  67. error(configFactoryErr);
  68. });
  69. }
  70. // Destroy server customer data
  71. var destroy = function(id, success, error) {
  72. configFactory(function (configuration) {
  73. odooFactory.unlink('res.partner', id, configuration, function (response) {
  74. success(response);
  75. }, function (odooInteroperabilityFactoryErr) {
  76. error(odooInteroperabilityFactoryErr);
  77. });
  78. }, function (configFactoryErr) {
  79. error(configFactoryErr);
  80. });
  81. }
  82. // Get local customer data
  83. var get = function (constraint, success, error) {
  84. sqlFactory.selectByConstraint('partner', constraint, function (customers) {
  85. success(customers);
  86. }, function (err) {
  87. error(err);
  88. });
  89. }
  90. // Send all new data from local to remote server
  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. // Update all data from server
  109. var syncUpdatedData = function (success, error) {
  110. get('remote_id != 0 AND customer = 1 AND modified = 1', function (updatedCustomers) {
  111. asyncLoopFactory(updatedCustomers.length, function (loop) {
  112. var data = updatedCustomers.item(loop.iteration());
  113. push(data.remote_id, data, function (response) {
  114. loop.next();
  115. }, function () {
  116. loop.next();
  117. });
  118. // End loop
  119. }, function () {
  120. success(updatedCustomers);
  121. });
  122. }, function (getErr) {
  123. error(getErr);
  124. });
  125. }
  126. // Delete all data
  127. var syncDeletedData = function (success, error) {
  128. get('remote_id != 0 AND customer = 1 AND modified = 2', function (deletedCustomers) {
  129. asyncLoopFactory(deletedCustomers.length, function (loop) {
  130. var id = deletedCustomers.item(loop.iteration()).remote_id;
  131. destroy(id, function (response) {
  132. loop.next();
  133. }, function (destroyErr) {
  134. loop.next();
  135. });
  136. // End loop
  137. }, function () {
  138. success(deletedCustomers);
  139. });
  140. }, function(getErr) {
  141. error(getErr);
  142. });
  143. }
  144. // Download all sync data from server
  145. var downloadSyncData = function (success, error) {
  146. pull(null, function (customers) {
  147. console.log(customers);
  148. customersStorageFactory.removeAll(function () {
  149. asyncLoopFactory(customers.length, function (loop) {
  150. var data = customers[loop.iteration()];
  151. data.remote_id = data.id;
  152. delete data.id;
  153. customersStorageFactory.save(data, function (customerId) {
  154. loop.next();
  155. }, function (saveErr) {
  156. loop.next();
  157. });
  158. // End loop
  159. }, function () {
  160. success(customers);
  161. });
  162. }, function (removeAllErr) {
  163. error(removeAllErr);
  164. });
  165. }, function (pullErr) {
  166. error(pullErr);
  167. });
  168. }
  169. // Sync customers data between remote and local storage
  170. var sync = function (success, error) {
  171. syncNewData(function () {
  172. syncUpdatedData(function () {
  173. syncDeletedData(function () {
  174. downloadSyncData(function (data) {
  175. success(data);
  176. }, function (downloadErr) {
  177. error(downloadErr);
  178. });
  179. }, function (syncErr) {
  180. error(syncErr);
  181. });
  182. }, function (syncErr) {
  183. error(syncErr);
  184. });
  185. }, function (syncErr) {
  186. error(syncErr);
  187. });
  188. }
  189. return {
  190. pull: pull,
  191. push: push,
  192. destroy: destroy,
  193. sync: sync
  194. }
  195. });