Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Add option to filter for empty variables #12112

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@n8n/api-types/src/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { RoleChangeRequestDto } from './user/role-change-request.dto';
export { SettingsUpdateRequestDto } from './user/settings-update-request.dto';
export { UserUpdateRequestDto } from './user/user-update-request.dto';
export { CommunityRegisteredRequestDto } from './license/community-registered-request.dto';
export { VariableListRequestDto } from './variables/variables-list-request.dto';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { z } from 'zod';
import { Z } from 'zod-class';

export class VariableListRequestDto extends Z.class({
state: z.literal('empty').optional(),
}) {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { VariableListRequestDto } from '@n8n/api-types';

import { Delete, Get, GlobalScope, Licensed, Patch, Post, RestController } from '@/decorators';
import { Query } from '@/decorators/args';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { NotFoundError } from '@/errors/response-errors/not-found.error';
import { VariableCountLimitReachedError } from '@/errors/variable-count-limit-reached.error';
Expand All @@ -13,8 +16,8 @@ export class VariablesController {

@Get('/')
@GlobalScope('variable:list')
async getVariables() {
return await this.variablesService.getAllCached();
async getVariables(_req: unknown, _res: unknown, @Query query: VariableListRequestDto) {
return await this.variablesService.getAllCached(query.state);
}

@Post('/')
Expand Down
15 changes: 12 additions & 3 deletions packages/cli/src/environments/variables/variables.service.ee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@ export class VariablesService {
private readonly eventService: EventService,
) {}

async getAllCached(): Promise<Variables[]> {
const variables = await this.cacheService.get('variables', {
async getAllCached(state?: 'empty'): Promise<Variables[]> {
let variables = await this.cacheService.get('variables', {
async refreshFn() {
return await Container.get(VariablesService).findAll();
},
});
return (variables as Array<Partial<Variables>>).map((v) => this.variablesRepository.create(v));

if (variables === undefined) {
return [];
}

if (state === 'empty') {
variables = variables.filter((v) => v.value === '');
}

return variables.map((v) => this.variablesRepository.create(v));
}

async getCount(): Promise<number> {
Expand Down
29 changes: 26 additions & 3 deletions packages/cli/test/integration/variables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Variables } from '@/databases/entities/variables';
import { VariablesRepository } from '@/databases/repositories/variables.repository';
import { generateNanoId } from '@/databases/utils/generators';
import { VariablesService } from '@/environments/variables/variables.service.ee';
import { CacheService } from '@/services/cache/cache.service';

import { createOwner, createUser } from './shared/db/users';
import * as testDb from './shared/test-db';
Expand Down Expand Up @@ -65,19 +66,41 @@ beforeEach(async () => {
// ----------------------------------------
describe('GET /variables', () => {
beforeEach(async () => {
await Promise.all([createVariable('test1', 'value1'), createVariable('test2', 'value2')]);
await Promise.all([
createVariable('test1', 'value1'),
createVariable('test2', 'value2'),
createVariable('empty', ''),
]);
});

test('should return an empty array if there is nothing in the cache', async () => {
const cacheService = Container.get(CacheService);
const spy = jest.spyOn(cacheService, 'get').mockResolvedValueOnce(undefined);
const response = await authOwnerAgent.get('/variables');
expect(spy).toHaveBeenCalledTimes(1);
expect(response.statusCode).toBe(200);
expect(response.body.data.length).toBe(0);
});

test('should return all variables for an owner', async () => {
const response = await authOwnerAgent.get('/variables');
expect(response.statusCode).toBe(200);
expect(response.body.data.length).toBe(2);
expect(response.body.data.length).toBe(3);
});

test('should return all variables for a member', async () => {
const response = await authMemberAgent.get('/variables');
expect(response.statusCode).toBe(200);
expect(response.body.data.length).toBe(2);
expect(response.body.data.length).toBe(3);
});

describe('state:empty', () => {
test('only return empty variables', async () => {
const response = await authOwnerAgent.get('/variables').query({ state: 'empty' });
expect(response.statusCode).toBe(200);
expect(response.body.data.length).toBe(1);
expect(response.body.data[0]).toMatchObject({ key: 'empty', value: '', type: 'string' });
});
});
});

Expand Down
Loading