mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-16 01:24:05 -08:00
90 lines
2.1 KiB
TypeScript
90 lines
2.1 KiB
TypeScript
|
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;
|
||
|
}
|