From d0038812d7ab6b18d64602c0c5275e5d16029b61 Mon Sep 17 00:00:00 2001 From: agobrech Date: Wed, 5 Apr 2023 17:25:27 +0200 Subject: [PATCH] Feat rounding operation --- .../nodes/DateTime/V2/DateTimeV2.node.ts | 24 ++++ .../DateTime/V2/FormatDateDescription.ts | 3 +- .../nodes/DateTime/V2/RoundDateDescription.ts | 121 ++++++++++++++++++ 3 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 packages/nodes-base/nodes/DateTime/V2/RoundDateDescription.ts diff --git a/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts b/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts index ff919520d8..6a2f13587e 100644 --- a/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts +++ b/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts @@ -15,6 +15,7 @@ import { CurrentDateDescription } from './CurrentDateDescription'; import { AddToDateDescription } from './AddToDateDescription'; import { SubtractFromDateDescription } from './SubtractFromDateDescription'; import { FormatDateDescription } from './FormatDateDescription'; +import { RoundDateDescription } from './RoundDateDescription'; export class DateTimeV2 implements INodeType { description: INodeTypeDescription; @@ -71,6 +72,7 @@ export class DateTimeV2 implements INodeType { ...AddToDateDescription, ...SubtractFromDateDescription, ...FormatDateDescription, + ...RoundDateDescription, ], }; } @@ -155,6 +157,28 @@ export class DateTimeV2 implements INodeType { } catch { throw new NodeOperationError(this.getNode(), 'Invalid date format'); } + } else if (operation === 'roundDate') { + const date = this.getNodeParameter('date', i) as string; + const mode = this.getNodeParameter('mode', i) as string; + const outputFieldName = this.getNodeParameter('outputFieldName', i) as string; + try { + const momentDate = moment.tz(date, workflowTimezone); + if (mode === 'roundDown') { + const toNearest = this.getNodeParameter('toNearest', i) as string; + responseData.push({ + [outputFieldName]: momentDate.startOf(toNearest as moment.unitOfTime.StartOf), + }); + } else if (mode === 'roundUp') { + const to = this.getNodeParameter('to', i) as string; + responseData.push({ + [outputFieldName]: momentDate + .add(1, to as moment.unitOfTime.DurationConstructor) + .startOf(to as moment.unitOfTime.StartOf), + }); + } + } catch { + throw new NodeOperationError(this.getNode(), 'Invalid date format'); + } } const executionData = this.helpers.constructExecutionMetaData( this.helpers.returnJsonArray(responseData as IDataObject[]), diff --git a/packages/nodes-base/nodes/DateTime/V2/FormatDateDescription.ts b/packages/nodes-base/nodes/DateTime/V2/FormatDateDescription.ts index d9d80ed2da..3971664491 100644 --- a/packages/nodes-base/nodes/DateTime/V2/FormatDateDescription.ts +++ b/packages/nodes-base/nodes/DateTime/V2/FormatDateDescription.ts @@ -89,7 +89,8 @@ export const FormatDateDescription: INodeProperties[] = [ }, }, hint: 'List of special tokens More info', - default: 'yyyy-MM-dd', + default: '', + placeholder: 'yyyy-MM-dd', }, { displayName: 'Output Field Name', diff --git a/packages/nodes-base/nodes/DateTime/V2/RoundDateDescription.ts b/packages/nodes-base/nodes/DateTime/V2/RoundDateDescription.ts new file mode 100644 index 0000000000..8aaaf33619 --- /dev/null +++ b/packages/nodes-base/nodes/DateTime/V2/RoundDateDescription.ts @@ -0,0 +1,121 @@ +import type { INodeProperties } from 'n8n-workflow'; + +export const RoundDateDescription: INodeProperties[] = [ + { + displayName: + "You can also do this using an expression, e.g. {{ your_date.endOfMonth() }}. More info", + name: 'notice', + type: 'notice', + default: '', + displayOptions: { + show: { + operation: ['roundDate'], + }, + }, + }, + { + displayName: 'Date', + name: 'date', + type: 'string', + description: 'The date that you want to round', + default: '', + displayOptions: { + show: { + operation: ['roundDate'], + }, + }, + }, + { + displayName: 'Mode', + name: 'mode', + type: 'options', + options: [ + { + name: 'Round Down', + value: 'roundDown', + }, + { + name: 'Round Up', + value: 'roundUp', + }, + ], + default: 'roundDown', + displayOptions: { + show: { + operation: ['roundDate'], + }, + }, + }, + { + displayName: 'To Nearest', + name: 'toNearest', + type: 'options', + options: [ + { + name: 'Day', + value: 'day', + }, + { + name: 'Hour', + value: 'hour', + }, + { + name: 'Minute', + value: 'minute', + }, + { + name: 'Month', + value: 'month', + }, + { + name: 'Second', + value: 'second', + }, + { + name: 'Week', + value: 'week', + }, + { + name: 'Year', + value: 'year', + }, + ], + default: 'month', + displayOptions: { + show: { + operation: ['roundDate'], + mode: ['roundDown'], + }, + }, + }, + { + displayName: 'To', + name: 'to', + type: 'options', + options: [ + { + name: 'End of Month', + value: 'month', + }, + ], + default: 'month', + displayOptions: { + show: { + operation: ['roundDate'], + mode: ['roundUp'], + }, + }, + }, + { + displayName: 'Output Field Name', + name: 'outputFieldName', + type: 'string', + default: 'roundedDate', + description: 'Name of the field to put the output in', + displayOptions: { + show: { + operation: ['roundDate'], + }, + }, + }, +];