@@ -23,7 +23,7 @@ export class GmailTrigger implements INodeType {
23
23
name : 'gmailTrigger' ,
24
24
icon : 'file:gmail.svg' ,
25
25
group : [ 'trigger' ] ,
26
- version : 1 ,
26
+ version : [ 1 , 1.1 ] ,
27
27
description :
28
28
'Fetches emails from Gmail and starts the workflow on specified polling intervals.' ,
29
29
subtitle : '={{"Gmail Trigger"}}' ,
@@ -226,11 +226,24 @@ export class GmailTrigger implements INodeType {
226
226
} ;
227
227
228
228
async poll ( this : IPollFunctions ) : Promise < INodeExecutionData [ ] [ ] | null > {
229
- const webhookData = this . getWorkflowStaticData ( 'node' ) ;
229
+ const workflowStaticData = this . getWorkflowStaticData ( 'node' ) ;
230
+ const node = this . getNode ( ) ;
231
+
232
+ let nodeStaticData = workflowStaticData ;
233
+ if ( node . typeVersion > 1 ) {
234
+ const nodeName = node . name ;
235
+ if ( workflowStaticData [ nodeName ] === undefined ) {
236
+ workflowStaticData [ nodeName ] = { } as IDataObject ;
237
+ nodeStaticData = workflowStaticData [ nodeName ] as IDataObject ;
238
+ } else {
239
+ nodeStaticData = workflowStaticData [ nodeName ] as IDataObject ;
240
+ }
241
+ }
242
+
230
243
let responseData ;
231
244
232
245
const now = Math . floor ( DateTime . now ( ) . toSeconds ( ) ) . toString ( ) ;
233
- const startDate = ( webhookData . lastTimeChecked as string ) || + now ;
246
+ const startDate = ( nodeStaticData . lastTimeChecked as string ) || + now ;
234
247
const endDate = + now ;
235
248
236
249
const options = this . getNodeParameter ( 'options' , { } ) as IDataObject ;
@@ -257,7 +270,7 @@ export class GmailTrigger implements INodeType {
257
270
responseData = responseData . messages ;
258
271
259
272
if ( ! responseData ?. length ) {
260
- webhookData . lastTimeChecked = endDate ;
273
+ nodeStaticData . lastTimeChecked = endDate ;
261
274
return null ;
262
275
}
263
276
@@ -297,11 +310,10 @@ export class GmailTrigger implements INodeType {
297
310
) ;
298
311
}
299
312
} catch ( error ) {
300
- if ( this . getMode ( ) === 'manual' || ! webhookData . lastTimeChecked ) {
313
+ if ( this . getMode ( ) === 'manual' || ! nodeStaticData . lastTimeChecked ) {
301
314
throw error ;
302
315
}
303
316
const workflow = this . getWorkflow ( ) ;
304
- const node = this . getNode ( ) ;
305
317
this . logger . error (
306
318
`There was a problem in '${ node . name } ' node in workflow '${ workflow . id } ': '${ error . description } '` ,
307
319
{
@@ -313,15 +325,31 @@ export class GmailTrigger implements INodeType {
313
325
}
314
326
315
327
if ( ! responseData ?. length ) {
316
- webhookData . lastTimeChecked = endDate ;
328
+ nodeStaticData . lastTimeChecked = endDate ;
317
329
return null ;
318
330
}
319
331
320
- const getEmailDateAsSeconds = ( email : IDataObject ) => {
321
- const { internalDate, date } = email ;
322
- return internalDate
323
- ? + ( internalDate as string ) / 1000
324
- : + DateTime . fromJSDate ( new Date ( date as string ) ) . toSeconds ( ) ;
332
+ const emailsWithInvalidDate = new Set < string > ( ) ;
333
+
334
+ const getEmailDateAsSeconds = ( email : IDataObject ) : number => {
335
+ let date ;
336
+
337
+ if ( email . internalDate ) {
338
+ date = + ( email . internalDate as string ) / 1000 ;
339
+ } else if ( email . date ) {
340
+ date = + DateTime . fromJSDate ( new Date ( email . date as string ) ) . toSeconds ( ) ;
341
+ } else {
342
+ date = + DateTime . fromJSDate (
343
+ new Date ( ( email ?. headers as IDataObject ) ?. date as string ) ,
344
+ ) . toSeconds ( ) ;
345
+ }
346
+
347
+ if ( ! date || isNaN ( date ) ) {
348
+ emailsWithInvalidDate . add ( email . id as string ) ;
349
+ return + startDate ;
350
+ }
351
+
352
+ return date ;
325
353
} ;
326
354
327
355
const lastEmailDate = ( responseData as IDataObject [ ] ) . reduce ( ( lastDate , { json } ) => {
@@ -336,19 +364,19 @@ export class GmailTrigger implements INodeType {
336
364
? duplicates . concat ( ( json as IDataObject ) . id as string )
337
365
: duplicates ;
338
366
} ,
339
- [ ] as string [ ] ,
367
+ Array . from ( emailsWithInvalidDate ) ,
340
368
) ;
341
369
342
- const possibleDuplicates = ( webhookData . possibleDuplicates as string [ ] ) || [ ] ;
370
+ const possibleDuplicates = ( nodeStaticData . possibleDuplicates as string [ ] ) || [ ] ;
343
371
if ( possibleDuplicates . length ) {
344
372
responseData = ( responseData as IDataObject [ ] ) . filter ( ( { json } ) => {
345
373
const { id } = json as IDataObject ;
346
374
return ! possibleDuplicates . includes ( id as string ) ;
347
375
} ) ;
348
376
}
349
377
350
- webhookData . possibleDuplicates = nextPollPossibleDuplicates ;
351
- webhookData . lastTimeChecked = lastEmailDate || endDate ;
378
+ nodeStaticData . possibleDuplicates = nextPollPossibleDuplicates ;
379
+ nodeStaticData . lastTimeChecked = lastEmailDate || endDate ;
352
380
353
381
if ( Array . isArray ( responseData ) && responseData . length ) {
354
382
return [ responseData as INodeExecutionData [ ] ] ;
0 commit comments