diff --git a/server/src/metadata/relation-metadata/relation-metadata.module.ts b/server/src/metadata/relation-metadata/relation-metadata.module.ts index 05588828aa..7b2b7b9ef4 100644 --- a/server/src/metadata/relation-metadata/relation-metadata.module.ts +++ b/server/src/metadata/relation-metadata/relation-metadata.module.ts @@ -42,7 +42,7 @@ import { RelationMetadataDTO } from './dtos/relation-metadata.dto'; pagingStrategy: PagingStrategies.CURSOR, create: { many: { disabled: true } }, update: { disabled: true }, - delete: { disabled: true }, + delete: { many: { disabled: true } }, guards: [JwtAuthGuard], }, ], diff --git a/server/src/metadata/relation-metadata/relation-metadata.service.ts b/server/src/metadata/relation-metadata/relation-metadata.service.ts index 8f39f2456a..f4f0490f86 100644 --- a/server/src/metadata/relation-metadata/relation-metadata.service.ts +++ b/server/src/metadata/relation-metadata/relation-metadata.service.ts @@ -35,6 +35,44 @@ export class RelationMetadataService extends TypeOrmQueryService { + const relationMetadata = await this.relationMetadataRepository.findOne({ + where: { id }, + relations: ['fromFieldMetadata', 'toFieldMetadata'], + }); + + if (!relationMetadata) { + throw new NotFoundException('Relation does not exist'); + } + + if ( + !relationMetadata.toFieldMetadata.isCustom || + !relationMetadata.fromFieldMetadata.isCustom + ) { + throw new BadRequestException("Standard Relations can't be deleted"); + } + + if ( + relationMetadata.toFieldMetadata.isActive || + relationMetadata.fromFieldMetadata.isActive + ) { + throw new BadRequestException("Active relations can't be deleted"); + } + + const deletedRelationMetadata = super.deleteOne(id); + + this.fieldMetadataService.deleteMany({ + id: { + in: [ + relationMetadata.fromFieldMetadataId, + relationMetadata.toFieldMetadataId, + ], + }, + }); + + return deletedRelationMetadata; + } + override async createOne( record: CreateRelationInput, ): Promise {