mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
fix(Gmail Trigger Node): Trigger node missing some emails
This commit is contained in:
@@ -166,19 +166,13 @@ export async function parseRawEmail(
|
|||||||
dataPropertyNameDownload: string,
|
dataPropertyNameDownload: string,
|
||||||
): Promise<INodeExecutionData> {
|
): Promise<INodeExecutionData> {
|
||||||
const messageEncoded = Buffer.from(messageData.raw, 'base64').toString('utf8');
|
const messageEncoded = Buffer.from(messageData.raw, 'base64').toString('utf8');
|
||||||
let responseData = await simpleParser(messageEncoded);
|
const responseData = await simpleParser(messageEncoded);
|
||||||
|
|
||||||
const headers: IDataObject = {};
|
const headers: IDataObject = {};
|
||||||
// @ts-ignore
|
|
||||||
for (const header of responseData.headerLines) {
|
for (const header of responseData.headerLines) {
|
||||||
headers[header.key] = header.line;
|
headers[header.key] = header.line;
|
||||||
}
|
}
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
responseData.headers = headers;
|
|
||||||
// @ts-ignore
|
|
||||||
responseData.headerLines = undefined;
|
|
||||||
|
|
||||||
const binaryData: IBinaryKeyData = {};
|
const binaryData: IBinaryKeyData = {};
|
||||||
if (responseData.attachments) {
|
if (responseData.attachments) {
|
||||||
const downloadAttachments = this.getNodeParameter(
|
const downloadAttachments = this.getNodeParameter(
|
||||||
@@ -196,8 +190,6 @@ export async function parseRawEmail(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// @ts-ignore
|
|
||||||
responseData.attachments = undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const mailBaseData: IDataObject = {};
|
const mailBaseData: IDataObject = {};
|
||||||
@@ -205,14 +197,17 @@ export async function parseRawEmail(
|
|||||||
const resolvedModeAddProperties = ['id', 'threadId', 'labelIds', 'sizeEstimate'];
|
const resolvedModeAddProperties = ['id', 'threadId', 'labelIds', 'sizeEstimate'];
|
||||||
|
|
||||||
for (const key of resolvedModeAddProperties) {
|
for (const key of resolvedModeAddProperties) {
|
||||||
// @ts-ignore
|
|
||||||
mailBaseData[key] = messageData[key];
|
mailBaseData[key] = messageData[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
responseData = Object.assign(mailBaseData, responseData);
|
const json = Object.assign({}, mailBaseData, responseData, {
|
||||||
|
headers,
|
||||||
|
headerLines: undefined,
|
||||||
|
attachments: undefined,
|
||||||
|
}) as IDataObject;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
json: responseData as unknown as IDataObject,
|
json,
|
||||||
binary: Object.keys(binaryData).length ? binaryData : undefined,
|
binary: Object.keys(binaryData).length ? binaryData : undefined,
|
||||||
} as INodeExecutionData;
|
} as INodeExecutionData;
|
||||||
}
|
}
|
||||||
@@ -390,6 +385,14 @@ export function prepareQuery(
|
|||||||
let timestamp = DateTime.fromISO(qs.receivedAfter as string).toSeconds();
|
let timestamp = DateTime.fromISO(qs.receivedAfter as string).toSeconds();
|
||||||
const timestampLengthInMilliseconds1990 = 12;
|
const timestampLengthInMilliseconds1990 = 12;
|
||||||
|
|
||||||
|
if (
|
||||||
|
!timestamp &&
|
||||||
|
typeof qs.receivedAfter === 'number' &&
|
||||||
|
qs.receivedAfter.toString().length < timestampLengthInMilliseconds1990
|
||||||
|
) {
|
||||||
|
timestamp = qs.receivedAfter;
|
||||||
|
}
|
||||||
|
|
||||||
if (!timestamp && (qs.receivedAfter as string).length < timestampLengthInMilliseconds1990) {
|
if (!timestamp && (qs.receivedAfter as string).length < timestampLengthInMilliseconds1990) {
|
||||||
timestamp = parseInt(qs.receivedAfter as string, 10);
|
timestamp = parseInt(qs.receivedAfter as string, 10);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -194,8 +194,8 @@ export class GmailTrigger implements INodeType {
|
|||||||
let responseData;
|
let responseData;
|
||||||
|
|
||||||
const now = Math.floor(DateTime.now().toSeconds()) + '';
|
const now = Math.floor(DateTime.now().toSeconds()) + '';
|
||||||
const startDate = (webhookData.lastTimeChecked as string) || now;
|
const startDate = (webhookData.lastTimeChecked as string) || +now;
|
||||||
const endDate = now;
|
const endDate = +now;
|
||||||
|
|
||||||
const options = this.getNodeParameter('options', {}) as IDataObject;
|
const options = this.getNodeParameter('options', {}) as IDataObject;
|
||||||
const filters = this.getNodeParameter('filters', {}) as IDataObject;
|
const filters = this.getNodeParameter('filters', {}) as IDataObject;
|
||||||
@@ -273,7 +273,38 @@ export class GmailTrigger implements INodeType {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
webhookData.lastTimeChecked = endDate;
|
const getEmailDateAsSeconds = (email: IDataObject) => {
|
||||||
|
const { internalDate, date } = email;
|
||||||
|
return internalDate
|
||||||
|
? +(internalDate as string) / 1000
|
||||||
|
: +DateTime.fromJSDate(new Date(date as string)).toSeconds();
|
||||||
|
};
|
||||||
|
|
||||||
|
const lastEmailDate = (responseData as IDataObject[]).reduce((lastDate, { json }) => {
|
||||||
|
const emailDate = getEmailDateAsSeconds(json as IDataObject);
|
||||||
|
return emailDate > lastDate ? emailDate : lastDate;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
const nextPollPossibleDuplicates = (responseData as IDataObject[]).reduce(
|
||||||
|
(duplicates, { json }) => {
|
||||||
|
const emailDate = getEmailDateAsSeconds(json as IDataObject);
|
||||||
|
return emailDate === lastEmailDate
|
||||||
|
? duplicates.concat((json as IDataObject).id as string)
|
||||||
|
: duplicates;
|
||||||
|
},
|
||||||
|
[] as string[],
|
||||||
|
);
|
||||||
|
|
||||||
|
const possibleDuplicates = (webhookData.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;
|
||||||
|
|
||||||
if (Array.isArray(responseData) && responseData.length) {
|
if (Array.isArray(responseData) && responseData.length) {
|
||||||
return [responseData as INodeExecutionData[]];
|
return [responseData as INodeExecutionData[]];
|
||||||
|
|||||||
Reference in New Issue
Block a user