From 1deb742ac9a4573e8af9d17ee322435acdb7165d Mon Sep 17 00:00:00 2001 From: Weiko Date: Fri, 17 Nov 2023 19:13:42 +0100 Subject: [PATCH] Add deleteOneRelation resolver (#2569) --- .../relation-metadata.module.ts | 2 +- .../relation-metadata.service.ts | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) 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 {