customer.sync.factory.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 getAll = function (success, error) {
  95. sqlFactory.selectByConstraint('partner', 'customer = 1 AND modified != 2', function (customers) {
  96. var data = [];
  97. for (var i = 0; i < customers.length; i++) {
  98. data.push(customers.item(i));
  99. }
  100. success(data);
  101. }, function (err) {
  102. error(err);
  103. });
  104. }
  105. /**
  106. *
  107. */
  108. var syncNewData = function (success, error) {
  109. get('remote_id = 0 AND customer = 1', function (newCustomers) {
  110. asyncLoopFactory(newCustomers.length, function (loop) {
  111. var data = newCustomers.item(loop.iteration());
  112. push(null, data, function (response) {
  113. loop.next();
  114. }, function (pushErr) {
  115. loop.break();
  116. });
  117. // end loop
  118. }, function () {
  119. success(newCustomers);
  120. });
  121. }, function (getErr) {
  122. error(getErr);
  123. });
  124. }
  125. /**
  126. *
  127. */
  128. var syncUpdatedData = function (success, error) {
  129. get('remote_id != 0 AND customer = 1 AND modified = 1', function (updatedCustomers) {
  130. asyncLoopFactory(updatedCustomers.length, function (loop) {
  131. var data = updatedCustomers.item(loop.iteration());
  132. push(data.remote_id, data, function (response) {
  133. loop.next();
  134. }, function () {
  135. loop.next();
  136. });
  137. // end loop
  138. }, function () {
  139. success(updatedCustomers);
  140. });
  141. }, function (getErr) {
  142. error(getErr);
  143. });
  144. }
  145. /**
  146. *
  147. */
  148. var syncDeletedData = function (success, error) {
  149. get('remote_id != 0 AND customer = 1 AND modified = 2', function (deletedCustomers) {
  150. asyncLoopFactory(deletedCustomers.length, function (loop) {
  151. var id = deletedCustomers.item(loop.iteration()).remote_id;
  152. destroy(id, function (response) {
  153. loop.next();
  154. }, function (destroyErr) {
  155. loop.next();
  156. });
  157. // end loop
  158. }, function () {
  159. success(deletedCustomers);
  160. });
  161. }, function (getErr) {
  162. error(getErr);
  163. });
  164. }
  165. /**
  166. *
  167. */
  168. var downloadSyncData = function (success, error) {
  169. pull(null, function (customers) {
  170. customersStorageFactory.removeAll(function () {
  171. asyncLoopFactory(customers.length, function (loop) {
  172. var data = customers[loop.iteration()];
  173. data.remote_id = data.id;
  174. delete data.id;
  175. customersStorageFactory.save(data, function (customerId) {
  176. loop.next();
  177. }, function (saveErr) {
  178. loop.next();
  179. });
  180. // end loop
  181. }, function () {
  182. success(customers);
  183. });
  184. }, function (removeAllErr) {
  185. error(removeAllErr);
  186. });
  187. }, function (pullErr) {
  188. error(pullErr);
  189. });
  190. }
  191. /**
  192. *
  193. */
  194. var sync = function (success, error) {
  195. syncNewData(function () {
  196. console.log("customers --> new data ok");
  197. syncUpdatedData(function () {
  198. console.log("customers --> updated data ok");
  199. syncDeletedData(function () {
  200. console.log("customers -> deleted data ok");
  201. downloadSyncData(function (data) {
  202. console.log("customers -> download data ok");
  203. success(data);
  204. }, function (downloadErr) {
  205. error(downloadErr);
  206. });
  207. }, function (syncErr) {
  208. error(syncErr);
  209. });
  210. }, function (syncErr) {
  211. error(syncErr);
  212. });
  213. }, function (syncErr) {
  214. error(syncErr);
  215. });
  216. }
  217. return {
  218. pull: pull,
  219. push: push,
  220. destroy: destroy,
  221. getAll: getAll,
  222. sync: sync
  223. }
  224. });