feat: Add Onedrive Trigger Node (#8742)

Co-authored-by: Giulio Andreini <andreini@netseven.it>
Co-authored-by: Marcus <marcus@n8n.io>
This commit is contained in:
Bram Kn
2024-03-19 15:52:45 +01:00
committed by GitHub
parent 4f0b52c45d
commit ff8dd4e604
6 changed files with 571 additions and 3 deletions

View File

@@ -5,11 +5,13 @@ import type {
JsonObject,
IHttpRequestMethods,
IRequestOptions,
IPollFunctions,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
import { DateTime } from 'luxon';
export async function microsoftApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions,
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
method: IHttpRequestMethods,
resource: string,
@@ -47,7 +49,7 @@ export async function microsoftApiRequest(
}
export async function microsoftApiRequestAllItems(
this: IExecuteFunctions | ILoadOptionsFunctions,
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
propertyName: string,
method: IHttpRequestMethods,
endpoint: string,
@@ -74,7 +76,7 @@ export async function microsoftApiRequestAllItems(
}
export async function microsoftApiRequestAllItemsSkip(
this: IExecuteFunctions | ILoadOptionsFunctions,
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
propertyName: string,
method: IHttpRequestMethods,
endpoint: string,
@@ -96,3 +98,77 @@ export async function microsoftApiRequestAllItemsSkip(
return returnData;
}
export async function microsoftApiRequestAllItemsDelta(
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
link: string,
lastDate: DateTime,
eventType: string,
): Promise<any> {
const returnData: IDataObject[] = [];
let responseData;
let deltaLink: string = '';
let uri: string = link;
do {
responseData = (await microsoftApiRequest.call(this, 'GET', '', {}, {}, uri)) as IDataObject;
uri = responseData['@odata.nextLink'] as string;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
for (const value of responseData.value as IDataObject[]) {
if (value.fileSystemInfo as IDataObject) {
const updatedTimeStamp = (value.fileSystemInfo as IDataObject)
?.lastModifiedDateTime as string;
const createdTimeStamp = (value.fileSystemInfo as IDataObject)?.createdDateTime as string;
if (eventType === 'created') {
if (DateTime.fromISO(createdTimeStamp) >= lastDate) {
returnData.push(value);
}
}
if (eventType === 'updated') {
if (
DateTime.fromISO(updatedTimeStamp) >= lastDate &&
DateTime.fromISO(createdTimeStamp) < lastDate
) {
returnData.push(value);
}
}
}
}
//returnData.push.apply(returnData, responseData.value as IDataObject[]);
deltaLink = (responseData['@odata.deltaLink'] as string) || '';
} while (responseData['@odata.nextLink'] !== undefined);
return { deltaLink, returnData };
}
export async function getPath(
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
itemId: string,
): Promise<string> {
const responseData = (await microsoftApiRequest.call(
this,
'GET',
'',
{},
{},
`https://graph.microsoft.com/v1.0/me/drive/items/${itemId}`,
)) as IDataObject;
if (responseData.folder) {
return (responseData?.parentReference as IDataObject)?.path + `/${responseData?.name}`;
} else {
const workflow = this.getWorkflow();
const node = this.getNode();
this.logger.error(
`There was a problem in '${node.name}' node in workflow '${workflow.id}': 'Item to watch is not a folder'`,
{
node: node.name,
workflowId: workflow.id,
error: 'Item to watch is not a folder',
},
);
throw new NodeApiError(this.getNode(), {
error: 'Item to watch is not a folder',
} as JsonObject);
}
}