services.js 8.5 KB

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