feat(TheHive Node): Overhaul (#6457)
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
import { updateDisplayOptions, wrapData } from '@utils/utilities';
|
||||
|
||||
import { theHiveApiRequest } from '../../transport';
|
||||
|
||||
import { fixFieldType, prepareInputItem } from '../../helpers/utils';
|
||||
import { caseRLC } from '../../descriptions';
|
||||
|
||||
const properties: INodeProperties[] = [
|
||||
caseRLC,
|
||||
{
|
||||
displayName: 'Fields',
|
||||
name: 'taskFields',
|
||||
type: 'resourceMapper',
|
||||
default: {
|
||||
mappingMode: 'defineBelow',
|
||||
value: null,
|
||||
},
|
||||
noDataExpression: true,
|
||||
required: true,
|
||||
typeOptions: {
|
||||
resourceMapper: {
|
||||
resourceMapperMethod: 'getTaskFields',
|
||||
mode: 'add',
|
||||
valuesLabel: 'Fields',
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const displayOptions = {
|
||||
show: {
|
||||
resource: ['task'],
|
||||
operation: ['create'],
|
||||
},
|
||||
};
|
||||
|
||||
export const description = updateDisplayOptions(displayOptions, properties);
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
i: number,
|
||||
item: INodeExecutionData,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
let responseData: IDataObject | IDataObject[] = [];
|
||||
let body: IDataObject = {};
|
||||
|
||||
const dataMode = this.getNodeParameter('taskFields.mappingMode', i) as string;
|
||||
const caseId = this.getNodeParameter('caseId', i, '', { extractValue: true }) as string;
|
||||
|
||||
if (dataMode === 'autoMapInputData') {
|
||||
const schema = this.getNodeParameter('taskFields.schema', i) as IDataObject[];
|
||||
body = prepareInputItem(item.json, schema, i);
|
||||
}
|
||||
|
||||
if (dataMode === 'defineBelow') {
|
||||
const taskFields = this.getNodeParameter('taskFields.value', i, []) as IDataObject;
|
||||
body = taskFields;
|
||||
}
|
||||
|
||||
body = fixFieldType(body);
|
||||
|
||||
responseData = await theHiveApiRequest.call(this, 'POST', `/v1/case/${caseId}/task`, body);
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(wrapData(responseData), {
|
||||
itemData: { item: i },
|
||||
});
|
||||
|
||||
return executionData;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import type { IExecuteFunctions, INodeExecutionData, INodeProperties } from 'n8n-workflow';
|
||||
import { updateDisplayOptions, wrapData } from '@utils/utilities';
|
||||
import { theHiveApiRequest } from '../../transport';
|
||||
import { taskRLC } from '../../descriptions';
|
||||
|
||||
const properties: INodeProperties[] = [taskRLC];
|
||||
|
||||
const displayOptions = {
|
||||
show: {
|
||||
resource: ['task'],
|
||||
operation: ['deleteTask'],
|
||||
},
|
||||
};
|
||||
|
||||
export const description = updateDisplayOptions(displayOptions, properties);
|
||||
|
||||
export async function execute(this: IExecuteFunctions, i: number): Promise<INodeExecutionData[]> {
|
||||
const taskId = this.getNodeParameter('taskId', i, '', { extractValue: true }) as string;
|
||||
|
||||
await theHiveApiRequest.call(this, 'DELETE', `/v1/task/${taskId}`);
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(wrapData({ success: true }), {
|
||||
itemData: { item: i },
|
||||
});
|
||||
|
||||
return executionData;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
import { updateDisplayOptions, wrapData } from '@utils/utilities';
|
||||
import { responderOptions, taskRLC } from '../../descriptions';
|
||||
import { theHiveApiRequest } from '../../transport';
|
||||
|
||||
const properties: INodeProperties[] = [{ ...taskRLC, name: 'id' }, responderOptions];
|
||||
|
||||
const displayOptions = {
|
||||
show: {
|
||||
resource: ['task'],
|
||||
operation: ['executeResponder'],
|
||||
},
|
||||
};
|
||||
|
||||
export const description = updateDisplayOptions(displayOptions, properties);
|
||||
|
||||
export async function execute(this: IExecuteFunctions, i: number): Promise<INodeExecutionData[]> {
|
||||
let responseData: IDataObject | IDataObject[] = [];
|
||||
|
||||
const taskId = this.getNodeParameter('id', i);
|
||||
const responderId = this.getNodeParameter('responder', i) as string;
|
||||
let body: IDataObject;
|
||||
let response;
|
||||
responseData = [];
|
||||
|
||||
const qs: IDataObject = {};
|
||||
|
||||
body = {
|
||||
responderId,
|
||||
objectId: taskId,
|
||||
objectType: 'case_task',
|
||||
};
|
||||
response = await theHiveApiRequest.call(this, 'POST', '/connector/cortex/action' as string, body);
|
||||
body = {
|
||||
query: [
|
||||
{
|
||||
_name: 'listAction',
|
||||
},
|
||||
{
|
||||
_name: 'filter',
|
||||
_and: [
|
||||
{
|
||||
_field: 'cortexId',
|
||||
_value: response.cortexId,
|
||||
},
|
||||
{
|
||||
_field: 'objectId',
|
||||
_value: response.objectId,
|
||||
},
|
||||
{
|
||||
_field: 'startDate',
|
||||
_value: response.startDate,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
qs.name = 'task-actions';
|
||||
do {
|
||||
response = await theHiveApiRequest.call(this, 'POST', '/v1/query', body, qs);
|
||||
} while (response.status === 'Waiting' || response.status === 'InProgress');
|
||||
|
||||
responseData = response;
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(wrapData(responseData), {
|
||||
itemData: { item: i },
|
||||
});
|
||||
|
||||
return executionData;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
import { updateDisplayOptions, wrapData } from '@utils/utilities';
|
||||
import { theHiveApiRequest } from '../../transport';
|
||||
import { taskRLC } from '../../descriptions';
|
||||
|
||||
const properties: INodeProperties[] = [taskRLC];
|
||||
|
||||
const displayOptions = {
|
||||
show: {
|
||||
resource: ['task'],
|
||||
operation: ['get'],
|
||||
},
|
||||
};
|
||||
|
||||
export const description = updateDisplayOptions(displayOptions, properties);
|
||||
|
||||
export async function execute(this: IExecuteFunctions, i: number): Promise<INodeExecutionData[]> {
|
||||
let responseData: IDataObject | IDataObject[] = [];
|
||||
|
||||
const taskId = this.getNodeParameter('taskId', i, '', { extractValue: true }) as string;
|
||||
|
||||
const qs: IDataObject = {};
|
||||
|
||||
const body = {
|
||||
query: [
|
||||
{
|
||||
_name: 'getTask',
|
||||
idOrName: taskId,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
qs.name = `get-task-${taskId}`;
|
||||
|
||||
responseData = await theHiveApiRequest.call(this, 'POST', '/v1/query', body, qs);
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(wrapData(responseData), {
|
||||
itemData: { item: i },
|
||||
});
|
||||
|
||||
return executionData;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import type { INodeProperties } from 'n8n-workflow';
|
||||
|
||||
import * as create from './create.operation';
|
||||
import * as deleteTask from './deleteTask.operation';
|
||||
import * as executeResponder from './executeResponder.operation';
|
||||
import * as get from './get.operation';
|
||||
import * as search from './search.operation';
|
||||
import * as update from './update.operation';
|
||||
|
||||
export { create, deleteTask, executeResponder, get, search, update };
|
||||
|
||||
export const description: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
default: 'create',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
required: true,
|
||||
options: [
|
||||
{
|
||||
name: 'Create',
|
||||
value: 'create',
|
||||
action: 'Create a task',
|
||||
},
|
||||
{
|
||||
name: 'Delete',
|
||||
value: 'deleteTask',
|
||||
action: 'Delete an task',
|
||||
},
|
||||
{
|
||||
name: 'Execute Responder',
|
||||
value: 'executeResponder',
|
||||
action: 'Execute responder on a task',
|
||||
},
|
||||
{
|
||||
name: 'Get',
|
||||
value: 'get',
|
||||
action: 'Get a task',
|
||||
},
|
||||
{
|
||||
name: 'Search',
|
||||
value: 'search',
|
||||
action: 'Search tasks',
|
||||
},
|
||||
{
|
||||
name: 'Update',
|
||||
value: 'update',
|
||||
action: 'Update a task',
|
||||
},
|
||||
],
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['task'],
|
||||
},
|
||||
},
|
||||
},
|
||||
...create.description,
|
||||
...deleteTask.description,
|
||||
...executeResponder.description,
|
||||
...get.description,
|
||||
...search.description,
|
||||
...update.description,
|
||||
];
|
||||
@@ -0,0 +1,87 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
import { updateDisplayOptions, wrapData } from '@utils/utilities';
|
||||
import {
|
||||
caseRLC,
|
||||
genericFiltersCollection,
|
||||
returnAllAndLimit,
|
||||
searchOptions,
|
||||
sortCollection,
|
||||
} from '../../descriptions';
|
||||
import { theHiveApiQuery } from '../../transport';
|
||||
import type { QueryScope } from '../../helpers/interfaces';
|
||||
|
||||
const properties: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Search in All Cases',
|
||||
name: 'allCases',
|
||||
type: 'boolean',
|
||||
default: true,
|
||||
description: 'Whether to search in all cases or only in a selected case',
|
||||
},
|
||||
{
|
||||
...caseRLC,
|
||||
displayOptions: {
|
||||
show: {
|
||||
allCases: [false],
|
||||
},
|
||||
},
|
||||
},
|
||||
...returnAllAndLimit,
|
||||
genericFiltersCollection,
|
||||
sortCollection,
|
||||
searchOptions,
|
||||
];
|
||||
|
||||
const displayOptions = {
|
||||
show: {
|
||||
resource: ['task'],
|
||||
operation: ['search'],
|
||||
},
|
||||
};
|
||||
|
||||
export const description = updateDisplayOptions(displayOptions, properties);
|
||||
|
||||
export async function execute(this: IExecuteFunctions, i: number): Promise<INodeExecutionData[]> {
|
||||
let responseData: IDataObject | IDataObject[] = [];
|
||||
|
||||
const allCases = this.getNodeParameter('allCases', i) as boolean;
|
||||
const filtersValues = this.getNodeParameter('filters.values', i, []) as IDataObject[];
|
||||
const sortFields = this.getNodeParameter('sort.fields', i, []) as IDataObject[];
|
||||
const returnAll = this.getNodeParameter('returnAll', i);
|
||||
const { returnCount, extraData } = this.getNodeParameter('options', i);
|
||||
|
||||
let limit;
|
||||
let scope: QueryScope;
|
||||
|
||||
if (allCases) {
|
||||
scope = { query: 'listTask' };
|
||||
} else {
|
||||
const caseId = this.getNodeParameter('caseId', i, '', { extractValue: true }) as string;
|
||||
scope = { query: 'getCase', id: caseId, restrictTo: 'tasks' };
|
||||
}
|
||||
|
||||
if (!returnAll) {
|
||||
limit = this.getNodeParameter('limit', i);
|
||||
}
|
||||
|
||||
responseData = await theHiveApiQuery.call(
|
||||
this,
|
||||
scope,
|
||||
filtersValues,
|
||||
sortFields,
|
||||
limit,
|
||||
returnCount as boolean,
|
||||
extraData as string[],
|
||||
);
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(wrapData(responseData), {
|
||||
itemData: { item: i },
|
||||
});
|
||||
|
||||
return executionData;
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeOperationError } from 'n8n-workflow';
|
||||
import { updateDisplayOptions, wrapData } from '@utils/utilities';
|
||||
|
||||
import { theHiveApiRequest } from '../../transport';
|
||||
import { fixFieldType, prepareInputItem } from '../../helpers/utils';
|
||||
import set from 'lodash/set';
|
||||
|
||||
const properties: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Fields',
|
||||
name: 'taskUpdateFields',
|
||||
type: 'resourceMapper',
|
||||
default: {
|
||||
mappingMode: 'defineBelow',
|
||||
value: null,
|
||||
},
|
||||
noDataExpression: true,
|
||||
required: true,
|
||||
typeOptions: {
|
||||
resourceMapper: {
|
||||
resourceMapperMethod: 'getTaskUpdateFields',
|
||||
mode: 'update',
|
||||
valuesLabel: 'Fields',
|
||||
addAllFields: true,
|
||||
multiKeyMatch: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const displayOptions = {
|
||||
show: {
|
||||
resource: ['task'],
|
||||
operation: ['update'],
|
||||
},
|
||||
};
|
||||
|
||||
export const description = updateDisplayOptions(displayOptions, properties);
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
i: number,
|
||||
item: INodeExecutionData,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
let body: IDataObject = {};
|
||||
let updated = 1;
|
||||
|
||||
const dataMode = this.getNodeParameter('taskUpdateFields.mappingMode', i) as string;
|
||||
|
||||
if (dataMode === 'autoMapInputData') {
|
||||
const schema = this.getNodeParameter('taskUpdateFields.schema', i) as IDataObject[];
|
||||
body = prepareInputItem(item.json, schema, i);
|
||||
}
|
||||
|
||||
if (dataMode === 'defineBelow') {
|
||||
const taskUpdateFields = this.getNodeParameter('taskUpdateFields.value', i, []) as IDataObject;
|
||||
body = taskUpdateFields;
|
||||
}
|
||||
|
||||
body = fixFieldType(body);
|
||||
|
||||
const fieldsToMatchOn = this.getNodeParameter('taskUpdateFields.matchingColumns', i) as string[];
|
||||
|
||||
const updateBody: IDataObject = {};
|
||||
const matchFields: IDataObject = {};
|
||||
const { id } = body; // id would be used if matching on id, also we need to remove it from the body
|
||||
|
||||
for (const field of Object.keys(body)) {
|
||||
if (fieldsToMatchOn.includes(field)) {
|
||||
// if field is in fieldsToMatchOn, we need to exclude it from the updateBody, as values used for matching should not be updated
|
||||
matchFields[field] = body[field];
|
||||
} else {
|
||||
// use set to construct the updateBody, as it allows to process customFields.fieldName
|
||||
// if customFields provided under customFields property, it will be send as is
|
||||
set(updateBody, field, body[field]);
|
||||
}
|
||||
}
|
||||
|
||||
if (fieldsToMatchOn.includes('id')) {
|
||||
await theHiveApiRequest.call(this, 'PATCH', `/v1/task/${id}`, body);
|
||||
} else {
|
||||
const filter = {
|
||||
_name: 'filter',
|
||||
_and: fieldsToMatchOn.map((field) => ({
|
||||
_eq: {
|
||||
_field: field,
|
||||
_value: matchFields[field],
|
||||
},
|
||||
})),
|
||||
};
|
||||
|
||||
const queryBody = {
|
||||
query: [
|
||||
{
|
||||
_name: 'listTask',
|
||||
},
|
||||
filter,
|
||||
],
|
||||
};
|
||||
|
||||
const matches = (await theHiveApiRequest.call(
|
||||
this,
|
||||
'POST',
|
||||
'/v1/query',
|
||||
queryBody,
|
||||
)) as IDataObject[];
|
||||
|
||||
if (!matches.length) {
|
||||
throw new NodeOperationError(this.getNode(), 'No matching alerts found');
|
||||
}
|
||||
const ids = matches.map((match) => match._id);
|
||||
updated = ids.length;
|
||||
|
||||
updateBody.ids = ids;
|
||||
|
||||
await theHiveApiRequest.call(this, 'PATCH', '/v1/task/_bulk', updateBody);
|
||||
}
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
wrapData({ success: true, updated }),
|
||||
{
|
||||
itemData: { item: i },
|
||||
},
|
||||
);
|
||||
|
||||
return executionData;
|
||||
}
|
||||
Reference in New Issue
Block a user