chore: Allow admins to choose the agent bot from the UI (#5895)
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import Vue from 'vue';
|
||||
import * as MutationHelpers from 'shared/helpers/vuex/mutationHelpers';
|
||||
import types from '../mutation-types';
|
||||
import AgentBotsAPI from '../../api/agentBots';
|
||||
import InboxesAPI from '../../api/inboxes';
|
||||
import { throwErrorMessage } from '../utils/api';
|
||||
|
||||
export const state = {
|
||||
@@ -11,7 +13,10 @@ export const state = {
|
||||
isCreating: false,
|
||||
isDeleting: false,
|
||||
isUpdating: false,
|
||||
isFetchingAgentBot: false,
|
||||
isSettingAgentBot: false,
|
||||
},
|
||||
agentBotInbox: {},
|
||||
};
|
||||
|
||||
export const getters = {
|
||||
@@ -25,6 +30,10 @@ export const getters = {
|
||||
const [bot] = $state.records.filter(record => record.id === Number(botId));
|
||||
return bot || {};
|
||||
},
|
||||
getActiveAgentBot: $state => inboxId => {
|
||||
const associatedAgentBotId = $state.agentBotInbox[Number(inboxId)];
|
||||
return getters.getBot($state)(associatedAgentBotId);
|
||||
},
|
||||
};
|
||||
|
||||
export const actions = {
|
||||
@@ -85,6 +94,31 @@ export const actions = {
|
||||
commit(types.SET_AGENT_BOT_UI_FLAG, { isFetchingItem: false });
|
||||
}
|
||||
},
|
||||
|
||||
fetchAgentBotInbox: async ({ commit }, inboxId) => {
|
||||
commit(types.SET_AGENT_BOT_UI_FLAG, { isFetchingAgentBot: true });
|
||||
try {
|
||||
const { data } = await InboxesAPI.getAgentBot(inboxId);
|
||||
const { agent_bot: agentBot = {} } = data || {};
|
||||
commit(types.SET_AGENT_BOT_INBOX, { agentBotId: agentBot.id, inboxId });
|
||||
} catch (error) {
|
||||
throwErrorMessage(error);
|
||||
} finally {
|
||||
commit(types.SET_AGENT_BOT_UI_FLAG, { isFetchingAgentBot: false });
|
||||
}
|
||||
},
|
||||
|
||||
setAgentBotInbox: async ({ commit }, { inboxId, botId }) => {
|
||||
commit(types.SET_AGENT_BOT_UI_FLAG, { isSettingAgentBot: true });
|
||||
try {
|
||||
await InboxesAPI.setAgentBot(inboxId, botId);
|
||||
commit(types.SET_AGENT_BOT_INBOX, { agentBotId: botId, inboxId });
|
||||
} catch (error) {
|
||||
throwErrorMessage(error);
|
||||
} finally {
|
||||
commit(types.SET_AGENT_BOT_UI_FLAG, { isSettingAgentBot: false });
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export const mutations = {
|
||||
@@ -98,6 +132,9 @@ export const mutations = {
|
||||
[types.SET_AGENT_BOTS]: MutationHelpers.set,
|
||||
[types.EDIT_AGENT_BOT]: MutationHelpers.update,
|
||||
[types.DELETE_AGENT_BOT]: MutationHelpers.destroy,
|
||||
[types.SET_AGENT_BOT_INBOX]($state, { agentBotId, inboxId }) {
|
||||
Vue.set($state.agentBotInbox, inboxId, agentBotId);
|
||||
},
|
||||
};
|
||||
|
||||
export default {
|
||||
|
||||
@@ -90,4 +90,46 @@ describe('#actions', () => {
|
||||
]);
|
||||
});
|
||||
});
|
||||
describe('#setAgentBotInbox', () => {
|
||||
it('sends correct actions if API is success', async () => {
|
||||
axios.post.mockResolvedValue({ data: {} });
|
||||
await actions.setAgentBotInbox({ commit }, { inboxId: 2, botId: 3 });
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_AGENT_BOT_UI_FLAG, { isSettingAgentBot: true }],
|
||||
[types.SET_AGENT_BOT_INBOX, { inboxId: 2, agentBotId: 3 }],
|
||||
[types.SET_AGENT_BOT_UI_FLAG, { isSettingAgentBot: false }],
|
||||
]);
|
||||
});
|
||||
it('sends correct actions if API is error', async () => {
|
||||
axios.post.mockRejectedValue({ message: 'Incorrect header' });
|
||||
await expect(
|
||||
actions.setAgentBotInbox({ commit }, { inboxId: 2, botId: 3 })
|
||||
).rejects.toThrow(Error);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_AGENT_BOT_UI_FLAG, { isSettingAgentBot: true }],
|
||||
[types.SET_AGENT_BOT_UI_FLAG, { isSettingAgentBot: false }],
|
||||
]);
|
||||
});
|
||||
});
|
||||
describe('#fetchAgentBotInbox', () => {
|
||||
it('sends correct actions if API is success', async () => {
|
||||
axios.get.mockResolvedValue({ data: { agent_bot: { id: 3 } } });
|
||||
await actions.fetchAgentBotInbox({ commit }, 2);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_AGENT_BOT_UI_FLAG, { isFetchingAgentBot: true }],
|
||||
[types.SET_AGENT_BOT_INBOX, { inboxId: 2, agentBotId: 3 }],
|
||||
[types.SET_AGENT_BOT_UI_FLAG, { isFetchingAgentBot: false }],
|
||||
]);
|
||||
});
|
||||
it('sends correct actions if API is error', async () => {
|
||||
axios.get.mockRejectedValue({ message: 'Incorrect header' });
|
||||
await expect(
|
||||
actions.fetchAgentBotInbox({ commit }, { inboxId: 2, agentBotId: 3 })
|
||||
).rejects.toThrow(Error);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_AGENT_BOT_UI_FLAG, { isFetchingAgentBot: true }],
|
||||
[types.SET_AGENT_BOT_UI_FLAG, { isFetchingAgentBot: false }],
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -41,4 +41,14 @@ describe('#mutations', () => {
|
||||
expect(state.records).toEqual([agentBotRecords[0]]);
|
||||
});
|
||||
});
|
||||
describe('#SET_AGENT_BOT_INBOX', () => {
|
||||
it('set agent bot in the object', () => {
|
||||
const state = { agentBotInbox: {} };
|
||||
mutations[types.SET_AGENT_BOT_INBOX](state, {
|
||||
agentBotId: 2,
|
||||
inboxId: 3,
|
||||
});
|
||||
expect(state.agentBotInbox).toEqual({ 3: 2 });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user