Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Gmail Trigger Node): Fetching duplicate emails #9424

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 44 additions & 16 deletions packages/nodes-base/nodes/Google/Gmail/GmailTrigger.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class GmailTrigger implements INodeType {
name: 'gmailTrigger',
icon: 'file:gmail.svg',
group: ['trigger'],
version: 1,
version: [1, 1.1],
description:
'Fetches emails from Gmail and starts the workflow on specified polling intervals.',
subtitle: '={{"Gmail Trigger"}}',
Expand Down Expand Up @@ -226,11 +226,24 @@ export class GmailTrigger implements INodeType {
};

async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {
const webhookData = this.getWorkflowStaticData('node');
const workflowStaticData = this.getWorkflowStaticData('node');
const node = this.getNode();

let nodeStaticData = workflowStaticData;
if (node.typeVersion > 1) {
const nodeName = node.name;
if (workflowStaticData[nodeName] === undefined) {
workflowStaticData[nodeName] = {} as IDataObject;
nodeStaticData = workflowStaticData[nodeName] as IDataObject;
} else {
nodeStaticData = workflowStaticData[nodeName] as IDataObject;
}
}

let responseData;

const now = Math.floor(DateTime.now().toSeconds()).toString();
const startDate = (webhookData.lastTimeChecked as string) || +now;
const startDate = (nodeStaticData.lastTimeChecked as string) || +now;
const endDate = +now;

const options = this.getNodeParameter('options', {}) as IDataObject;
Expand All @@ -257,7 +270,7 @@ export class GmailTrigger implements INodeType {
responseData = responseData.messages;

if (!responseData?.length) {
webhookData.lastTimeChecked = endDate;
nodeStaticData.lastTimeChecked = endDate;
return null;
}

Expand Down Expand Up @@ -297,11 +310,10 @@ export class GmailTrigger implements INodeType {
);
}
} catch (error) {
if (this.getMode() === 'manual' || !webhookData.lastTimeChecked) {
if (this.getMode() === 'manual' || !nodeStaticData.lastTimeChecked) {
throw error;
}
const workflow = this.getWorkflow();
const node = this.getNode();
this.logger.error(
`There was a problem in '${node.name}' node in workflow '${workflow.id}': '${error.description}'`,
{
Expand All @@ -313,15 +325,31 @@ export class GmailTrigger implements INodeType {
}

if (!responseData?.length) {
webhookData.lastTimeChecked = endDate;
nodeStaticData.lastTimeChecked = endDate;
return null;
}

const getEmailDateAsSeconds = (email: IDataObject) => {
const { internalDate, date } = email;
return internalDate
? +(internalDate as string) / 1000
: +DateTime.fromJSDate(new Date(date as string)).toSeconds();
const emailsWithInvalidDate = new Set<string>();

const getEmailDateAsSeconds = (email: IDataObject): number => {
let date;

if (email.internalDate) {
date = +(email.internalDate as string) / 1000;
} else if (email.date) {
date = +DateTime.fromJSDate(new Date(email.date as string)).toSeconds();
} else {
date = +DateTime.fromJSDate(
new Date((email?.headers as IDataObject)?.date as string),
).toSeconds();
}

if (!date || isNaN(date)) {
emailsWithInvalidDate.add(email.id as string);
return +startDate;
}

return date;
};

const lastEmailDate = (responseData as IDataObject[]).reduce((lastDate, { json }) => {
Expand All @@ -336,19 +364,19 @@ export class GmailTrigger implements INodeType {
? duplicates.concat((json as IDataObject).id as string)
: duplicates;
},
[] as string[],
Array.from(emailsWithInvalidDate),
);

const possibleDuplicates = (webhookData.possibleDuplicates as string[]) || [];
const possibleDuplicates = (nodeStaticData.possibleDuplicates as string[]) || [];
if (possibleDuplicates.length) {
responseData = (responseData as IDataObject[]).filter(({ json }) => {
const { id } = json as IDataObject;
return !possibleDuplicates.includes(id as string);
});
}

webhookData.possibleDuplicates = nextPollPossibleDuplicates;
webhookData.lastTimeChecked = lastEmailDate || endDate;
nodeStaticData.possibleDuplicates = nextPollPossibleDuplicates;
nodeStaticData.lastTimeChecked = lastEmailDate || endDate;

if (Array.isArray(responseData) && responseData.length) {
return [responseData as INodeExecutionData[]];
Expand Down
Loading