refactor(系统设置): 补充模板相关接口

This commit is contained in:
AgAngle 2023-10-18 21:43:32 +08:00 committed by Craftsman
parent abbb7fc4d8
commit 75de4ad7ce
14 changed files with 93 additions and 30 deletions

View File

@ -453,6 +453,7 @@ node.name.repeat=节点名称重复
project.cannot.match.parent=和父节点的项目无法匹配
set_default_template=设置默认模板
project_template_enable=开启项目模板
# 状态流
status_flow.name=状态流

View File

@ -451,6 +451,7 @@ custom_field.functional_priority=Priority
template.functional_default=Default
set_default_template=Set the default template
project_template_enable=Enable the project template
global_parameters_already_exist=Global parameters already exist
global_parameters_is_not_exist=Global parameters is not exist

View File

@ -449,6 +449,7 @@ custom_field.functional_priority=优先级
template.functional_default=默认模板
set_default_template=设置默认模板
project_template_enable=开启项目模板
global_parameters_already_exist=全局参数已存在
global_parameters_is_not_exist=全局参数不存在

View File

@ -448,6 +448,7 @@ custom_field.functional_priority=優先級
template.functional_default=默認模板
set_default_template=設置默認模板
project_template_enable=開啟項目模板
global_parameters_already_exist=全局參數已存在
global_parameters_is_not_exist=全局參數不存在

View File

