Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;(function () {
- 'use strict';
- angular
- .module('model.user', [])
- .service('user', user);
- /* @ngInject */
- function user(http, url, $rootScope, clientData, $localStorage, $sessionStorage, dateFactory, $state, messagesNotice) {
- // servise which work with user and send request on server
- var service = {
- setPassword: setPassword,
- edit: edit,
- contact: contact,
- remove: remove,
- forgotPassword: forgotPassword,
- sendActivation: sendActivation,
- update: update,
- availableItems: availableItems,
- setMood: setMood,
- getUserInfo: getUserInfo,
- login: login,
- block: block,
- helpManager: helpManager,
- response: response,
- all: all,
- signup: signup,
- getUser: getUser,
- setUser: setUser,
- setUserSettings: setUserSettings,
- getUserSettings: getUserSettings,
- delUser: delUser,
- getSettings: getSettings,
- exportCsv: exportCsv,
- leaveTeam: leaveTeam,
- loginUserGetData: loginUserGetData,
- logOut: logOut,
- role: role,
- getCategoriesName: getCategoriesName,
- userAutoLogin: userAutoLogin,
- myTeamManagersList: myTeamManagersList,
- personalImprovement: personalImprovement,
- updateInfo: updateInfo,
- updateSettings: updateSettings,
- roleIds: roleIds
- };
- $rootScope.$on('receiveNotification', function (event, data) {
- switch (data) {
- case messagesNotice.notification.userRemoveFromTeam:
- processRemoveUserFromTeam();
- break;
- case messagesNotice.notification.changeTMtoUser:
- processUpdateUser();
- break;
- case messagesNotice.notification.changeUserToTM:
- processUpdateUser();
- break;
- case messagesNotice.notification.userAddToTeam:
- processUpdateUser();
- break;
- }
- }
- );
- return service;
- function processUpdateUser() {
- updateInfo().then(function (res) {
- $rootScope.$broadcast('userDataUpdated',{
- user: getUser()
- });
- })
- }
- function processRemoveUserFromTeam() {
- updateInfo().then(function () {
- if (role().isUser()) {
- if (getUser().teams.length === 0) {
- $state.go('app.mobile-app-teams');
- }
- } else if (role().isTM()) {
- if (getUser().manager_in_teams.length === 0) {
- $state.go('app.teams');
- }
- }
- $rootScope.$broadcast('userDataUpdated',{
- user: getUser()
- })
- });
- }
- function loginUserGetData() {
- }
- function personalImprovement(data) {
- return http.get(url.user.personalImprovement, data).then(function (res) {
- angular.forEach(res, function (val, key) {
- if (Array.isArray(val)) {
- val.forEach(function (item) {
- if (item.created_at) {
- var tmpDate = dateFactory.created_date(item.created_at);
- item.created_day_row = tmpDate.dayTime;
- item.created_month_row = tmpDate.dayMonth;
- }
- });
- }
- });
- return res;
- });
- }
- function leaveTeam(data) {
- return http.post(url.user.leaveTeam, data).then(function (res) {
- return res;
- });
- }
- function exportCsv(data) {
- return http.post(url.user.exportCsv, data).then(function (res) {
- return res;
- });
- }
- /**
- * settings for user (role 1)
- * @returns users setttings*}
- */
- function getSettings() {
- return http.get(url.user.getSettings).then(function (res) {
- return res;
- });
- }
- function helpManager(data) {
- return http.post(url.user.contactToYouManager, data);
- }
- function myTeamManagersList(data) {
- return http.get(url.user.getMyTeamManagers, data);
- }
- /**
- * @param {object } data - data for signup
- */
- function signup(data) {
- return http.post(url.user.signup, data);
- }
- /** @desc user all */
- function all(data) {
- return http.get(url.user.all, data)
- .then(function (res) {
- return res;
- });
- }
- /** @desc response */
- function response(data) {
- return http.post(url.user.response, data)
- .then(function (res) {
- return res;
- });
- }
- /** @desc user block */
- function block(data) {
- return http.post(url.user.block, data)
- .then(function (res) {
- return res;
- });
- }
- /** @desc Login */
- function login(data) {
- return http.post(url.user.login, data)
- .then(function (res) {
- return res;
- });
- }
- /** @desc Get current user */
- function getUserInfo(data) {
- return http.get(url.user.info, data)
- .then(function (res) {
- return res;
- });
- }
- function setUser(userData) {
- //need save settings
- var settings = null;
- if ($rootScope.user && $rootScope.user.settings) {
- settings = $rootScope.user.settings;
- }
- $rootScope.user = userData;
- if (settings !== null) {
- $rootScope.user.settings = settings;
- }
- }
- function setUserSettings(userSettings) {
- if ($rootScope.user) {
- $rootScope.user.settings = userSettings;
- }
- }
- function getUserSettings() {
- if ($rootScope.user && $rootScope.user.settings) {
- return $rootScope.user.settings;
- }
- }
- function getUser() {
- return $rootScope.user;
- }
- function delUser() {
- if ($rootScope.user) {
- delete $rootScope.user;
- }
- }
- function update(data) {
- return http.file(url.user.update, data)
- .then(function (response) {
- return response;
- });
- }
- function availableItems() {
- return http.get(url.user.getAvailableItems);
- }
- function setMood(data) {
- return http.post(url.mood.set, data);
- }
- /** @desc create */
- function setPassword(data) {
- return http.post(url.user.setPassword, data)
- .then(function (res) {
- return res;
- });
- }
- /** @desc user user_edit */
- function edit(data) {
- return http.put(url.user.edit, data)
- .then(function (res) {
- return res;
- });
- }
- /** @desc user contact */
- function contact(data) {
- return http.post(url.user.contact, data)
- .then(function (res) {
- return res;
- });
- }
- /** @desc user remove */
- function remove(data) {
- return http.delete(url.user.remove, data)
- .then(function (res) {
- return res;
- });
- }
- /** @desc user forgotPassword */
- function forgotPassword(data) {
- return http.post(url.user.forgotPassword, data)
- .then(function (res) {
- return res;
- });
- }
- /** @desc user sendActivation */
- function sendActivation(data) {
- return http.post(url.user.sendActivation, data)
- .then(function (res) {
- return res;
- });
- }
- function logOut(data) {
- return http.post(url.user.logOut, data);
- }
- function userAutoLogin() {
- if ($localStorage.auth_key && $localStorage.auth_key.length && !getUser()) {
- $sessionStorage.auth_key = $localStorage.auth_key;
- return getUserInfo().then(function (data) {
- return getSettings()
- .then(function (res) {
- setUser(data);
- setUserSettings(res);
- });
- });
- } else if (getUser() && !getUserSettings()) {
- $sessionStorage.auth_key = $localStorage.auth_key;
- return getSettings()
- .then(function (res) {
- setUserSettings(res);
- });
- }
- }
- function role(roleNum) {
- if (typeof $rootScope.user === 'undefined' || typeof $rootScope.user.role === 'undefined') {
- return false;
- }
- var role = $rootScope.user.role;
- return {
- isTM: function isTM() {
- if (role === roleIds().USER && $rootScope.user.manager_in_teams.length &&
- $rootScope.user.observer_in_teams && !$rootScope.user.observer_in_teams.length) {
- return true;
- } else if (role === roleIds().TEAM_MANAGER && $rootScope.user.observer_in_teams && !$rootScope.user.observer_in_teams.length) {
- return true;
- }
- return false;
- },
- isTMext: function isTMext() {
- return (role === roleIds().TEAM_MANAGER);
- },
- isObs: function isObs() {
- return !!(($rootScope.user.observer_in_teams &&
- $rootScope.user.observer_in_teams.length) ||
- role === roleIds().OBSERVER);
- },
- isUser: function isUser() {
- return (role === roleIds().USER &&
- !$rootScope.user.manager_in_teams.length &&
- !$rootScope.user.observer_in_teams.length);
- },
- isQA: function isQA() {
- return (role === roleIds().CLIENT_ADMIN);
- },
- isSA: function isSA() {
- return (role === roleIds().SUPER_ADMIN);
- }
- };
- }
- function getCategoriesName() {
- var cat = {
- speciality_title: 'Profile Level 3',
- grade_title: 'Profile Level 2',
- directorate_title: 'Profile Level 1'
- };
- var settings;
- if (getUser()) {
- if ((role().isSA() || role().isQA()) && clientData.getCurrentClientData().settings) {
- settings = clientData.getCurrentClientData().settings;
- } else if (getUserSettings()) {
- settings = getUserSettings();
- }
- if (settings && settings.speciality_title && settings.speciality_title.length) {
- cat.speciality_title = settings.speciality_title;
- }
- if (settings && settings.grade_title && settings.grade_title.length) {
- cat.grade_title = settings.grade_title;
- }
- if (settings && settings.directorate_title && settings.directorate_title.length) {
- cat.directorate_title = settings.directorate_title;
- }
- }
- return cat;
- }
- function updateInfo() {
- return getUserInfo().then(function (data) {
- setUser(data);
- });
- }
- function updateSettings() {
- return getSettings().then(function (res) {
- setUserSettings(res);
- });
- }
- function roleIds() {
- return {
- USER: 1,
- CLIENT_ADMIN: 5,
- TEAM_MEMBER: 6,
- TEAM_MANAGER: 7,
- OBSERVER: 8,
- SUPER_ADMIN: 10
- };
- }
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement