From 61ed3040aedbcb49845b98f03dfc037e07427d0e Mon Sep 17 00:00:00 2001 From: Lucas Bordeau Date: Thu, 28 Nov 2024 18:26:41 +0100 Subject: [PATCH] Fixed circular dependency problem --- ...-field-metadata-default-value.validator.ts | 14 +++++++++---- .../is-field-metadata-options.validator.ts | 12 ++++++++--- .../hooks/before-delete-one-relation.hook.ts | 21 ++++++++++++------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/packages/twenty-server/src/engine/metadata-modules/field-metadata/validators/is-field-metadata-default-value.validator.ts b/packages/twenty-server/src/engine/metadata-modules/field-metadata/validators/is-field-metadata-default-value.validator.ts index 977358788c60..cdb17c52e827 100644 --- a/packages/twenty-server/src/engine/metadata-modules/field-metadata/validators/is-field-metadata-default-value.validator.ts +++ b/packages/twenty-server/src/engine/metadata-modules/field-metadata/validators/is-field-metadata-default-value.validator.ts @@ -1,20 +1,21 @@ import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; import { ValidationArguments, ValidatorConstraint, ValidatorConstraintInterface, } from 'class-validator'; +import { Repository } from 'typeorm'; import { FieldMetadataDefaultValue } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata-default-value.interface'; -import { FieldMetadataService } from 'src/engine/metadata-modules/field-metadata/field-metadata.service'; +import { LoggerService } from 'src/engine/core-modules/logger/logger.service'; import { FieldMetadataEntity, FieldMetadataType, } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity'; import { validateDefaultValueForType } from 'src/engine/metadata-modules/field-metadata/utils/validate-default-value-for-type.util'; -import { LoggerService } from 'src/engine/core-modules/logger/logger.service'; @Injectable() @ValidatorConstraint({ name: 'isFieldMetadataDefaultValue', async: true }) @@ -22,7 +23,8 @@ export class IsFieldMetadataDefaultValue implements ValidatorConstraintInterface { constructor( - private readonly fieldMetadataService: FieldMetadataService, + @InjectRepository(FieldMetadataEntity, 'metadata') + private readonly fieldMetadataRepository: Repository, private readonly loggerService: LoggerService, ) {} @@ -44,7 +46,11 @@ export class IsFieldMetadataDefaultValue let fieldMetadata: FieldMetadataEntity; try { - fieldMetadata = await this.fieldMetadataService.findOneOrFail(id); + fieldMetadata = await this.fieldMetadataRepository.findOneOrFail({ + where: { + id, + }, + }); } catch { return false; } diff --git a/packages/twenty-server/src/engine/metadata-modules/field-metadata/validators/is-field-metadata-options.validator.ts b/packages/twenty-server/src/engine/metadata-modules/field-metadata/validators/is-field-metadata-options.validator.ts index ade111179ee0..bbfad46aa818 100644 --- a/packages/twenty-server/src/engine/metadata-modules/field-metadata/validators/is-field-metadata-options.validator.ts +++ b/packages/twenty-server/src/engine/metadata-modules/field-metadata/validators/is-field-metadata-options.validator.ts @@ -1,10 +1,11 @@ import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; import { ValidationArguments, ValidatorConstraint } from 'class-validator'; +import { Repository } from 'typeorm'; import { FieldMetadataOptions } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata-options.interface'; -import { FieldMetadataService } from 'src/engine/metadata-modules/field-metadata/field-metadata.service'; import { FieldMetadataEntity, FieldMetadataType, @@ -16,7 +17,10 @@ import { validateOptionsForType } from 'src/engine/metadata-modules/field-metada export class IsFieldMetadataOptions { private validationErrors: string[] = []; - constructor(private readonly fieldMetadataService: FieldMetadataService) {} + constructor( + @InjectRepository(FieldMetadataEntity, 'metadata') + private readonly fieldMetadataRepository: Repository, + ) {} async validate( value: FieldMetadataOptions, @@ -36,7 +40,9 @@ export class IsFieldMetadataOptions { let fieldMetadata: FieldMetadataEntity; try { - fieldMetadata = await this.fieldMetadataService.findOneOrFail(id); + fieldMetadata = await this.fieldMetadataRepository.findOneOrFail({ + where: { id }, + }); } catch { return false; } diff --git a/packages/twenty-server/src/engine/metadata-modules/relation-metadata/hooks/before-delete-one-relation.hook.ts b/packages/twenty-server/src/engine/metadata-modules/relation-metadata/hooks/before-delete-one-relation.hook.ts index df7880df5183..95530ef3c6cb 100644 --- a/packages/twenty-server/src/engine/metadata-modules/relation-metadata/hooks/before-delete-one-relation.hook.ts +++ b/packages/twenty-server/src/engine/metadata-modules/relation-metadata/hooks/before-delete-one-relation.hook.ts @@ -3,17 +3,22 @@ import { Injectable, UnauthorizedException, } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; import { BeforeDeleteOneHook, DeleteOneInputType, } from '@ptc-org/nestjs-query-graphql'; +import { Repository } from 'typeorm'; -import { RelationMetadataService } from 'src/engine/metadata-modules/relation-metadata/relation-metadata.service'; +import { RelationMetadataEntity } from 'src/engine/metadata-modules/relation-metadata/relation-metadata.entity'; @Injectable() export class BeforeDeleteOneRelation implements BeforeDeleteOneHook { - constructor(readonly relationMetadataService: RelationMetadataService) {} + constructor( + @InjectRepository(RelationMetadataEntity, 'metadata') + private readonly relationMetadataRepository: Repository, + ) {} async run( instance: DeleteOneInputType, @@ -25,12 +30,12 @@ export class BeforeDeleteOneRelation implements BeforeDeleteOneHook { throw new UnauthorizedException(); } - const relationMetadata = - await this.relationMetadataService.findOneWithinWorkspace(workspaceId, { - where: { - id: instance.id.toString(), - }, - }); + const relationMetadata = await this.relationMetadataRepository.findOne({ + where: { + workspaceId, + id: instance.id.toString(), + }, + }); if (!relationMetadata) { throw new BadRequestException('Relation does not exist');