feat: Add Outlook Trigger Node (#8656)

Co-authored-by: Jonathan Bennetts <jonathan.bennetts@gmail.com>
This commit is contained in:
Bram Kn
2024-02-28 10:23:58 +01:00
committed by GitHub
parent 1e750c26b7
commit 720ae1b96b
7 changed files with 348 additions and 5 deletions

View File

@@ -0,0 +1,85 @@
import { NodeApiError } from 'n8n-workflow';
import type { JsonObject, IDataObject, INodeExecutionData, IPollFunctions } from 'n8n-workflow';
import {
downloadAttachments,
microsoftApiRequest,
microsoftApiRequestAllItems,
} from '../v2/transport';
import { prepareFilterString, simplifyOutputMessages } from '../v2/helpers/utils';
export async function getPollResponse(
this: IPollFunctions,
pollStartDate: string,
pollEndDate: string,
) {
let responseData;
const qs = {} as IDataObject;
try {
const filters = this.getNodeParameter('filters', {}) as IDataObject;
const options = this.getNodeParameter('options', {}) as IDataObject;
const output = this.getNodeParameter('output') as string;
if (output === 'fields') {
const fields = this.getNodeParameter('fields') as string[];
if (options.downloadAttachments) {
fields.push('hasAttachments');
}
qs.$select = fields.join(',');
}
if (output === 'simple') {
qs.$select =
'id,conversationId,subject,bodyPreview,from,toRecipients,categories,hasAttachments';
}
const filterString = prepareFilterString({ filters });
if (filterString) {
qs.$filter = filterString;
}
const endpoint = '/messages';
if (this.getMode() !== 'manual') {
if (qs.$filter) {
qs.$filter = `${qs.$filter} and receivedDateTime ge ${pollStartDate} and receivedDateTime lt ${pollEndDate}`;
} else {
qs.$filter = `receivedDateTime ge ${pollStartDate} and receivedDateTime lt ${pollEndDate}`;
}
responseData = await microsoftApiRequestAllItems.call(
this,
'value',
'GET',
endpoint,
undefined,
qs,
);
} else {
qs.$top = 1;
responseData = await microsoftApiRequest.call(this, 'GET', endpoint, undefined, qs);
responseData = responseData.value;
}
if (output === 'simple') {
responseData = simplifyOutputMessages(responseData as IDataObject[]);
}
let executionData: INodeExecutionData[] = [];
if (options.downloadAttachments) {
const prefix = (options.attachmentsPrefix as string) || 'attachment_';
executionData = await downloadAttachments.call(this, responseData as IDataObject[], prefix);
} else {
executionData = this.helpers.returnJsonArray(responseData as IDataObject[]);
}
return executionData;
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject, {
message: error.message,
description: error.description,
});
}
}

View File

@@ -0,0 +1,139 @@
import type { INodeProperties } from 'n8n-workflow';
import { messageFields } from '../v2/helpers/utils';
export const properties: INodeProperties[] = [
{
displayName: 'Output',
name: 'output',
type: 'options',
default: 'simple',
options: [
{
name: 'Simplified',
value: 'simple',
},
{
name: 'Raw',
value: 'raw',
},
{
name: 'Select Included Fields',
value: 'fields',
},
],
},
{
displayName: 'Fields',
name: 'fields',
type: 'multiOptions',
description: 'The fields to add to the output',
displayOptions: {
show: {
output: ['fields'],
},
},
options: messageFields,
default: [],
},
{
displayName: 'Filters',
name: 'filters',
type: 'collection',
placeholder: 'Add Filter',
default: {},
options: [
{
displayName: 'Filter Query',
name: 'custom',
type: 'string',
default: '',
placeholder: 'e.g. isRead eq false',
hint: 'Search query to filter messages. <a href="https://learn.microsoft.com/en-us/graph/filter-query-parameter">More info</a>.',
},
{
displayName: 'Has Attachments',
name: 'hasAttachments',
type: 'boolean',
default: false,
},
{
displayName: 'Folders to Exclude',
name: 'foldersToExclude',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getFolders',
},
default: [],
description:
'Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
},
{
displayName: 'Folders to Include',
name: 'foldersToInclude',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getFolders',
},
default: [],
description:
'Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
},
{
displayName: 'Read Status',
name: 'readStatus',
type: 'options',
default: 'unread',
hint: 'Filter messages by whether they have been read or not',
options: [
{
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
name: 'Unread and read messages',
value: 'both',
},
{
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
name: 'Unread messages only',
value: 'unread',
},
{
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
name: 'Read messages only',
value: 'read',
},
],
},
{
displayName: 'Sender',
name: 'sender',
type: 'string',
default: '',
description: 'Sender name or email to filter by',
},
],
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Attachments Prefix',
name: 'attachmentsPrefix',
type: 'string',
default: 'attachment_',
description:
'Prefix for name of the output fields to put the binary files data in. An index starting from 0 will be added. So if name is "attachment_" the first attachment is saved to "attachment_0".',
},
{
displayName: 'Download Attachments',
name: 'downloadAttachments',
type: 'boolean',
default: false,
description:
"Whether the message's attachments will be downloaded and included in the output",
},
],
},
];