diff --git a/packages/nodes-base/nodes/EmailReadImap/test/v2/EmailReadImapV2.node.test.ts b/packages/nodes-base/nodes/EmailReadImap/test/v2/EmailReadImapV2.node.test.ts index c1651600c1..99bccc9a4e 100644 --- a/packages/nodes-base/nodes/EmailReadImap/test/v2/EmailReadImapV2.node.test.ts +++ b/packages/nodes-base/nodes/EmailReadImap/test/v2/EmailReadImapV2.node.test.ts @@ -1,5 +1,5 @@ import { mock } from 'jest-mock-extended'; -import { type INodeTypeBaseDescription, type ITriggerFunctions } from 'n8n-workflow'; +import type { INode, INodeTypeBaseDescription, ITriggerFunctions } from 'n8n-workflow'; import { type ICredentialsDataImap } from '@credentials/Imap.credentials'; @@ -42,6 +42,7 @@ describe('Test IMap V2', () => { }; triggerFunctions.getCredentials.calledWith('imap').mockResolvedValue(credentials); + triggerFunctions.getNode.mockReturnValue(mock({ typeVersion: 2.1 })); triggerFunctions.logger.debug = jest.fn(); triggerFunctions.getNodeParameter.calledWith('options').mockReturnValue({ name: 'Mark as Read', diff --git a/packages/nodes-base/nodes/EmailReadImap/v2/EmailReadImapV2.node.ts b/packages/nodes-base/nodes/EmailReadImap/v2/EmailReadImapV2.node.ts index 5b5f9dcba0..044e6358f9 100644 --- a/packages/nodes-base/nodes/EmailReadImap/v2/EmailReadImapV2.node.ts +++ b/packages/nodes-base/nodes/EmailReadImap/v2/EmailReadImapV2.node.ts @@ -177,6 +177,19 @@ const versionDescription: INodeTypeDescription = { default: 60, description: 'Sets an interval (in minutes) to force a reconnection', }, + { + displayName: 'Fetch Only New Emails', + name: 'trackLastMessageId', + type: 'boolean', + default: true, + description: + 'Whether to fetch only new emails since the last run, or all emails that match the "Custom Email Rules" (["UNSEEN"] by default)', + displayOptions: { + show: { + '@version': [{ _cnd: { gte: 2.1 } }], + }, + }, + }, ], }, ], @@ -247,6 +260,7 @@ export class EmailReadImapV2 implements INodeType { }; async trigger(this: ITriggerFunctions): Promise { + const node = this.getNode(); const credentialsObject = await this.getCredentials('imap'); const credentials = isCredentialsDataImap(credentialsObject) ? credentialsObject : undefined; if (!credentials) { @@ -258,6 +272,15 @@ export class EmailReadImapV2 implements INodeType { const activatedAt = DateTime.now(); const staticData = this.getWorkflowStaticData('node'); + if (node.typeVersion <= 2) { + // before v 2.1 staticData.lastMessageUid was never set, preserve that behavior + staticData.lastMessageUid = undefined; + } + + if (options.trackLastMessageId === false) { + staticData.lastMessageUid = undefined; + } + this.logger.debug('Loaded static data for node "EmailReadImap"', { staticData }); let connection: ImapSimple; @@ -387,7 +410,7 @@ export class EmailReadImapV2 implements INodeType { * by checking UIDValidity. */ searchCriteria.push(['UID', `${staticData.lastMessageUid as number}:*`]); - } else { + } else if (node.typeVersion > 2 && options.trackLastMessageId !== false) { searchCriteria.push(['SINCE', activatedAt.toFormat('dd-LLL-yyyy')]); }