Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce0388d9de | ||
|
|
17052113f5 | ||
|
|
c4dd3df6ad | ||
|
|
ad278c5d60 | ||
|
|
d66c95d0b6 | ||
|
|
6adc1f31e9 | ||
|
|
1db4850a5d | ||
|
|
d67c541943 | ||
|
|
e89e213245 | ||
|
|
c57a108f13 |
@@ -1,46 +1,87 @@
|
||||
/**
|
||||
* Formats an array of elements into a string.
|
||||
* @param {Array} arr - The array of elements.
|
||||
* @returns {boolean} - Returns true if all elements in the array are of type 'string', otherwise returns false.
|
||||
*/
|
||||
const allElementsString = arr => {
|
||||
return arr.every(elem => typeof elem === 'string');
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if all elements in an array are of type 'number'.
|
||||
* @param {Array} arr - The array of elements.
|
||||
* @returns {boolean} - Returns true if all elements in the array are of type 'number', otherwise returns false.
|
||||
*/
|
||||
const allElementsNumbers = arr => {
|
||||
return arr.every(elem => typeof elem === 'number');
|
||||
};
|
||||
|
||||
const formatArray = params => {
|
||||
if (params.length <= 0) {
|
||||
params = [];
|
||||
} else if (allElementsString(params) || allElementsNumbers(params)) {
|
||||
params = [...params];
|
||||
} else {
|
||||
params = params.map(val => val.id);
|
||||
/**
|
||||
* Formats an array of action parameters.
|
||||
* @param {Array} params - The array of action parameters.
|
||||
* @returns {Array} - Returns the formatted array of action parameters.
|
||||
*/
|
||||
const formatActionParamsArray = params => {
|
||||
if (params.length === 0) {
|
||||
return [];
|
||||
}
|
||||
return params;
|
||||
|
||||
if (allElementsString(params) || allElementsNumbers(params)) {
|
||||
return [...params];
|
||||
}
|
||||
|
||||
return params.map(val => val.id);
|
||||
};
|
||||
|
||||
const generatePayloadForObject = item => {
|
||||
if (item.action_params.id) {
|
||||
item.action_params = [item.action_params.id];
|
||||
} else {
|
||||
item.action_params = [item.action_params];
|
||||
/**
|
||||
* Formats an object of action parameters.
|
||||
* @param {Object} params - The object of action parameters.
|
||||
* @returns {Array} - Returns the formatted array of action parameters.
|
||||
*/
|
||||
const formatActionParamsObject = params => {
|
||||
if (params?.id) {
|
||||
return [params.id];
|
||||
}
|
||||
return item.action_params;
|
||||
|
||||
return [params];
|
||||
};
|
||||
|
||||
/**
|
||||
* Processes the action parameters and returns them in a formatted array.
|
||||
* @param {Array|Object} action_params - The action parameters to be processed.
|
||||
* @returns {Array} - Returns the processed and formatted array of action parameters.
|
||||
*/
|
||||
const processActionParams = action_params => {
|
||||
if (!action_params) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (Array.isArray(action_params)) {
|
||||
return formatActionParamsArray(action_params);
|
||||
}
|
||||
|
||||
if (typeof action_params === 'object') {
|
||||
return formatActionParamsObject(action_params);
|
||||
}
|
||||
|
||||
return [action_params];
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates the payload by processing the data and formatting the action parameters.
|
||||
* @param {Array} data - The data containing actions and their parameters.
|
||||
* @returns {Array} - Returns the generated payload with processed action parameters.
|
||||
*/
|
||||
const generatePayload = data => {
|
||||
const actions = JSON.parse(JSON.stringify(data));
|
||||
let payload = actions.map(item => {
|
||||
if (Array.isArray(item.action_params)) {
|
||||
item.action_params = formatArray(item.action_params);
|
||||
} else if (typeof item.action_params === 'object') {
|
||||
item.action_params = generatePayloadForObject(item);
|
||||
} else if (!item.action_params) {
|
||||
item.action_params = [];
|
||||
} else {
|
||||
item.action_params = [item.action_params];
|
||||
}
|
||||
return item;
|
||||
|
||||
return actions.map(item => {
|
||||
const { action_params } = item;
|
||||
return {
|
||||
...item,
|
||||
action_params: processActionParams(action_params),
|
||||
};
|
||||
});
|
||||
return payload;
|
||||
};
|
||||
|
||||
export default generatePayload;
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
} from 'dashboard/routes/dashboard/settings/automation/operators';
|
||||
import filterQueryGenerator from './filterQueryGenerator';
|
||||
import actionQueryGenerator from './actionQueryGenerator';
|
||||
|
||||
const MESSAGE_CONDITION_VALUES = [
|
||||
{
|
||||
id: 'incoming',
|
||||
|
||||
@@ -5,6 +5,18 @@ const testData = [
|
||||
action_name: 'add_label',
|
||||
action_params: [{ id: 'testlabel', name: 'testlabel' }],
|
||||
},
|
||||
{
|
||||
action_name: 'add_label_empty',
|
||||
action_params: [],
|
||||
},
|
||||
{
|
||||
action_name: 'add_label_string_array',
|
||||
action_params: ['test_1', 'test_2', 'test_3'],
|
||||
},
|
||||
{
|
||||
action_name: 'add_label_number_array',
|
||||
action_params: [1, 2, 3],
|
||||
},
|
||||
{
|
||||
action_name: 'assign_team',
|
||||
action_params: [
|
||||
@@ -18,6 +30,22 @@ const testData = [
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
action_name: 'assign_priority',
|
||||
action_params: { id: 'high', name: 'High' },
|
||||
},
|
||||
{
|
||||
action_name: 'test_action_with_no_id_no_value',
|
||||
action_params: { name: 'High' },
|
||||
},
|
||||
{
|
||||
action_name: 'test_action_with_nullish_action_params',
|
||||
action_params: null,
|
||||
},
|
||||
{
|
||||
action_name: 'test_action_with_simple_value_as_action_params',
|
||||
action_params: 'some-value',
|
||||
},
|
||||
];
|
||||
|
||||
const finalResult = [
|
||||
@@ -25,10 +53,42 @@ const finalResult = [
|
||||
action_name: 'add_label',
|
||||
action_params: ['testlabel'],
|
||||
},
|
||||
{
|
||||
action_name: 'add_label_empty',
|
||||
action_params: [],
|
||||
},
|
||||
{
|
||||
action_name: 'add_label_string_array',
|
||||
action_params: ['test_1', 'test_2', 'test_3'],
|
||||
},
|
||||
{
|
||||
action_name: 'add_label_number_array',
|
||||
action_params: [1, 2, 3],
|
||||
},
|
||||
{
|
||||
action_name: 'assign_team',
|
||||
action_params: [1],
|
||||
},
|
||||
{
|
||||
action_name: 'assign_priority',
|
||||
action_params: ['high'],
|
||||
},
|
||||
{
|
||||
action_name: 'test_action_with_no_id_no_value',
|
||||
action_params: [
|
||||
{
|
||||
name: 'High',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
action_name: 'test_action_with_nullish_action_params',
|
||||
action_params: [],
|
||||
},
|
||||
{
|
||||
action_name: 'test_action_with_simple_value_as_action_params',
|
||||
action_params: ['some-value'],
|
||||
},
|
||||
];
|
||||
|
||||
describe('#actionQueryGenerator', () => {
|
||||
|
||||
Reference in New Issue
Block a user