feat: Agent assignment policy Create/Edit pages (#12400)
This commit is contained in:
@@ -3,6 +3,7 @@ import types from '../mutation-types';
|
||||
import AssignmentPoliciesAPI from '../../api/assignmentPolicies';
|
||||
import { throwErrorMessage } from '../utils/api';
|
||||
import camelcaseKeys from 'camelcase-keys';
|
||||
import snakecaseKeys from 'snakecase-keys';
|
||||
|
||||
export const state = {
|
||||
records: [],
|
||||
@@ -15,6 +16,7 @@ export const state = {
|
||||
},
|
||||
inboxUiFlags: {
|
||||
isFetching: false,
|
||||
isDeleting: false,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -51,7 +53,7 @@ export const actions = {
|
||||
try {
|
||||
const response = await AssignmentPoliciesAPI.show(policyId);
|
||||
const policy = camelcaseKeys(response.data);
|
||||
commit(types.EDIT_ASSIGNMENT_POLICY, policy);
|
||||
commit(types.SET_ASSIGNMENT_POLICY, policy);
|
||||
} catch (error) {
|
||||
throwErrorMessage(error);
|
||||
} finally {
|
||||
@@ -62,7 +64,9 @@ export const actions = {
|
||||
create: async function create({ commit }, policyObj) {
|
||||
commit(types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isCreating: true });
|
||||
try {
|
||||
const response = await AssignmentPoliciesAPI.create(policyObj);
|
||||
const response = await AssignmentPoliciesAPI.create(
|
||||
snakecaseKeys(policyObj)
|
||||
);
|
||||
commit(types.ADD_ASSIGNMENT_POLICY, camelcaseKeys(response.data));
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
@@ -76,7 +80,10 @@ export const actions = {
|
||||
update: async function update({ commit }, { id, ...policyParams }) {
|
||||
commit(types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isUpdating: true });
|
||||
try {
|
||||
const response = await AssignmentPoliciesAPI.update(id, policyParams);
|
||||
const response = await AssignmentPoliciesAPI.update(
|
||||
id,
|
||||
snakecaseKeys(policyParams)
|
||||
);
|
||||
commit(types.EDIT_ASSIGNMENT_POLICY, camelcaseKeys(response.data));
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
@@ -117,6 +124,68 @@ export const actions = {
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
setInboxPolicy: async function setInboxPolicy(
|
||||
{ commit },
|
||||
{ inboxId, policyId }
|
||||
) {
|
||||
try {
|
||||
const response = await AssignmentPoliciesAPI.setInboxPolicy(
|
||||
inboxId,
|
||||
policyId
|
||||
);
|
||||
commit(
|
||||
types.ADD_ASSIGNMENT_POLICIES_INBOXES,
|
||||
camelcaseKeys(response.data)
|
||||
);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throwErrorMessage(error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
getInboxPolicy: async function getInboxPolicy(_, { inboxId }) {
|
||||
try {
|
||||
const response = await AssignmentPoliciesAPI.getInboxPolicy(inboxId);
|
||||
return camelcaseKeys(response.data);
|
||||
} catch (error) {
|
||||
throwErrorMessage(error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
updateInboxPolicy: async function updateInboxPolicy({ commit }, { policy }) {
|
||||
try {
|
||||
commit(types.EDIT_ASSIGNMENT_POLICY, policy);
|
||||
} catch (error) {
|
||||
throwErrorMessage(error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
removeInboxPolicy: async function removeInboxPolicy(
|
||||
{ commit },
|
||||
{ policyId, inboxId }
|
||||
) {
|
||||
commit(types.SET_ASSIGNMENT_POLICIES_INBOXES_UI_FLAG, {
|
||||
isDeleting: true,
|
||||
});
|
||||
try {
|
||||
await AssignmentPoliciesAPI.removeInboxPolicy(inboxId);
|
||||
commit(types.DELETE_ASSIGNMENT_POLICIES_INBOXES, {
|
||||
policyId,
|
||||
inboxId,
|
||||
});
|
||||
} catch (error) {
|
||||
throwErrorMessage(error);
|
||||
throw error;
|
||||
} finally {
|
||||
commit(types.SET_ASSIGNMENT_POLICIES_INBOXES_UI_FLAG, {
|
||||
isDeleting: false,
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export const mutations = {
|
||||
@@ -128,8 +197,9 @@ export const mutations = {
|
||||
},
|
||||
|
||||
[types.SET_ASSIGNMENT_POLICIES]: MutationHelpers.set,
|
||||
[types.SET_ASSIGNMENT_POLICY]: MutationHelpers.setSingleRecord,
|
||||
[types.ADD_ASSIGNMENT_POLICY]: MutationHelpers.create,
|
||||
[types.EDIT_ASSIGNMENT_POLICY]: MutationHelpers.update,
|
||||
[types.EDIT_ASSIGNMENT_POLICY]: MutationHelpers.updateAttributes,
|
||||
[types.DELETE_ASSIGNMENT_POLICY]: MutationHelpers.destroy,
|
||||
|
||||
[types.SET_ASSIGNMENT_POLICIES_INBOXES_UI_FLAG](_state, data) {
|
||||
@@ -138,13 +208,19 @@ export const mutations = {
|
||||
...data,
|
||||
};
|
||||
},
|
||||
|
||||
[types.SET_ASSIGNMENT_POLICIES_INBOXES](_state, { policyId, inboxes }) {
|
||||
const policy = _state.records.find(p => p.id === policyId);
|
||||
if (policy) {
|
||||
policy.inboxes = inboxes;
|
||||
}
|
||||
},
|
||||
[types.DELETE_ASSIGNMENT_POLICIES_INBOXES](_state, { policyId, inboxId }) {
|
||||
const policy = _state.records.find(p => p.id === policyId);
|
||||
if (policy) {
|
||||
policy.inboxes = policy?.inboxes?.filter(inbox => inbox.id !== inboxId);
|
||||
}
|
||||
},
|
||||
[types.ADD_ASSIGNMENT_POLICIES_INBOXES]: MutationHelpers.updateAttributes,
|
||||
};
|
||||
|
||||
export default {
|
||||
|
||||
@@ -29,6 +29,9 @@ export const getters = {
|
||||
getInboxes($state) {
|
||||
return $state.records;
|
||||
},
|
||||
getAllInboxes($state) {
|
||||
return camelcaseKeys($state.records, { deep: true });
|
||||
},
|
||||
getWhatsAppTemplates: $state => inboxId => {
|
||||
const [inbox] = $state.records.filter(
|
||||
record => record.id === Number(inboxId)
|
||||
|
||||
@@ -3,12 +3,14 @@ import { actions } from '../../assignmentPolicies';
|
||||
import types from '../../../mutation-types';
|
||||
import assignmentPoliciesList, { camelCaseFixtures } from './fixtures';
|
||||
import camelcaseKeys from 'camelcase-keys';
|
||||
import snakecaseKeys from 'snakecase-keys';
|
||||
|
||||
const commit = vi.fn();
|
||||
|
||||
global.axios = axios;
|
||||
vi.mock('axios');
|
||||
vi.mock('camelcase-keys');
|
||||
vi.mock('snakecase-keys');
|
||||
vi.mock('../../../utils/api');
|
||||
|
||||
describe('#actions', () => {
|
||||
@@ -56,7 +58,7 @@ describe('#actions', () => {
|
||||
expect(camelcaseKeys).toHaveBeenCalledWith(policyData);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isFetchingItem: true }],
|
||||
[types.EDIT_ASSIGNMENT_POLICY, camelCasedPolicy],
|
||||
[types.SET_ASSIGNMENT_POLICY, camelCasedPolicy],
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isFetchingItem: false }],
|
||||
]);
|
||||
});
|
||||
@@ -77,12 +79,15 @@ describe('#actions', () => {
|
||||
it('sends correct actions if API is success', async () => {
|
||||
const newPolicy = assignmentPoliciesList[0];
|
||||
const camelCasedData = camelCaseFixtures[0];
|
||||
const snakeCasedPolicy = { assignment_order: 'round_robin' };
|
||||
|
||||
axios.post.mockResolvedValue({ data: newPolicy });
|
||||
camelcaseKeys.mockReturnValue(camelCasedData);
|
||||
snakecaseKeys.mockReturnValue(snakeCasedPolicy);
|
||||
|
||||
const result = await actions.create({ commit }, newPolicy);
|
||||
|
||||
expect(snakecaseKeys).toHaveBeenCalledWith(newPolicy);
|
||||
expect(camelcaseKeys).toHaveBeenCalledWith(newPolicy);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isCreating: true }],
|
||||
@@ -115,12 +120,15 @@ describe('#actions', () => {
|
||||
...camelCaseFixtures[0],
|
||||
name: 'Updated Policy',
|
||||
};
|
||||
const snakeCasedParams = { name: 'Updated Policy' };
|
||||
|
||||
axios.patch.mockResolvedValue({ data: responseData });
|
||||
camelcaseKeys.mockReturnValue(camelCasedData);
|
||||
snakecaseKeys.mockReturnValue(snakeCasedParams);
|
||||
|
||||
const result = await actions.update({ commit }, updateParams);
|
||||
|
||||
expect(snakecaseKeys).toHaveBeenCalledWith({ name: 'Updated Policy' });
|
||||
expect(camelcaseKeys).toHaveBeenCalledWith(responseData);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isUpdating: true }],
|
||||
@@ -211,4 +219,108 @@ describe('#actions', () => {
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#setInboxPolicy', () => {
|
||||
it('sends correct actions if API is success', async () => {
|
||||
const responseData = { success: true, policy_id: 2 };
|
||||
const camelCasedData = { success: true, policyId: 2 };
|
||||
|
||||
axios.post.mockResolvedValue({ data: responseData });
|
||||
camelcaseKeys.mockReturnValue(camelCasedData);
|
||||
|
||||
const result = await actions.setInboxPolicy(
|
||||
{ commit },
|
||||
{ inboxId: 1, policyId: 2 }
|
||||
);
|
||||
|
||||
expect(camelcaseKeys).toHaveBeenCalledWith(responseData);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.ADD_ASSIGNMENT_POLICIES_INBOXES, camelCasedData],
|
||||
]);
|
||||
expect(result).toEqual(responseData);
|
||||
});
|
||||
|
||||
it('throws error if API fails', async () => {
|
||||
axios.post.mockRejectedValue(new Error('API Error'));
|
||||
|
||||
await expect(
|
||||
actions.setInboxPolicy({ commit }, { inboxId: 1, policyId: 2 })
|
||||
).rejects.toThrow(Error);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getInboxPolicy', () => {
|
||||
it('returns camelCased response data if API is success', async () => {
|
||||
const responseData = { policy_id: 1, name: 'Round Robin' };
|
||||
const camelCasedData = { policyId: 1, name: 'Round Robin' };
|
||||
|
||||
axios.get.mockResolvedValue({ data: responseData });
|
||||
camelcaseKeys.mockReturnValue(camelCasedData);
|
||||
|
||||
const result = await actions.getInboxPolicy({}, { inboxId: 1 });
|
||||
|
||||
expect(camelcaseKeys).toHaveBeenCalledWith(responseData);
|
||||
expect(result).toEqual(camelCasedData);
|
||||
});
|
||||
|
||||
it('throws error if API fails', async () => {
|
||||
axios.get.mockRejectedValue(new Error('Not found'));
|
||||
|
||||
await expect(
|
||||
actions.getInboxPolicy({}, { inboxId: 999 })
|
||||
).rejects.toThrow(Error);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#updateInboxPolicy', () => {
|
||||
it('commits EDIT_ASSIGNMENT_POLICY mutation', async () => {
|
||||
const policy = { id: 1, name: 'Updated Policy' };
|
||||
|
||||
await actions.updateInboxPolicy({ commit }, { policy });
|
||||
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.EDIT_ASSIGNMENT_POLICY, policy],
|
||||
]);
|
||||
});
|
||||
|
||||
it('throws error if commit fails', async () => {
|
||||
commit.mockImplementation(() => {
|
||||
throw new Error('Commit failed');
|
||||
});
|
||||
|
||||
await expect(
|
||||
actions.updateInboxPolicy({ commit }, { policy: {} })
|
||||
).rejects.toThrow(Error);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#removeInboxPolicy', () => {
|
||||
it('sends correct actions if API is success', async () => {
|
||||
const policyId = 1;
|
||||
const inboxId = 2;
|
||||
|
||||
axios.delete.mockResolvedValue({});
|
||||
|
||||
await actions.removeInboxPolicy({ commit }, { policyId, inboxId });
|
||||
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_INBOXES_UI_FLAG, { isDeleting: true }],
|
||||
[types.DELETE_ASSIGNMENT_POLICIES_INBOXES, { policyId, inboxId }],
|
||||
[types.SET_ASSIGNMENT_POLICIES_INBOXES_UI_FLAG, { isDeleting: false }],
|
||||
]);
|
||||
});
|
||||
|
||||
it('sends correct actions if API fails', async () => {
|
||||
axios.delete.mockRejectedValue(new Error('Not found'));
|
||||
|
||||
await expect(
|
||||
actions.removeInboxPolicy({ commit }, { policyId: 1, inboxId: 999 })
|
||||
).rejects.toThrow(Error);
|
||||
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_INBOXES_UI_FLAG, { isDeleting: true }],
|
||||
[types.SET_ASSIGNMENT_POLICIES_INBOXES_UI_FLAG, { isDeleting: false }],
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -32,10 +32,12 @@ describe('#getters', () => {
|
||||
const state = {
|
||||
inboxUiFlags: {
|
||||
isFetching: false,
|
||||
isDeleting: false,
|
||||
},
|
||||
};
|
||||
expect(getters.getInboxUiFlags(state)).toEqual({
|
||||
isFetching: false,
|
||||
isDeleting: false,
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -62,6 +62,24 @@ describe('#mutations', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#SET_ASSIGNMENT_POLICY', () => {
|
||||
it('sets single assignment policy record', () => {
|
||||
const state = { records: [] };
|
||||
|
||||
mutations[types.SET_ASSIGNMENT_POLICY](state, assignmentPoliciesList[0]);
|
||||
|
||||
expect(state.records).toEqual([assignmentPoliciesList[0]]);
|
||||
});
|
||||
|
||||
it('replaces existing record', () => {
|
||||
const state = { records: [{ id: 1, name: 'Old Policy' }] };
|
||||
|
||||
mutations[types.SET_ASSIGNMENT_POLICY](state, assignmentPoliciesList[0]);
|
||||
|
||||
expect(state.records).toEqual([assignmentPoliciesList[0]]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#ADD_ASSIGNMENT_POLICY', () => {
|
||||
it('adds new policy to empty records', () => {
|
||||
const state = { records: [] };
|
||||
@@ -264,4 +282,104 @@ describe('#mutations', () => {
|
||||
expect(state).toEqual(originalState);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#DELETE_ASSIGNMENT_POLICIES_INBOXES', () => {
|
||||
it('removes inbox from policy', () => {
|
||||
const mockInboxes = [
|
||||
{ id: 1, name: 'Support Inbox' },
|
||||
{ id: 2, name: 'Sales Inbox' },
|
||||
{ id: 3, name: 'Marketing Inbox' },
|
||||
];
|
||||
|
||||
const state = {
|
||||
records: [
|
||||
{ id: 1, name: 'Policy 1', inboxes: mockInboxes },
|
||||
{ id: 2, name: 'Policy 2', inboxes: [] },
|
||||
],
|
||||
};
|
||||
|
||||
mutations[types.DELETE_ASSIGNMENT_POLICIES_INBOXES](state, {
|
||||
policyId: 1,
|
||||
inboxId: 2,
|
||||
});
|
||||
|
||||
expect(state.records[0].inboxes).toEqual([
|
||||
{ id: 1, name: 'Support Inbox' },
|
||||
{ id: 3, name: 'Marketing Inbox' },
|
||||
]);
|
||||
expect(state.records[1].inboxes).toEqual([]);
|
||||
});
|
||||
|
||||
it('does nothing if policy not found', () => {
|
||||
const state = {
|
||||
records: [
|
||||
{ id: 1, name: 'Policy 1', inboxes: [{ id: 1, name: 'Test' }] },
|
||||
],
|
||||
};
|
||||
|
||||
const originalState = JSON.parse(JSON.stringify(state));
|
||||
|
||||
mutations[types.DELETE_ASSIGNMENT_POLICIES_INBOXES](state, {
|
||||
policyId: 999,
|
||||
inboxId: 1,
|
||||
});
|
||||
|
||||
expect(state).toEqual(originalState);
|
||||
});
|
||||
|
||||
it('does nothing if inbox not found in policy', () => {
|
||||
const mockInboxes = [{ id: 1, name: 'Support Inbox' }];
|
||||
|
||||
const state = {
|
||||
records: [{ id: 1, name: 'Policy 1', inboxes: mockInboxes }],
|
||||
};
|
||||
|
||||
mutations[types.DELETE_ASSIGNMENT_POLICIES_INBOXES](state, {
|
||||
policyId: 1,
|
||||
inboxId: 999,
|
||||
});
|
||||
|
||||
expect(state.records[0].inboxes).toEqual(mockInboxes);
|
||||
});
|
||||
|
||||
it('handles policy with no inboxes', () => {
|
||||
const state = {
|
||||
records: [{ id: 1, name: 'Policy 1' }],
|
||||
};
|
||||
|
||||
mutations[types.DELETE_ASSIGNMENT_POLICIES_INBOXES](state, {
|
||||
policyId: 1,
|
||||
inboxId: 1,
|
||||
});
|
||||
|
||||
expect(state.records[0]).toEqual({ id: 1, name: 'Policy 1' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('#ADD_ASSIGNMENT_POLICIES_INBOXES', () => {
|
||||
it('updates policy attributes using MutationHelpers.updateAttributes', () => {
|
||||
const state = {
|
||||
records: [
|
||||
{ id: 1, name: 'Policy 1', assignedInboxCount: 2 },
|
||||
{ id: 2, name: 'Policy 2', assignedInboxCount: 1 },
|
||||
],
|
||||
};
|
||||
|
||||
const updatedPolicy = {
|
||||
id: 1,
|
||||
name: 'Policy 1',
|
||||
assignedInboxCount: 3,
|
||||
inboxes: [{ id: 1, name: 'New Inbox' }],
|
||||
};
|
||||
|
||||
mutations[types.ADD_ASSIGNMENT_POLICIES_INBOXES](state, updatedPolicy);
|
||||
|
||||
expect(state.records[0]).toEqual(updatedPolicy);
|
||||
expect(state.records[1]).toEqual({
|
||||
id: 2,
|
||||
name: 'Policy 2',
|
||||
assignedInboxCount: 1,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user