2022-08-01 13:47:55 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2020-07-25 10:58:38 -07:00
|
|
|
|
|
|
|
import {
|
2020-10-01 05:01:39 -07:00
|
|
|
IBinaryKeyData,
|
2020-07-25 10:58:38 -07:00
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeTypeDescription,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeOperationError,
|
2020-07-25 10:58:38 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { boxApiRequest, boxApiRequestAllItems } from './GenericFunctions';
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { fileFields, fileOperations } from './FileDescription';
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { folderFields, folderOperations } from './FolderDescription';
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment-timezone';
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { noCase } from 'change-case';
|
2021-07-02 14:58:18 -07:00
|
|
|
|
2020-07-25 10:58:38 -07:00
|
|
|
export class Box implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Box',
|
|
|
|
name: 'box',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-07-25 10:58:38 -07:00
|
|
|
icon: 'file:box.png',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Box API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Box',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'boxOAuth2Api',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-07-25 10:58:38 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'File',
|
|
|
|
value: 'file',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Folder',
|
|
|
|
value: 'folder',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'file',
|
|
|
|
},
|
|
|
|
...fileOperations,
|
|
|
|
...fileFields,
|
|
|
|
...folderOperations,
|
|
|
|
...folderFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-07-25 10:58:38 -07:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
2021-07-02 14:58:18 -07:00
|
|
|
const timezone = this.getTimezone();
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-07-25 10:58:38 -07:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'file') {
|
|
|
|
// https://developer.box.com/reference/post-files-id-copy
|
|
|
|
if (operation === 'copy') {
|
|
|
|
const fileId = this.getNodeParameter('fileId', i) as string;
|
|
|
|
const parentId = this.getNodeParameter('parentId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {};
|
|
|
|
if (additionalFields.name) {
|
|
|
|
body.name = additionalFields.name as string;
|
|
|
|
}
|
|
|
|
if (parentId) {
|
|
|
|
body.parent = { id: parentId };
|
|
|
|
} else {
|
|
|
|
body.parent = { id: 0 };
|
|
|
|
}
|
|
|
|
if (additionalFields.fields) {
|
|
|
|
qs.fields = additionalFields.fields as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.version) {
|
|
|
|
body.version = additionalFields.version as string;
|
|
|
|
}
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await boxApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/files/${fileId}/copy`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
2020-07-25 10:58:38 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.box.com/reference/delete-files-id
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const fileId = this.getNodeParameter('fileId', i) as string;
|
|
|
|
responseData = await boxApiRequest.call(this, 'DELETE', `/files/${fileId}`);
|
|
|
|
responseData = { success: true };
|
2020-07-25 10:58:38 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.box.com/reference/get-files-id-content
|
|
|
|
if (operation === 'download') {
|
|
|
|
const fileId = this.getNodeParameter('fileId', i) as string;
|
2022-08-01 13:47:55 -07:00
|
|
|
const dataPropertyNameDownload = this.getNodeParameter(
|
|
|
|
'binaryPropertyName',
|
|
|
|
i,
|
|
|
|
) as string;
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await boxApiRequest.call(this, 'GET', `/files/${fileId}`);
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const fileName = responseData.name;
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let mimeType: string | undefined;
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await boxApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/files/${fileId}/content`,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
undefined,
|
|
|
|
{ encoding: null, resolveWithFullResponse: true },
|
|
|
|
);
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const newItem: INodeExecutionData = {
|
|
|
|
json: items[i].json,
|
|
|
|
binary: {},
|
|
|
|
};
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (mimeType === undefined && responseData.headers['content-type']) {
|
|
|
|
mimeType = responseData.headers['content-type'];
|
|
|
|
}
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (items[i].binary !== undefined) {
|
|
|
|
// Create a shallow copy of the binary data so that the old
|
|
|
|
// data references which do not get changed still stay behind
|
|
|
|
// but the incoming data does not get changed.
|
2022-08-30 08:55:33 -07:00
|
|
|
Object.assign(newItem.binary!, items[i].binary);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
items[i] = newItem;
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const data = Buffer.from(responseData.body);
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
items[i].binary![dataPropertyNameDownload] = await this.helpers.prepareBinaryData(
|
|
|
|
data as unknown as Buffer,
|
|
|
|
fileName,
|
|
|
|
mimeType,
|
|
|
|
);
|
2020-07-25 10:58:38 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.box.com/reference/get-files-id
|
|
|
|
if (operation === 'get') {
|
|
|
|
const fileId = this.getNodeParameter('fileId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.fields) {
|
|
|
|
qs.fields = additionalFields.fields as string;
|
|
|
|
}
|
|
|
|
responseData = await boxApiRequest.call(this, 'GET', `/files/${fileId}`, {}, qs);
|
2020-07-25 10:58:38 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.box.com/reference/get-search/
|
|
|
|
if (operation === 'search') {
|
|
|
|
const query = this.getNodeParameter('query', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-12-02 12:54:28 -08:00
|
|
|
const tz = this.getTimezone();
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.type = 'file';
|
|
|
|
qs.query = query;
|
|
|
|
Object.assign(qs, additionalFields);
|
|
|
|
|
|
|
|
if (qs.content_types) {
|
|
|
|
qs.content_types = (qs.content_types as string).split(',');
|
|
|
|
}
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.createdRangeUi) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const createdRangeValues = (additionalFields.createdRangeUi as IDataObject)
|
|
|
|
.createdRangeValuesUi as IDataObject;
|
2021-07-19 23:58:54 -07:00
|
|
|
if (createdRangeValues) {
|
2022-12-02 12:54:28 -08:00
|
|
|
const from = moment.tz(createdRangeValues.from, tz).format();
|
|
|
|
const to = moment.tz(createdRangeValues.to, tz).format();
|
|
|
|
qs.created_at_range = `${from},${to}`;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
delete qs.createdRangeUi;
|
2020-07-25 10:58:38 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.updatedRangeUi) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const updateRangeValues = (additionalFields.updatedRangeUi as IDataObject)
|
|
|
|
.updatedRangeValuesUi as IDataObject;
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateRangeValues) {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.updated_at_range = `${moment.tz(updateRangeValues.from, tz).format()},${moment
|
|
|
|
.tz(updateRangeValues.to, tz)
|
|
|
|
.format()}`;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
delete qs.updatedRangeUi;
|
2020-07-25 10:58:38 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await boxApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'entries',
|
|
|
|
'GET',
|
|
|
|
`/search`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.limit = this.getNodeParameter('limit', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await boxApiRequest.call(this, 'GET', `/search`, {}, qs);
|
|
|
|
responseData = responseData.entries;
|
|
|
|
}
|
2021-07-02 14:58:18 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.box.com/reference/post-collaborations/
|
|
|
|
if (operation === 'share') {
|
|
|
|
const fileId = this.getNodeParameter('fileId', i) as string;
|
|
|
|
const role = this.getNodeParameter('role', i) as string;
|
|
|
|
const accessibleBy = this.getNodeParameter('accessibleBy', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
const body: { accessible_by: IDataObject; [key: string]: any } = {
|
2021-07-19 23:58:54 -07:00
|
|
|
accessible_by: {},
|
|
|
|
item: {
|
|
|
|
id: fileId,
|
|
|
|
type: 'file',
|
|
|
|
},
|
2022-08-01 13:47:55 -07:00
|
|
|
role: role === 'coOwner' ? 'co-owner' : noCase(role),
|
2021-07-19 23:58:54 -07:00
|
|
|
...options,
|
|
|
|
};
|
2021-07-02 14:58:18 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.fields) {
|
|
|
|
qs.fields = body.fields;
|
|
|
|
delete body.fields;
|
|
|
|
}
|
2021-07-02 14:58:18 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.expires_at) {
|
|
|
|
body.expires_at = moment.tz(body.expires_at, timezone).format();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.notify) {
|
|
|
|
qs.notify = body.notify;
|
|
|
|
delete body.notify;
|
|
|
|
}
|
2021-07-02 14:58:18 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (accessibleBy === 'user') {
|
|
|
|
const useEmail = this.getNodeParameter('useEmail', i) as boolean;
|
|
|
|
if (useEmail) {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.accessible_by.login = this.getNodeParameter('email', i) as string;
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.accessible_by.id = this.getNodeParameter('userId', i) as string;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-07-02 14:58:18 -07:00
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.accessible_by.id = this.getNodeParameter('groupId', i) as string;
|
2021-07-02 14:58:18 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await boxApiRequest.call(this, 'POST', `/collaborations`, body, qs);
|
2020-07-25 10:58:38 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.box.com/reference/post-files-content
|
|
|
|
if (operation === 'upload') {
|
|
|
|
const parentId = this.getNodeParameter('parentId', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const isBinaryData = this.getNodeParameter('binaryData', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const fileName = this.getNodeParameter('fileName', i) as string;
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const attributes: IDataObject = {};
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (parentId !== '') {
|
2022-12-02 12:54:28 -08:00
|
|
|
attributes.parent = { id: parentId };
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
// if not parent defined save it on the root directory
|
2022-12-02 12:54:28 -08:00
|
|
|
attributes.parent = { id: 0 };
|
2020-07-25 10:58:38 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (isBinaryData) {
|
|
|
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', 0) as string;
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (items[i].binary === undefined) {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No binary data exists on item!', {
|
|
|
|
itemIndex: i,
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
//@ts-ignore
|
|
|
|
if (items[i].binary[binaryPropertyName] === undefined) {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`No binary data property "${binaryPropertyName}" does not exists on item!`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const binaryData = (items[i].binary as IBinaryKeyData)[binaryPropertyName];
|
2022-08-01 13:47:55 -07:00
|
|
|
const binaryDataBuffer = await this.helpers.getBinaryDataBuffer(
|
|
|
|
i,
|
|
|
|
binaryPropertyName,
|
|
|
|
);
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {};
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
attributes.name = fileName || binaryData.fileName;
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
body.attributes = JSON.stringify(attributes);
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
body.file = {
|
2021-08-20 09:08:40 -07:00
|
|
|
value: binaryDataBuffer,
|
2021-07-19 23:58:54 -07:00
|
|
|
options: {
|
|
|
|
filename: binaryData.fileName,
|
|
|
|
contentType: binaryData.mimeType,
|
|
|
|
},
|
|
|
|
};
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await boxApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
'',
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
'https://upload.box.com/api/2.0/files/content',
|
|
|
|
{ formData: body },
|
|
|
|
);
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = responseData.entries;
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
const content = this.getNodeParameter('fileContent', i) as string;
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (fileName === '') {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'File name must be set!', {
|
|
|
|
itemIndex: i,
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
attributes.name = fileName;
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {};
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
body.attributes = JSON.stringify(attributes);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
body.file = {
|
2021-07-19 23:58:54 -07:00
|
|
|
value: Buffer.from(content),
|
|
|
|
options: {
|
|
|
|
filename: fileName,
|
|
|
|
contentType: 'text/plain',
|
|
|
|
},
|
|
|
|
};
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await boxApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
'',
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
'https://upload.box.com/api/2.0/files/content',
|
|
|
|
{ formData: body },
|
|
|
|
);
|
2022-08-30 08:55:33 -07:00
|
|
|
responseData = responseData.entries;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-07-25 10:58:38 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'folder') {
|
|
|
|
// https://developer.box.com/reference/post-folders
|
|
|
|
if (operation === 'create') {
|
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
|
|
|
const parentId = this.getNodeParameter('parentId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
name,
|
2020-07-25 10:58:38 -07:00
|
|
|
};
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (parentId) {
|
|
|
|
body.parent = { id: parentId };
|
|
|
|
} else {
|
|
|
|
body.parent = { id: 0 };
|
|
|
|
}
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.access) {
|
|
|
|
body.folder_upload_email = {
|
|
|
|
access: options.access as string,
|
|
|
|
};
|
|
|
|
}
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.fields) {
|
|
|
|
qs.fields = options.fields as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await boxApiRequest.call(this, 'POST', '/folders', body, qs);
|
2020-07-25 10:58:38 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.box.com/reference/delete-folders-id
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const folderId = this.getNodeParameter('folderId', i) as string;
|
|
|
|
const recursive = this.getNodeParameter('recursive', i) as boolean;
|
|
|
|
qs.recursive = recursive;
|
|
|
|
|
|
|
|
responseData = await boxApiRequest.call(this, 'DELETE', `/folders/${folderId}`, qs);
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
// https://developer.box.com/reference/get-folders-id/
|
|
|
|
if (operation === 'get') {
|
|
|
|
const folderId = this.getNodeParameter('folderId', i) as string;
|
|
|
|
responseData = await boxApiRequest.call(this, 'GET', `/folders/${folderId}`, qs);
|
|
|
|
}
|
|
|
|
// https://developer.box.com/reference/get-search/
|
|
|
|
if (operation === 'search') {
|
|
|
|
const query = this.getNodeParameter('query', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-12-02 12:54:28 -08:00
|
|
|
const tz = this.getTimezone();
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.type = 'folder';
|
|
|
|
qs.query = query;
|
|
|
|
Object.assign(qs, additionalFields);
|
|
|
|
|
|
|
|
if (qs.content_types) {
|
|
|
|
qs.content_types = (qs.content_types as string).split(',');
|
|
|
|
}
|
2020-07-25 10:58:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.createdRangeUi) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const createdRangeValues = (additionalFields.createdRangeUi as IDataObject)
|
|
|
|
.createdRangeValuesUi as IDataObject;
|
2021-07-19 23:58:54 -07:00
|
|
|
if (createdRangeValues) {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.created_at_range = `${moment.tz(createdRangeValues.from, tz).format()},${moment
|
|
|
|
.tz(createdRangeValues.to, tz)
|
|
|
|
.format()}`;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
delete qs.createdRangeUi;
|
2020-07-25 10:58:38 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.updatedRangeUi) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const updateRangeValues = (additionalFields.updatedRangeUi as IDataObject)
|
|
|
|
.updatedRangeValuesUi as IDataObject;
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateRangeValues) {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.updated_at_range = `${moment.tz(updateRangeValues.from, tz).format()},${moment
|
|
|
|
.tz(updateRangeValues.to, tz)
|
|
|
|
.format()}`;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
delete qs.updatedRangeUi;
|
2020-07-25 10:58:38 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await boxApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'entries',
|
|
|
|
'GET',
|
|
|
|
`/search`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.limit = this.getNodeParameter('limit', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await boxApiRequest.call(this, 'GET', `/search`, {}, qs);
|
|
|
|
responseData = responseData.entries;
|
|
|
|
}
|
2021-07-02 14:58:18 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
// https://developer.box.com/reference/post-collaborations/
|
|
|
|
if (operation === 'share') {
|
|
|
|
const folderId = this.getNodeParameter('folderId', i) as string;
|
|
|
|
const role = this.getNodeParameter('role', i) as string;
|
|
|
|
const accessibleBy = this.getNodeParameter('accessibleBy', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
const body: { accessible_by: IDataObject; [key: string]: any } = {
|
2021-07-19 23:58:54 -07:00
|
|
|
accessible_by: {},
|
|
|
|
item: {
|
|
|
|
id: folderId,
|
|
|
|
type: 'folder',
|
|
|
|
},
|
2022-08-01 13:47:55 -07:00
|
|
|
role: role === 'coOwner' ? 'co-owner' : noCase(role),
|
2021-07-19 23:58:54 -07:00
|
|
|
...options,
|
|
|
|
};
|
2021-07-02 14:58:18 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.fields) {
|
|
|
|
qs.fields = body.fields;
|
|
|
|
delete body.fields;
|
|
|
|
}
|
2021-07-02 14:58:18 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.expires_at) {
|
|
|
|
body.expires_at = moment.tz(body.expires_at, timezone).format();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.notify) {
|
|
|
|
qs.notify = body.notify;
|
|
|
|
delete body.notify;
|
|
|
|
}
|
2021-07-02 14:58:18 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (accessibleBy === 'user') {
|
|
|
|
const useEmail = this.getNodeParameter('useEmail', i) as boolean;
|
|
|
|
if (useEmail) {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.accessible_by.login = this.getNodeParameter('email', i) as string;
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.accessible_by.id = this.getNodeParameter('userId', i) as string;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-07-02 14:58:18 -07:00
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.accessible_by.id = this.getNodeParameter('groupId', i) as string;
|
2021-07-02 14:58:18 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await boxApiRequest.call(this, 'POST', `/collaborations`, body, qs);
|
2021-07-02 14:58:18 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developer.box.com/guides/folders/single/move/
|
|
|
|
if (operation === 'update') {
|
|
|
|
const folderId = this.getNodeParameter('folderId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
if (updateFields.fields) {
|
|
|
|
qs.fields = updateFields.fields;
|
|
|
|
delete updateFields.fields;
|
|
|
|
}
|
2021-07-02 14:58:18 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body = {
|
|
|
|
...updateFields,
|
|
|
|
} as IDataObject;
|
2021-07-02 14:58:18 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.parentId) {
|
|
|
|
body.parent = {
|
|
|
|
id: body.parentId,
|
|
|
|
};
|
|
|
|
delete body.parentId;
|
|
|
|
}
|
2021-07-02 14:58:18 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.tags) {
|
|
|
|
body.tags = (body.tags as string).split(',');
|
|
|
|
}
|
2021-07-02 14:58:18 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await boxApiRequest.call(this, 'PUT', `/folders/${folderId}`, body, qs);
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
2022-09-20 06:34:00 -07:00
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
2021-07-02 14:58:18 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
throw error;
|
2020-07-25 10:58:38 -07:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
2020-07-25 10:58:38 -07:00
|
|
|
if (resource === 'file' && operation === 'download') {
|
|
|
|
// For file downloads the files get attached to the existing items
|
|
|
|
return this.prepareOutputData(items);
|
|
|
|
} else {
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2020-07-25 10:58:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|