diff --git a/app/javascript/dashboard/helper/URLHelper.js b/app/javascript/dashboard/helper/URLHelper.js index 66a376962..04bd4104e 100644 --- a/app/javascript/dashboard/helper/URLHelper.js +++ b/app/javascript/dashboard/helper/URLHelper.js @@ -5,16 +5,31 @@ export const frontendURL = (path, params) => { return `/app/${path}${stringifiedParams}`; }; -export const getLoginRedirectURL = (ssoAccountId, user) => { +const getSSOAccountPath = ({ ssoAccountId, user }) => { const { accounts = [] } = user || {}; const ssoAccount = accounts.find( account => account.id === Number(ssoAccountId) ); + let accountPath = ''; if (ssoAccount) { - return frontendURL(`accounts/${ssoAccountId}/dashboard`); + accountPath = `accounts/${ssoAccountId}`; + } else if (accounts.length) { + accountPath = `accounts/${accounts[0].id}`; } - if (accounts.length) { - return frontendURL(`accounts/${accounts[0].id}/dashboard`); + return accountPath; +}; + +export const getLoginRedirectURL = ({ + ssoAccountId, + ssoConversationId, + user, +}) => { + const accountPath = getSSOAccountPath({ ssoAccountId, user }); + if (accountPath) { + if (ssoConversationId) { + return frontendURL(`${accountPath}/conversations/${ssoConversationId}`); + } + return frontendURL(`${accountPath}/dashboard`); } return DEFAULT_REDIRECT_URL; }; diff --git a/app/javascript/dashboard/helper/specs/URLHelper.spec.js b/app/javascript/dashboard/helper/specs/URLHelper.spec.js index aab570280..088f384d6 100644 --- a/app/javascript/dashboard/helper/specs/URLHelper.spec.js +++ b/app/javascript/dashboard/helper/specs/URLHelper.spec.js @@ -74,17 +74,37 @@ describe('#URL Helpers', () => { describe('getLoginRedirectURL', () => { it('should return correct Account URL if account id is present', () => { expect( - getLoginRedirectURL('7500', { - accounts: [{ id: 7500, name: 'Test Account 7500' }], + getLoginRedirectURL({ + ssoAccountId: '7500', + user: { + accounts: [{ id: 7500, name: 'Test Account 7500' }], + }, }) ).toBe('/app/accounts/7500/dashboard'); }); - it('should return default URL if account id is not present', () => { - expect(getLoginRedirectURL('7500', {})).toBe('/app/'); + it('should return correct conversation URL if account id and conversationId is present', () => { expect( - getLoginRedirectURL('7500', { - accounts: [{ id: '7501', name: 'Test Account 7501' }], + getLoginRedirectURL({ + ssoAccountId: '7500', + ssoConversationId: '752', + user: { + accounts: [{ id: 7500, name: 'Test Account 7500' }], + }, + }) + ).toBe('/app/accounts/7500/conversations/752'); + }); + + it('should return default URL if account id is not present', () => { + expect(getLoginRedirectURL({ ssoAccountId: '7500', user: {} })).toBe( + '/app/' + ); + expect( + getLoginRedirectURL({ + ssoAccountId: '7500', + user: { + accounts: [{ id: '7501', name: 'Test Account 7501' }], + }, }) ).toBe('/app/accounts/7501/dashboard'); expect(getLoginRedirectURL('7500', null)).toBe('/app/'); diff --git a/app/javascript/dashboard/routes/login/Login.vue b/app/javascript/dashboard/routes/login/Login.vue index c0626a87c..eac42730c 100644 --- a/app/javascript/dashboard/routes/login/Login.vue +++ b/app/javascript/dashboard/routes/login/Login.vue @@ -80,7 +80,7 @@ export default { props: { ssoAuthToken: { type: String, default: '' }, ssoAccountId: { type: String, default: '' }, - redirectUrl: { type: String, default: '' }, + ssoConversationId: { type: String, default: '' }, config: { type: String, default: '' }, email: { type: String, default: '' }, }, @@ -139,6 +139,7 @@ export default { password: this.credentials.password, sso_auth_token: this.ssoAuthToken, ssoAccountId: this.ssoAccountId, + ssoConversationId: this.ssoConversationId, }; this.$store .dispatch('login', credentials) diff --git a/app/javascript/dashboard/routes/login/login.routes.js b/app/javascript/dashboard/routes/login/login.routes.js index f43e632bf..40038a154 100644 --- a/app/javascript/dashboard/routes/login/login.routes.js +++ b/app/javascript/dashboard/routes/login/login.routes.js @@ -11,8 +11,8 @@ export default { config: route.query.config, email: route.query.email, ssoAuthToken: route.query.sso_auth_token, - redirectUrl: route.query.route_url, ssoAccountId: route.query.sso_account_id, + ssoConversationId: route.query.sso_conversation_id, }), }, ], diff --git a/app/javascript/dashboard/store/modules/auth.js b/app/javascript/dashboard/store/modules/auth.js index cb28f0d01..4c69c8519 100644 --- a/app/javascript/dashboard/store/modules/auth.js +++ b/app/javascript/dashboard/store/modules/auth.js @@ -84,12 +84,16 @@ export const getters = { // actions export const actions = { - login(_, { ssoAccountId, ...credentials }) { + login(_, { ssoAccountId, ssoConversationId, ...credentials }) { return new Promise((resolve, reject) => { authAPI .login(credentials) .then(response => { - window.location = getLoginRedirectURL(ssoAccountId, response.data); + window.location = getLoginRedirectURL({ + ssoAccountId, + ssoConversationId, + user: response.data, + }); resolve(); }) .catch(error => {