Refactor methods

This commit is contained in:
Fayaz Ahmed 2022-05-16 14:38:34 +05:30
parent bfc61ab0f7
commit 7c338d0b68
4 changed files with 59 additions and 66 deletions

View file

@ -3,7 +3,8 @@ import {
OPERATOR_TYPES_3, OPERATOR_TYPES_3,
OPERATOR_TYPES_4, OPERATOR_TYPES_4,
} from '../routes/dashboard/settings/automation/operators'; } from '../routes/dashboard/settings/automation/operators';
import filterQueryGenerator from './filterQueryGenerator.js';
import actionQueryGenerator from './actionQueryGenerator.js';
const MESSAGE_CONDITION_VALUES = [ const MESSAGE_CONDITION_VALUES = [
{ {
id: 'incoming', id: 'incoming',
@ -190,3 +191,20 @@ export const filterCustomAttributes = customAttributes => {
}; };
}); });
}; };
export const getStandardAttributeInputType = (automationTypes, event, key) => {
return automationTypes[event].conditions.find(item => item.key === key)
.inputType;
};
export const generateAutomationPayload = payload => {
const automation = JSON.parse(JSON.stringify(payload));
automation.conditions[automation.conditions.length - 1].query_operator = null;
automation.conditions = filterQueryGenerator(automation.conditions).payload;
automation.actions = actionQueryGenerator(automation.actions);
return automation;
};
export const isCustomAttribute = (attrs, key) => {
return attrs.find(attr => attr.key === key);
};

View file

