opportunity.sync.factory.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. angular.module('odoo')
  2. /**
  3. * -----------------------------------------------------------------------------
  4. * Description: Local storage manager for crm stage data
  5. * -----------------------------------------------------------------------------
  6. */
  7. .factory('opportunitiesDataFactory', function (
  8. opportunitiesStorageFactory,
  9. leadsRemoteFactory,
  10. odooInteroperabilityFactory,
  11. configFactory,
  12. sqlFactory,
  13. asyncLoopFactory
  14. ) {
  15. /**
  16. *
  17. */
  18. var pull = function (id, success, error) {
  19. configFactory(function (configuration) {
  20. if (id) {
  21. odooInteroperabilityFactory.read('crm.lead', [['id', '=', id]], configuration, function (response) {
  22. success(response);
  23. }, function (odooErr) {
  24. error(odooErr);
  25. });
  26. } else {
  27. odooInteroperabilityFactory.read('crm.lead', [['type', '=', 'opportunity']], configuration, function (response) {
  28. success(response);
  29. }, function (odooErr) {
  30. error(odooErr);
  31. });
  32. }
  33. }, function(configErr) {
  34. error(configErr);
  35. });
  36. }
  37. /**
  38. *
  39. */
  40. var push = function (id, data, success, error) {
  41. leadsRemoteFactory.push(id, data, function (response) {
  42. success(response);
  43. }, function (err) {
  44. error(err);
  45. });
  46. }
  47. /**
  48. *
  49. */
  50. var destroy = function (id, success, error) {
  51. leadsRemoteFactory.destroy(id, function (response) {
  52. success(response);
  53. }, function (err) {
  54. error(err);
  55. });
  56. }
  57. /**
  58. *
  59. */
  60. var get = function (constraint, success, error) {
  61. sqlFactory.selectByConstraint('lead', constraint, function (leads) {
  62. success(leads);
  63. }, function (err) {
  64. error(err);
  65. });
  66. }
  67. /**
  68. *
  69. */
  70. var getAll = function (success, error) {
  71. get("type = 'opportunity' AND modified != 2", function (opportunities) {
  72. var data = [];
  73. for (var i = 0; i < opportunities.length; i++) {
  74. data.push(opportunities.item(i));
  75. }
  76. success(data);
  77. }, function (selectErr) {
  78. error(selectErr);
  79. });
  80. }
  81. /**
  82. *
  83. */
  84. var syncNewData = function (success, error) {
  85. get("remote_id = 0 AND type = 'opportunity'", function (newOpportunities) {
  86. asyncLoopFactory(newOpportunities.length, function (loop) {
  87. var data = newOpportunities.item(loop.iteration());
  88. push(null, data, function (response) {
  89. loop.next();
  90. }, function (pushErr) {
  91. loop.next();
  92. });
  93. // end loop
  94. }, function () {
  95. success(newOpportunities);
  96. });
  97. }, function (getErr) {
  98. error(getErr);
  99. });
  100. }
  101. /**
  102. *
  103. */
  104. var syncUpdatedData = function (success, error) {
  105. get("remote_id != 0 AND modified = 1 AND type = 'opportunity'", function (updatedOpportunities) {
  106. asyncLoopFactory(updatedOpportunities.length, function (loop) {
  107. var data = updatedOpportunities.item(loop.iteration());
  108. push(data.remote_id, data, function (response) {
  109. loop.next();
  110. }, function (pushErr) {
  111. loop.next();
  112. });
  113. // end loop
  114. }, function () {
  115. success(updatedOpportunities);
  116. });
  117. }, function (getErr) {
  118. error(getErr);
  119. });
  120. }
  121. var syncDeletedData = function (success, error) {
  122. get("remote_id != 0 AND modified = 2 AND type = 'opportunity'", function (deletedOpportunities) {
  123. asyncLoopFactory(deletedOpportunities.length, function (loop) {
  124. var id = deletedOpportunities.item(loop.iteration()).remote_id;
  125. destroy(id, function (response) {
  126. loop.next();
  127. }, function (destroyErr) {
  128. loop.next();
  129. });
  130. // end loop
  131. }, function () {
  132. success(deletedOpportunities);
  133. });
  134. }, function (getErr) {
  135. error(getErr);
  136. });
  137. }
  138. /**
  139. *
  140. */
  141. var downloadSyncData = function (success, error) {
  142. pull(null, function (opportunities) {
  143. opportunitiesStorageFactory.removeAll(function () {
  144. asyncLoopFactory(opportunities.length, function (loop) {
  145. var data = opportunities[loop.iteration()];
  146. data.remote_id = data.id;
  147. delete data.id;
  148. opportunitiesStorageFactory.save(data, function (opportunity) {
  149. loop.next();
  150. }, function (saveErr) {
  151. loop.next();
  152. });
  153. }, function () {
  154. success(opportunities);
  155. });
  156. }, function (removeAllErr) {
  157. error(removeAllErr);
  158. });
  159. }, function (pullErr) {
  160. error(pullErr);
  161. });
  162. }
  163. /**
  164. *
  165. */
  166. var sync = function (success, error) {
  167. syncNewData(function () {
  168. console.log('opportunities --> new data ok');
  169. syncUpdatedData(function () {
  170. console.log('opportunities --> updated data ok');
  171. syncDeletedData(function () {
  172. console.log('opportunities --> deleted data ok');
  173. downloadSyncData(function (data) {
  174. console.log('opportunities --> download data ok');
  175. success(data);
  176. }, function (syncErr) {
  177. error(syncErr);
  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. getAll: getAll,
  194. sync: sync
  195. }
  196. });