n8n/packages/nodes-base/nodes/TheHiveProject/actions/case/addAttachment.operation.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-09-04 08:15:52 -07:00
import type {
IDataObject,
IExecuteFunctions,
INodeExecutionData,
INodeProperties,
} from 'n8n-workflow';
import { updateDisplayOptions, wrapData } from '@utils/utilities';
import { theHiveApiRequest } from '../../transport';
import { attachmentsUi, caseRLC } from '../../descriptions';
const properties: INodeProperties[] = [
caseRLC,
attachmentsUi,
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Rename Files',
name: 'canRename',
type: 'boolean',
description: 'Whether to rename the file in case a file with the same name already exists',
default: false,
},
],
},
];
const displayOptions = {
show: {
resource: ['case'],
operation: ['addAttachment'],
},
};
export const description = updateDisplayOptions(displayOptions, properties);
export async function execute(this: IExecuteFunctions, i: number): Promise<INodeExecutionData[]> {
let responseData: IDataObject | IDataObject[] = [];
const caseId = this.getNodeParameter('caseId', i, '', { extractValue: true }) as string;
const canRename = this.getNodeParameter('options.canRename', i, false) as boolean;
const inputDataFields = (
this.getNodeParameter('attachmentsUi.values', i, []) as IDataObject[]
).map((entry) => (entry.field as string).trim());
const attachments = [];
for (const inputDataField of inputDataFields) {
const binaryData = this.helpers.assertBinaryData(i, inputDataField);
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, inputDataField);
attachments.push({
value: dataBuffer,
options: {
contentType: binaryData.mimeType,
filename: binaryData.fileName,
},
});
}
responseData = await theHiveApiRequest.call(
this,
'POST',
`/v1/case/${caseId}/attachments`,
undefined,
undefined,
undefined,
{
Headers: {
'Content-Type': 'multipart/form-data',
},
formData: {
attachments,
canRename: JSON.stringify(canRename),
},
},
);
const executionData = this.helpers.constructExecutionMetaData(wrapData(responseData), {
itemData: { item: i },
});
return executionData;
}