@ -37,7 +37,7 @@ public class ProjectCustomFieldController {
@GetMapping("/list/{projectId}/{scene}")
@Operation(summary = "获取自定义字段列表")
@RequiresPermissions(PermissionConstants.PROJECT_CUSTOM_FIELD_READ)
public List<CustomField> list(@Schema(description = "项目ID", requiredMode = Schema.RequiredMode.REQUIRED)
public List<CustomFieldDTO> list(@Schema(description = "项目ID", requiredMode = Schema.RequiredMode.REQUIRED)
@PathVariable String projectId,
@Schema(description = "模板的使用场景FUNCTIONAL,BUG,API,UI,TEST_PLAN", requiredMode = Schema.RequiredMode.REQUIRED)
@PathVariable String scene) {

View File

@ -27,7 +27,7 @@ public class ProjectCustomFieldService extends BaseCustomFieldService {
private ProjectService projectService;
@Override
public List<CustomField> list(String projectId, String scene) {
public List<CustomFieldDTO> list(String projectId, String scene) {
projectService.checkResourceExist(projectId);
return super.list(projectId, scene);
}

View File

@ -26,6 +26,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MvcResult;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@ -203,12 +204,12 @@ public class ProjectCustomFieldControllerTests extends BaseTest {
MvcResult mvcResult = this.requestGetWithOk(LIST, DEFAULT_PROJECT_ID, scene)
.andReturn();
// 校验数据是否正确
List<CustomField> resultList = getResultDataArray(mvcResult, CustomField.class);
List<CustomFieldDTO> resultList = getResultDataArray(mvcResult, CustomFieldDTO.class);
List<CustomField> customFields = baseCustomFieldService.getByScopeIdAndScene(DEFAULT_PROJECT_ID, scene);
List<String> userIds = customFields.stream().map(CustomField::getCreateUser).toList();
Map<String, String> userNameMap = userLoginService.getUserNameMap(userIds);
for (int i = 0; i < resultList.size(); i++) {
CustomField resultItem = resultList.get(i);
CustomField resultItem = BeanUtils.copyBean(new CustomField(), resultList.get(i));;
CustomField customField = customFields.get(i);
customField.setCreateUser(userNameMap.get(customField.getCreateUser()));
if (customField.getInternal()) {
@ -217,6 +218,11 @@ public class ProjectCustomFieldControllerTests extends BaseTest {
}
Assertions.assertEquals(customField, resultItem);
Assertions.assertEquals(resultItem.getScene(), scene);
// 有下拉框选项的校验选项
if (CustomFieldType.getHasOptionValueSet().contains(resultItem.getType())) {
Assertions.assertEquals(resultList.get(i).getOptions().stream().sorted(Comparator.comparing(CustomFieldOption::getValue)).toList(),
baseCustomFieldOptionService.getByFieldId(customField.getId()).stream().sorted(Comparator.comparing(CustomFieldOption::getValue)).toList());
}
}
// @@校验组织是否存在

View File

@ -37,7 +37,7 @@ public class OrganizationCustomFieldController {
@GetMapping("/list/{organizationId}/{scene}")
@Operation(summary = "获取自定义字段列表")
@RequiresPermissions(PermissionConstants.ORGANIZATION_TEMPLATE_READ)
public List<CustomField> list(@Schema(description = "组织ID", requiredMode = Schema.RequiredMode.REQUIRED)
public List<CustomFieldDTO> list(@Schema(description = "组织ID", requiredMode = Schema.RequiredMode.REQUIRED)
@PathVariable String organizationId,
@Schema(description = "模板的使用场景FUNCTIONAL,BUG,API,UI,TEST_PLAN", requiredMode = Schema.RequiredMode.REQUIRED)
@PathVariable String scene) {

View File

@ -32,7 +32,7 @@ import java.util.List;
public class OrganizationTemplateController {
@Resource
private OrganizationTemplateService organizationTemplateservice;
private OrganizationTemplateService organizationTemplateService;
@GetMapping("/list/{organizationId}/{scene}")
@Operation(summary = "获取模版列表")
@ -41,14 +41,14 @@ public class OrganizationTemplateController {
@PathVariable String organizationId,
@Schema(description = "模板的使用场景FUNCTIONAL,BUG,API,UI,TEST_PLAN", requiredMode = Schema.RequiredMode.REQUIRED)
@PathVariable String scene) {
return organizationTemplateservice.list(organizationId, scene);
return organizationTemplateService.list(organizationId, scene);
}
@GetMapping("/get/{id}")
@Operation(summary = "获取模版详情")
@RequiresPermissions(PermissionConstants.ORGANIZATION_TEMPLATE_READ)
public TemplateDTO get(@PathVariable String id) {
return organizationTemplateservice.geDTOWithCheck(id);
return organizationTemplateService.geDTOWithCheck(id);
}
@PostMapping("/add")
@ -59,7 +59,7 @@ public class OrganizationTemplateController {
Template template = new Template();
BeanUtils.copyBean(template, request);
template.setCreateUser(SessionUtils.getUserId());
return organizationTemplateservice.add(template, request.getCustomFields());
return organizationTemplateService.add(template, request.getCustomFields());
}
@PostMapping("/update")
@ -69,7 +69,7 @@ public class OrganizationTemplateController {
public Template update(@Validated({Updated.class}) @RequestBody TemplateUpdateRequest request) {
Template template = new Template();
BeanUtils.copyBean(template, request);
return organizationTemplateservice.update(template, request.getCustomFields());
return organizationTemplateService.update(template, request.getCustomFields());
}
@GetMapping("/delete/{id}")
@ -77,16 +77,15 @@ public class OrganizationTemplateController {
@RequiresPermissions(PermissionConstants.ORGANIZATION_TEMPLATE_DELETE)
@Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#id)", msClass = OrganizationTemplateLogService.class)
public void delete(@PathVariable String id) {
organizationTemplateservice.delete(id);
organizationTemplateService.delete(id);
}
@GetMapping("/disable/{organizationId}/{scene}")
@Operation(summary = "关闭组织模板,开启项目模板")
@RequiresPermissions(PermissionConstants.ORGANIZATION_TEMPLATE_ENABLE)
// todo 操作日志待页面设计
// @Log(type = OperationLogType.UPDATE, expression = "#msClass.update(#id)", msClass = OrganizationTemplateLogService.class)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.disableOrganizationTemplateLog(#organizationId,#scene)", msClass = OrganizationTemplateLogService.class)
public void disableOrganizationTemplate(@PathVariable String organizationId, @PathVariable String scene) {
organizationTemplateservice.disableOrganizationTemplate(organizationId, scene);
organizationTemplateService.disableOrganizationTemplate(organizationId, scene);
}
@GetMapping("/set-default/{id}")
@ -94,6 +93,13 @@ public class OrganizationTemplateController {
@RequiresPermissions(PermissionConstants.ORGANIZATION_TEMPLATE_UPDATE)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.setDefaultTemplateLog(#id)", msClass = OrganizationTemplateLogService.class)
public void setDefaultTemplate(@PathVariable String id) {
organizationTemplateservice.setDefaultTemplate(id);
organizationTemplateService.setDefaultTemplate(id);
}
@GetMapping("/is-enable/{organizationId}/{scene}")
@Operation(summary = "是否启用组织模版")
@RequiresPermissions(PermissionConstants.ORGANIZATION_TEMPLATE_READ)
public boolean isOrganizationTemplateEnable(@PathVariable String organizationId, @PathVariable String scene) {
return organizationTemplateService.isOrganizationTemplateEnable(organizationId, scene);
}
}

View File

@ -1,5 +1,6 @@
package io.metersphere.system.service;
import io.metersphere.sdk.constants.CustomFieldType;
import io.metersphere.sdk.constants.DefaultFunctionalCustomField;
import io.metersphere.sdk.constants.TemplateScene;
import io.metersphere.sdk.constants.TemplateScopeType;
@ -25,6 +26,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static io.metersphere.system.controller.handler.result.CommonResultCode.*;
@ -44,18 +46,26 @@ public class BaseCustomFieldService {
@Resource
protected BaseOrganizationParameterService baseOrganizationParameterService;
public List<CustomField> list(String scopeId, String scene) {
public List<CustomFieldDTO> list(String scopeId, String scene) {
checkScene(scene);
List<CustomField> customFields = getByScopeIdAndScene(scopeId, scene);
List<String> userIds = customFields.stream().map(CustomField::getCreateUser).toList();
Map<String, String> userNameMap = userLoginService.getUserNameMap(userIds);
customFields.forEach(item -> {
List<CustomFieldOption> customFieldOptions = baseCustomFieldOptionService.getByFieldIds(customFields.stream().map(CustomField::getId).toList());
Map<String, List<CustomFieldOption>> optionMap = customFieldOptions.stream().collect(Collectors.groupingBy(CustomFieldOption::getFieldId));
return customFields.stream().map(item -> {
item.setCreateUser(userNameMap.get(item.getCreateUser()));
if (item.getInternal()) {
item.setName(translateInternalField(item.getName()));
}
});
return customFields;
CustomFieldDTO customFieldDTO = new CustomFieldDTO();
BeanUtils.copyBean(customFieldDTO, item);
customFieldDTO.setOptions(optionMap.get(item.getId()));
if (CustomFieldType.getHasOptionValueSet().contains(customFieldDTO.getType()) && customFieldDTO.getOptions() == null) {
customFieldDTO.setOptions(List.of());
}
return customFieldDTO;
}).toList();
}
/**

View File

@ -33,7 +33,7 @@ public class OrganizationCustomFieldService extends BaseCustomFieldService {
private ExtOrganizationCustomFieldMapper extOrganizationCustomFieldMapper;
@Override
public List<CustomField> list(String orgId, String scene) {
public List<CustomFieldDTO> list(String orgId, String scene) {
OrganizationService.checkResourceExist(orgId);
return super.list(orgId, scene);
}

View File

@ -11,6 +11,7 @@ import io.metersphere.system.domain.Template;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author jianxing
* @date : 2023-8-30
@ -52,6 +53,17 @@ public class OrganizationTemplateLogService {
return dto;
}
public LogDTO disableOrganizationTemplateLog(String organizationId, String scene) {
return new LogDTO(
OperationLogConstants.ORGANIZATION,
organizationId,
scene,
null,
OperationLogType.UPDATE.name(),
OperationLogModule.SETTING_SYSTEM_ORGANIZATION_TEMPLATE,
Translator.get("project_template_enable"));
}
public LogDTO updateLog(TemplateUpdateRequest request) {
Template template = organizationTemplateService.getWithCheck(request.getId());
LogDTO dto = null;

View File

@ -4,20 +4,23 @@ import io.metersphere.project.domain.Project;
import io.metersphere.project.domain.ProjectExample;
import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.*;
import io.metersphere.system.base.BaseTest;
import io.metersphere.sdk.dto.CustomFieldDTO;
import io.metersphere.sdk.dto.request.CustomFieldOptionRequest;
import io.metersphere.sdk.dto.request.CustomFieldUpdateRequest;
import io.metersphere.system.domain.*;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.param.CustomFieldUpdateRequestDefinition;
import io.metersphere.system.domain.CustomField;
import io.metersphere.system.domain.CustomFieldExample;
import io.metersphere.system.domain.CustomFieldOption;
import io.metersphere.system.domain.OrganizationParameter;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.mapper.CustomFieldMapper;
import io.metersphere.system.mapper.OrganizationParameterMapper;
import io.metersphere.system.service.BaseCustomFieldOptionService;
import io.metersphere.system.service.BaseCustomFieldService;
import io.metersphere.system.service.UserLoginService;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.system.controller.param.CustomFieldUpdateRequestDefinition;
import io.metersphere.system.mapper.CustomFieldMapper;
import io.metersphere.system.service.OrganizationCustomFieldService;
import io.metersphere.system.service.UserLoginService;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
@ -27,6 +30,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MvcResult;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@ -208,12 +212,12 @@ public class OrganizationCustomFieldControllerTests extends BaseTest {
MvcResult mvcResult = this.requestGetWithOk(LIST, DEFAULT_ORGANIZATION_ID, scene)
.andReturn();
// 校验数据是否正确
List<CustomField> resultList = getResultDataArray(mvcResult, CustomField.class);
List<CustomFieldDTO> resultList = getResultDataArray(mvcResult, CustomFieldDTO.class);
List<CustomField> customFields = baseCustomFieldService.getByScopeIdAndScene(DEFAULT_ORGANIZATION_ID, scene);
List<String> userIds = customFields.stream().map(CustomField::getCreateUser).toList();
Map<String, String> userNameMap = userLoginService.getUserNameMap(userIds);
for (int i = 0; i < resultList.size(); i++) {
CustomField resultItem = resultList.get(i);
CustomField resultItem = BeanUtils.copyBean(new CustomField(), resultList.get(i));
CustomField customField = customFields.get(i);
customField.setCreateUser(userNameMap.get(customField.getCreateUser()));
if (customField.getInternal()) {
@ -222,6 +226,11 @@ public class OrganizationCustomFieldControllerTests extends BaseTest {
}
Assertions.assertEquals(customField, resultItem);
Assertions.assertEquals(resultItem.getScene(), scene);
// 有下拉框选项的校验选项
if (CustomFieldType.getHasOptionValueSet().contains(resultItem.getType())) {
Assertions.assertEquals(resultList.get(i).getOptions().stream().sorted(Comparator.comparing(CustomFieldOption::getValue)).toList(),
baseCustomFieldOptionService.getByFieldId(customField.getId()).stream().sorted(Comparator.comparing(CustomFieldOption::getValue)).toList());
}
}
// @@校验组织是否存在

View File

@ -45,6 +45,7 @@ public class OrganizationTemplateControllerTests extends BaseTest {
private static final String LIST = "list/{0}/{1}";
private static final String DISABLE_ORG_TEMPLATE = "disable/{0}/{1}";
private static final String SET_DEFAULT = "set-default/{0}";
protected static final String IS_ORGANIZATION_TEMPLATE_ENABLE = "is-enable/{0}/{1}";
@Resource
private TemplateMapper templateMapper;
@ -342,8 +343,8 @@ public class OrganizationTemplateControllerTests extends BaseTest {
// 提高覆盖率
this.requestGetWithOk(DISABLE_ORG_TEMPLATE, addTemplate.getScopeId(), addTemplate.getScene());
// todo @@校验日志
// checkLog(addTemplate.getId(), OperationLogType.DELETE);
// @@校验日志
checkLog(addTemplate.getScene(), OperationLogType.UPDATE);
// @@校验权限
requestGetPermissionTest(PermissionConstants.ORGANIZATION_TEMPLATE_ENABLE, DISABLE_ORG_TEMPLATE, addTemplate.getScopeId(), addTemplate.getScene());
}
@ -425,4 +426,19 @@ public class OrganizationTemplateControllerTests extends BaseTest {
// @@校验权限
requestGetPermissionTest(PermissionConstants.ORGANIZATION_TEMPLATE_DELETE, DEFAULT_DELETE, addTemplate.getId());
}
@Test
@Order(8)
public void isOrganizationTemplateEnable() throws Exception {
// @@请求成功
MvcResult mvcResult = this.requestGetWithOkAndReturn(IS_ORGANIZATION_TEMPLATE_ENABLE, DEFAULT_ORGANIZATION_ID, TemplateScene.FUNCTIONAL.name());
Assertions.assertTrue(getResultData(mvcResult, Boolean.class));
changeOrgTemplateEnable(false);
mvcResult = this.requestGetWithOkAndReturn(IS_ORGANIZATION_TEMPLATE_ENABLE, DEFAULT_ORGANIZATION_ID, TemplateScene.FUNCTIONAL.name());
Assertions.assertFalse(getResultData(mvcResult, Boolean.class));
changeOrgTemplateEnable(true);
// @@校验权限
requestGetPermissionTest(PermissionConstants.ORGANIZATION_TEMPLATE_READ, IS_ORGANIZATION_TEMPLATE_ENABLE, DEFAULT_ORGANIZATION_ID, TemplateScene.FUNCTIONAL.name());
}
}