Skip to content

Commit

Permalink
fix(YouTube Node): Issue in published before and after dates filters (n…
Browse files Browse the repository at this point in the history
  • Loading branch information
ShireenMissi authored Nov 14, 2024
1 parent d9259a2 commit 7381c28
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 26 deletions.
33 changes: 26 additions & 7 deletions packages/nodes-base/nodes/Google/GenericFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import type {
IExecuteFunctions,
ILoadOptionsFunctions,
ICredentialTestFunctions,
IDataObject,
IPollFunctions,
IRequestOptions,
import {
type IExecuteFunctions,
type ILoadOptionsFunctions,
type ICredentialTestFunctions,
type IDataObject,
type IPollFunctions,
type IRequestOptions,
NodeOperationError,
} from 'n8n-workflow';

import moment from 'moment-timezone';
import * as jwt from 'jsonwebtoken';

import { formatPrivateKey } from '@utils/utilities';
import { DateTime } from 'luxon';

const googleServiceAccountScopes = {
bigquery: ['https://www.googleapis.com/auth/bigquery'],
Expand Down Expand Up @@ -110,3 +112,20 @@ export async function getGoogleAccessToken(

return await this.helpers.request(options);
}

export function validateAndSetDate(
filter: IDataObject,
key: string,
timezone: string,
context: IExecuteFunctions,
) {
const date = DateTime.fromISO(filter[key] as string);
if (date.isValid) {
filter[key] = date.setZone(timezone).toISO();
} else {
throw new NodeOperationError(
context.getNode(),
`The value "${filter[key] as string}" is not a valid DateTime.`,
);
}
}
24 changes: 5 additions & 19 deletions packages/nodes-base/nodes/Google/YouTube/YouTube.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type {
} from 'n8n-workflow';
import { NodeConnectionType, BINARY_ENCODING, NodeOperationError } from 'n8n-workflow';

import { DateTime } from 'luxon';
import { googleApiRequest, googleApiRequestAllItems } from './GenericFunctions';

import { channelFields, channelOperations } from './ChannelDescription';
Expand All @@ -24,6 +23,7 @@ import { videoFields, videoOperations } from './VideoDescription';
import { videoCategoryFields, videoCategoryOperations } from './VideoCategoryDescription';

import { isoCountryCodes } from '@utils/ISOCountryCodes';
import { validateAndSetDate } from '../GenericFunctions';

const UPLOAD_CHUNK_SIZE = 1024 * 1024;

Expand Down Expand Up @@ -763,27 +763,13 @@ export class YouTube implements INodeType {
qs.type = 'video';

qs.forMine = true;

if (filters.publishedAfter) {
const publishedAfter = DateTime.fromISO(filters.publishedAfter as string);
if (publishedAfter.isValid) {
filters.publishedAfter = publishedAfter.setZone(this.getTimezone()).toISO();
} else {
throw new NodeOperationError(
this.getNode(),
`The value "${filters.publishedAfter as string}" is not a valid DateTime.`,
);
}
validateAndSetDate(filters, 'publishedAfter', this.getTimezone(), this);
}

if (filters.publishedBefore) {
const publishedBefore = DateTime.fromISO(filters.publishedBefore as string);
if (publishedBefore.isValid) {
filters.publishedAfter = publishedBefore.setZone(this.getTimezone()).toISO();
} else {
throw new NodeOperationError(
this.getNode(),
`The value "${filters.publishedBefore as string}" is not a valid DateTime.`,
);
}
validateAndSetDate(filters, 'publishedBefore', this.getTimezone(), this);
}

Object.assign(qs, options, filters);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { DateTime } from 'luxon';
import { NodeOperationError, type IExecuteFunctions } from 'n8n-workflow';

import { validateAndSetDate } from '../../GenericFunctions';

const mockContext = {
getNode: jest.fn().mockReturnValue('Youtube'),
} as unknown as IExecuteFunctions;

describe('validateAndSetDate', () => {
const timezone = 'America/New_York';
let filter: { [key: string]: string };

beforeEach(() => {
filter = {};
});

it('should convert a valid ISO date and set it with the specified timezone', () => {
filter.publishedAfter = '2023-10-05T10:00:00.000Z';
validateAndSetDate(filter, 'publishedAfter', timezone, mockContext);

expect(filter.publishedAfter).toBe(
DateTime.fromISO('2023-10-05T10:00:00.000Z').setZone(timezone).toISO(),
);
});

it('should throw NodeOperationError for an invalid date', () => {
filter.publishedAfter = 'invalid-date';

expect(() => validateAndSetDate(filter, 'publishedAfter', timezone, mockContext)).toThrow(
NodeOperationError,
);

expect(() => validateAndSetDate(filter, 'publishedAfter', timezone, mockContext)).toThrow(
`The value "${filter.publishedAfter}" is not a valid DateTime.`,
);
});
});

0 comments on commit 7381c28

Please sign in to comment.