@ -1,5 +1,5 @@
import filterQueryGenerator from '../../helper/filterQueryGenerator.js'; import languages from '../../../dashboard/components/widgets/conversation/advancedFilterItems/languages.js';
import actionQueryGenerator from '../../helper/actionQueryGenerator.js'; import countries from '../../../shared/constants/countries.js';
import { import {
generateCustomAttributeTypes, generateCustomAttributeTypes,
getActionOptions, getActionOptions,
@ -11,6 +11,9 @@ import {
getDefaultConditions, getDefaultConditions,
getDefaultActions, getDefaultActions,
filterCustomAttributes, filterCustomAttributes,
generateAutomationPayload,
getStandardAttributeInputType,
isCustomAttribute,
} from '../../helper/automationHelper.js'; } from '../../helper/automationHelper.js';
import { mapGetters } from 'vuex'; import { mapGetters } from 'vuex';
@ -115,20 +118,12 @@ export default {
labels, labels,
statusFilterOptions, statusFilterOptions,
teams, teams,
languages,
countries,
type, type,
}); });
}, },
appendNewCondition() { appendNewCondition() {
if (this.mode === 'edit') {
if (
!this.automation.conditions[this.automation.conditions.length - 1]
.query_operator
) {
this.automation.conditions[
this.automation.conditions.length - 1
].query_operator = 'and';
}
}
this.automation.conditions.push( this.automation.conditions.push(
...getDefaultConditions(this.automation.event_name) ...getDefaultConditions(this.automation.event_name)
); );
@ -153,19 +148,8 @@ export default {
submitAutomation() { submitAutomation() {
this.$v.$touch(); this.$v.$touch();
if (this.$v.$invalid) return; if (this.$v.$invalid) return;
const automation = JSON.parse(JSON.stringify(this.automation)); const automation = generateAutomationPayload(this.automation);
automation.conditions[ this.$emit('saveAutomation', automation, this.mode);
automation.conditions.length - 1
].query_operator = null;
automation.conditions = filterQueryGenerator(
automation.conditions
).payload;
automation.actions = actionQueryGenerator(automation.actions);
this.$emit(
'saveAutomation',
automation,
this.mode === 'edit' ? 'EDIT' : null
);
}, },
resetFilter(index, currentCondition) { resetFilter(index, currentCondition) {
this.automation.conditions[index].filter_operator = this.automationTypes[ this.automation.conditions[index].filter_operator = this.automationTypes[
@ -175,19 +159,15 @@ export default {
).filterOperators[0].value; ).filterOperators[0].value;
this.automation.conditions[index].values = ''; this.automation.conditions[index].values = '';
}, },
showUserInput(operatorType) { showUserInput(type) {
if (operatorType === 'is_present' || operatorType === 'is_not_present') return !(type === 'is_present' || type === 'is_not_present');
return false;
return true;
}, },
showActionInput(actionName) { showActionInput(action) {
if (actionName === 'send_email_to_team' || actionName === 'send_message') if (action === 'send_email_to_team' || action === 'send_message')
return false; return false;
const type = this.automationActionTypes.find( const type = this.automationActionTypes.find(i => i.key === action)
action => action.key === actionName .inputType;
).inputType; return !!type;
if (type === null) return false;
return true;
}, },
resetAction(index) { resetAction(index) {
this.automation.actions[index].action_params = []; this.automation.actions[index].action_params = [];
@ -195,17 +175,19 @@ export default {
manifestConditions(automation) { manifestConditions(automation) {
const customAttributes = filterCustomAttributes(this.allCustomAttributes); const customAttributes = filterCustomAttributes(this.allCustomAttributes);
const conditions = automation.conditions.map(condition => { const conditions = automation.conditions.map(condition => {
const isCustomAttribute = customAttributes.find( const customAttr = isCustomAttribute(
attr => attr.key === condition.attribute_key customAttributes,
condition.attribute_key
); );
let inputType = 'plain_text'; let inputType = 'plain_text';
if (isCustomAttribute) { if (customAttr) {
inputType = getCustomAttributeInputType(isCustomAttribute.type); inputType = getCustomAttributeInputType(customAttr.type);
} else { } else {
inputType = this.automationTypes[ inputType = getStandardAttributeInputType(
automation.event_name this.automationTypes,
].conditions.find(item => item.key === condition.attribute_key) automation.event_name,
.inputType; condition.attribute_key
);
} }
if (inputType === 'plain_text' || inputType === 'date') { if (inputType === 'plain_text' || inputType === 'date') {
return { return {
@ -215,6 +197,7 @@ export default {
} }
return { return {
...condition, ...condition,
query_operator: condition.query_operator || 'and',
values: [ values: [
...this.getConditionDropdownValues(condition.attribute_key), ...this.getConditionDropdownValues(condition.attribute_key),
].filter(item => [...condition.values].includes(item.id)), ].filter(item => [...condition.values].includes(item.id)),
@ -223,6 +206,7 @@ export default {
return conditions; return conditions;
}, },
generateActionsArray(action) { generateActionsArray(action) {
const params = action.action_params;
let actionParams = []; let actionParams = [];
const inputType = this.automationActionTypes.find( const inputType = this.automationActionTypes.find(
item => item.key === action.action_name item => item.key === action.action_name
@ -230,17 +214,15 @@ export default {
if (inputType === 'multi_select') { if (inputType === 'multi_select') {
actionParams = [ actionParams = [
...this.getActionDropdownValues(action.action_name), ...this.getActionDropdownValues(action.action_name),
].filter(item => [...action.action_params].includes(item.id)); ].filter(item => [...params].includes(item.id));
} else if (inputType === 'team_message') { } else if (inputType === 'team_message') {
actionParams = { actionParams = {
team_ids: [ team_ids: [
...this.getActionDropdownValues(action.action_name), ...this.getActionDropdownValues(action.action_name),
].filter(item => ].filter(item => [...params[0].team_ids].includes(item.id)),
[...action.action_params[0].team_ids].includes(item.id) message: params[0].message,
),
message: action.action_params[0].message,
}; };
} else actionParams = [...action.action_params]; } else actionParams = [...params];
return actionParams; return actionParams;
}, },
manifestActions(automation) { manifestActions(automation) {

View file

@ -224,9 +224,9 @@ export default {
async submitAutomation(payload, mode) { async submitAutomation(payload, mode) {
try { try {
const action = const action =
mode === 'EDIT' ? 'automations/update' : 'automations/create'; mode === 'edit' ? 'automations/update' : 'automations/create';
const successMessage = const successMessage =
mode === 'EDIT' mode === 'edit'
? this.$t('AUTOMATION.EDIT.API.SUCCESS_MESSAGE') ? this.$t('AUTOMATION.EDIT.API.SUCCESS_MESSAGE')
: this.$t('AUTOMATION.ADD.API.SUCCESS_MESSAGE'); : this.$t('AUTOMATION.ADD.API.SUCCESS_MESSAGE');
await await this.$store.dispatch(action, payload); await await this.$store.dispatch(action, payload);
@ -235,7 +235,7 @@ export default {
this.hideEditPopup(); this.hideEditPopup();
} catch (error) { } catch (error) {
const errorMessage = const errorMessage =
mode === 'EDIT' mode === 'edit'
? this.$t('AUTOMATION.EDIT.API.ERROR_MESSAGE') ? this.$t('AUTOMATION.EDIT.API.ERROR_MESSAGE')
: this.$t('AUTOMATION.ADD.API.ERROR_MESSAGE'); : this.$t('AUTOMATION.ADD.API.ERROR_MESSAGE');
this.showAlert(errorMessage); this.showAlert(errorMessage);

View file

@ -17,29 +17,22 @@ const i18nConfig = new VueI18n({
describe('Automation Mixin function', () => { describe('Automation Mixin function', () => {
let addAutomationRule = null; let addAutomationRule = null;
let actions = null; let getters = null;
let modules = null;
let store = null; let store = null;
beforeEach(() => { beforeEach(() => {
actions = {}; getters = {
'attributes/getAttributesByModel': () => customAttributes,
modules = {
auth: {
getters: {
'attributes/getAttributes': () => customAttributes,
},
},
}; };
store = new Vuex.Store({ store = new Vuex.Store({
actions, getters,
modules,
}); });
addAutomationRule = shallowMount(AddAutomationRule, { addAutomationRule = shallowMount(AddAutomationRule, {
localVue, localVue,
i18n: i18nConfig, i18n: i18nConfig,
mixins: [methodsMixin], mixins: [methodsMixin],
store,
}); });
}); });