diff --git a/packages/nodes-base/nodes/Google/Gmail/GmailTrigger.node.ts b/packages/nodes-base/nodes/Google/Gmail/GmailTrigger.node.ts index d6b114e03b..73d0ca9b19 100644 --- a/packages/nodes-base/nodes/Google/Gmail/GmailTrigger.node.ts +++ b/packages/nodes-base/nodes/Google/Gmail/GmailTrigger.node.ts @@ -33,7 +33,7 @@ export class GmailTrigger implements INodeType { name: 'gmailTrigger', icon: 'file:gmail.svg', group: ['trigger'], - version: [1, 1.1, 1.2], + version: [1, 1.1, 1.2, 1.3], description: 'Fetches emails from Gmail and starts the workflow on specified polling intervals.', subtitle: '={{"Gmail Trigger"}}', @@ -327,6 +327,10 @@ export class GmailTrigger implements INodeType { } } + if (node.typeVersion > 1.2 && fullMessage.labelIds?.includes('SENT')) { + continue; + } + if (!simple) { const dataPropertyNameDownload = options.dataPropertyAttachmentsPrefixName || 'attachment_'; diff --git a/packages/nodes-base/nodes/Google/Gmail/test/GmailTrigger.test.ts b/packages/nodes-base/nodes/Google/Gmail/test/GmailTrigger.test.ts index c8aebdf694..35da637dd5 100644 --- a/packages/nodes-base/nodes/Google/Gmail/test/GmailTrigger.test.ts +++ b/packages/nodes-base/nodes/Google/Gmail/test/GmailTrigger.test.ts @@ -271,4 +271,112 @@ describe('GmailTrigger', () => { expect(response).toMatchSnapshot(); }); + + it('should skip DRAFTS when option is set', async () => { + const messageListResponse: MessageListResponse = { + messages: [createListMessage({ id: '1' }), createListMessage({ id: '2' })], + resultSizeEstimate: 2, + }; + nock(baseUrl) + .get('/gmail/v1/users/me/labels') + .reply(200, { + labels: [ + { id: 'INBOX', name: 'INBOX' }, + { id: 'DRAFT', name: 'DRAFT' }, + ], + }); + nock(baseUrl).get(new RegExp('/gmail/v1/users/me/messages?.*')).reply(200, messageListResponse); + nock(baseUrl) + .get(new RegExp('/gmail/v1/users/me/messages/1?.*')) + .reply(200, createMessage({ id: '1', labelIds: ['DRAFT'] })); + nock(baseUrl) + .get(new RegExp('/gmail/v1/users/me/messages/2?.*')) + .reply(200, createMessage({ id: '2', labelIds: ['INBOX'] })); + + const { response } = await testPollingTriggerNode(GmailTrigger, { + node: { parameters: { filters: { includeDrafts: false } } }, + }); + + expect(response).toEqual([ + [ + { + binary: undefined, + json: { + attachements: undefined, + date: '2024-08-31T00:00:00.000Z', + from: { + html: 'from@example.com', + text: 'from@example.com', + value: [{ address: 'from@example.com', name: 'From' }], + }, + headerlines: undefined, + headers: { headerKey: 'headerValue' }, + html: '
test
', + id: '2', + labelIds: ['INBOX'], + sizeEstimate: 4, + threadId: 'testThreadId', + to: { + html: 'to@example.com', + text: 'to@example.com', + value: [{ address: 'to@example.com', name: 'To' }], + }, + }, + }, + ], + ]); + }); + + it('should skip emails with SENT label', async () => { + const messageListResponse: MessageListResponse = { + messages: [createListMessage({ id: '1' }), createListMessage({ id: '2' })], + resultSizeEstimate: 2, + }; + nock(baseUrl) + .get('/gmail/v1/users/me/labels') + .reply(200, { + labels: [ + { id: 'INBOX', name: 'INBOX' }, + { id: 'SENT', name: 'SENT' }, + ], + }); + nock(baseUrl).get(new RegExp('/gmail/v1/users/me/messages?.*')).reply(200, messageListResponse); + nock(baseUrl) + .get(new RegExp('/gmail/v1/users/me/messages/1?.*')) + .reply(200, createMessage({ id: '1', labelIds: ['INBOX'] })); + nock(baseUrl) + .get(new RegExp('/gmail/v1/users/me/messages/2?.*')) + .reply(200, createMessage({ id: '2', labelIds: ['SENT'] })); + + const { response } = await testPollingTriggerNode(GmailTrigger); + + expect(response).toEqual([ + [ + { + binary: undefined, + json: { + attachements: undefined, + date: '2024-08-31T00:00:00.000Z', + from: { + html: 'from@example.com', + text: 'from@example.com', + value: [{ address: 'from@example.com', name: 'From' }], + }, + headerlines: undefined, + headers: { headerKey: 'headerValue' }, + html: 'test
', + id: '1', + labelIds: ['INBOX'], + sizeEstimate: 4, + threadId: 'testThreadId', + to: { + html: 'to@example.com', + text: 'to@example.com', + value: [{ address: 'to@example.com', name: 'To' }], + }, + }, + }, + ], + ]); + }); });