services.js 9.1 KB

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