n8n/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

231 lines
8.1 KiB
TypeScript
Raw Normal View History

feat(Date & Time Node): Overhaul of the node (#5904) * Setup versionized node * Fix node naming * Set all possible actions * Add Current Date operation * Add timezone to current date * feat add to date operator * Change output field name to camel case * Fix info box for luxons tip * Feat subtract to date operation * Feat format date operation * Fix to node field for format date * Feat rounding operation * Feat get in between date operation * Feat add extract date operation * Add generic function for parsing date * Remove moment methods from operations * Change moment to luxon for the rest of the operations * Fix Format date operation * Fix format value * Add timezone option for current date * Add tests, improve workflow settings for testing, toString the results * Change icon for V2 * Revert "Change icon for V2" This reverts commit 46b59bea2ec6dd02a22f8d07a9736b42d751d10f. * Change workflow test name * Fix ui bug for custom format * Fix default value for format operation * Fix info box for rounding operation * Change default for units for between time operation * Inprove fields and resort time units * Fix extract week number * Resolve issue with formating and timezones * Fix field name and unit order * :zap: restored removed test case, sync v1 with curent master * :zap: parseDate update to support timestamps, tests * Keep same field for substract and add time * Update unit test * Improve visibility, add iso to string option * Update option naming --------- Co-authored-by: Michael Kret <michael.k@radency.com>
2023-05-08 08:34:14 -07:00
import type {
IExecuteFunctions,
INodeExecutionData,
INodeType,
INodeTypeBaseDescription,
INodeTypeDescription,
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import type { DateTimeUnit, DurationUnit } from 'luxon';
import { DateTime } from 'luxon';
import { CurrentDateDescription } from './CurrentDateDescription';
import { AddToDateDescription } from './AddToDateDescription';
import { SubtractFromDateDescription } from './SubtractFromDateDescription';
import { FormatDateDescription } from './FormatDateDescription';
import { RoundDateDescription } from './RoundDateDescription';
import { GetTimeBetweenDatesDescription } from './GetTimeBetweenDates';
import { ExtractDateDescription } from './ExtractDateDescription';
import { parseDate } from './GenericFunctions';
export class DateTimeV2 implements INodeType {
description: INodeTypeDescription;
constructor(baseDescription: INodeTypeBaseDescription) {
this.description = {
...baseDescription,
version: 2,
defaults: {
name: 'Date & Time',
color: '#408000',
},
inputs: ['main'],
outputs: ['main'],
description: 'Manipulate date and time values',
feat(Date & Time Node): Overhaul of the node (#5904) * Setup versionized node * Fix node naming * Set all possible actions * Add Current Date operation * Add timezone to current date * feat add to date operator * Change output field name to camel case * Fix info box for luxons tip * Feat subtract to date operation * Feat format date operation * Fix to node field for format date * Feat rounding operation * Feat get in between date operation * Feat add extract date operation * Add generic function for parsing date * Remove moment methods from operations * Change moment to luxon for the rest of the operations * Fix Format date operation * Fix format value * Add timezone option for current date * Add tests, improve workflow settings for testing, toString the results * Change icon for V2 * Revert "Change icon for V2" This reverts commit 46b59bea2ec6dd02a22f8d07a9736b42d751d10f. * Change workflow test name * Fix ui bug for custom format * Fix default value for format operation * Fix info box for rounding operation * Change default for units for between time operation * Inprove fields and resort time units * Fix extract week number * Resolve issue with formating and timezones * Fix field name and unit order * :zap: restored removed test case, sync v1 with curent master * :zap: parseDate update to support timestamps, tests * Keep same field for substract and add time * Update unit test * Improve visibility, add iso to string option * Update option naming --------- Co-authored-by: Michael Kret <michael.k@radency.com>
2023-05-08 08:34:14 -07:00
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Add to a Date',
value: 'addToDate',
},
{
name: 'Extract Part of a Date',
value: 'extractDate',
},
{
name: 'Format a Date',
value: 'formatDate',
},
{
name: 'Get Current Date',
value: 'getCurrentDate',
},
{
name: 'Get Time Between Dates',
value: 'getTimeBetweenDates',
},
{
name: 'Round a Date',
value: 'roundDate',
},
{
name: 'Subtract From a Date',
value: 'subtractFromDate',
},
],
default: 'getCurrentDate',
},
...CurrentDateDescription,
...AddToDateDescription,
...SubtractFromDateDescription,
...FormatDateDescription,
...RoundDateDescription,
...GetTimeBetweenDatesDescription,
...ExtractDateDescription,
],
};
}
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
const operation = this.getNodeParameter('operation', 0);
const workflowTimezone = this.getTimezone();
const includeInputFields = this.getNodeParameter(
'options.includeInputFields',
0,
false,
) as boolean;
const copyShallow = (item: INodeExecutionData) => ({
json: { ...item.json },
binary: item.binary,
});
feat(Date & Time Node): Overhaul of the node (#5904) * Setup versionized node * Fix node naming * Set all possible actions * Add Current Date operation * Add timezone to current date * feat add to date operator * Change output field name to camel case * Fix info box for luxons tip * Feat subtract to date operation * Feat format date operation * Fix to node field for format date * Feat rounding operation * Feat get in between date operation * Feat add extract date operation * Add generic function for parsing date * Remove moment methods from operations * Change moment to luxon for the rest of the operations * Fix Format date operation * Fix format value * Add timezone option for current date * Add tests, improve workflow settings for testing, toString the results * Change icon for V2 * Revert "Change icon for V2" This reverts commit 46b59bea2ec6dd02a22f8d07a9736b42d751d10f. * Change workflow test name * Fix ui bug for custom format * Fix default value for format operation * Fix info box for rounding operation * Change default for units for between time operation * Inprove fields and resort time units * Fix extract week number * Resolve issue with formating and timezones * Fix field name and unit order * :zap: restored removed test case, sync v1 with curent master * :zap: parseDate update to support timestamps, tests * Keep same field for substract and add time * Update unit test * Improve visibility, add iso to string option * Update option naming --------- Co-authored-by: Michael Kret <michael.k@radency.com>
2023-05-08 08:34:14 -07:00
for (let i = 0; i < items.length; i++) {
try {
const item: INodeExecutionData = includeInputFields ? copyShallow(items[i]) : { json: {} };
item.pairedItem = {
item: i,
feat(Date & Time Node): Overhaul of the node (#5904) * Setup versionized node * Fix node naming * Set all possible actions * Add Current Date operation * Add timezone to current date * feat add to date operator * Change output field name to camel case * Fix info box for luxons tip * Feat subtract to date operation * Feat format date operation * Fix to node field for format date * Feat rounding operation * Feat get in between date operation * Feat add extract date operation * Add generic function for parsing date * Remove moment methods from operations * Change moment to luxon for the rest of the operations * Fix Format date operation * Fix format value * Add timezone option for current date * Add tests, improve workflow settings for testing, toString the results * Change icon for V2 * Revert "Change icon for V2" This reverts commit 46b59bea2ec6dd02a22f8d07a9736b42d751d10f. * Change workflow test name * Fix ui bug for custom format * Fix default value for format operation * Fix info box for rounding operation * Change default for units for between time operation * Inprove fields and resort time units * Fix extract week number * Resolve issue with formating and timezones * Fix field name and unit order * :zap: restored removed test case, sync v1 with curent master * :zap: parseDate update to support timestamps, tests * Keep same field for substract and add time * Update unit test * Improve visibility, add iso to string option * Update option naming --------- Co-authored-by: Michael Kret <michael.k@radency.com>
2023-05-08 08:34:14 -07:00
};
if (operation === 'getCurrentDate') {
const includeTime = this.getNodeParameter('includeTime', i) as boolean;
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
const { timezone } = this.getNodeParameter('options', i) as {
timezone: string;
};
const newLocal = timezone ? timezone : workflowTimezone;
if (DateTime.now().setZone(newLocal).invalidReason === 'unsupported zone') {
throw new NodeOperationError(
this.getNode(),
`The timezone ${newLocal} is not valid. Please check the timezone.`,
);
}
if (includeTime) {
item.json[outputFieldName] = DateTime.now().setZone(newLocal).toString();
} else {
item.json[outputFieldName] = DateTime.now().setZone(newLocal).startOf('day').toString();
}
returnData.push(item);
} else if (operation === 'addToDate') {
const addToDate = this.getNodeParameter('magnitude', i) as string;
const timeUnit = this.getNodeParameter('timeUnit', i) as string;
const duration = this.getNodeParameter('duration', i) as number;
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
const dateToAdd = parseDate.call(this, addToDate, { timezone: workflowTimezone });
const returnedDate = dateToAdd.plus({ [timeUnit]: duration });
item.json[outputFieldName] = returnedDate.toString();
returnData.push(item);
} else if (operation === 'subtractFromDate') {
const subtractFromDate = this.getNodeParameter('magnitude', i) as string;
const timeUnit = this.getNodeParameter('timeUnit', i) as string;
const duration = this.getNodeParameter('duration', i) as number;
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
const dateToAdd = parseDate.call(this, subtractFromDate, { timezone: workflowTimezone });
const returnedDate = dateToAdd.minus({ [timeUnit]: duration });
item.json[outputFieldName] = returnedDate.toString();
returnData.push(item);
} else if (operation === 'formatDate') {
const date = this.getNodeParameter('date', i) as string;
const format = this.getNodeParameter('format', i) as string;
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
const { timezone, fromFormat } = this.getNodeParameter('options', i) as {
timezone: boolean;
fromFormat: string;
};
if (date === null || date === undefined) {
item.json[outputFieldName] = date;
} else {
const dateLuxon = parseDate.call(this, date, {
timezone: timezone ? workflowTimezone : undefined,
fromFormat,
});
if (format === 'custom') {
const customFormat = this.getNodeParameter('customFormat', i) as string;
item.json[outputFieldName] = dateLuxon.toFormat(customFormat);
} else {
item.json[outputFieldName] = dateLuxon.toFormat(format);
}
}
returnData.push(item);
} 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;
const dateLuxon = parseDate.call(this, date, { timezone: workflowTimezone });
if (mode === 'roundDown') {
const toNearest = this.getNodeParameter('toNearest', i) as string;
item.json[outputFieldName] = dateLuxon.startOf(toNearest as DateTimeUnit).toString();
} else if (mode === 'roundUp') {
const to = this.getNodeParameter('to', i) as string;
item.json[outputFieldName] = dateLuxon
feat(Date & Time Node): Overhaul of the node (#5904) * Setup versionized node * Fix node naming * Set all possible actions * Add Current Date operation * Add timezone to current date * feat add to date operator * Change output field name to camel case * Fix info box for luxons tip * Feat subtract to date operation * Feat format date operation * Fix to node field for format date * Feat rounding operation * Feat get in between date operation * Feat add extract date operation * Add generic function for parsing date * Remove moment methods from operations * Change moment to luxon for the rest of the operations * Fix Format date operation * Fix format value * Add timezone option for current date * Add tests, improve workflow settings for testing, toString the results * Change icon for V2 * Revert "Change icon for V2" This reverts commit 46b59bea2ec6dd02a22f8d07a9736b42d751d10f. * Change workflow test name * Fix ui bug for custom format * Fix default value for format operation * Fix info box for rounding operation * Change default for units for between time operation * Inprove fields and resort time units * Fix extract week number * Resolve issue with formating and timezones * Fix field name and unit order * :zap: restored removed test case, sync v1 with curent master * :zap: parseDate update to support timestamps, tests * Keep same field for substract and add time * Update unit test * Improve visibility, add iso to string option * Update option naming --------- Co-authored-by: Michael Kret <michael.k@radency.com>
2023-05-08 08:34:14 -07:00
.plus({ [to]: 1 })
.startOf(to as DateTimeUnit)
.toString();
}
returnData.push(item);
} else if (operation === 'getTimeBetweenDates') {
const startDate = this.getNodeParameter('startDate', i) as string;
const endDate = this.getNodeParameter('endDate', i) as string;
const unit = this.getNodeParameter('units', i) as DurationUnit[];
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
const { isoString } = this.getNodeParameter('options', i) as {
isoString: boolean;
};
feat(Date & Time Node): Overhaul of the node (#5904) * Setup versionized node * Fix node naming * Set all possible actions * Add Current Date operation * Add timezone to current date * feat add to date operator * Change output field name to camel case * Fix info box for luxons tip * Feat subtract to date operation * Feat format date operation * Fix to node field for format date * Feat rounding operation * Feat get in between date operation * Feat add extract date operation * Add generic function for parsing date * Remove moment methods from operations * Change moment to luxon for the rest of the operations * Fix Format date operation * Fix format value * Add timezone option for current date * Add tests, improve workflow settings for testing, toString the results * Change icon for V2 * Revert "Change icon for V2" This reverts commit 46b59bea2ec6dd02a22f8d07a9736b42d751d10f. * Change workflow test name * Fix ui bug for custom format * Fix default value for format operation * Fix info box for rounding operation * Change default for units for between time operation * Inprove fields and resort time units * Fix extract week number * Resolve issue with formating and timezones * Fix field name and unit order * :zap: restored removed test case, sync v1 with curent master * :zap: parseDate update to support timestamps, tests * Keep same field for substract and add time * Update unit test * Improve visibility, add iso to string option * Update option naming --------- Co-authored-by: Michael Kret <michael.k@radency.com>
2023-05-08 08:34:14 -07:00
const luxonStartDate = parseDate.call(this, startDate, { timezone: workflowTimezone });
const luxonEndDate = parseDate.call(this, endDate, { timezone: workflowTimezone });
const duration = luxonEndDate.diff(luxonStartDate, unit);
if (isoString) {
item.json[outputFieldName] = duration.toString();
} else {
item.json[outputFieldName] = duration.toObject();
}
returnData.push(item);
} else if (operation === 'extractDate') {
const date = this.getNodeParameter('date', i) as string | DateTime;
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
const part = this.getNodeParameter('part', i) as keyof DateTime | 'week';
feat(Date & Time Node): Overhaul of the node (#5904) * Setup versionized node * Fix node naming * Set all possible actions * Add Current Date operation * Add timezone to current date * feat add to date operator * Change output field name to camel case * Fix info box for luxons tip * Feat subtract to date operation * Feat format date operation * Fix to node field for format date * Feat rounding operation * Feat get in between date operation * Feat add extract date operation * Add generic function for parsing date * Remove moment methods from operations * Change moment to luxon for the rest of the operations * Fix Format date operation * Fix format value * Add timezone option for current date * Add tests, improve workflow settings for testing, toString the results * Change icon for V2 * Revert "Change icon for V2" This reverts commit 46b59bea2ec6dd02a22f8d07a9736b42d751d10f. * Change workflow test name * Fix ui bug for custom format * Fix default value for format operation * Fix info box for rounding operation * Change default for units for between time operation * Inprove fields and resort time units * Fix extract week number * Resolve issue with formating and timezones * Fix field name and unit order * :zap: restored removed test case, sync v1 with curent master * :zap: parseDate update to support timestamps, tests * Keep same field for substract and add time * Update unit test * Improve visibility, add iso to string option * Update option naming --------- Co-authored-by: Michael Kret <michael.k@radency.com>
2023-05-08 08:34:14 -07:00
const parsedDate = parseDate.call(this, date, { timezone: workflowTimezone });
const selectedPart = part === 'week' ? parsedDate.weekNumber : parsedDate.get(part);
item.json[outputFieldName] = selectedPart;
returnData.push(item);
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { error: error.message } });
continue;
}
throw new NodeOperationError(this.getNode(), error, { itemIndex: i });
}
feat(Date & Time Node): Overhaul of the node (#5904) * Setup versionized node * Fix node naming * Set all possible actions * Add Current Date operation * Add timezone to current date * feat add to date operator * Change output field name to camel case * Fix info box for luxons tip * Feat subtract to date operation * Feat format date operation * Fix to node field for format date * Feat rounding operation * Feat get in between date operation * Feat add extract date operation * Add generic function for parsing date * Remove moment methods from operations * Change moment to luxon for the rest of the operations * Fix Format date operation * Fix format value * Add timezone option for current date * Add tests, improve workflow settings for testing, toString the results * Change icon for V2 * Revert "Change icon for V2" This reverts commit 46b59bea2ec6dd02a22f8d07a9736b42d751d10f. * Change workflow test name * Fix ui bug for custom format * Fix default value for format operation * Fix info box for rounding operation * Change default for units for between time operation * Inprove fields and resort time units * Fix extract week number * Resolve issue with formating and timezones * Fix field name and unit order * :zap: restored removed test case, sync v1 with curent master * :zap: parseDate update to support timestamps, tests * Keep same field for substract and add time * Update unit test * Improve visibility, add iso to string option * Update option naming --------- Co-authored-by: Michael Kret <michael.k@radency.com>
2023-05-08 08:34:14 -07:00
}
return [returnData];
feat(Date & Time Node): Overhaul of the node (#5904) * Setup versionized node * Fix node naming * Set all possible actions * Add Current Date operation * Add timezone to current date * feat add to date operator * Change output field name to camel case * Fix info box for luxons tip * Feat subtract to date operation * Feat format date operation * Fix to node field for format date * Feat rounding operation * Feat get in between date operation * Feat add extract date operation * Add generic function for parsing date * Remove moment methods from operations * Change moment to luxon for the rest of the operations * Fix Format date operation * Fix format value * Add timezone option for current date * Add tests, improve workflow settings for testing, toString the results * Change icon for V2 * Revert "Change icon for V2" This reverts commit 46b59bea2ec6dd02a22f8d07a9736b42d751d10f. * Change workflow test name * Fix ui bug for custom format * Fix default value for format operation * Fix info box for rounding operation * Change default for units for between time operation * Inprove fields and resort time units * Fix extract week number * Resolve issue with formating and timezones * Fix field name and unit order * :zap: restored removed test case, sync v1 with curent master * :zap: parseDate update to support timestamps, tests * Keep same field for substract and add time * Update unit test * Improve visibility, add iso to string option * Update option naming --------- Co-authored-by: Michael Kret <michael.k@radency.com>
2023-05-08 08:34:14 -07:00
}
}