feat: Allow disconnecting agent bots (#6245)

* Allow disconnecting the bot

* Code Climate fix

* Show error message if exists

* Codeclimate test - rename file

Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
This commit is contained in:
Fayaz Ahmed
2023-01-13 22:28:45 +05:30
committed by GitHub
parent 9e4a5d028c
commit d488a42664
4 changed files with 75 additions and 4 deletions

View File

@@ -15,6 +15,7 @@ export const state = {
isUpdating: false,
isFetchingAgentBot: false,
isSettingAgentBot: false,
isDisconnecting: false,
},
agentBotInbox: {},
};
@@ -119,6 +120,18 @@ export const actions = {
commit(types.SET_AGENT_BOT_UI_FLAG, { isSettingAgentBot: false });
}
},
disconnectBot: async ({ commit }, { inboxId }) => {
commit(types.SET_AGENT_BOT_UI_FLAG, { isDisconnecting: true });
try {
await InboxesAPI.setAgentBot(inboxId, null);
commit(types.SET_AGENT_BOT_INBOX, { agentBotId: '', inboxId });
} catch (error) {
throwErrorMessage(error);
} finally {
commit(types.SET_AGENT_BOT_UI_FLAG, { isDisconnecting: false });
}
},
};
export const mutations = {

View File

@@ -132,4 +132,25 @@ describe('#actions', () => {
]);
});
});
describe('#disconnectBot', () => {
it('sends correct actions if API is success', async () => {
axios.post.mockResolvedValue({ data: {} });
await actions.disconnectBot({ commit }, { inboxId: 2 });
expect(commit.mock.calls).toEqual([
[types.SET_AGENT_BOT_UI_FLAG, { isDisconnecting: true }],
[types.SET_AGENT_BOT_INBOX, { inboxId: 2, agentBotId: '' }],
[types.SET_AGENT_BOT_UI_FLAG, { isDisconnecting: false }],
]);
});
it('sends correct actions if API is error', async () => {
axios.post.mockRejectedValue({ message: 'Incorrect header' });
await expect(
actions.disconnectBot({ commit }, { inboxId: 2, agentBotId: '' })
).rejects.toThrow(Error);
expect(commit.mock.calls).toEqual([
[types.SET_AGENT_BOT_UI_FLAG, { isDisconnecting: true }],
[types.SET_AGENT_BOT_UI_FLAG, { isDisconnecting: false }],
]);
});
});
});