feat: Add projects to variables endpoints (no-changelog)

This commit is contained in:
Valya Bullions 2024-10-23 18:41:37 +01:00
parent 197a1264d1
commit 3f8408f855
No known key found for this signature in database
9 changed files with 58 additions and 11 deletions

View file

@ -3,3 +3,4 @@ export { RoleChangeRequestDto } from './user/role-change-request.dto';
export { SettingsUpdateRequestDto } from './user/settings-update-request.dto'; export { SettingsUpdateRequestDto } from './user/settings-update-request.dto';
export { UserUpdateRequestDto } from './user/user-update-request.dto'; export { UserUpdateRequestDto } from './user/user-update-request.dto';
export { CommunityRegisteredRequestDto } from './license/community-registered-request.dto'; export { CommunityRegisteredRequestDto } from './license/community-registered-request.dto';
export { CreateVariableRequestDto } from './variables/create-variable-request.dto';

View file

@ -1,6 +1,7 @@
import { Column, Entity } from '@n8n/typeorm'; import { Column, Entity, ManyToOne } from '@n8n/typeorm';
import { WithStringId } from './abstract-entity'; import { WithStringId } from './abstract-entity';
import type { Project } from './project';
@Entity() @Entity()
export class Variables extends WithStringId { export class Variables extends WithStringId {
@ -12,4 +13,14 @@ export class Variables extends WithStringId {
@Column('text') @Column('text')
value: string; value: string;
// If null, it's a global variable
@ManyToOne('Project', {
onDelete: 'CASCADE',
nullable: true,
})
project: Project | null;
@Column('text', { nullable: true })
projectId: string | null;
} }

View file

@ -68,6 +68,7 @@ import { CreateProcessedDataTable1726606152711 } from '../common/1726606152711-C
import { SeparateExecutionCreationFromStart1727427440136 } from '../common/1727427440136-SeparateExecutionCreationFromStart'; import { SeparateExecutionCreationFromStart1727427440136 } from '../common/1727427440136-SeparateExecutionCreationFromStart';
import { AddMissingPrimaryKeyOnAnnotationTagMapping1728659839644 } from '../common/1728659839644-AddMissingPrimaryKeyOnAnnotationTagMapping'; import { AddMissingPrimaryKeyOnAnnotationTagMapping1728659839644 } from '../common/1728659839644-AddMissingPrimaryKeyOnAnnotationTagMapping';
import { UpdateProcessedDataValueColumnToText1729607673464 } from '../common/1729607673464-UpdateProcessedDataValueColumnToText'; import { UpdateProcessedDataValueColumnToText1729607673464 } from '../common/1729607673464-UpdateProcessedDataValueColumnToText';
import { AddProjectToVariables1729695079000 } from '../common/1729695079000-AddProjectToVariables';
export const mysqlMigrations: Migration[] = [ export const mysqlMigrations: Migration[] = [
InitialMigration1588157391238, InitialMigration1588157391238,
@ -138,4 +139,5 @@ export const mysqlMigrations: Migration[] = [
CreateProcessedDataTable1726606152711, CreateProcessedDataTable1726606152711,
AddMissingPrimaryKeyOnAnnotationTagMapping1728659839644, AddMissingPrimaryKeyOnAnnotationTagMapping1728659839644,
UpdateProcessedDataValueColumnToText1729607673464, UpdateProcessedDataValueColumnToText1729607673464,
AddProjectToVariables1729695079000,
]; ];

View file

@ -68,6 +68,7 @@ import { CreateProcessedDataTable1726606152711 } from '../common/1726606152711-C
import { SeparateExecutionCreationFromStart1727427440136 } from '../common/1727427440136-SeparateExecutionCreationFromStart'; import { SeparateExecutionCreationFromStart1727427440136 } from '../common/1727427440136-SeparateExecutionCreationFromStart';
import { AddMissingPrimaryKeyOnAnnotationTagMapping1728659839644 } from '../common/1728659839644-AddMissingPrimaryKeyOnAnnotationTagMapping'; import { AddMissingPrimaryKeyOnAnnotationTagMapping1728659839644 } from '../common/1728659839644-AddMissingPrimaryKeyOnAnnotationTagMapping';
import { UpdateProcessedDataValueColumnToText1729607673464 } from '../common/1729607673464-UpdateProcessedDataValueColumnToText'; import { UpdateProcessedDataValueColumnToText1729607673464 } from '../common/1729607673464-UpdateProcessedDataValueColumnToText';
import { AddProjectToVariables1729695079000 } from '../common/1729695079000-AddProjectToVariables';
export const postgresMigrations: Migration[] = [ export const postgresMigrations: Migration[] = [
InitialMigration1587669153312, InitialMigration1587669153312,
@ -138,4 +139,5 @@ export const postgresMigrations: Migration[] = [
CreateProcessedDataTable1726606152711, CreateProcessedDataTable1726606152711,
AddMissingPrimaryKeyOnAnnotationTagMapping1728659839644, AddMissingPrimaryKeyOnAnnotationTagMapping1728659839644,
UpdateProcessedDataValueColumnToText1729607673464, UpdateProcessedDataValueColumnToText1729607673464,
AddProjectToVariables1729695079000,
]; ];

View file

@ -65,6 +65,7 @@ import { CreateAnnotationTables1724753530828 } from '../common/1724753530828-Cre
import { CreateProcessedDataTable1726606152711 } from '../common/1726606152711-CreateProcessedDataTable'; import { CreateProcessedDataTable1726606152711 } from '../common/1726606152711-CreateProcessedDataTable';
import { SeparateExecutionCreationFromStart1727427440136 } from '../common/1727427440136-SeparateExecutionCreationFromStart'; import { SeparateExecutionCreationFromStart1727427440136 } from '../common/1727427440136-SeparateExecutionCreationFromStart';
import { UpdateProcessedDataValueColumnToText1729607673464 } from '../common/1729607673464-UpdateProcessedDataValueColumnToText'; import { UpdateProcessedDataValueColumnToText1729607673464 } from '../common/1729607673464-UpdateProcessedDataValueColumnToText';
import { AddProjectToVariables1729695079000 } from '../common/1729695079000-AddProjectToVariables';
const sqliteMigrations: Migration[] = [ const sqliteMigrations: Migration[] = [
InitialMigration1588102412422, InitialMigration1588102412422,
@ -132,6 +133,7 @@ const sqliteMigrations: Migration[] = [
CreateProcessedDataTable1726606152711, CreateProcessedDataTable1726606152711,
AddMissingPrimaryKeyOnAnnotationTagMapping1728659839644, AddMissingPrimaryKeyOnAnnotationTagMapping1728659839644,
UpdateProcessedDataValueColumnToText1729607673464, UpdateProcessedDataValueColumnToText1729607673464,
AddProjectToVariables1729695079000,
]; ];
export { sqliteMigrations }; export { sqliteMigrations };

View file

@ -1,9 +1,20 @@
import { Delete, Get, GlobalScope, Licensed, Patch, Post, RestController } from '@/decorators'; import { CreateVariableRequestDto } from '@n8n/api-types';
import {
Body,
Delete,
Get,
GlobalScope,
Licensed,
Patch,
Post,
RestController,
} from '@/decorators';
import { BadRequestError } from '@/errors/response-errors/bad-request.error'; import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { NotFoundError } from '@/errors/response-errors/not-found.error'; import { NotFoundError } from '@/errors/response-errors/not-found.error';
import { VariableCountLimitReachedError } from '@/errors/variable-count-limit-reached.error'; import { VariableCountLimitReachedError } from '@/errors/variable-count-limit-reached.error';
import { VariableValidationError } from '@/errors/variable-validation.error'; import { VariableValidationError } from '@/errors/variable-validation.error';
import { VariablesRequest } from '@/requests'; import { AuthenticatedRequest, VariablesRequest } from '@/requests';
import { VariablesService } from './variables.service.ee'; import { VariablesService } from './variables.service.ee';
@ -20,11 +31,13 @@ export class VariablesController {
@Post('/') @Post('/')
@Licensed('feat:variables') @Licensed('feat:variables')
@GlobalScope('variable:create') @GlobalScope('variable:create')
async createVariable(req: VariablesRequest.Create) { async createVariable(
const variable = req.body; req: AuthenticatedRequest,
delete variable.id; _res: Response,
@Body variable: CreateVariableRequestDto,
) {
try { try {
return await this.variablesService.create(variable); return await this.variablesService.create(variable, req.user);
} catch (error) { } catch (error) {
if (error instanceof VariableCountLimitReachedError) { if (error instanceof VariableCountLimitReachedError) {
throw new BadRequestError(error.message); throw new BadRequestError(error.message);

View file

@ -1,3 +1,4 @@
import type { CreateVariableRequestDto } from '@n8n/api-types';
import { Container, Service } from 'typedi'; import { Container, Service } from 'typedi';
import type { Variables } from '@/databases/entities/variables'; import type { Variables } from '@/databases/entities/variables';
@ -9,6 +10,7 @@ import { EventService } from '@/events/event.service';
import { CacheService } from '@/services/cache/cache.service'; import { CacheService } from '@/services/cache/cache.service';
import { canCreateNewVariable } from './environment-helpers'; import { canCreateNewVariable } from './environment-helpers';
import { User } from '@/databases/entities/user';
@Service() @Service()
export class VariablesService { export class VariablesService {
@ -67,11 +69,10 @@ export class VariablesService {
} }
} }
async create(variable: Omit<Variables, 'id'>): Promise<Variables> { async create(variable: CreateVariableRequestDto, user: User): Promise<Variables> {
if (!canCreateNewVariable(await this.getCount())) { if (!canCreateNewVariable(await this.getCount())) {
throw new VariableCountLimitReachedError('Variables limit reached'); throw new VariableCountLimitReachedError('Variables limit reached');
} }
this.validateVariable(variable);
this.eventService.emit('variable-created'); this.eventService.emit('variable-created');
const saveResult = await this.variablesRepository.save( const saveResult = await this.variablesRepository.save(

View file

@ -3,6 +3,10 @@ import Container from 'typedi';
import { VariablesRepository } from '@/databases/repositories/variables.repository'; import { VariablesRepository } from '@/databases/repositories/variables.repository';
import { VariablesController } from '@/environments/variables/variables.controller.ee'; import { VariablesController } from '@/environments/variables/variables.controller.ee';
import { VariablesService } from '@/environments/variables/variables.service.ee';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { VariableCountLimitReachedError } from '@/errors/variable-count-limit-reached.error';
import { VariableValidationError } from '@/errors/variable-validation.error';
import type { PaginatedRequest } from '@/public-api/types'; import type { PaginatedRequest } from '@/public-api/types';
import type { VariablesRequest } from '@/requests'; import type { VariablesRequest } from '@/requests';
@ -18,7 +22,18 @@ export = {
isLicensed('feat:variables'), isLicensed('feat:variables'),
globalScope('variable:create'), globalScope('variable:create'),
async (req: Create, res: Response) => { async (req: Create, res: Response) => {
await Container.get(VariablesController).createVariable(req); delete req.body.id;
try {
Container.get(VariablesService).validateVariable(req.body);
await Container.get(VariablesService).create(req.body, req.user);
} catch (error) {
if (error instanceof VariableCountLimitReachedError) {
throw new BadRequestError(error.message);
} else if (error instanceof VariableValidationError) {
throw new BadRequestError(error.message);
}
throw error;
}
res.status(201).send(); res.status(201).send();
}, },

View file

@ -453,7 +453,7 @@ export type BinaryDataRequest = AuthenticatedRequest<
// ---------------------------------- // ----------------------------------
// //
export declare namespace VariablesRequest { export declare namespace VariablesRequest {
type CreateUpdatePayload = Omit<Variables, 'id'> & { id?: unknown }; type CreateUpdatePayload = Omit<Variables, 'id' | 'type'> & { id?: unknown; type: 'string' };
type GetAll = AuthenticatedRequest; type GetAll = AuthenticatedRequest;
type Get = AuthenticatedRequest<{ id: string }, {}, {}, {}>; type Get = AuthenticatedRequest<{ id: string }, {}, {}, {}>;