Add deleteOneRelation resolver (#2569)

This commit is contained in:
Weiko 2023-11-17 19:13:42 +01:00 committed by GitHub
parent ed71ef67af
commit 1deb742ac9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View File

@ -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],
},
],

View File

@ -35,6 +35,44 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
super(relationMetadataRepository);
}
override async deleteOne(id: string): Promise<RelationMetadataEntity> {
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<RelationMetadataEntity> {