fix(SeaTableTrigger Node): Fix timezone issues (#2726)

* SeaTable: Add timezone field to credentials, make trigger query timezone-aware

*  replaced string input with options for timezone, small fixes

*  small fixes

Co-authored-by: Michael Kret <michael.k@radency.com>
This commit is contained in:
Tom 2022-03-13 10:45:26 +01:00 committed by GitHub
parent 3c5df3f892
commit 2d8ac4b477
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 5 deletions

View file

@ -1,8 +1,17 @@
import { import {
ICredentialType, ICredentialType,
INodeProperties, INodeProperties,
INodePropertyOptions,
} from 'n8n-workflow'; } from 'n8n-workflow';
import * as moment from 'moment-timezone';
// Get options for timezones
const timezones: INodePropertyOptions[] = moment.tz.countries().reduce( (timezones: INodePropertyOptions[], country: string) => {
const zonesForCountry = moment.tz.zonesForCountry(country).map(zone => ({value: zone, name: zone}));
return timezones.concat(zonesForCountry);
}, []);
export class SeaTableApi implements ICredentialType { export class SeaTableApi implements ICredentialType {
name = 'seaTableApi'; name = 'seaTableApi';
displayName = 'SeaTable API'; displayName = 'SeaTable API';
@ -44,5 +53,15 @@ export class SeaTableApi implements ICredentialType {
type: 'string', type: 'string',
default: '', default: '',
}, },
{
displayName: 'Timezone',
name: 'timezone',
type: 'options',
default: 'Europe/Berlin',
description: `Seatable server's timezone`,
options: [
...timezones,
],
},
]; ];
} }

View file

@ -3,6 +3,7 @@ import {
} from 'n8n-core'; } from 'n8n-core';
import { import {
IDataObject,
ILoadOptionsFunctions, ILoadOptionsFunctions,
INodeExecutionData, INodeExecutionData,
INodePropertyOptions, INodePropertyOptions,
@ -108,13 +109,12 @@ export class SeaTableTrigger implements INodeType {
const simple = this.getNodeParameter('simple') as boolean; const simple = this.getNodeParameter('simple') as boolean;
const event = this.getNodeParameter('event') as string; const event = this.getNodeParameter('event') as string;
const ctx: ICtx = {}; const ctx: ICtx = {};
const credentials = await this.getCredentials('seaTableApi') as IDataObject;
const timezone = credentials.timezone as string || 'Europe/Berlin';
const now = moment().utc().format(); const now = moment().utc().format();
const startDate = webhookData.lastTimeChecked as string || now; const startDate = webhookData.lastTimeChecked as string || now;
const endDate = now; const endDate = now;
webhookData.lastTimeChecked = endDate; webhookData.lastTimeChecked = endDate;
let rows; let rows;
@ -126,8 +126,15 @@ export class SeaTableTrigger implements INodeType {
if (this.getMode() === 'manual') { if (this.getMode() === 'manual') {
rows = await seaTableApiRequest.call(this, ctx, 'POST', endpoint, { sql: `SELECT * FROM ${tableName} LIMIT 1` }) as IRowResponse; rows = await seaTableApiRequest.call(this, ctx, 'POST', endpoint, { sql: `SELECT * FROM ${tableName} LIMIT 1` }) as IRowResponse;
} else { } else {
rows = await seaTableApiRequest.call(this, ctx, 'POST', endpoint, rows = (await seaTableApiRequest.call(this, ctx, 'POST', endpoint, {
{ sql: `SELECT * FROM ${tableName} WHERE ${filterField} BETWEEN "${moment(startDate).utc().format('YYYY-MM-D HH:mm:ss')}" AND "${moment(endDate).utc().format('YYYY-MM-D HH:mm:ss')}"` }) as IRowResponse; sql: `SELECT * FROM ${tableName}
WHERE ${filterField} BETWEEN "${moment(startDate)
.tz(timezone)
.format('YYYY-MM-D HH:mm:ss')}"
AND "${moment(endDate)
.tz(timezone)
.format('YYYY-MM-D HH:mm:ss')}"`,
})) as IRowResponse;
} }
let response; let response;