n8n/packages/nodes-base/nodes/TheHiveProject/actions/case/addAttachment.operation.ts
Iván Ovejero 62c096710f
refactor: Run lintfix (no-changelog) (#7537)
- Fix autofixable violations
- Remove unused directives
- Allow for PascalCased variables - needed for dynamically imported or
assigned classes, decorators, routers, etc.
2023-10-27 14:15:02 +02:00

90 lines
2.1 KiB
TypeScript

import type {
IDataObject,
IExecuteFunctions,
INodeExecutionData,
INodeProperties,
} from 'n8n-workflow';
import { theHiveApiRequest } from '../../transport';
import { attachmentsUi, caseRLC } from '../../descriptions';
import { updateDisplayOptions, wrapData } from '@utils/utilities';
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;
}