mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
refactor: Refactor variables controller into a RestController (no-changelog) (#7822)
Github issue / Community forum post (link here to close automatically):
This commit is contained in:
@@ -3,13 +3,60 @@ import type { Variables } from '@db/entities/Variables';
|
||||
import { InternalHooks } from '@/InternalHooks';
|
||||
import { generateNanoId } from '@db/utils/generators';
|
||||
import { canCreateNewVariable } from './enviromentHelpers';
|
||||
import { VariablesService } from './variables.service';
|
||||
import { CacheService } from '@/services/cache.service';
|
||||
import { VariablesRepository } from '@db/repositories/variables.repository';
|
||||
import type { DeepPartial } from 'typeorm';
|
||||
|
||||
export class VariablesLicenseError extends Error {}
|
||||
export class VariablesValidationError extends Error {}
|
||||
|
||||
@Service()
|
||||
export class EEVariablesService extends VariablesService {
|
||||
export class VariablesService {
|
||||
constructor(
|
||||
protected cacheService: CacheService,
|
||||
protected variablesRepository: VariablesRepository,
|
||||
) {}
|
||||
|
||||
async getAllCached(): Promise<Variables[]> {
|
||||
const variables = await this.cacheService.get('variables', {
|
||||
async refreshFunction() {
|
||||
// TODO: log refresh cache metric
|
||||
return Container.get(VariablesService).findAll();
|
||||
},
|
||||
});
|
||||
return (variables as Array<DeepPartial<Variables>>).map((v) =>
|
||||
this.variablesRepository.create(v),
|
||||
);
|
||||
}
|
||||
|
||||
async getCount(): Promise<number> {
|
||||
return (await this.getAllCached()).length;
|
||||
}
|
||||
|
||||
async getCached(id: string): Promise<Variables | null> {
|
||||
const variables = await this.getAllCached();
|
||||
const foundVariable = variables.find((variable) => variable.id === id);
|
||||
if (!foundVariable) {
|
||||
return null;
|
||||
}
|
||||
return this.variablesRepository.create(foundVariable as DeepPartial<Variables>);
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
await this.variablesRepository.delete(id);
|
||||
await this.updateCache();
|
||||
}
|
||||
|
||||
async updateCache(): Promise<void> {
|
||||
// TODO: log update cache metric
|
||||
const variables = await this.findAll();
|
||||
await this.cacheService.set('variables', variables);
|
||||
}
|
||||
|
||||
async findAll(): Promise<Variables[]> {
|
||||
return this.variablesRepository.find();
|
||||
}
|
||||
|
||||
validateVariable(variable: Omit<Variables, 'id'>): void {
|
||||
if (variable.key.length > 50) {
|
||||
throw new VariablesValidationError('key cannot be longer than 50 characters');
|
||||
|
||||
Reference in New Issue
Block a user