services.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. },
  53. group_persons: {
  54. method: 'GET',
  55. url: 'events/:id/group_persons',
  56. isArray: true,
  57. transformResponse: function(data, headers) {
  58. data = angular.fromJson(data);
  59. return data.persons || [];
  60. }
  61. }
  62. });
  63. }]
  64. );
  65. eventManServices.factory('EventTicket', ['$resource', '$rootScope',
  66. function($resource, $rootScope) {
  67. return $resource('events/:id/tickets', {event_id: '@event_id', ticket_id: '@_id'}, {
  68. all: {
  69. method: 'GET',
  70. url: '/tickets',
  71. interceptor : {responseError: $rootScope.errorHandler},
  72. isArray: true,
  73. transformResponse: function(data, headers) {
  74. data = angular.fromJson(data);
  75. return data.tickets;
  76. }
  77. },
  78. get: {
  79. method: 'GET',
  80. url: 'events/:id/tickets/:ticket_id',
  81. interceptor : {responseError: $rootScope.errorHandler},
  82. transformResponse: function(data, headers) {
  83. data = angular.fromJson(data);
  84. if (data.error) {
  85. return data;
  86. }
  87. return data.ticket;
  88. }
  89. },
  90. add: {
  91. method: 'POST',
  92. interceptor : {responseError: $rootScope.errorHandler},
  93. isArray: false,
  94. url: 'events/:event_id/tickets',
  95. params: {uuid: $rootScope.app_uuid},
  96. transformResponse: function(data, headers) {
  97. data = angular.fromJson(data);
  98. if (data.error) {
  99. return data;
  100. }
  101. return data.ticket;
  102. }
  103. },
  104. update: {
  105. method: 'PUT',
  106. interceptor : {responseError: $rootScope.errorHandler},
  107. isArray: false,
  108. url: 'events/:event_id/tickets/:ticket_id',
  109. params: {uuid: $rootScope.app_uuid},
  110. transformResponse: function(data, headers) {
  111. if (data.error) {
  112. return data;
  113. }
  114. return angular.fromJson(data);
  115. }
  116. },
  117. 'delete': {
  118. method: 'DELETE',
  119. interceptor : {responseError: $rootScope.errorHandler},
  120. isArray: false,
  121. url: 'events/:event_id/tickets/:ticket_id',
  122. params: {uuid: $rootScope.app_uuid},
  123. transformResponse: function(data, headers) {
  124. return angular.fromJson(data);
  125. }
  126. }
  127. });
  128. }]
  129. );
  130. eventManServices.factory('Setting', ['$resource', '$rootScope',
  131. function($resource, $rootScope) {
  132. return $resource('settings/', {}, {
  133. query: {
  134. method: 'GET',
  135. interceptor : {responseError: $rootScope.errorHandler},
  136. isArray: true,
  137. transformResponse: function(data, headers) {
  138. data = angular.fromJson(data);
  139. if (data.error) {
  140. return data;
  141. }
  142. return data.settings;
  143. }
  144. },
  145. update: {
  146. method: 'PUT',
  147. interceptor : {responseError: $rootScope.errorHandler}
  148. }
  149. });
  150. }]
  151. );
  152. eventManServices.factory('Info', ['$resource', '$rootScope',
  153. function($resource, $rootScope) {
  154. return $resource('info/', {}, {
  155. get: {
  156. method: 'GET',
  157. interceptor : {responseError: $rootScope.errorHandler},
  158. isArray: false,
  159. transformResponse: function(data, headers) {
  160. data = angular.fromJson(data);
  161. if (data.error) {
  162. return data;
  163. }
  164. return data.info || {};
  165. }
  166. }
  167. });
  168. }]
  169. );
  170. eventManServices.factory('User', ['$resource', '$rootScope',
  171. function($resource, $rootScope) {
  172. return $resource('users/:id', {id: '@_id'}, {
  173. all: {
  174. method: 'GET',
  175. interceptor : {responseError: $rootScope.errorHandler},
  176. isArray: true,
  177. transformResponse: function(data, headers) {
  178. data = angular.fromJson(data);
  179. if (data.error) {
  180. return data;
  181. }
  182. return data.users;
  183. }
  184. },
  185. get: {
  186. method: 'GET',
  187. interceptor : {responseError: $rootScope.errorHandler},
  188. transformResponse: function(data, headers) {
  189. return angular.fromJson(data);
  190. }
  191. },
  192. add: {
  193. method: 'POST',
  194. interceptor : {responseError: $rootScope.errorHandler}
  195. },
  196. update: {
  197. method: 'PUT',
  198. interceptor : {responseError: $rootScope.errorHandler}
  199. },
  200. login: {
  201. method: 'POST',
  202. url: '/login',
  203. interceptor : {responseError: $rootScope.errorHandler}
  204. },
  205. logout: {
  206. method: 'GET',
  207. url: '/logout',
  208. interceptor : {responseError: $rootScope.errorHandler}
  209. }
  210. });
  211. }]
  212. );
  213. /* WebSocket collection used to update the list of tickets of an Event. */
  214. eventManApp.factory('EventUpdates', ['$websocket', '$location', '$log', '$rootScope',
  215. function($websocket, $location, $log, $rootScope) {
  216. var dataStream = null;
  217. var data = {};
  218. var methods = {
  219. data: data,
  220. close: function() {
  221. $log.debug('close WebSocket connection');
  222. dataStream.close();
  223. },
  224. open: function() {
  225. var proto = $location.protocol() == 'https' ? 'wss' : 'ws';
  226. var url = proto + '://' + $location.host() + ':' + $location.port() +
  227. '/ws/' + $location.path() + '/updates?uuid=' + $rootScope.app_uuid;
  228. $log.debug('open WebSocket connection to ' + url);
  229. //dataStream && dataStream.close();
  230. dataStream = $websocket(url);
  231. dataStream.onMessage(function(message) {
  232. $log.debug('EventUpdates message received');
  233. data.update = angular.fromJson(message.data);
  234. });
  235. }
  236. };
  237. return methods;
  238. }]
  239. );