UNPKG

n8n-nodes-base

Version:

Base nodes of n8n

208 lines 10.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DateTimeV2 = void 0; const luxon_1 = require("luxon"); const n8n_workflow_1 = require("n8n-workflow"); const AddToDateDescription_1 = require("./AddToDateDescription"); const CurrentDateDescription_1 = require("./CurrentDateDescription"); const ExtractDateDescription_1 = require("./ExtractDateDescription"); const FormatDateDescription_1 = require("./FormatDateDescription"); const GenericFunctions_1 = require("./GenericFunctions"); const GetTimeBetweenDates_1 = require("./GetTimeBetweenDates"); const RoundDateDescription_1 = require("./RoundDateDescription"); const SubtractFromDateDescription_1 = require("./SubtractFromDateDescription"); class DateTimeV2 { description; constructor(baseDescription) { this.description = { ...baseDescription, version: 2, defaults: { name: 'Date & Time', color: '#408000', }, usableAsTool: true, inputs: [n8n_workflow_1.NodeConnectionTypes.Main], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], description: 'Manipulate date and time values', 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_1.CurrentDateDescription, ...AddToDateDescription_1.AddToDateDescription, ...SubtractFromDateDescription_1.SubtractFromDateDescription, ...FormatDateDescription_1.FormatDateDescription, ...RoundDateDescription_1.RoundDateDescription, ...GetTimeBetweenDates_1.GetTimeBetweenDatesDescription, ...ExtractDateDescription_1.ExtractDateDescription, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const operation = this.getNodeParameter('operation', 0); const workflowTimezone = this.getTimezone(); const includeInputFields = this.getNodeParameter('options.includeInputFields', 0, false); const copyShallow = (item) => ({ json: { ...item.json }, binary: item.binary, }); for (let i = 0; i < items.length; i++) { try { const item = includeInputFields ? copyShallow(items[i]) : { json: {} }; item.pairedItem = { item: i, }; if (operation === 'getCurrentDate') { const includeTime = this.getNodeParameter('includeTime', i); const outputFieldName = this.getNodeParameter('outputFieldName', i); const { timezone } = this.getNodeParameter('options', i); const newLocal = timezone ? timezone : workflowTimezone; if (luxon_1.DateTime.now().setZone(newLocal).invalidReason === 'unsupported zone') { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The timezone ${newLocal} is not valid. Please check the timezone.`); } if (includeTime) { item.json[outputFieldName] = luxon_1.DateTime.now().setZone(newLocal).toString(); } else { item.json[outputFieldName] = luxon_1.DateTime.now().setZone(newLocal).startOf('day').toString(); } returnData.push(item); } else if (operation === 'addToDate') { const addToDate = this.getNodeParameter('magnitude', i); const timeUnit = this.getNodeParameter('timeUnit', i); const duration = this.getNodeParameter('duration', i); const outputFieldName = this.getNodeParameter('outputFieldName', i); const dateToAdd = GenericFunctions_1.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); const timeUnit = this.getNodeParameter('timeUnit', i); const duration = this.getNodeParameter('duration', i); const outputFieldName = this.getNodeParameter('outputFieldName', i); const dateToAdd = GenericFunctions_1.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); const format = this.getNodeParameter('format', i); const outputFieldName = this.getNodeParameter('outputFieldName', i); const { timezone, fromFormat } = this.getNodeParameter('options', i); if (date === null || date === undefined) { item.json[outputFieldName] = date; } else { const dateLuxon = GenericFunctions_1.parseDate.call(this, date, { timezone: timezone ? workflowTimezone : undefined, fromFormat, }); if (format === 'custom') { const customFormat = this.getNodeParameter('customFormat', i); 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); const mode = this.getNodeParameter('mode', i); const outputFieldName = this.getNodeParameter('outputFieldName', i); const dateLuxon = GenericFunctions_1.parseDate.call(this, date, { timezone: workflowTimezone }); if (mode === 'roundDown') { const toNearest = this.getNodeParameter('toNearest', i); item.json[outputFieldName] = dateLuxon.startOf(toNearest).toString(); } else if (mode === 'roundUp') { const to = this.getNodeParameter('to', i); item.json[outputFieldName] = dateLuxon .plus({ [to]: 1 }) .startOf(to) .toString(); } returnData.push(item); } else if (operation === 'getTimeBetweenDates') { const startDate = this.getNodeParameter('startDate', i); const endDate = this.getNodeParameter('endDate', i); const unit = this.getNodeParameter('units', i); const outputFieldName = this.getNodeParameter('outputFieldName', i); const { isoString } = this.getNodeParameter('options', i); const luxonStartDate = GenericFunctions_1.parseDate.call(this, startDate, { timezone: workflowTimezone }); const luxonEndDate = GenericFunctions_1.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); const outputFieldName = this.getNodeParameter('outputFieldName', i); const part = this.getNodeParameter('part', i); const parsedDate = GenericFunctions_1.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 n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i }); } } return [returnData]; } } exports.DateTimeV2 = DateTimeV2; //# sourceMappingURL=DateTimeV2.node.js.map