services.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. 'use strict';
  2. /* Services that are used to interact with the backend. */
  3. var eventManServices = angular.module('eventManServices', ['ngResource']);
  4. /* Modify, in place, an object to convert datetime. */
  5. function convert_dates(obj) {
  6. angular.forEach(['begin_date', 'end_date', 'ticket_sales_begin_date', 'ticket_sales_end_date'], function(key, key_idx) {
  7. if (!obj[key]) {
  8. return;
  9. }
  10. obj[key] = obj[key].getTime();
  11. });
  12. return obj;
  13. }
  14. eventManServices.factory('Event', ['$resource', '$rootScope',
  15. function($resource, $rootScope) {
  16. return $resource('events/:id', {id: '@_id'}, {
  17. all: {
  18. method: 'GET',
  19. interceptor: {responseError: $rootScope.errorHandler},
  20. isArray: true,
  21. transformResponse: function(data, headers) {
  22. data = angular.fromJson(data);
  23. if (data.error) {
  24. return data;
  25. }
  26. angular.forEach(data.events || [], function(event_, event_idx) {
  27. convert_dates(event_);
  28. });
  29. return data.events;
  30. }
  31. },
  32. get: {
  33. method: 'GET',
  34. interceptor: {responseError: $rootScope.errorHandler},
  35. transformResponse: function(data, headers) {
  36. data = angular.fromJson(data);
  37. convert_dates(data);
  38. // strip empty keys.
  39. angular.forEach(data.tickets || [], function(ticket, ticket_idx) {
  40. angular.forEach(ticket, function(value, key) {
  41. if (value === "") {
  42. delete ticket[key];
  43. }
  44. });
  45. });
  46. return data;
  47. }
  48. },
  49. update: {
  50. method: 'PUT',
  51. interceptor: {responseError: $rootScope.errorHandler},
  52. transformResponse: function(data, headers) {
  53. data = angular.fromJson(data);
  54. convert_dates(data);
  55. return data;
  56. }
  57. },
  58. group_persons: {
  59. method: 'GET',
  60. url: 'events/:id/group_persons',
  61. isArray: true,
  62. transformResponse: function(data, headers) {
  63. data = angular.fromJson(data);
  64. return data.persons || [];
  65. }
  66. }
  67. });
  68. }]
  69. );
  70. eventManServices.factory('EventTicket', ['$resource', '$rootScope',
  71. function($resource, $rootScope) {
  72. return $resource('events/:id/tickets', {event_id: '@event_id', ticket_id: '@_id'}, {
  73. all: {
  74. method: 'GET',
  75. url: '/tickets',
  76. interceptor: {responseError: $rootScope.errorHandler},
  77. isArray: true,
  78. transformResponse: function(data, headers) {
  79. data = angular.fromJson(data);
  80. return data.tickets;
  81. }
  82. },
  83. get: {
  84. method: 'GET',
  85. url: 'events/:id/tickets/:ticket_id',
  86. interceptor: {responseError: $rootScope.errorHandler},
  87. transformResponse: function(data, headers) {
  88. data = angular.fromJson(data);
  89. if (data.error) {
  90. return data;
  91. }
  92. return data.ticket;
  93. }
  94. },
  95. add: {
  96. method: 'POST',
  97. interceptor: {responseError: $rootScope.errorHandler},
  98. isArray: false,
  99. url: 'events/:event_id/tickets',
  100. params: {uuid: $rootScope.app_uuid},
  101. transformResponse: function(data, headers) {
  102. data = angular.fromJson(data);
  103. if (data.error) {
  104. return data;
  105. }
  106. return data.ticket;
  107. }
  108. },
  109. update: {
  110. method: 'PUT',
  111. interceptor: {responseError: $rootScope.errorHandler},
  112. isArray: false,
  113. url: 'events/:event_id/tickets/:ticket_id',
  114. params: {uuid: $rootScope.app_uuid},
  115. transformResponse: function(data, headers) {
  116. if (data.error) {
  117. return data;
  118. }
  119. return angular.fromJson(data);
  120. }
  121. },
  122. 'delete': {
  123. method: 'DELETE',
  124. interceptor: {responseError: $rootScope.errorHandler},
  125. isArray: false,
  126. url: 'events/:event_id/tickets/:ticket_id',
  127. params: {uuid: $rootScope.app_uuid},
  128. transformResponse: function(data, headers) {
  129. return angular.fromJson(data);
  130. }
  131. }
  132. });
  133. }]
  134. );
  135. eventManServices.factory('Setting', ['$resource', '$rootScope',
  136. function($resource, $rootScope) {
  137. return $resource('settings/', {}, {
  138. query: {
  139. method: 'GET',
  140. interceptor: {responseError: $rootScope.errorHandler},
  141. isArray: true,
  142. transformResponse: function(data, headers) {
  143. data = angular.fromJson(data);
  144. if (data.error) {
  145. return data;
  146. }
  147. return data.settings;
  148. }
  149. },
  150. update: {
  151. method: 'PUT',
  152. interceptor: {responseError: $rootScope.errorHandler}
  153. }
  154. });
  155. }]
  156. );
  157. eventManServices.factory('Info', ['$resource', '$rootScope',
  158. function($resource, $rootScope) {
  159. return $resource('info/', {}, {
  160. get: {
  161. method: 'GET',
  162. interceptor: {responseError: $rootScope.errorHandler},
  163. isArray: false,
  164. transformResponse: function(data, headers) {
  165. data = angular.fromJson(data);
  166. if (data.error) {
  167. return data;
  168. }
  169. return data.info || {};
  170. }
  171. }
  172. });
  173. }]
  174. );
  175. eventManServices.factory('EbAPI', ['$resource', '$rootScope',
  176. function($resource, $rootScope) {
  177. return $resource('ebapi/', {}, {
  178. apiImport: {
  179. method: 'POST',
  180. interceptor: {responseError: $rootScope.errorHandler},
  181. isArray: false,
  182. transformResponse: function(data, headers) {
  183. return angular.fromJson(data);
  184. }
  185. }
  186. });
  187. }]
  188. );
  189. eventManServices.factory('User', ['$resource', '$rootScope',
  190. function($resource, $rootScope) {
  191. return $resource('users/:id', {id: '@_id'}, {
  192. all: {
  193. method: 'GET',
  194. interceptor: {responseError: $rootScope.errorHandler},
  195. isArray: true,
  196. transformResponse: function(data, headers) {
  197. data = angular.fromJson(data);
  198. if (data.error) {
  199. return data;
  200. }
  201. return data.users;
  202. }
  203. },
  204. get: {
  205. method: 'GET',
  206. interceptor: {responseError: $rootScope.errorHandler},
  207. transformResponse: function(data, headers) {
  208. return angular.fromJson(data);
  209. }
  210. },
  211. add: {
  212. method: 'POST',
  213. interceptor: {responseError: $rootScope.errorHandler}
  214. },
  215. update: {
  216. method: 'PUT',
  217. interceptor: {responseError: $rootScope.errorHandler}
  218. },
  219. login: {
  220. method: 'POST',
  221. url: '/login',
  222. interceptor: {responseError: $rootScope.errorHandler}
  223. },
  224. logout: {
  225. method: 'GET',
  226. url: '/logout',
  227. interceptor: {responseError: $rootScope.errorHandler}
  228. }
  229. });
  230. }]
  231. );
  232. /* WebSocket collection used to update the list of tickets of an Event. */
  233. eventManApp.factory('EventUpdates', ['$websocket', '$location', '$log', '$rootScope',
  234. function($websocket, $location, $log, $rootScope) {
  235. var dataStream = null;
  236. var data = {};
  237. var methods = {
  238. data: data,
  239. close: function() {
  240. $log.debug('close WebSocket connection');
  241. dataStream.close();
  242. },
  243. open: function() {
  244. var proto = $location.protocol() == 'https' ? 'wss' : 'ws';
  245. var url = proto + '://' + $location.host() + ':' + $location.port() +
  246. '/ws/' + $location.path() + '/updates?uuid=' + $rootScope.app_uuid;
  247. $log.debug('open WebSocket connection to ' + url);
  248. //dataStream && dataStream.close();
  249. dataStream = $websocket(url);
  250. dataStream.onMessage(function(message) {
  251. $log.debug('EventUpdates message received');
  252. data.update = angular.fromJson(message.data);
  253. });
  254. }
  255. };
  256. return methods;
  257. }]
  258. );