Skip to content

Commit

Permalink
Fixed circular dependency problem
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasbordeau committed Nov 28, 2024
1 parent 1247412 commit 61ed304
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
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 })
export class IsFieldMetadataDefaultValue
implements ValidatorConstraintInterface
{
constructor(
private readonly fieldMetadataService: FieldMetadataService,
@InjectRepository(FieldMetadataEntity, 'metadata')
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>,
private readonly loggerService: LoggerService,
) {}

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<FieldMetadataEntity>,
) {}

async validate(
value: FieldMetadataOptions,
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RelationMetadataEntity>,
) {}

async run(
instance: DeleteOneInputType,
Expand All @@ -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');
Expand Down

0 comments on commit 61ed304

Please sign in to comment.