mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 02:21:13 +00:00
feat(Date & Time Node): Option to include other fields in output item (#7661)
Github issue / Community forum post (link here to close automatically): Community: https://community.n8n.io/t/date-time-deletes-incoming-props/27492/3 GH Issue: https://github.com/n8n-io/n8n/issues/7646 --------- Co-authored-by: Marcus <marcus@n8n.io>
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
@@ -84,134 +83,142 @@ export class DateTimeV2 implements INodeType {
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const returnData: INodeExecutionData[] = [];
|
||||
const responseData = [];
|
||||
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,
|
||||
});
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
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;
|
||||
try {
|
||||
const item: INodeExecutionData = includeInputFields ? copyShallow(items[i]) : { json: {} };
|
||||
item.pairedItem = {
|
||||
item: i,
|
||||
};
|
||||
|
||||
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.`,
|
||||
);
|
||||
}
|
||||
responseData.push(
|
||||
includeTime
|
||||
? { [outputFieldName]: DateTime.now().setZone(newLocal).toString() }
|
||||
: {
|
||||
[outputFieldName]: DateTime.now().setZone(newLocal).startOf('day').toString(),
|
||||
},
|
||||
);
|
||||
} 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;
|
||||
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 dateToAdd = parseDate.call(this, addToDate, workflowTimezone);
|
||||
const returnedDate = dateToAdd.plus({ [timeUnit]: duration });
|
||||
responseData.push({ [outputFieldName]: returnedDate.toString() });
|
||||
} 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, workflowTimezone);
|
||||
const returnedDate = dateToAdd.minus({ [timeUnit]: duration });
|
||||
responseData.push({ [outputFieldName]: returnedDate.toString() });
|
||||
} 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 } = this.getNodeParameter('options', i) as { timezone: boolean };
|
||||
|
||||
if (date === null || date === undefined) {
|
||||
responseData.push({
|
||||
[outputFieldName]: date,
|
||||
});
|
||||
} else {
|
||||
const dateLuxon = timezone
|
||||
? parseDate.call(this, date, workflowTimezone)
|
||||
: parseDate.call(this, date);
|
||||
if (format === 'custom') {
|
||||
const customFormat = this.getNodeParameter('customFormat', i) as string;
|
||||
responseData.push({
|
||||
[outputFieldName]: dateLuxon.toFormat(customFormat),
|
||||
});
|
||||
} else {
|
||||
responseData.push({
|
||||
[outputFieldName]: dateLuxon.toFormat(format),
|
||||
});
|
||||
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.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
} 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, workflowTimezone);
|
||||
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;
|
||||
|
||||
if (mode === 'roundDown') {
|
||||
const toNearest = this.getNodeParameter('toNearest', i) as string;
|
||||
responseData.push({
|
||||
[outputFieldName]: dateLuxon.startOf(toNearest as DateTimeUnit).toString(),
|
||||
});
|
||||
} else if (mode === 'roundUp') {
|
||||
const to = this.getNodeParameter('to', i) as string;
|
||||
responseData.push({
|
||||
[outputFieldName]: dateLuxon
|
||||
const dateToAdd = parseDate.call(this, addToDate, 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, 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 } = this.getNodeParameter('options', i) as { timezone: boolean };
|
||||
|
||||
if (date === null || date === undefined) {
|
||||
item.json[outputFieldName] = date;
|
||||
} else {
|
||||
const dateLuxon = timezone
|
||||
? parseDate.call(this, date, workflowTimezone)
|
||||
: parseDate.call(this, date);
|
||||
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, 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
|
||||
.plus({ [to]: 1 })
|
||||
.startOf(to as DateTimeUnit)
|
||||
.toString(),
|
||||
});
|
||||
.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;
|
||||
};
|
||||
|
||||
const luxonStartDate = parseDate.call(this, startDate, workflowTimezone);
|
||||
const luxonEndDate = parseDate.call(this, endDate, 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';
|
||||
|
||||
const parsedDate = parseDate.call(this, date, workflowTimezone);
|
||||
const selectedPart = part === 'week' ? parsedDate.weekNumber : parsedDate.get(part);
|
||||
item.json[outputFieldName] = selectedPart;
|
||||
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;
|
||||
};
|
||||
|
||||
const luxonStartDate = parseDate.call(this, startDate, workflowTimezone);
|
||||
const luxonEndDate = parseDate.call(this, endDate, workflowTimezone);
|
||||
const duration = luxonEndDate.diff(luxonStartDate, unit);
|
||||
isoString
|
||||
? responseData.push({
|
||||
[outputFieldName]: duration.toString(),
|
||||
})
|
||||
: responseData.push({
|
||||
[outputFieldName]: duration.toObject(),
|
||||
});
|
||||
} 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';
|
||||
|
||||
const parsedDate = parseDate.call(this, date, workflowTimezone);
|
||||
const selectedPart = part === 'week' ? parsedDate.weekNumber : parsedDate.get(part);
|
||||
responseData.push({ [outputFieldName]: selectedPart });
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({ json: { error: error.message } });
|
||||
continue;
|
||||
}
|
||||
throw new NodeOperationError(this.getNode(), error, { itemIndex: i });
|
||||
}
|
||||
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
||||
{
|
||||
itemData: { item: i },
|
||||
},
|
||||
);
|
||||
returnData.push(...executionData);
|
||||
// Reset responseData
|
||||
responseData.length = 0;
|
||||
}
|
||||
return [returnData];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user