fix(Gmail Node): Gmail luxon object support, fix for timestamp

This commit is contained in:
Michael Kret 2023-04-03 12:48:11 +03:00 committed by कारतोफ्फेलस्क्रिप्ट™
parent f0a51a0b76
commit 695fabb284
4 changed files with 184 additions and 61 deletions

View file

@ -9,6 +9,7 @@ import type {
IExecuteFunctions,
IExecuteSingleFunctions,
ILoadOptionsFunctions,
INode,
INodeExecutionData,
IPollFunctions,
JsonObject,
@ -351,9 +352,64 @@ export function extractEmail(s: string) {
return s;
}
export const prepareTimestamp = (
node: INode,
itemIndex: number,
query: string,
dateValue: string | number | DateTime,
label: 'after' | 'before',
) => {
if (dateValue instanceof DateTime) {
dateValue = dateValue.toISO();
}
let timestamp = DateTime.fromISO(dateValue as string).toSeconds();
const timestampLengthInMilliseconds1990 = 12;
if (typeof timestamp === 'number') {
timestamp = Math.round(timestamp);
}
if (
!timestamp &&
typeof dateValue === 'number' &&
dateValue.toString().length < timestampLengthInMilliseconds1990
) {
timestamp = dateValue;
}
if (!timestamp && (dateValue as string).length < timestampLengthInMilliseconds1990) {
timestamp = parseInt(dateValue as string, 10);
}
if (!timestamp) {
timestamp = Math.floor(DateTime.fromMillis(parseInt(dateValue as string, 10)).toSeconds());
}
if (!timestamp) {
const description = `'${dateValue}' isn't a valid date and time. If you're using an expression, be sure to set an ISO date string or a timestamp.`;
throw new NodeOperationError(
node,
`Invalid date/time in 'Received ${label[0].toUpperCase() + label.slice(1)}' field`,
{
description,
itemIndex,
},
);
}
if (query) {
query += ` ${label}:${timestamp}`;
} else {
query = `${label}:${timestamp}`;
}
return query;
};
export function prepareQuery(
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IPollFunctions,
fields: IDataObject,
itemIndex: number,
) {
const qs: IDataObject = { ...fields };
if (qs.labelIds) {
@ -383,68 +439,24 @@ export function prepareQuery(
}
if (qs.receivedAfter) {
let timestamp = DateTime.fromISO(qs.receivedAfter as string).toSeconds();
const timestampLengthInMilliseconds1990 = 12;
if (
!timestamp &&
typeof qs.receivedAfter === 'number' &&
qs.receivedAfter.toString().length < timestampLengthInMilliseconds1990
) {
timestamp = qs.receivedAfter;
}
if (!timestamp && (qs.receivedAfter as string).length < timestampLengthInMilliseconds1990) {
timestamp = parseInt(qs.receivedAfter as string, 10);
}
if (!timestamp) {
timestamp = Math.floor(
DateTime.fromMillis(parseInt(qs.receivedAfter as string, 10)).toSeconds(),
qs.q = prepareTimestamp(
this.getNode(),
itemIndex,
qs.q as string,
qs.receivedAfter as string,
'after',
);
}
if (!timestamp) {
const description = `'${qs.receivedAfter}' isn't a valid date and time. If you're using an expression, be sure to set an ISO date string or a timestamp.`;
throw new NodeOperationError(this.getNode(), "Invalid date/time in 'Received After' field", {
description,
});
}
if (qs.q) {
qs.q += ` after:${timestamp}`;
} else {
qs.q = `after:${timestamp}`;
}
delete qs.receivedAfter;
}
if (qs.receivedBefore) {
let timestamp = DateTime.fromISO(qs.receivedBefore as string).toSeconds();
const timestampLengthInMilliseconds1990 = 12;
if (!timestamp && (qs.receivedBefore as string).length < timestampLengthInMilliseconds1990) {
timestamp = parseInt(qs.receivedBefore as string, 10);
}
if (!timestamp) {
timestamp = Math.floor(
DateTime.fromMillis(parseInt(qs.receivedBefore as string, 10)).toSeconds(),
qs.q = prepareTimestamp(
this.getNode(),
itemIndex,
qs.q as string,
qs.receivedBefore as string,
'before',
);
}
if (!timestamp) {
const description = `'${qs.receivedBefore}' isn't a valid date and time. If you're using an expression, be sure to set an ISO date string or a timestamp.`;
throw new NodeOperationError(this.getNode(), "Invalid date/time in 'Received Before' field", {
description,
});
}
if (qs.q) {
qs.q += ` before:${timestamp}`;
} else {
qs.q = `before:${timestamp}`;
}
delete qs.receivedBefore;
}

View file

@ -246,7 +246,7 @@ export class GmailTrigger implements INodeType {
delete filters.receivedAfter;
}
Object.assign(qs, prepareQuery.call(this, filters), options);
Object.assign(qs, prepareQuery.call(this, filters, 0), options);
responseData = await googleApiRequest.call(
this,

View file

@ -0,0 +1,111 @@
import type { INode } from 'n8n-workflow';
import { prepareTimestamp } from '../../GenericFunctions';
import { DateTime } from 'luxon';
const node: INode = {
id: '1',
name: 'Gmail node',
typeVersion: 2,
type: 'n8n-nodes-base.gmail',
position: [50, 50],
parameters: {
operation: 'getAll',
},
};
describe('Google Gmail v2, prepareTimestamp', () => {
it('should return a valid timestamp from ISO', () => {
const dateInput = '2020-01-01T00:00:00.000Z';
const timestampBefore = prepareTimestamp(node, 0, '', dateInput, 'before');
const timestampAfter = prepareTimestamp(node, 0, '', dateInput, 'after');
expect(timestampBefore).toBeDefined();
expect(timestampBefore).toBe('before:1577836800');
expect(timestampAfter).toBeDefined();
expect(timestampAfter).toBe('after:1577836800');
});
it('should return a valid timestamp from integer in miliseconds', () => {
const dateInput = 1577836800000;
const timestampBefore = prepareTimestamp(node, 0, '', dateInput, 'before');
const timestampAfter = prepareTimestamp(node, 0, '', dateInput, 'after');
expect(timestampBefore).toBeDefined();
expect(timestampBefore).toBe('before:1577836800');
expect(timestampAfter).toBeDefined();
expect(timestampAfter).toBe('after:1577836800');
});
it('should return a valid timestamp from integer in seconds', () => {
const dateInput = 1577836800;
const timestampBefore = prepareTimestamp(node, 0, '', dateInput, 'before');
const timestampAfter = prepareTimestamp(node, 0, '', dateInput, 'after');
expect(timestampBefore).toBeDefined();
expect(timestampBefore).toBe('before:1577836800');
expect(timestampAfter).toBeDefined();
expect(timestampAfter).toBe('after:1577836800');
});
it('should return a valid timestamp from string in miliseconds', () => {
const dateInput = '1577836800000';
const timestampBefore = prepareTimestamp(node, 0, '', dateInput, 'before');
const timestampAfter = prepareTimestamp(node, 0, '', dateInput, 'after');
expect(timestampBefore).toBeDefined();
expect(timestampBefore).toBe('before:1577836800');
expect(timestampAfter).toBeDefined();
expect(timestampAfter).toBe('after:1577836800');
});
it('should return a valid timestamp from string in seconds', () => {
const dateInput = '1577836800';
const timestampBefore = prepareTimestamp(node, 0, '', dateInput, 'before');
const timestampAfter = prepareTimestamp(node, 0, '', dateInput, 'after');
expect(timestampBefore).toBeDefined();
expect(timestampBefore).toBe('before:1577836800');
expect(timestampAfter).toBeDefined();
expect(timestampAfter).toBe('after:1577836800');
});
it('should return a valid timestamp from luxon DateTime', () => {
const dateInput = DateTime.fromISO('2020-01-01T00:00:00.000Z');
const timestampBefore = prepareTimestamp(node, 0, '', dateInput, 'before');
const timestampAfter = prepareTimestamp(node, 0, '', dateInput, 'after');
expect(timestampBefore).toBeDefined();
expect(timestampBefore).toBe('before:1577836800');
expect(timestampAfter).toBeDefined();
expect(timestampAfter).toBe('after:1577836800');
});
it('should return a valid timestamp from luxon DateTime ISO', () => {
const dateInput = DateTime.fromISO('2020-01-01T00:00:00.000Z').toISO();
const timestampBefore = prepareTimestamp(node, 0, '', dateInput, 'before');
const timestampAfter = prepareTimestamp(node, 0, '', dateInput, 'after');
expect(timestampBefore).toBeDefined();
expect(timestampBefore).toBe('before:1577836800');
expect(timestampAfter).toBeDefined();
expect(timestampAfter).toBe('after:1577836800');
});
it('should throw error on invalid data', () => {
const dateInput = 'invalid';
expect(() => prepareTimestamp(node, 0, '', dateInput, 'before')).toThrow(
"Invalid date/time in 'Received Before' field",
);
expect(() => prepareTimestamp(node, 0, '', dateInput, 'after')).toThrow(
"Invalid date/time in 'Received After' field",
);
});
});

View file

@ -382,7 +382,7 @@ export class GmailV2 implements INodeType {
const options = this.getNodeParameter('options', i, {});
const filters = this.getNodeParameter('filters', i, {});
const qs: IDataObject = {};
Object.assign(qs, prepareQuery.call(this, filters), options);
Object.assign(qs, prepareQuery.call(this, filters, i), options, i);
if (returnAll) {
responseData = await googleApiRequestAllItems.call(
@ -708,7 +708,7 @@ export class GmailV2 implements INodeType {
const returnAll = this.getNodeParameter('returnAll', i);
const filters = this.getNodeParameter('filters', i);
const qs: IDataObject = {};
Object.assign(qs, prepareQuery.call(this, filters));
Object.assign(qs, prepareQuery.call(this, filters, i));
if (returnAll) {
responseData = await googleApiRequestAllItems.call(