Files
n8n-enterprise-unlocked/packages/nodes-base/nodes/Google/Drive/v2/actions/folder/create.operation.ts
2023-06-27 11:51:41 +03:00

110 lines
2.9 KiB
TypeScript

import type { IExecuteFunctions } from 'n8n-core';
import type { IDataObject, INodeExecutionData, INodeProperties } from 'n8n-workflow';
import { updateDisplayOptions } from '../../../../../../utils/utilities';
import { googleApiRequest } from '../../transport';
import { driveRLC, folderRLC } from '../common.descriptions';
import { DRIVE } from '../../helpers/interfaces';
import { setParentFolder } from '../../helpers/utils';
const properties: INodeProperties[] = [
{
displayName: 'Folder Name',
name: 'name',
type: 'string',
default: '',
placeholder: 'e.g. New Folder',
description: "The name of the new folder. If not set, 'Untitled' will be used.",
},
{
...driveRLC,
displayName: 'Parent Drive',
description: 'The drive where to create the new folder',
},
{
...folderRLC,
displayName: 'Parent Folder',
description: 'The parent folder where to create the new folder',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Simplify Output',
name: 'simplifyOutput',
type: 'boolean',
default: true,
description: 'Whether to return a simplified version of the response instead of all fields',
},
{
displayName: 'Folder Color',
name: 'folderColorRgb',
type: 'color',
default: '',
description:
'The color of the folder as an RGB hex string. If an unsupported color is specified, the closest color in the palette will be used instead.',
},
],
},
];
const displayOptions = {
show: {
resource: ['folder'],
operation: ['create'],
},
};
export const description = updateDisplayOptions(displayOptions, properties);
export async function execute(this: IExecuteFunctions, i: number): Promise<INodeExecutionData[]> {
const name = (this.getNodeParameter('name', i) as string) || 'Untitled';
const driveId = this.getNodeParameter('driveId', i, undefined, {
extractValue: true,
}) as string;
const folderId = this.getNodeParameter('folderId', i, undefined, {
extractValue: true,
}) as string;
const body: IDataObject = {
name,
mimeType: DRIVE.FOLDER,
parents: [setParentFolder(folderId, driveId)],
};
const folderColorRgb =
(this.getNodeParameter('options.folderColorRgb', i, '') as string) || undefined;
if (folderColorRgb) {
body.folderColorRgb = folderColorRgb;
}
const simplifyOutput = this.getNodeParameter('options.simplifyOutput', i, true) as boolean;
let fields;
if (!simplifyOutput) {
fields = '*';
}
const qs = {
fields,
includeItemsFromAllDrives: true,
supportsAllDrives: true,
spaces: 'appDataFolder, drive',
corpora: 'allDrives',
};
const response = await googleApiRequest.call(this, 'POST', '/drive/v3/files', body, qs);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(response as IDataObject[]),
{ itemData: { item: i } },
);
return executionData;
}