2023-09-04 08:15:52 -07:00
|
|
|
import type {
|
|
|
|
IDataObject,
|
|
|
|
IExecuteFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeProperties,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import set from 'lodash/set';
|
|
|
|
|
|
|
|
import FormData from 'form-data';
|
|
|
|
import { theHiveApiRequest } from '../../transport';
|
|
|
|
import { fixFieldType, prepareInputItem, splitAndTrim } from '../../helpers/utils';
|
|
|
|
import { observableTypeOptions } from '../../descriptions';
|
|
|
|
import { updateDisplayOptions, wrapData } from '@utils/utilities';
|
|
|
|
|
|
|
|
const properties: INodeProperties[] = [
|
|
|
|
{
|
|
|
|
displayName: 'Fields',
|
|
|
|
name: 'alertFields',
|
|
|
|
type: 'resourceMapper',
|
|
|
|
default: {
|
|
|
|
mappingMode: 'defineBelow',
|
|
|
|
value: null,
|
|
|
|
},
|
|
|
|
noDataExpression: true,
|
|
|
|
required: true,
|
|
|
|
typeOptions: {
|
|
|
|
resourceMapper: {
|
|
|
|
resourceMapperMethod: 'getAlertFields',
|
|
|
|
mode: 'add',
|
|
|
|
valuesLabel: 'Fields',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Observables',
|
|
|
|
name: 'observableUi',
|
|
|
|
type: 'fixedCollection',
|
|
|
|
placeholder: 'Add Observable',
|
|
|
|
default: {},
|
|
|
|
typeOptions: {
|
|
|
|
multipleValues: true,
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Values',
|
|
|
|
name: 'values',
|
|
|
|
values: [
|
|
|
|
observableTypeOptions,
|
|
|
|
{
|
|
|
|
displayName: 'Data',
|
|
|
|
name: 'data',
|
|
|
|
type: 'string',
|
|
|
|
displayOptions: {
|
|
|
|
hide: {
|
|
|
|
dataType: ['file'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
{
|
2024-01-03 03:08:16 -08:00
|
|
|
displayName: 'Input Binary Field',
|
2023-09-04 08:15:52 -07:00
|
|
|
name: 'binaryProperty',
|
|
|
|
type: 'string',
|
2024-01-03 03:08:16 -08:00
|
|
|
hint: 'The name of the input binary field containing the file to be written',
|
2023-09-04 08:15:52 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
dataType: ['file'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
default: 'data',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Message',
|
|
|
|
name: 'message',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Tags',
|
|
|
|
name: 'tags',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const displayOptions = {
|
|
|
|
show: {
|
|
|
|
resource: ['alert'],
|
|
|
|
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 inputData: IDataObject = {};
|
|
|
|
|
|
|
|
const dataMode = this.getNodeParameter('alertFields.mappingMode', i) as string;
|
|
|
|
|
|
|
|
if (dataMode === 'autoMapInputData') {
|
|
|
|
const schema = this.getNodeParameter('alertFields.schema', i) as IDataObject[];
|
|
|
|
inputData = prepareInputItem(item.json, schema, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dataMode === 'defineBelow') {
|
|
|
|
const alertFields = this.getNodeParameter('alertFields.value', i, []) as IDataObject;
|
|
|
|
inputData = alertFields;
|
|
|
|
}
|
|
|
|
|
|
|
|
inputData = fixFieldType(inputData);
|
|
|
|
|
|
|
|
const body: IDataObject = {};
|
|
|
|
|
|
|
|
for (const field of Object.keys(inputData)) {
|
|
|
|
// 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(body, field, inputData[field]);
|
|
|
|
}
|
|
|
|
|
|
|
|
let multiPartRequest = false;
|
|
|
|
const formData = new FormData();
|
|
|
|
|
|
|
|
const observableUi = this.getNodeParameter('observableUi', i) as IDataObject;
|
|
|
|
if (observableUi) {
|
|
|
|
const values = observableUi.values as IDataObject[];
|
|
|
|
|
|
|
|
if (values) {
|
|
|
|
const observables = [];
|
|
|
|
|
|
|
|
for (const value of values) {
|
|
|
|
const observable: IDataObject = {};
|
|
|
|
|
|
|
|
observable.dataType = value.dataType as string;
|
|
|
|
observable.message = value.message as string;
|
|
|
|
observable.tags = splitAndTrim(value.tags as string);
|
|
|
|
|
|
|
|
if (value.dataType === 'file') {
|
|
|
|
multiPartRequest = true;
|
|
|
|
|
|
|
|
const attachmentIndex = `attachment${i}`;
|
|
|
|
observable.attachment = attachmentIndex;
|
|
|
|
|
|
|
|
const binaryPropertyName = value.binaryProperty as string;
|
|
|
|
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
|
2023-10-17 06:53:44 -07:00
|
|
|
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
2023-09-04 08:15:52 -07:00
|
|
|
|
2023-10-17 06:53:44 -07:00
|
|
|
formData.append(attachmentIndex, dataBuffer, {
|
2023-09-04 08:15:52 -07:00
|
|
|
filename: binaryData.fileName,
|
|
|
|
contentType: binaryData.mimeType,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
observable.data = value.data as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
observables.push(observable);
|
|
|
|
}
|
|
|
|
body.observables = observables;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (multiPartRequest) {
|
|
|
|
formData.append('_json', JSON.stringify(body));
|
|
|
|
responseData = await theHiveApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
'/v1/alert',
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
Headers: {
|
|
|
|
'Content-Type': 'multipart/form-data',
|
|
|
|
},
|
|
|
|
formData,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
responseData = await theHiveApiRequest.call(this, 'POST', '/v1/alert' as string, body);
|
|
|
|
}
|
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(wrapData(responseData), {
|
|
|
|
itemData: { item: i },
|
|
|
|
});
|
|
|
|
|
|
|
|
return executionData;
|
|
|
|
}
|