Chore: Scope URLs with account_id (#601)

* Chore: Enable Users to create multiple accounts

Addresses: #402
- migrations to split roles and other attributes from users table
- make changes in code to accommodate this change

Co-authored-by: Pranav Raj Sreepuram <pranavrajs@gmail.com>
This commit is contained in:
Sojan Jose
2020-03-09 23:27:10 +05:30
committed by GitHub
parent 2a6670f0da
commit 19ab0fe108
105 changed files with 480 additions and 402 deletions

View File

@@ -3,9 +3,25 @@
const API_VERSION = `/api/v1`;
class ApiClient {
constructor(url) {
constructor(resource, options = {}) {
this.apiVersion = API_VERSION;
this.url = `${this.apiVersion}/${url}`;
this.options = options;
this.resource = resource;
}
get url() {
let url = this.apiVersion;
if (this.options.accountScoped) {
const isInsideAccountScopedURLs = window.location.pathname.includes(
'/app/accounts'
);
if (isInsideAccountScopedURLs) {
const accountId = window.location.pathname.split('/')[3];
url = `${url}/accounts/${accountId}`;
}
}
return `${url}/${this.resource}`;
}
get() {

View File

@@ -2,7 +2,7 @@ import ApiClient from './ApiClient';
class Agents extends ApiClient {
constructor() {
super('agents');
super('agents', { accountScoped: true });
}
}

View File

@@ -4,7 +4,7 @@ import ApiClient from './ApiClient';
class CannedResponse extends ApiClient {
constructor() {
super('canned_responses');
super('canned_responses', { accountScoped: true });
}
get({ searchKey }) {

View File

@@ -3,7 +3,7 @@ import ApiClient from './ApiClient';
class ContactAPI extends ApiClient {
constructor() {
super('contacts');
super('contacts', { accountScoped: true });
}
getConversations(contactId) {

View File

@@ -3,7 +3,7 @@ import ApiClient from './ApiClient';
class ConversationApi extends ApiClient {
constructor() {
super('conversations');
super('conversations', { accountScoped: true });
}
getLabels(conversationID) {

View File

@@ -28,23 +28,10 @@ const endPoints = {
},
fetchFacebookPages: {
url: 'api/v1/callbacks/get_facebook_pages.json',
url: 'api/v1/callbacks/facebook_pages.json',
params: { omniauth_token: '' },
},
reports: {
account(metric, from, to) {
return {
url: `/api/v1/reports/account?metric=${metric}&since=${from}&to=${to}`,
};
},
accountSummary(accountId, from, to) {
return {
url: `/api/v1/reports/${accountId}/account_summary?since=${from}&to=${to}`,
};
},
},
subscriptions: {
get() {
return {

View File

@@ -3,7 +3,7 @@ import ApiClient from '../ApiClient';
class ConversationApi extends ApiClient {
constructor() {
super('conversations');
super('conversations', { accountScoped: true });
}
get({ inboxId, status, assigneeType, page }) {

View File

@@ -4,7 +4,7 @@ import ApiClient from '../ApiClient';
class MessageApi extends ApiClient {
constructor() {
super('conversations');
super('conversations', { accountScoped: true });
}
create({ conversationId, message, private: isPrivate }) {

View File

@@ -3,7 +3,7 @@ import ApiClient from './ApiClient';
class InboxMembers extends ApiClient {
constructor() {
super('inbox_members');
super('inbox_members', { accountScoped: true });
}
create({ inboxId, agentList }) {

View File

@@ -2,7 +2,7 @@ import ApiClient from './ApiClient';
class Inboxes extends ApiClient {
constructor() {
super('inboxes');
super('inboxes', { accountScoped: true });
}
}

View File

@@ -1,14 +1,22 @@
/* global axios */
import ApiClient from './ApiClient';
import endPoints from './endPoints';
class ReportsAPI extends ApiClient {
constructor() {
super('reports', { accountScoped: true });
}
export default {
getAccountReports(metric, from, to) {
const { url } = endPoints('reports').account(metric, from, to);
return axios.get(url);
},
getAccountSummary(accountId, from, to) {
const urlData = endPoints('reports').accountSummary(accountId, from, to);
return axios.get(urlData.url);
},
};
getAccountReports(metric, since, until) {
return axios.get(`${this.url}/account`, {
params: { metric, since, until },
});
}
getAccountSummary(accountId, since, until) {
return axios.get(`${this.url}/${accountId}/account_summary`, {
params: { since, until },
});
}
}
export default new ReportsAPI();

View File

@@ -3,7 +3,7 @@ import ApiClient from './ApiClient';
class UserNotificationSettings extends ApiClient {
constructor() {
super('user/notification_settings');
super('notification_settings', { accountScoped: true });
}
update(params) {

View File

@@ -2,7 +2,7 @@ import ApiClient from './ApiClient';
class WebHooks extends ApiClient {
constructor() {
super('account/webhooks');
super('webhooks', { accountScoped: true });
}
}

View File

@@ -43,7 +43,7 @@
>
<ul class="vertical dropdown menu">
<li>
<router-link to="/app/profile/settings">
<router-link :to="`/app/accounts/${accountId}/profile/settings`">
{{ $t('SIDEBAR.PROFILE_SETTINGS') }}
</router-link>
</li>
@@ -144,18 +144,18 @@ export default {
newLink: true,
key: 'inbox',
cssClass: 'menu-title align-justify',
toState: frontendURL('settings/inboxes'),
toState: frontendURL(`accounts/${this.accountId}/settings/inboxes`),
toStateName: 'settings_inbox_list',
children: this.inboxes.map(inbox => ({
id: inbox.id,
label: inbox.name,
toState: frontendURL(`inbox/${inbox.id}`),
toState: frontendURL(`accounts/${this.accountId}/inbox/${inbox.id}`),
type: inbox.channel_type,
})),
};
},
dashboardPath() {
return frontendURL('dashboard');
return frontendURL(`accounts/${this.accountId}/dashboard`);
},
shouldShowStatusBox() {
return (
@@ -176,6 +176,9 @@ export default {
trialMessage() {
return `${this.daysLeft} ${this.$t('APP_GLOBAL.TRIAL_MESSAGE')}`;
},
accountId() {
return this.currentUser.account_id;
},
},
mounted() {
this.$store.dispatch('inboxes/get');

View File

@@ -74,6 +74,7 @@ export default {
currentChat: 'getSelectedChat',
inboxesList: 'inboxes/getInboxes',
activeInbox: 'getSelectedInbox',
currentUser: 'getCurrentUser',
}),
isActiveChat() {
@@ -96,7 +97,11 @@ export default {
methods: {
cardClick(chat) {
const { activeInbox } = this;
const path = conversationUrl(activeInbox, chat.id);
const path = conversationUrl(
this.currentUser.account_id,
activeInbox,
chat.id
);
router.push({ path: frontendURL(path) });
},
extractMessageText(chatItem) {

View File

@@ -5,9 +5,9 @@ export const frontendURL = (path, params) => {
return `/app/${path}${stringifiedParams}`;
};
export const conversationUrl = (activeInbox, id) => {
export const conversationUrl = (accountId, activeInbox, id) => {
const path = activeInbox
? `inbox/${activeInbox}/conversations/${id}`
: `conversations/${id}`;
? `accounts/${accountId}/inbox/${activeInbox}/conversations/${id}`
: `accounts/${accountId}/conversations/${id}`;
return path;
};

View File

@@ -3,10 +3,14 @@ import { frontendURL, conversationUrl } from '../URLHelper';
describe('#URL Helpers', () => {
describe('conversationUrl', () => {
it('should return direct conversation URL if activeInbox is nil', () => {
expect(conversationUrl(undefined, 1)).toBe('conversations/1');
expect(conversationUrl(1, undefined, 1)).toBe(
'accounts/1/conversations/1'
);
});
it('should return ibox conversation URL if activeInbox is not nil', () => {
expect(conversationUrl(2, 1)).toBe('inbox/2/conversations/1');
expect(conversationUrl(1, 2, 1)).toBe(
'accounts/1/inbox/2/conversations/1'
);
});
});

View File

@@ -1,4 +1,8 @@
import { frontendURL } from '../helper/URLHelper';
import auth from '../api/auth';
const user = auth.getCurrentUser() || {};
const accountId = user.account_id;
export default {
common: {
@@ -18,7 +22,7 @@ export default {
label: 'Conversations',
hasSubMenu: false,
key: '',
toState: frontendURL('dashboard'),
toState: frontendURL(`accounts/${accountId}/dashboard`),
toolTip: 'Conversation from all subscribed inboxes',
toStateName: 'home',
},
@@ -26,14 +30,14 @@ export default {
icon: 'ion-arrow-graph-up-right',
label: 'Reports',
hasSubMenu: false,
toState: frontendURL('reports'),
toState: frontendURL(`accounts/${accountId}/reports`),
toStateName: 'settings_account_reports',
},
settings: {
icon: 'ion-settings',
label: 'Settings',
hasSubMenu: false,
toState: frontendURL('settings'),
toState: frontendURL(`accounts/${accountId}/settings`),
toStateName: 'settings_home',
},
},
@@ -61,41 +65,43 @@ export default {
label: 'Home',
hasSubMenu: false,
toStateName: 'home',
toState: frontendURL('dashboard'),
toState: frontendURL(`accounts/${accountId}/dashboard`),
},
agents: {
icon: 'ion-person-stalker',
label: 'Agents',
hasSubMenu: false,
toState: frontendURL('settings/agents/list'),
toState: frontendURL(`accounts/${accountId}/settings/agents/list`),
toStateName: 'agent_list',
},
inboxes: {
icon: 'ion-archive',
label: 'Inboxes',
hasSubMenu: false,
toState: frontendURL('settings/inboxes/list'),
toState: frontendURL(`accounts/${accountId}/settings/inboxes/list`),
toStateName: 'settings_inbox_list',
},
cannedResponses: {
icon: 'ion-chatbox-working',
label: 'Canned Responses',
hasSubMenu: false,
toState: frontendURL('settings/canned-response/list'),
toState: frontendURL(
`accounts/${accountId}/settings/canned-response/list`
),
toStateName: 'canned_list',
},
billing: {
icon: 'ion-card',
label: 'Billing',
hasSubMenu: false,
toState: frontendURL('settings/billing'),
toState: frontendURL(`accounts/${accountId}/settings/billing`),
toStateName: 'billing',
},
settings_integrations: {
icon: 'ion-flash',
label: 'Integrations',
hasSubMenu: false,
toState: frontendURL('settings/integrations'),
toState: frontendURL(`accounts/${accountId}/settings/integrations`),
toStateName: 'settings_integrations',
},
},

View File

@@ -22,9 +22,9 @@ export default {
Auth.verifyPasswordToken({
confirmationToken: this.confirmationToken
}).then(res => {
window.location = res.data.redirect_url;
window.location = '/';
}).catch(res => {
window.location = res.data.redirect_url;
window.location = '/';
});
}
}

View File

@@ -118,7 +118,7 @@ export default {
Auth.setNewPassword(credentials)
.then(res => {
if (res.status === 200) {
window.location = res.data.redirect_url;
window.location = '/';
}
})
.catch(() => {

View File

@@ -63,7 +63,7 @@
</form>
<div class="column text-center sigin--footer">
<span>Already have an account?</span>
<router-link to="auth/login">
<router-link to="/app/login">
{{ $t('LOGIN.TITLE') }}
</router-link>
</div>
@@ -77,7 +77,6 @@
import { required, minLength, email } from 'vuelidate/lib/validators';
import Auth from '../../api/auth';
import { frontendURL } from '../../helper/URLHelper';
export default {
data() {
@@ -118,7 +117,7 @@ export default {
Auth.register(this.credentials)
.then(res => {
if (res.status === 200) {
window.location = frontendURL('dashboard');
window.location = '/';
}
})
.catch(error => {

View File

@@ -5,7 +5,7 @@ import { frontendURL } from '../../../helper/URLHelper';
export default {
routes: [
{
path: frontendURL('dashboard'),
path: frontendURL('accounts/:accountId/dashboard'),
name: 'home',
roles: ['administrator', 'agent'],
component: ConversationView,
@@ -14,7 +14,7 @@ export default {
},
},
{
path: frontendURL('inbox/:inbox_id'),
path: frontendURL('accounts/:accountId/inbox/:inbox_id'),
name: 'inbox_dashboard',
roles: ['administrator', 'agent'],
component: ConversationView,
@@ -23,7 +23,7 @@ export default {
},
},
{
path: frontendURL('conversations/:conversation_id'),
path: frontendURL('accounts/:accountId/conversations/:conversation_id'),
name: 'inbox_conversation',
roles: ['administrator', 'agent'],
component: ConversationView,
@@ -32,7 +32,9 @@ export default {
},
},
{
path: frontendURL('inbox/:inbox_id/conversations/:conversation_id'),
path: frontendURL(
'accounts/:accountId/inbox/:inbox_id/conversations/:conversation_id'
),
name: 'conversation_through_inbox',
roles: ['administrator', 'agent'],
component: ConversationView,

View File

@@ -6,7 +6,7 @@ import { frontendURL } from '../../helper/URLHelper';
export default {
routes: [
{
path: frontendURL(''),
path: frontendURL('accounts/:account_id'),
component: AppContainer,
children: [...conversation.routes, ...settings.routes],
},

View File

@@ -5,7 +5,7 @@ import { frontendURL } from '../../../../helper/URLHelper';
export default {
routes: [
{
path: frontendURL('settings/agents'),
path: frontendURL('accounts/:accountId/settings/agents'),
component: SettingsContent,
props: {
headerTitle: 'AGENT_MGMT.HEADER',

View File

@@ -6,7 +6,7 @@ import { frontendURL } from '../../../../helper/URLHelper';
export default {
routes: [
{
path: frontendURL('settings/billing'),
path: frontendURL('accounts/:accountId/settings/billing'),
component: SettingsContent,
props: {
headerTitle: 'BILLING.HEADER',

View File

@@ -5,7 +5,7 @@ import { frontendURL } from '../../../../helper/URLHelper';
export default {
routes: [
{
path: frontendURL('settings/canned-response'),
path: frontendURL('accounts/:accountId/settings/canned-response'),
component: SettingsContent,
props: {
headerTitle: 'CANNED_MGMT.HEADER',

View File

@@ -47,7 +47,11 @@
<!-- Action Buttons -->
<td>
<div class="button-wrapper">
<router-link :to="`/app/settings/inboxes/${item.id}`">
<router-link
:to="
`/app/accounts/${accountId}/settings/inboxes/${item.id}`
"
>
<woot-submit-button
v-if="isAdmin()"
:button-text="$t('INBOX_MGMT.SETTINGS')"
@@ -101,6 +105,7 @@ import Settings from './Settings';
import DeleteInbox from './DeleteInbox';
import adminMixin from '../../../../mixins/isAdmin';
import { frontendURL } from '../../../../helper/URLHelper';
import auth from '../../../../api/auth';
export default {
components: {
@@ -136,6 +141,9 @@ export default {
this.selectedInbox.name
} ?`;
},
accountId() {
return auth.getCurrentUser().account_id;
},
},
methods: {
openSettings(inbox) {

View File

@@ -12,7 +12,7 @@ import { frontendURL } from '../../../../helper/URLHelper';
export default {
routes: [
{
path: frontendURL('settings/inboxes'),
path: frontendURL('accounts/:accountId/settings/inboxes'),
component: SettingsContent,
props: params => {
const showBackButton = params.name !== 'settings_inbox_list';

View File

@@ -17,7 +17,13 @@
</p>
</div>
<div class="small-2 column button-wrap">
<router-link :to="frontendURL('settings/integrations/webhook')">
<router-link
:to="
frontendURL(
`accounts/${accountId}/settings/integrations/webhook`
)
"
>
<button class="button success nice">
{{ $t('INTEGRATION_SETTINGS.WEBHOOK.CONFIGURE') }}
</button>
@@ -34,9 +40,18 @@
</div>
</template>
<script>
import { mapGetters } from 'vuex';
import { frontendURL } from '../../../../helper/URLHelper';
export default {
computed: {
...mapGetters({
currentUser: 'getCurrentUser',
}),
accountId() {
return this.currentUser.account_id;
},
},
methods: {
frontendURL,
},

View File

@@ -6,7 +6,7 @@ import { frontendURL } from '../../../../helper/URLHelper';
export default {
routes: [
{
path: frontendURL('settings/integrations'),
path: frontendURL('accounts/:accountId/settings/integrations'),
component: SettingsContent,
props: params => {
const showBackButton = params.name !== 'settings_integrations';

View File

@@ -5,7 +5,7 @@ import { frontendURL } from '../../../../helper/URLHelper';
export default {
routes: [
{
path: frontendURL('profile'),
path: frontendURL('accounts/:accountId/profile'),
name: 'profile_settings',
roles: ['administrator', 'agent'],
component: SettingsContent,

View File

@@ -5,7 +5,7 @@ import { frontendURL } from '../../../../helper/URLHelper';
export default {
routes: [
{
path: frontendURL('reports'),
path: frontendURL('accounts/:accountId/reports'),
component: SettingsContent,
props: {
headerTitle: 'REPORT.HEADER',

View File

@@ -11,14 +11,14 @@ import integrations from './integrations/integrations.routes';
export default {
routes: [
{
path: frontendURL('settings'),
path: frontendURL('accounts/:accountId/settings'),
name: 'settings_home',
roles: ['administrator', 'agent'],
redirect: () => {
if (Auth.isAdmin()) {
return frontendURL('settings/agents');
return frontendURL('accounts/:accountId/settings/agents');
}
return frontendURL('settings/canned-response');
return frontendURL('accounts/:accountId/settings/canned-response');
},
},
...agent.routes,

View File

@@ -7,13 +7,14 @@ import dashboard from './dashboard/dashboard.routes';
import authRoute from './auth/auth.routes';
import { frontendURL } from '../helper/URLHelper';
const loggedInUser = auth.getCurrentUser() || {};
const routes = [
...login.routes,
...dashboard.routes,
...authRoute.routes,
{
path: '/',
redirect: frontendURL('dashboard'),
redirect: frontendURL(`accounts/${loggedInUser.account_id}/dashboard`),
},
];
@@ -102,7 +103,8 @@ const validateRouteAccess = (to, from, next) => {
to.meta &&
to.meta.requireSignupEnabled
) {
next(frontendURL('dashboard'));
const user = auth.getCurrentUser();
next(frontendURL(`accounts/${user.account_id}/dashboard`));
}
if (authIgnoreRoutes.includes(to.name)) {
@@ -114,7 +116,8 @@ const validateRouteAccess = (to, from, next) => {
// protecting routes
router.beforeEach((to, from, next) => {
if (!to.name) {
return next(frontendURL('dashboard'));
const user = auth.getCurrentUser();
return next(frontendURL(`accounts/${user.account_id}/dashboard`));
}
return validateRouteAccess(to, from, next);

View File

@@ -3,7 +3,6 @@ import axios from 'axios';
import moment from 'moment';
import Vue from 'vue';
import * as types from '../mutation-types';
import router from '../../routes';
import authAPI from '../../api/auth';
import createAxios from '../../helper/APIHelper';
import actionCable from '../../helper/actionCable';
@@ -65,7 +64,7 @@ export const actions = {
commit(types.default.SET_CURRENT_USER);
window.axios = createAxios(axios);
actionCable.init(Vue);
router.replace({ name: 'home' });
window.location = '/';
resolve();
})
.catch(error => {