fix: Update the relevant agent presence only (#5220)

This commit is contained in:
Pranav Raj S
2022-08-09 12:32:09 +05:30
committed by GitHub
parent cbcee6414c
commit 657bd44418
11 changed files with 100 additions and 62 deletions

View File

@@ -35,9 +35,6 @@ export const getters = {
};
return status;
},
getAgentsCount($state) {
return $state.records.length;
},
};
export const actions = {
@@ -73,14 +70,15 @@ export const actions = {
throw new Error(error);
}
},
updatePresence: async ({ commit, dispatch }, data) => {
commit(types.default.SET_AGENT_UPDATING_STATUS, true);
commit(types.default.UPDATE_AGENTS_PRESENCE, data);
dispatch('updateReportAgentStatus', data, { root: true });
commit(types.default.SET_AGENT_UPDATING_STATUS, false);
updateSingleAgentPresence: ({ commit }, { id, availabilityStatus }) => {
commit(types.default.UPDATE_SINGLE_AGENT_PRESENCE, {
id,
availabilityStatus,
});
},
updatePresence: async ({ commit }, data) => {
commit(types.default.UPDATE_AGENTS_PRESENCE, data);
},
delete: async ({ commit }, agentId) => {
commit(types.default.SET_AGENT_DELETING_STATUS, true);
try {
@@ -113,6 +111,14 @@ export const mutations = {
[types.default.EDIT_AGENT]: MutationHelpers.update,
[types.default.DELETE_AGENT]: MutationHelpers.destroy,
[types.default.UPDATE_AGENTS_PRESENCE]: MutationHelpers.updatePresence,
[types.default.UPDATE_SINGLE_AGENT_PRESENCE]: (
$state,
{ id, availabilityStatus }
) =>
MutationHelpers.updateSingleRecordPresence($state.records, {
id,
availabilityStatus,
}),
};
export default {

View File

@@ -159,7 +159,10 @@ export const actions = {
const userData = response.data;
const { id } = userData;
commit(types.SET_CURRENT_USER, response.data);
dispatch('agents/updatePresence', { [id]: params.availability });
dispatch('agents/updateSingleAgentPresence', {
id,
availabilityStatus: params.availability,
});
} catch (error) {
// Ignore error
}

View File

@@ -1,10 +1,6 @@
/* eslint no-console: 0 */
/* eslint no-param-reassign: 0 */
/* eslint no-shadow: 0 */
import * as types from '../mutation-types';
import Report from '../../api/reports';
import Vue from 'vue';
import { downloadCsvFile } from '../../helper/downloadHelper';
const state = {
@@ -116,9 +112,6 @@ export const actions = {
commit(types.default.TOGGLE_AGENT_CONVERSATION_METRIC_LOADING, false);
});
},
updateReportAgentStatus({ commit }, data) {
commit(types.default.UPDATE_REPORT_AGENTS_STATUS, data);
},
downloadAgentReports(_, reportObj) {
return Report.getAgentReports(reportObj)
.then(response => {
@@ -179,16 +172,6 @@ const mutations = {
[types.default.TOGGLE_AGENT_CONVERSATION_METRIC_LOADING](_state, flag) {
_state.overview.uiFlags.isFetchingAgentConversationMetric = flag;
},
[types.default.UPDATE_REPORT_AGENTS_STATUS](_state, data) {
_state.overview.agentConversationMetric.forEach((element, index) => {
const availabilityStatus = data[element.id];
Vue.set(
_state.overview.agentConversationMetric[index],
'availability',
availabilityStatus || 'offline'
);
});
},
};
export default {

View File

@@ -96,13 +96,21 @@ describe('#actions', () => {
});
describe('#updatePresence', () => {
it('sends correct actions if API is success', async () => {
it('sends correct actions', async () => {
const data = { users: { 1: 'online' }, contacts: { 2: 'online' } };
actions.updatePresence({ commit, dispatch }, data);
expect(commit.mock.calls).toEqual([
[types.default.SET_AGENT_UPDATING_STATUS, true],
[types.default.UPDATE_AGENTS_PRESENCE, data],
[types.default.SET_AGENT_UPDATING_STATUS, false],
]);
});
});
describe('#updateSingleAgentPresence', () => {
it('sends correct actions', async () => {
const data = { id: 1, availabilityStatus: 'online' };
actions.updateSingleAgentPresence({ commit, dispatch }, data);
expect(commit.mock.calls).toEqual([
[types.default.UPDATE_SINGLE_AGENT_PRESENCE, data],
]);
});
});

View File

@@ -129,19 +129,4 @@ describe('#getters', () => {
offline: 1,
});
});
it('getAgentStatus', () => {
const state = {
records: [
{
id: 1,
name: 'Agent 1',
email: 'agent1@chatwoot.com',
confirmed: true,
availability_status: 'online',
},
],
};
expect(getters.getAgentsCount(state)).toEqual(1);
});
});

View File

@@ -97,4 +97,44 @@ describe('#mutations', () => {
]);
});
});
describe('#UPDATE_SINGLE_AGENT_PRESENCE', () => {
it('updates single agent presence', () => {
const state = {
records: [
{
id: 1,
name: 'Agent1',
email: 'agent1@chatwoot.com',
availability_status: 'offline',
},
{
id: 2,
name: 'Agent1',
email: 'agent1@chatwoot.com',
availability_status: 'online',
},
],
};
mutations[types.default.UPDATE_SINGLE_AGENT_PRESENCE](state, {
id: 1,
availabilityStatus: 'busy',
});
expect(state.records).toEqual([
{
id: 1,
name: 'Agent1',
email: 'agent1@chatwoot.com',
availability_status: 'busy',
},
{
id: 2,
name: 'Agent1',
email: 'agent1@chatwoot.com',
availability_status: 'online',
},
]);
});
});
});

View File

@@ -80,7 +80,10 @@ describe('#actions', () => {
],
]);
expect(dispatch.mock.calls).toEqual([
['agents/updatePresence', { 1: 'offline' }],
[
'agents/updateSingleAgentPresence',
{ availabilityStatus: 'offline', id: 1 },
],
]);
});
});