fix: prevent deletion of predefined operators and improve error handling (#192)

* fix: prevent deletion of predefined operators and improve error handling

* fix: prevent deletion of predefined operators and improve error handling
This commit is contained in:
hhhhsc701
2025-12-22 19:30:41 +08:00
committed by GitHub
parent c1516c87b6
commit d82bff441a
15 changed files with 98 additions and 55 deletions

View File

@@ -95,6 +95,9 @@ public class OperatorService {
if (operatorRepo.operatorInTemplateOrRunning(id)) {
throw BusinessException.of(OperatorErrorCode.OPERATOR_IN_INSTANCE);
}
if (relationRepo.operatorIsPredefined(id)) {
throw BusinessException.of(OperatorErrorCode.CANT_DELETE_PREDEFINED_OPERATOR);
}
operatorRepo.deleteOperator(id);
relationRepo.deleteByOperatorId(id);
}

View File

@@ -30,6 +30,8 @@ public class OperatorConstant {
public static String CATEGORY_STAR_ID = "51847c24-bba9-11f0-888b-5b143cb738aa";
public static String CATEGORY_PREDEFINED_ID = "96a3b07a-3439-4557-a835-525faad60ca3";
public static Map<String, String> CATEGORY_MAP = new HashMap<>();
static {

View File

@@ -15,4 +15,6 @@ public interface CategoryRelationRepository extends IRepository<CategoryRelation
void batchUpdate(String operatorId, List<String> categories);
void deleteByOperatorId(String operatorId);
boolean operatorIsPredefined(String operatorId);
}

View File

@@ -18,7 +18,9 @@ public enum OperatorErrorCode implements ErrorCode {
SETTINGS_PARSE_FAILED("op.0004", "settings字段解析失败"),
OPERATOR_IN_INSTANCE("op.0005", "算子已被编排在模板或未完成的任务中");
OPERATOR_IN_INSTANCE("op.0005", "算子已被编排在模板或未完成的任务中"),
CANT_DELETE_PREDEFINED_OPERATOR("op.0006", "预置算子无法删除");
private final String code;
private final String message;

View File

@@ -2,6 +2,7 @@ package com.datamate.operator.infrastructure.persistence.Impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.repository.CrudRepository;
import com.datamate.operator.domain.contants.OperatorConstant;
import com.datamate.operator.domain.model.CategoryRelation;
import com.datamate.operator.domain.repository.CategoryRelationRepository;
import com.datamate.operator.infrastructure.converter.CategoryRelationConverter;
@@ -48,4 +49,12 @@ public class CategoryRelationRepositoryImpl extends CrudRepository<CategoryRelat
queryWrapper.eq(CategoryRelation::getOperatorId, operatorId);
mapper.delete(queryWrapper);
}
@Override
public boolean operatorIsPredefined(String operatorId) {
LambdaQueryWrapper<CategoryRelation> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CategoryRelation::getOperatorId, operatorId)
.eq(CategoryRelation::getCategoryId, OperatorConstant.CATEGORY_PREDEFINED_ID);
return this.exists(queryWrapper);
}
}

View File

@@ -64,6 +64,12 @@
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>