Feat rounding operation

This commit is contained in:
agobrech 2023-04-05 17:25:27 +02:00
parent 7bbb0ba289
commit d0038812d7
3 changed files with 147 additions and 1 deletions

View file

@ -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[]),

View file

@ -89,7 +89,8 @@ export const FormatDateDescription: INodeProperties[] = [
},
},
hint: 'List of special tokens <a target="_blank" href="https://moment.github.io/luxon/#/formatting?id=table-of-tokens">More info</a>',
default: 'yyyy-MM-dd',
default: '',
placeholder: 'yyyy-MM-dd',
},
{
displayName: 'Output Field Name',

View file

@ -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. <code>{{ your_date.endOfMonth() }}</code>. <a target='_blank' href='https://docs.n8n.io/code-examples/expressions/luxon/'>More info</a>",
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'],
},
},
},
];