Advertisement
Guest User

Untitled

a guest
May 29th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.13 KB | None | 0 0
  1. ;(function () {
  2. 'use strict';
  3.  
  4. angular
  5. .module('model.user', [])
  6. .service('user', user);
  7.  
  8. /* @ngInject */
  9. function user(http, url, $rootScope, clientData, $localStorage, $sessionStorage, dateFactory, $state, messagesNotice) {
  10.  
  11. // servise which work with user and send request on server
  12. var service = {
  13. setPassword: setPassword,
  14. edit: edit,
  15. contact: contact,
  16. remove: remove,
  17. forgotPassword: forgotPassword,
  18. sendActivation: sendActivation,
  19. update: update,
  20. availableItems: availableItems,
  21. setMood: setMood,
  22. getUserInfo: getUserInfo,
  23. login: login,
  24. block: block,
  25. helpManager: helpManager,
  26. response: response,
  27. all: all,
  28. signup: signup,
  29. getUser: getUser,
  30. setUser: setUser,
  31. setUserSettings: setUserSettings,
  32. getUserSettings: getUserSettings,
  33. delUser: delUser,
  34. getSettings: getSettings,
  35. exportCsv: exportCsv,
  36. leaveTeam: leaveTeam,
  37. loginUserGetData: loginUserGetData,
  38. logOut: logOut,
  39. role: role,
  40. getCategoriesName: getCategoriesName,
  41. userAutoLogin: userAutoLogin,
  42. myTeamManagersList: myTeamManagersList,
  43. personalImprovement: personalImprovement,
  44. updateInfo: updateInfo,
  45. updateSettings: updateSettings,
  46. roleIds: roleIds
  47. };
  48.  
  49. $rootScope.$on('receiveNotification', function (event, data) {
  50. switch (data) {
  51. case messagesNotice.notification.userRemoveFromTeam:
  52. processRemoveUserFromTeam();
  53. break;
  54. case messagesNotice.notification.changeTMtoUser:
  55. processUpdateUser();
  56. break;
  57. case messagesNotice.notification.changeUserToTM:
  58. processUpdateUser();
  59. break;
  60. case messagesNotice.notification.userAddToTeam:
  61. processUpdateUser();
  62. break;
  63. }
  64. }
  65. );
  66.  
  67. return service;
  68.  
  69. function processUpdateUser() {
  70. updateInfo().then(function (res) {
  71. $rootScope.$broadcast('userDataUpdated',{
  72. user: getUser()
  73. });
  74. })
  75. }
  76.  
  77. function processRemoveUserFromTeam() {
  78. updateInfo().then(function () {
  79. if (role().isUser()) {
  80. if (getUser().teams.length === 0) {
  81. $state.go('app.mobile-app-teams');
  82. }
  83. } else if (role().isTM()) {
  84. if (getUser().manager_in_teams.length === 0) {
  85. $state.go('app.teams');
  86. }
  87. }
  88. $rootScope.$broadcast('userDataUpdated',{
  89. user: getUser()
  90. })
  91. });
  92. }
  93.  
  94. function loginUserGetData() {
  95. }
  96.  
  97. function personalImprovement(data) {
  98. return http.get(url.user.personalImprovement, data).then(function (res) {
  99. angular.forEach(res, function (val, key) {
  100. if (Array.isArray(val)) {
  101. val.forEach(function (item) {
  102. if (item.created_at) {
  103. var tmpDate = dateFactory.created_date(item.created_at);
  104. item.created_day_row = tmpDate.dayTime;
  105. item.created_month_row = tmpDate.dayMonth;
  106. }
  107. });
  108. }
  109. });
  110. return res;
  111. });
  112. }
  113.  
  114. function leaveTeam(data) {
  115. return http.post(url.user.leaveTeam, data).then(function (res) {
  116. return res;
  117. });
  118. }
  119.  
  120. function exportCsv(data) {
  121. return http.post(url.user.exportCsv, data).then(function (res) {
  122. return res;
  123. });
  124. }
  125.  
  126. /**
  127. * settings for user (role 1)
  128. * @returns users setttings*}
  129. */
  130. function getSettings() {
  131. return http.get(url.user.getSettings).then(function (res) {
  132. return res;
  133. });
  134. }
  135.  
  136. function helpManager(data) {
  137. return http.post(url.user.contactToYouManager, data);
  138. }
  139.  
  140. function myTeamManagersList(data) {
  141. return http.get(url.user.getMyTeamManagers, data);
  142. }
  143.  
  144. /**
  145. * @param {object } data - data for signup
  146. */
  147. function signup(data) {
  148. return http.post(url.user.signup, data);
  149. }
  150.  
  151. /** @desc user all */
  152. function all(data) {
  153. return http.get(url.user.all, data)
  154. .then(function (res) {
  155. return res;
  156. });
  157. }
  158.  
  159.  
  160. /** @desc response */
  161. function response(data) {
  162.  
  163. return http.post(url.user.response, data)
  164. .then(function (res) {
  165. return res;
  166. });
  167. }
  168.  
  169. /** @desc user block */
  170. function block(data) {
  171.  
  172. return http.post(url.user.block, data)
  173. .then(function (res) {
  174. return res;
  175. });
  176. }
  177.  
  178. /** @desc Login */
  179. function login(data) {
  180. return http.post(url.user.login, data)
  181. .then(function (res) {
  182. return res;
  183. });
  184. }
  185.  
  186. /** @desc Get current user */
  187. function getUserInfo(data) {
  188. return http.get(url.user.info, data)
  189. .then(function (res) {
  190. return res;
  191. });
  192. }
  193.  
  194. function setUser(userData) {
  195. //need save settings
  196. var settings = null;
  197. if ($rootScope.user && $rootScope.user.settings) {
  198. settings = $rootScope.user.settings;
  199. }
  200. $rootScope.user = userData;
  201. if (settings !== null) {
  202. $rootScope.user.settings = settings;
  203. }
  204. }
  205.  
  206. function setUserSettings(userSettings) {
  207. if ($rootScope.user) {
  208. $rootScope.user.settings = userSettings;
  209. }
  210. }
  211.  
  212. function getUserSettings() {
  213. if ($rootScope.user && $rootScope.user.settings) {
  214. return $rootScope.user.settings;
  215. }
  216. }
  217.  
  218. function getUser() {
  219. return $rootScope.user;
  220. }
  221.  
  222. function delUser() {
  223. if ($rootScope.user) {
  224. delete $rootScope.user;
  225. }
  226. }
  227.  
  228. function update(data) {
  229. return http.file(url.user.update, data)
  230. .then(function (response) {
  231. return response;
  232. });
  233. }
  234.  
  235.  
  236. function availableItems() {
  237. return http.get(url.user.getAvailableItems);
  238. }
  239.  
  240. function setMood(data) {
  241. return http.post(url.mood.set, data);
  242. }
  243.  
  244.  
  245. /** @desc create */
  246. function setPassword(data) {
  247. return http.post(url.user.setPassword, data)
  248. .then(function (res) {
  249. return res;
  250. });
  251. }
  252.  
  253. /** @desc user user_edit */
  254. function edit(data) {
  255. return http.put(url.user.edit, data)
  256. .then(function (res) {
  257. return res;
  258. });
  259. }
  260.  
  261. /** @desc user contact */
  262. function contact(data) {
  263. return http.post(url.user.contact, data)
  264. .then(function (res) {
  265. return res;
  266. });
  267. }
  268.  
  269.  
  270. /** @desc user remove */
  271. function remove(data) {
  272. return http.delete(url.user.remove, data)
  273. .then(function (res) {
  274. return res;
  275. });
  276. }
  277.  
  278.  
  279. /** @desc user forgotPassword */
  280. function forgotPassword(data) {
  281. return http.post(url.user.forgotPassword, data)
  282. .then(function (res) {
  283. return res;
  284. });
  285. }
  286.  
  287.  
  288. /** @desc user sendActivation */
  289. function sendActivation(data) {
  290.  
  291. return http.post(url.user.sendActivation, data)
  292. .then(function (res) {
  293. return res;
  294. });
  295. }
  296.  
  297. function logOut(data) {
  298. return http.post(url.user.logOut, data);
  299.  
  300. }
  301.  
  302.  
  303. function userAutoLogin() {
  304. if ($localStorage.auth_key && $localStorage.auth_key.length && !getUser()) {
  305. $sessionStorage.auth_key = $localStorage.auth_key;
  306. return getUserInfo().then(function (data) {
  307. return getSettings()
  308. .then(function (res) {
  309. setUser(data);
  310. setUserSettings(res);
  311. });
  312. });
  313. } else if (getUser() && !getUserSettings()) {
  314. $sessionStorage.auth_key = $localStorage.auth_key;
  315. return getSettings()
  316. .then(function (res) {
  317. setUserSettings(res);
  318. });
  319. }
  320. }
  321.  
  322.  
  323. function role(roleNum) {
  324. if (typeof $rootScope.user === 'undefined' || typeof $rootScope.user.role === 'undefined') {
  325. return false;
  326. }
  327. var role = $rootScope.user.role;
  328. return {
  329. isTM: function isTM() {
  330. if (role === roleIds().USER && $rootScope.user.manager_in_teams.length &&
  331. $rootScope.user.observer_in_teams && !$rootScope.user.observer_in_teams.length) {
  332. return true;
  333. } else if (role === roleIds().TEAM_MANAGER && $rootScope.user.observer_in_teams && !$rootScope.user.observer_in_teams.length) {
  334. return true;
  335. }
  336. return false;
  337. },
  338.  
  339. isTMext: function isTMext() {
  340. return (role === roleIds().TEAM_MANAGER);
  341. },
  342.  
  343. isObs: function isObs() {
  344. return !!(($rootScope.user.observer_in_teams &&
  345. $rootScope.user.observer_in_teams.length) ||
  346. role === roleIds().OBSERVER);
  347. },
  348.  
  349. isUser: function isUser() {
  350. return (role === roleIds().USER &&
  351. !$rootScope.user.manager_in_teams.length &&
  352. !$rootScope.user.observer_in_teams.length);
  353. },
  354.  
  355. isQA: function isQA() {
  356. return (role === roleIds().CLIENT_ADMIN);
  357. },
  358.  
  359. isSA: function isSA() {
  360. return (role === roleIds().SUPER_ADMIN);
  361. }
  362. };
  363. }
  364.  
  365. function getCategoriesName() {
  366. var cat = {
  367. speciality_title: 'Profile Level 3',
  368. grade_title: 'Profile Level 2',
  369. directorate_title: 'Profile Level 1'
  370. };
  371. var settings;
  372. if (getUser()) {
  373. if ((role().isSA() || role().isQA()) && clientData.getCurrentClientData().settings) {
  374. settings = clientData.getCurrentClientData().settings;
  375. } else if (getUserSettings()) {
  376. settings = getUserSettings();
  377. }
  378. if (settings && settings.speciality_title && settings.speciality_title.length) {
  379. cat.speciality_title = settings.speciality_title;
  380. }
  381. if (settings && settings.grade_title && settings.grade_title.length) {
  382. cat.grade_title = settings.grade_title;
  383. }
  384. if (settings && settings.directorate_title && settings.directorate_title.length) {
  385. cat.directorate_title = settings.directorate_title;
  386. }
  387. }
  388. return cat;
  389. }
  390.  
  391. function updateInfo() {
  392. return getUserInfo().then(function (data) {
  393. setUser(data);
  394. });
  395. }
  396.  
  397. function updateSettings() {
  398. return getSettings().then(function (res) {
  399. setUserSettings(res);
  400. });
  401. }
  402.  
  403. function roleIds() {
  404. return {
  405. USER: 1,
  406. CLIENT_ADMIN: 5,
  407. TEAM_MEMBER: 6,
  408. TEAM_MANAGER: 7,
  409. OBSERVER: 8,
  410. SUPER_ADMIN: 10
  411. };
  412. }
  413.  
  414. }
  415. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement