Skip to content

Commit

Permalink
fix(Gmail Node): Gmail luxon object support, fix for timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency authored and netroy committed Apr 4, 2023
1 parent f0a51a0 commit 695fabb
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 61 deletions.
128 changes: 70 additions & 58 deletions packages/nodes-base/nodes/Google/Gmail/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
IExecuteFunctions,
IExecuteSingleFunctions,
ILoadOptionsFunctions,
INode,
INodeExecutionData,
IPollFunctions,
JsonObject,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(),
);
}

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}`;
}
qs.q = prepareTimestamp(
this.getNode(),
itemIndex,
qs.q as string,
qs.receivedAfter as string,
'after',
);
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(),
);
}

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}`;
}
qs.q = prepareTimestamp(
this.getNode(),
itemIndex,
qs.q as string,
qs.receivedBefore as string,
'before',
);
delete qs.receivedBefore;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
111 changes: 111 additions & 0 deletions packages/nodes-base/nodes/Google/Gmail/test/v2/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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",
);
});
});
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/Google/Gmail/v2/GmailV2.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 695fabb

Please sign in to comment.