From a9987cd541617d7c514e004e01b32fa2cb8bf5b9 Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Fri, 13 Aug 2021 14:57:18 -0400 Subject: [PATCH] :zap: Add timezone support to date type field in Notion (#2082) * Add timezone in date property on Notion Signed-off-by: 5pecia1 * :zap: Improvements to #2036 * :zap: Minor improvements Co-authored-by: 5pecia1 Co-authored-by: Jan Oberhauser --- .../nodes/Notion/DatabasePageDescription.ts | 34 +++++++++++++++++++ .../nodes/Notion/GenericFunctions.ts | 15 ++++++-- .../nodes-base/nodes/Notion/Notion.node.ts | 24 +++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/packages/nodes-base/nodes/Notion/DatabasePageDescription.ts b/packages/nodes-base/nodes/Notion/DatabasePageDescription.ts index 9fd9a3901f..b81e010e81 100644 --- a/packages/nodes-base/nodes/Notion/DatabasePageDescription.ts +++ b/packages/nodes-base/nodes/Notion/DatabasePageDescription.ts @@ -405,6 +405,23 @@ export const databasePageFields = [ description: ` An ISO 8601 formatted date, with optional time. Represents the end of a date range.`, }, + { + displayName: 'Timezone', + name: 'timezone', + type: 'options', + displayOptions: { + show: { + type: [ + 'date', + ], + }, + }, + typeOptions: { + loadOptionsMethod: 'getTimezones', + }, + default: 'default', + description: 'Time zone to use. By default n8n timezone is used.', + }, ], }, ], @@ -765,6 +782,23 @@ export const databasePageFields = [ description: ` An ISO 8601 formatted date, with optional time. Represents the end of a date range.`, }, + { + displayName: 'Timezone', + name: 'timezone', + type: 'options', + displayOptions: { + show: { + type: [ + 'date', + ], + }, + }, + typeOptions: { + loadOptionsMethod: 'getTimezones', + }, + default: 'default', + description: 'Time zone to use. By default n8n timezone is used.', + }, ], }, ], diff --git a/packages/nodes-base/nodes/Notion/GenericFunctions.ts b/packages/nodes-base/nodes/Notion/GenericFunctions.ts index d36cc5b627..862aaa1152 100644 --- a/packages/nodes-base/nodes/Notion/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Notion/GenericFunctions.ts @@ -284,13 +284,22 @@ function getPropertyKeyValue(value: any, type: string, timezone: string) { break; case 'date': const format = getDateFormat(value.includeTime); + const timezoneValue = (value.timezone === 'default') ? timezone : value.timezone; if (value.range === true) { result = { - type: 'date', date: { start: moment.tz(value.dateStart, timezone).format(format), end: moment.tz(value.dateEnd, timezone).format(format) }, + type: 'date', + date: { + start: moment.tz(value.dateStart, timezoneValue).format(format), + end: moment.tz(value.dateEnd, timezoneValue).format(format), + }, }; } else { result = { - type: 'date', date: { start: moment.tz(value.date, timezone).format(format), end: null }, + type: 'date', + date: { + start: moment.tz(value.date, timezoneValue).format(format), + end: null, + }, }; } break; @@ -350,7 +359,7 @@ export function mapFilters(filters: IDataObject[], timezone: string) { } else if (key === 'date' && !['is_empty', 'is_not_empty'].includes(value.condition as string)) { valuePropertyName = (valuePropertyName !== undefined && !Object.keys(valuePropertyName).length) ? {} : moment.tz(value.date, timezone).utc().format(); } - + return Object.assign(obj, { ['property']: getNameAndType(value.key).name, [key]: { [`${value.condition}`]: valuePropertyName }, diff --git a/packages/nodes-base/nodes/Notion/Notion.node.ts b/packages/nodes-base/nodes/Notion/Notion.node.ts index 5ae2fe06c1..9dde404d70 100644 --- a/packages/nodes-base/nodes/Notion/Notion.node.ts +++ b/packages/nodes-base/nodes/Notion/Notion.node.ts @@ -48,6 +48,8 @@ import { databasePageOperations, } from './DatabasePageDescription'; +import * as moment from 'moment-timezone'; + export class Notion implements INodeType { description: INodeTypeDescription = { displayName: 'Notion (Beta)', @@ -266,6 +268,28 @@ export class Notion implements INodeType { const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`); return (properties[name][type].options).map((option: IDataObject) => ({ name: option.name, value: option.id })); }, + + // Get all the timezones to display them to user so that he can + // select them easily + async getTimezones( + this: ILoadOptionsFunctions, + ): Promise { + const returnData: INodePropertyOptions[] = []; + for (const timezone of moment.tz.names()) { + const timezoneName = timezone; + const timezoneId = timezone; + returnData.push({ + name: timezoneName, + value: timezoneId, + }); + } + returnData.unshift({ + name: 'Default', + value: 'default', + description: 'Timezone set in n8n', + }); + return returnData; + }, }, };