feat: Clone automation rules (#3893)

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
Fayaz Ahmed 2022-02-03 09:14:22 +05:30 committed by GitHub
parent 8821106da9
commit fc1f257793
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 26 deletions

View file

@ -1,9 +1,14 @@
/* global axios */
import ApiClient from './ApiClient'; import ApiClient from './ApiClient';
class AutomationsAPI extends ApiClient { class AutomationsAPI extends ApiClient {
constructor() { constructor() {
super('automation_rules', { accountScoped: true }); super('automation_rules', { accountScoped: true });
} }
clone(automationId) {
return axios.post(`${this.url}/${automationId}/clone`);
}
} }
export default new AutomationsAPI(); export default new AutomationsAPI();

View file

@ -9,6 +9,7 @@ describe('#AutomationsAPI', () => {
expect(automations).toHaveProperty('create'); expect(automations).toHaveProperty('create');
expect(automations).toHaveProperty('update'); expect(automations).toHaveProperty('update');
expect(automations).toHaveProperty('delete'); expect(automations).toHaveProperty('delete');
expect(automations).toHaveProperty('clone');
expect(automations.url).toBe('/api/v1/automation_rules'); expect(automations.url).toBe('/api/v1/automation_rules');
}); });
}); });

View file

@ -71,6 +71,13 @@
"ERROR_MESSAGE": "Could not update automation rule, Please try again later" "ERROR_MESSAGE": "Could not update automation rule, Please try again later"
} }
}, },
"CLONE": {
"TOOLTIP": "Clone",
"API": {
"SUCCESS_MESSAGE": "Automation cloned successfully",
"ERROR_MESSAGE": "Could not clone automation rule, Please try again later"
}
},
"FORM": { "FORM": {
"EDIT": "Edit", "EDIT": "Edit",
"CREATE": "Create", "CREATE": "Create",

View file

@ -54,17 +54,17 @@
@click="openEditPopup(automation)" @click="openEditPopup(automation)"
> >
</woot-button> </woot-button>
<!-- <woot-button <woot-button
v-tooltip.top="'Clone'" v-tooltip.top="$t('AUTOMATION.CLONE.TOOLTIP')"
variant="smooth" variant="smooth"
size="tiny" size="tiny"
color-scheme="primary" color-scheme="primary"
class-names="grey-btn" class-names="grey-btn"
:is-loading="loading[automation.id]" :is-loading="loading[automation.id]"
icon="copy" icon="copy"
@click="openEditPopup(automation)" @click="cloneAutomation(automation.id)"
> >
</woot-button> --> </woot-button>
<woot-button <woot-button
v-tooltip.top="$t('AUTOMATION.FORM.DELETE')" v-tooltip.top="$t('AUTOMATION.FORM.DELETE')"
variant="smooth" variant="smooth"
@ -204,6 +204,16 @@ export default {
this.showAlert(this.$t('AUTOMATION.DELETE.API.ERROR_MESSAGE')); this.showAlert(this.$t('AUTOMATION.DELETE.API.ERROR_MESSAGE'));
} }
}, },
async cloneAutomation(id) {
try {
await this.$store.dispatch('automations/clone', id);
this.showAlert(this.$t('AUTOMATION.CLONE.API.SUCCESS_MESSAGE'));
this.$store.dispatch('automations/get');
this.loading[this.selectedResponse.id] = false;
} catch (error) {
this.showAlert(this.$t('AUTOMATION.CLONE.API.ERROR_MESSAGE'));
}
},
async submitAutomation(payload, mode) { async submitAutomation(payload, mode) {
try { try {
const action = const action =

View file

@ -66,6 +66,16 @@ export const actions = {
commit(types.SET_AUTOMATION_UI_FLAG, { isDeleting: false }); commit(types.SET_AUTOMATION_UI_FLAG, { isDeleting: false });
} }
}, },
clone: async ({ commit }, id) => {
commit(types.SET_AUTOMATION_UI_FLAG, { isCloning: true });
try {
await AutomationAPI.clone(id);
} catch (error) {
throw new Error(error);
} finally {
commit(types.SET_AUTOMATION_UI_FLAG, { isCloning: false });
}
},
}; };
export const mutations = { export const mutations = {

View file

@ -47,28 +47,28 @@ describe('#actions', () => {
]); ]);
}); });
}); });
// API Work in progress
// describe('#update', () => { describe('#update', () => {
// it('sends correct actions if API is success', async () => { it('sends correct actions if API is success', async () => {
// axios.patch.mockResolvedValue({ data: automationsList[0] }); axios.patch.mockResolvedValue({ data: automationsList[0] });
// await actions.update({ commit }, automationsList[0]); await actions.update({ commit }, automationsList[0]);
// expect(commit.mock.calls).toEqual([ expect(commit.mock.calls).toEqual([
// [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: true }], [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: true }],
// [types.default.EDIT_AUTOMATION, automationsList[0]], [types.default.EDIT_AUTOMATION, automationsList[0]],
// [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: false }], [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: false }],
// ]); ]);
// }); });
// it('sends correct actions if API is error', async () => { it('sends correct actions if API is error', async () => {
// axios.patch.mockRejectedValue({ message: 'Incorrect header' }); axios.patch.mockRejectedValue({ message: 'Incorrect header' });
// await expect( await expect(
// actions.update({ commit }, automationsList[0]) actions.update({ commit }, automationsList[0])
// ).rejects.toThrow(Error); ).rejects.toThrow(Error);
// expect(commit.mock.calls).toEqual([ expect(commit.mock.calls).toEqual([
// [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: true }], [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: true }],
// [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: false }], [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: false }],
// ]); ]);
// }); });
// }); });
describe('#delete', () => { describe('#delete', () => {
it('sends correct actions if API is success', async () => { it('sends correct actions if API is success', async () => {
@ -91,4 +91,15 @@ describe('#actions', () => {
]); ]);
}); });
}); });
describe('#clone', () => {
it('clones the automation', async () => {
axios.post.mockResolvedValue({ data: automationsList[0] });
await actions.clone({ commit }, automationsList[0]);
expect(commit.mock.calls).toEqual([
[types.default.SET_AUTOMATION_UI_FLAG, { isCloning: true }],
[types.default.SET_AUTOMATION_UI_FLAG, { isCloning: false }],
]);
});
});
}); });