1
1
mirror of https://github.com/n8n-io/n8n.git synced 2024-12-29 15:14:40 +03:00

fix(YouTube Node): Issue in published before and after dates filters (#11741)

This commit is contained in:
Shireen Missi 2024-11-14 16:02:26 +00:00 committed by GitHub
parent d9259a2d93
commit 7381c28af0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 69 additions and 26 deletions

View File

@ -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'],
@ -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.`,
);
}
}

View File

@ -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';
@ -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;
@ -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);

View File

@ -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.`,
);
});
});