From a52d2ee0de16bdf0fa8f57a62a83d8c30bf3a177 Mon Sep 17 00:00:00 2001 From: Yamel Senih Date: Wed, 22 Apr 2020 03:08:09 -0400 Subject: [PATCH] Add support to set warehouse and organization for current role (#455) --- package.json | 4 +- src/api/ADempiere/data.js | 44 +++++- src/layout/components/Sidebar/Logo.vue | 10 +- src/store/getters.js | 2 +- src/store/modules/user.js | 143 ++++++++++++++++-- src/utils/ADempiere/auth.js | 26 ++++ .../permission/components/SwitchRoles.vue | 2 +- src/views/profile/components/Profile.vue | 6 +- src/views/profile/components/RolesNavbar.vue | 89 ++++++++--- src/views/profile/components/UserCard.vue | 8 +- src/views/profile/components/role.vue | 16 +- 11 files changed, 285 insertions(+), 65 deletions(-) diff --git a/package.json b/package.json index 1101f084..b4d0f48d 100644 --- a/package.json +++ b/package.json @@ -45,8 +45,8 @@ }, "dependencies": { "@adempiere/grpc-access-client": "^1.1.8", - "@adempiere/grpc-data-client": "^2.2.1", - "@adempiere/grpc-pos-client": "^1.0.3", + "@adempiere/grpc-data-client": "^2.2.3", + "@adempiere/grpc-pos-client": "^1.0.4", "@adempiere/grpc-dictionary-client": "^1.3.9", "@adempiere/grpc-enrollment-client": "^1.0.7", "autoprefixer": "^9.5.1", diff --git a/src/api/ADempiere/data.js b/src/api/ADempiere/data.js index ad1a397f..e9c51174 100644 --- a/src/api/ADempiere/data.js +++ b/src/api/ADempiere/data.js @@ -1,15 +1,17 @@ import { getLanguage } from '@/lang/index' -import { getToken } from '@/utils/auth' +import { getToken, getCurrentOrganization, getCurrentWarehouse } from '@/utils/auth' import BusinessData from '@adempiere/grpc-data-client' import { BUSINESS_DATA_ADDRESS } from '@/api/ADempiere/constants' // Get Instance for connection function Instance() { - return new BusinessData( - BUSINESS_DATA_ADDRESS, - getToken(), - getLanguage() || 'en_US' - ) + return new BusinessData({ + host: BUSINESS_DATA_ADDRESS, + sessionUuid: getToken(), + organizationUuid: getCurrentOrganization(), + warehouseUuid: getCurrentWarehouse(), + language: getLanguage() || 'en_US' + }) } /** @@ -91,6 +93,36 @@ export function getEntitiesList({ }) } +// Get Organization list from role +export function getOrganizationsList({ + roleUuid, + roleId, + pageToken, + pageSize +}) { + return Instance.call(this).requestListOrganizations({ + roleUuid, + roleId, + pageToken, + pageSize + }) +} + +// Get Warehouses of Organization +export function getWarehousesList({ + organizationUuid, + organizationId, + pageToken, + pageSize +}) { + return Instance.call(this).requestListWarehouses({ + organizationUuid, + organizationId, + pageToken, + pageSize + }) +} + /** * Get all operator or get key value type from value * @param {number} keyToFind diff --git a/src/layout/components/Sidebar/Logo.vue b/src/layout/components/Sidebar/Logo.vue index 3b884113..8b5b70fb 100644 --- a/src/layout/components/Sidebar/Logo.vue +++ b/src/layout/components/Sidebar/Logo.vue @@ -3,7 +3,7 @@ -
{{ getRol.name }} | {{ getRol.clientName }}
+
{{ getRole.name }} | {{ getRole.clientName }}

{{ title }}

@@ -12,9 +12,9 @@

{{ title }}


-
{{ getRol.name }} | {{ getRol.clientName }}
+
{{ getRole.name }} | {{ getRole.clientName }}
@@ -40,8 +40,8 @@ export default { } }, computed: { - getRol() { - return this.$store.getters['user/getRol'] + getRole() { + return this.$store.getters['user/getRole'] } }, methods: { diff --git a/src/store/getters.js b/src/store/getters.js index c713b3a3..b4203a1d 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -12,7 +12,7 @@ const getters = { router: state => state.permission.addRoutes, introduction: state => state.user.introduction, currentRole: state => state.user.currentRole, - getRoleUuid: state => state.user.rol.uuid, + getRoleUuid: state => state.user.role.uuid, roles: state => state.user.roles, permission_routes: state => state.permission.routes, errorLogs: state => state.errorLog.logs diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 553fbf60..b21c8e11 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,5 +1,19 @@ import { login, logout, requestUserInfoFromSession, getSessionInfo, changeRole } from '@/api/user' -import { getToken, setToken, removeToken, getCurrentRole, setCurrentRole, removeCurrentRole } from '@/utils/auth' +import { + getToken, + setToken, + removeToken, + getCurrentRole, + setCurrentRole, + removeCurrentRole, + getCurrentOrganization, + setCurrentOrganization, + getCurrentWarehouse, + setCurrentWarehouse, + removeCurrentWarehouse, + removeCurrentOrganization +} from '@/utils/auth' +import { getOrganizationsList, getWarehousesList } from '@/api/ADempiere/data' import router, { resetRouter } from '@/router' import { showMessage } from '@/utils/ADempiere/notification' import { isEmptyValue } from '@/utils/ADempiere/valueUtils' @@ -11,9 +25,13 @@ const state = { userUuid: '', avatar: '', introduction: '', - rol: {}, // info current rol + role: {}, // info current role rolesList: [], roles: [], + organizationsList: [], + organization: {}, + warehousesList: [], + warehouse: {}, isSession: false, sessionInfo: {} } @@ -37,8 +55,20 @@ const mutations = { SET_ROLES_LIST: (state, payload) => { state.rolesList = payload }, - SET_ROL: (state, rol) => { - state.rol = rol + SET_ORGANIZATIONS_LIST: (state, payload) => { + state.organizationsList = payload + }, + SET_ORGANIZATION: (state, organization) => { + state.organization = organization + }, + SET_WAREHOUSES_LIST: (state, payload) => { + state.warehousesList = payload + }, + SET_WAREHOUSE: (state, warehouse) => { + state.warehouse = warehouse + }, + SET_ROLE: (state, role) => { + state.role = role }, SET_USER_UUID: (state, payload) => { state.userUuid = payload @@ -110,9 +140,47 @@ const actions = { defaultContext: responseGetInfo.defaultContextMap } - commit('SET_ROL', role) + commit('SET_ROLE', role) setCurrentRole(role.uuid) - + getOrganizationsList({ roleUuid: role.uuid }) + .then(response => { + commit('SET_ORGANIZATIONS_LIST', response.organizationsList) + let organization = response.organizationsList.find(item => item.uuid === getCurrentOrganization()) + if (isEmptyValue(organization)) { + organization = response.organizationsList[0] + } + if (isEmptyValue(organization)) { + removeCurrentOrganization() + commit('SET_ORGANIZATION', undefined) + } else { + setCurrentOrganization(organization.uuid) + commit('SET_ORGANIZATION', organization) + } + getWarehousesList({ organizationUuid: getCurrentOrganization() }) + .then(response => { + commit('SET_WAREHOUSES_LIST', response.warehousesList) + let warehouse = response.warehousesList.find(item => item.uuid === getCurrentWarehouse()) + if (isEmptyValue(warehouse)) { + warehouse = response.warehousesList[0] + } + if (isEmptyValue(warehouse)) { + removeCurrentWarehouse() + commit('SET_WAREHOUSE', undefined) + } else { + setCurrentWarehouse(warehouse.uuid) + commit('SET_WAREHOUSE', warehouse) + } + }) + .catch(error => { + console.warn(`Error ${error.code} getting user info value: ${error.message}.`) + reject(error) + }) + }) + .catch(error => { + console.warn(`Error ${error.code} getting user info value: ${error.message}.`) + reject(error) + }) + // resolve(sessionResponse) dispatch('getUserInfoFromSession', sessionUuid) @@ -155,12 +223,12 @@ const actions = { }) commit('SET_ROLES', rolesName) - if (isEmptyValue(state.rol)) { + if (isEmptyValue(state.role)) { const role = responseGetInfo.rolesList.find(itemRole => { return itemRole.uuid === getCurrentRole() }) if (!isEmptyValue(role)) { - commit('SET_ROL', role) + commit('SET_ROLE', role) } } @@ -211,9 +279,40 @@ const actions = { resolve() }) }, + changeOrganization({ commit, dispatch }, { + organizationUuid + }) { + setCurrentOrganization(organizationUuid) + getWarehousesList({ organizationUuid: organizationUuid }) + .then(response => { + commit('SET_WAREHOUSES_LIST', response.warehousesList) + let warehouse = response.warehousesList.find(item => item.uuid === getCurrentWarehouse()) + if (isEmptyValue(warehouse)) { + warehouse = response.warehousesList[0] + } + if (isEmptyValue(warehouse)) { + removeCurrentWarehouse() + commit('SET_WAREHOUSE', undefined) + } else { + setCurrentWarehouse(warehouse.uuid) + commit('SET_WAREHOUSE', warehouse) + } + }) + .catch(error => { + console.warn(`Error ${error.code} getting user info value: ${error.message}.`) + }) + }, + changeWarehouse({ commit, state }, { + warehouseUuid + }) { + setCurrentWarehouse(warehouseUuid) + commit('SET_WAREHOUSE', state.warehousesList.find(warehouse => warehouse.uuid === warehouseUuid)) + }, // dynamically modify permissions - changeRoles({ commit, dispatch }, { + changeRole({ commit, dispatch }, { roleUuid, + organizationUuid, + warehouseUuid, isCloseAllViews = true }) { const route = router.app._route @@ -238,12 +337,12 @@ const actions = { return changeRole({ sessionUuid: getToken(), roleUuid, - organizationUuid: null, - warehouseUuid: null + organizationUuid, + warehouseUuid }) .then(changeRoleResponse => { const { role } = changeRoleResponse - commit('SET_ROL', role) + commit('SET_ROLE', role) setCurrentRole(role.uuid) commit('SET_TOKEN', changeRoleResponse.uuid) setToken(changeRoleResponse.uuid) @@ -303,9 +402,21 @@ const getters = { getRoles: (state) => { return state.rolesList }, - // current rol info - getRol: (state) => { - return state.rol + getOrganizations: (state) => { + return state.organizationsList + }, + getWarehouses: (state) => { + return state.warehousesList + }, + // current role info + getRole: (state) => { + return state.role + }, + getOrganization: (state) => { + return state.organization + }, + getWarehouse: (state) => { + return state.warehouse }, getIsSession: (state) => { return state.isSession @@ -314,7 +425,7 @@ const getters = { return state.userUuid }, getIsPersonalLock: (state) => { - return state.rol.isPersonalLock + return state.role.isPersonalLock } } diff --git a/src/utils/ADempiere/auth.js b/src/utils/ADempiere/auth.js index 55fdcb17..62aa3ee8 100644 --- a/src/utils/ADempiere/auth.js +++ b/src/utils/ADempiere/auth.js @@ -1,6 +1,8 @@ import Cookies from 'js-cookie' const roleKey = 'roleUuid' +const organizationKey = 'organizationUuid' +const warehouseKey = 'warehouseUuid' export function setCurrentRole(currentRole) { return Cookies.set(roleKey, currentRole) @@ -10,6 +12,30 @@ export function getCurrentRole() { return Cookies.get(roleKey) } +export function setCurrentOrganization(currentOrganization) { + return Cookies.set(organizationKey, currentOrganization) +} + +export function getCurrentOrganization() { + return Cookies.get(organizationKey) +} + +export function removeCurrentOrganization() { + return Cookies.remove(organizationKey) +} + +export function setCurrentWarehouse(currentWarehouse) { + return Cookies.set(warehouseKey, currentWarehouse) +} + +export function getCurrentWarehouse() { + return Cookies.get(warehouseKey) +} + +export function removeCurrentWarehouse() { + return Cookies.remove(warehouseKey) +} + export function removeCurrentRole() { return Cookies.remove(roleKey) } diff --git a/src/views/permission/components/SwitchRoles.vue b/src/views/permission/components/SwitchRoles.vue index de1176ad..bc37901e 100644 --- a/src/views/permission/components/SwitchRoles.vue +++ b/src/views/permission/components/SwitchRoles.vue @@ -22,7 +22,7 @@ export default { return this.roles[0] }, set(val) { - this.$store.dispatch('user/changeRoles', val).then(() => { + this.$store.dispatch('user/changeRole', val).then(() => { this.$emit('change') }) } diff --git a/src/views/profile/components/Profile.vue b/src/views/profile/components/Profile.vue index b00eb915..a106c97a 100644 --- a/src/views/profile/components/Profile.vue +++ b/src/views/profile/components/Profile.vue @@ -3,7 +3,7 @@

- {{ getRol.clientName }} + {{ getRole.clientName }}

@@ -33,8 +33,8 @@ export default { } }, computed: { - getRol() { - return this.$store.getters['user/getRol'] + getRole() { + return this.$store.getters['user/getRole'] }, getRoles() { return this.$store.getters['user/getRoles'] diff --git a/src/views/profile/components/RolesNavbar.vue b/src/views/profile/components/RolesNavbar.vue index db8509fb..4ff7bb6d 100644 --- a/src/views/profile/components/RolesNavbar.vue +++ b/src/views/profile/components/RolesNavbar.vue @@ -1,17 +1,45 @@