From 7a2be77f384a32ede3acad8fe24fb89227c058bf Mon Sep 17 00:00:00 2001 From: Ria Scholz <123465523+riascho@users.noreply.github.com> Date: Wed, 6 Nov 2024 18:58:26 +0530 Subject: [PATCH] feat(Gmail Trigger Node): Add filter option to include drafts (#11441) --- .../nodes/Google/Gmail/GmailTrigger.node.ts | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/packages/nodes-base/nodes/Google/Gmail/GmailTrigger.node.ts b/packages/nodes-base/nodes/Google/Gmail/GmailTrigger.node.ts index c0bacf2085..9fcac0e41d 100644 --- a/packages/nodes-base/nodes/Google/Gmail/GmailTrigger.node.ts +++ b/packages/nodes-base/nodes/Google/Gmail/GmailTrigger.node.ts @@ -24,7 +24,7 @@ export class GmailTrigger implements INodeType { name: 'gmailTrigger', icon: 'file:gmail.svg', group: ['trigger'], - version: [1, 1.1], + version: [1, 1.1, 1.2], description: 'Fetches emails from Gmail and starts the workflow on specified polling intervals.', subtitle: '={{"Gmail Trigger"}}', @@ -106,6 +106,13 @@ export class GmailTrigger implements INodeType { default: false, description: 'Whether to include messages from SPAM and TRASH in the results', }, + { + displayName: 'Include Drafts', + name: 'includeDrafts', + type: 'boolean', + default: false, + description: 'Whether to include email drafts in the results', + }, { displayName: 'Label Names or IDs', name: 'labelIds', @@ -284,6 +291,15 @@ export class GmailTrigger implements INodeType { qs.format = 'raw'; } + let includeDrafts; + if (node.typeVersion > 1.1) { + includeDrafts = (qs.includeDrafts as boolean) ?? false; + } else { + includeDrafts = (qs.includeDrafts as boolean) ?? true; + } + delete qs.includeDrafts; + const withoutDrafts = []; + for (let i = 0; i < responseData.length; i++) { responseData[i] = await googleApiRequest.call( this, @@ -292,8 +308,12 @@ export class GmailTrigger implements INodeType { {}, qs, ); - - if (!simple) { + if (!includeDrafts) { + if (responseData[i].labelIds.includes('DRAFT')) { + continue; + } + } + if (!simple && responseData?.length) { const dataPropertyNameDownload = (options.dataPropertyAttachmentsPrefixName as string) || 'attachment_'; @@ -303,9 +323,14 @@ export class GmailTrigger implements INodeType { dataPropertyNameDownload, ); } + withoutDrafts.push(responseData[i]); } - if (simple) { + if (!includeDrafts) { + responseData = withoutDrafts; + } + + if (simple && responseData?.length) { responseData = this.helpers.returnJsonArray( await simplifyOutput.call(this, responseData as IDataObject[]), ); @@ -324,17 +349,14 @@ export class GmailTrigger implements INodeType { }, ); } - if (!responseData?.length) { nodeStaticData.lastTimeChecked = endDate; return null; } const emailsWithInvalidDate = new Set(); - const getEmailDateAsSeconds = (email: IDataObject): number => { let date; - if (email.internalDate) { date = +(email.internalDate as string) / 1000; } else if (email.date) {