2019-08-14 09:48:44 +00:00
|
|
|
import Vue from 'vue';
|
2022-04-14 15:24:26 +00:00
|
|
|
import types from '../mutation-types';
|
2019-08-14 09:48:44 +00:00
|
|
|
import authAPI from '../../api/auth';
|
2022-04-14 15:24:26 +00:00
|
|
|
import { setUser, clearCookiesOnLogout } from '../utils/api';
|
2022-03-03 15:19:51 +00:00
|
|
|
import { getLoginRedirectURL } from '../../helper/URLHelper';
|
2020-02-16 11:50:38 +00:00
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
const initialState = {
|
2019-08-14 09:48:44 +00:00
|
|
|
currentUser: {
|
|
|
|
id: null,
|
|
|
|
account_id: null,
|
2022-04-14 15:24:26 +00:00
|
|
|
accounts: [],
|
2019-08-14 09:48:44 +00:00
|
|
|
email: null,
|
|
|
|
name: null,
|
2022-04-14 15:24:26 +00:00
|
|
|
},
|
|
|
|
uiFlags: {
|
|
|
|
isFetching: true,
|
|
|
|
},
|
2019-08-14 09:48:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// getters
|
2020-02-16 11:50:38 +00:00
|
|
|
export const getters = {
|
|
|
|
isLoggedIn($state) {
|
|
|
|
return !!$state.currentUser.id;
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
getCurrentUserID($state) {
|
|
|
|
return $state.currentUser.id;
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
getUISettings($state) {
|
|
|
|
return $state.currentUser.ui_settings || {};
|
2021-01-10 13:55:33 +00:00
|
|
|
},
|
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
getAuthUIFlags($state) {
|
|
|
|
return $state.uiFlags;
|
|
|
|
},
|
|
|
|
|
|
|
|
getCurrentUserAvailability($state, $getters) {
|
|
|
|
const { accounts = [] } = $state.currentUser;
|
2021-10-07 07:51:46 +00:00
|
|
|
const [currentAccount = {}] = accounts.filter(
|
2022-04-14 15:24:26 +00:00
|
|
|
account => account.id === $getters.getCurrentAccountId
|
2021-10-07 07:51:46 +00:00
|
|
|
);
|
2021-10-14 19:00:48 +00:00
|
|
|
return currentAccount.availability;
|
2020-07-04 14:33:16 +00:00
|
|
|
},
|
|
|
|
|
2021-11-23 12:07:01 +00:00
|
|
|
getCurrentAccountId(_, __, rootState) {
|
|
|
|
if (rootState.route.params && rootState.route.params.accountId) {
|
|
|
|
return Number(rootState.route.params.accountId);
|
|
|
|
}
|
|
|
|
return null;
|
2020-05-26 17:08:48 +00:00
|
|
|
},
|
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
getCurrentRole($state, $getters) {
|
|
|
|
const { accounts = [] } = $state.currentUser;
|
2020-05-26 17:08:48 +00:00
|
|
|
const [currentAccount = {}] = accounts.filter(
|
2022-04-14 15:24:26 +00:00
|
|
|
account => account.id === $getters.getCurrentAccountId
|
2020-05-26 17:08:48 +00:00
|
|
|
);
|
|
|
|
return currentAccount.role;
|
|
|
|
},
|
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
getCurrentUser($state) {
|
|
|
|
return $state.currentUser;
|
2020-02-16 11:50:38 +00:00
|
|
|
},
|
2022-02-15 05:08:24 +00:00
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
getMessageSignature($state) {
|
|
|
|
const { message_signature: messageSignature } = $state.currentUser;
|
2022-02-15 05:08:24 +00:00
|
|
|
|
|
|
|
return messageSignature || '';
|
|
|
|
},
|
2022-02-25 11:06:36 +00:00
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
getCurrentAccount($state, $getters) {
|
|
|
|
const { accounts = [] } = $state.currentUser;
|
2022-02-25 11:06:36 +00:00
|
|
|
const [currentAccount = {}] = accounts.filter(
|
2022-04-14 15:24:26 +00:00
|
|
|
account => account.id === $getters.getCurrentAccountId
|
2022-02-25 11:06:36 +00:00
|
|
|
);
|
|
|
|
return currentAccount || {};
|
|
|
|
},
|
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
getUserAccounts($state) {
|
|
|
|
const { accounts = [] } = $state.currentUser;
|
2022-02-25 11:06:36 +00:00
|
|
|
return accounts;
|
|
|
|
},
|
2019-08-14 09:48:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// actions
|
2020-02-16 11:50:38 +00:00
|
|
|
export const actions = {
|
2022-04-14 15:24:26 +00:00
|
|
|
login(_, { ssoAccountId, ...credentials }) {
|
2019-08-14 09:48:44 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-08-21 07:29:56 +00:00
|
|
|
authAPI
|
|
|
|
.login(credentials)
|
2022-03-03 15:19:51 +00:00
|
|
|
.then(response => {
|
|
|
|
window.location = getLoginRedirectURL(ssoAccountId, response.data);
|
2019-08-21 07:29:56 +00:00
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2019-08-14 09:48:44 +00:00
|
|
|
});
|
|
|
|
},
|
2020-02-16 11:50:38 +00:00
|
|
|
async validityCheck(context) {
|
|
|
|
try {
|
|
|
|
const response = await authAPI.validityCheck();
|
2022-04-14 15:24:26 +00:00
|
|
|
const currentUser = response.data.payload.data;
|
|
|
|
setUser(currentUser);
|
|
|
|
context.commit(types.SET_CURRENT_USER, currentUser);
|
2020-02-16 11:50:38 +00:00
|
|
|
} catch (error) {
|
2021-04-13 12:51:46 +00:00
|
|
|
if (error?.response?.status === 401) {
|
2020-02-16 11:50:38 +00:00
|
|
|
clearCookiesOnLogout();
|
|
|
|
}
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
|
|
|
},
|
2022-04-14 15:24:26 +00:00
|
|
|
async setUser({ commit, dispatch }) {
|
|
|
|
if (authAPI.hasAuthCookie()) {
|
|
|
|
await dispatch('validityCheck');
|
2019-08-14 09:48:44 +00:00
|
|
|
} else {
|
2022-04-14 15:24:26 +00:00
|
|
|
commit(types.CLEAR_USER);
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
2022-04-14 15:24:26 +00:00
|
|
|
commit(types.SET_CURRENT_USER_UI_FLAGS, { isFetching: false });
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
|
|
|
logout({ commit }) {
|
2022-04-14 15:24:26 +00:00
|
|
|
commit(types.CLEAR_USER);
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
2021-01-10 13:55:33 +00:00
|
|
|
|
2020-02-16 11:50:38 +00:00
|
|
|
updateProfile: async ({ commit }, params) => {
|
2021-06-02 12:22:24 +00:00
|
|
|
// eslint-disable-next-line no-useless-catch
|
2020-02-16 11:50:38 +00:00
|
|
|
try {
|
|
|
|
const response = await authAPI.profileUpdate(params);
|
2022-04-14 15:24:26 +00:00
|
|
|
commit(types.SET_CURRENT_USER, response.data);
|
2020-02-16 11:50:38 +00:00
|
|
|
} catch (error) {
|
2021-06-02 12:22:24 +00:00
|
|
|
throw error;
|
2020-02-16 11:50:38 +00:00
|
|
|
}
|
|
|
|
},
|
2020-05-26 17:08:48 +00:00
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
deleteAvatar: async () => {
|
2021-11-26 19:26:07 +00:00
|
|
|
try {
|
|
|
|
await authAPI.deleteAvatar();
|
|
|
|
} catch (error) {
|
|
|
|
// Ignore error
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-01-10 13:55:33 +00:00
|
|
|
updateUISettings: async ({ commit }, params) => {
|
|
|
|
try {
|
2022-04-14 15:24:26 +00:00
|
|
|
commit(types.SET_CURRENT_USER_UI_SETTINGS, params);
|
2021-01-10 13:55:33 +00:00
|
|
|
const response = await authAPI.updateUISettings(params);
|
2022-04-14 15:24:26 +00:00
|
|
|
commit(types.SET_CURRENT_USER, response.data);
|
2021-01-10 13:55:33 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Ignore error
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-10-07 07:51:46 +00:00
|
|
|
updateAvailability: async ({ commit, dispatch }, params) => {
|
|
|
|
try {
|
|
|
|
const response = await authAPI.updateAvailability(params);
|
2020-11-21 17:13:27 +00:00
|
|
|
const userData = response.data;
|
2021-10-07 07:51:46 +00:00
|
|
|
const { id } = userData;
|
2022-04-14 15:24:26 +00:00
|
|
|
commit(types.SET_CURRENT_USER, response.data);
|
2021-10-07 07:51:46 +00:00
|
|
|
dispatch('agents/updatePresence', { [id]: params.availability });
|
|
|
|
} catch (error) {
|
|
|
|
// Ignore error
|
|
|
|
}
|
2020-10-02 05:46:12 +00:00
|
|
|
},
|
|
|
|
|
2021-10-14 19:00:48 +00:00
|
|
|
setCurrentUserAvailability({ commit, state: $state }, data) {
|
2020-07-04 14:33:16 +00:00
|
|
|
if (data[$state.currentUser.id]) {
|
2022-04-14 15:24:26 +00:00
|
|
|
commit(types.SET_CURRENT_USER_AVAILABILITY, data[$state.currentUser.id]);
|
2020-07-04 14:33:16 +00:00
|
|
|
}
|
|
|
|
},
|
2019-08-14 09:48:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// mutations
|
2021-01-10 13:55:33 +00:00
|
|
|
export const mutations = {
|
2022-04-14 15:24:26 +00:00
|
|
|
[types.SET_CURRENT_USER_AVAILABILITY](_state, availability) {
|
2021-10-14 19:00:48 +00:00
|
|
|
Vue.set(_state.currentUser, 'availability', availability);
|
2020-07-04 14:33:16 +00:00
|
|
|
},
|
2022-04-14 15:24:26 +00:00
|
|
|
[types.CLEAR_USER](_state) {
|
|
|
|
_state.currentUser = initialState.currentUser;
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
2022-04-14 15:24:26 +00:00
|
|
|
[types.SET_CURRENT_USER](_state, currentUser) {
|
2020-02-16 11:50:38 +00:00
|
|
|
Vue.set(_state, 'currentUser', currentUser);
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
2022-04-14 15:24:26 +00:00
|
|
|
[types.SET_CURRENT_USER_UI_SETTINGS](_state, { uiSettings }) {
|
2021-01-10 13:55:33 +00:00
|
|
|
Vue.set(_state, 'currentUser', {
|
|
|
|
..._state.currentUser,
|
|
|
|
ui_settings: {
|
|
|
|
..._state.currentUser.ui_settings,
|
|
|
|
...uiSettings,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2022-04-14 15:24:26 +00:00
|
|
|
|
|
|
|
[types.SET_CURRENT_USER_UI_FLAGS](_state, { isFetching }) {
|
|
|
|
Vue.set(_state, 'uiFlags', { isFetching });
|
2020-05-26 17:08:48 +00:00
|
|
|
},
|
2019-08-14 09:48:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
2022-04-14 15:24:26 +00:00
|
|
|
state: initialState,
|
2019-08-14 09:48:44 +00:00
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations,
|
|
|
|
};
|