services.js 9.9 KB

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