refactor(消息管理): 增加获取模版详情的接口

This commit is contained in:
guoyuqi 2023-10-10 11:42:39 +08:00 committed by Yuki Guo
parent 23c8b1dc5d
commit 68ead1abfa
9 changed files with 142 additions and 32 deletions

View File

@ -2,13 +2,13 @@ package io.metersphere.project.controller;
import io.metersphere.project.dto.MessageTaskDTO;
import io.metersphere.project.dto.MessageTemplateConfigDTO;
import io.metersphere.project.service.MessageTaskLogService;
import io.metersphere.project.service.NoticeMessageTaskService;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.sdk.dto.request.MessageTaskRequest;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.log.annotation.Log;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.utils.SessionUtils;
@ -57,5 +57,15 @@ public class NoticeMessageTaskController {
return noticeMessageTaskService.getUserList(projectId, keyword);
}
@GetMapping("message/template/detail/{projectId}")
@Operation(summary = "项目管理-消息管理-消息设置-查看消息模版详情")
@RequiresPermissions(PermissionConstants.PROJECT_MESSAGE_READ_ADD)
public MessageTemplateConfigDTO getTemplateDetail(@PathVariable String projectId, @Schema(description = "消息配置功能类型")
@RequestParam(value = "taskType") String taskType, @Schema(description = "消息配置场景")
@RequestParam(value = "event") String event, @Schema(description = "消息配置机器人id")
@RequestParam(value = "robotId") String robotId) {
return noticeMessageTaskService.getTemplateDetail(projectId, taskType, event, robotId);
}
}

View File

@ -0,0 +1,18 @@
package io.metersphere.project.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.List;
@Data
@EqualsAndHashCode(callSuper = false)
public class MessageTemplateConfigDTO extends ProjectRobotConfigDTO{
@Schema(description = "消息配置接收人")
private List<String> receiverIds;
@Schema(description = "具体测试的ID")
public String testId;
}

View File

@ -2,28 +2,26 @@ package io.metersphere.project.service;
import io.metersphere.project.domain.*;
import io.metersphere.project.dto.MessageTaskDTO;
import io.metersphere.project.dto.MessageTaskDetailDTO;
import io.metersphere.project.dto.MessageTaskTypeDTO;
import io.metersphere.project.dto.ProjectRobotConfigDTO;
import io.metersphere.project.dto.*;
import io.metersphere.project.enums.ProjectRobotPlatform;
import io.metersphere.project.enums.result.ProjectResultCode;
import io.metersphere.project.mapper.*;
import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.sdk.dto.request.MessageTaskRequest;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.system.domain.UserExample;
import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.notice.utils.MessageTemplateUtils;
import io.metersphere.system.uid.UUID;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserExample;
import io.metersphere.system.domain.UserRoleRelation;
import io.metersphere.system.domain.UserRoleRelationExample;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;
import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.notice.utils.MessageTemplateUtils;
import io.metersphere.system.uid.UUID;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@ -31,7 +29,6 @@ import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -39,7 +36,10 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@ -323,7 +323,7 @@ public class NoticeMessageTaskService {
Map<String, String> taskTypeMap = MessageTemplateUtils.getTaskTypeMap();
Map<String, String> eventMap = MessageTemplateUtils.getEventMap();
Map<String, String> defaultTemplateMap = MessageTemplateUtils.getDefaultTemplateMap();
Map<String, String> defaultTemplateTitleMap = MessageTemplateUtils.getDefaultTemplateTitleMap();
Map<String, String> defaultTemplateSubjectMap = MessageTemplateUtils.getDefaultTemplateSubjectMap();
ProjectRobot projectRobot = setDefaultRobot(projectId, null);
for (MessageTaskDTO messageTaskDTO : messageTaskDTOList) {
messageTaskDTO.setProjectId(projectId);
@ -340,9 +340,9 @@ public class NoticeMessageTaskService {
List<MessageTask> messageTaskList = messageEventMap.get(messageTaskDetailDTO.getEvent());
List<OptionDTO> receivers = new ArrayList<>();
Map<String, ProjectRobotConfigDTO> projectRobotConfigMap = new HashMap<>();
String defaultTemplate = defaultTemplateMap.get(messageTaskTypeDTO.taskType + "_" + messageTaskDetailDTO.getEvent());
String defaultTemplate = defaultTemplateMap.get(messageTaskTypeDTO.getTaskType() + "_" + messageTaskDetailDTO.getEvent());
if (CollectionUtils.isEmpty(messageTaskList)) {
String defaultSubject = defaultTemplateTitleMap.get(messageTaskTypeDTO.taskType + "_" + messageTaskDetailDTO.getEvent());
String defaultSubject = defaultTemplateSubjectMap.get(messageTaskTypeDTO.getTaskType() + "_" + messageTaskDetailDTO.getEvent());
ProjectRobotConfigDTO projectRobotConfigDTO = getDefaultProjectRobotConfigDTO(defaultTemplate, defaultSubject, projectRobot);
projectRobotConfigMap.put(projectRobot.getId(), projectRobotConfigDTO);
} else {
@ -355,11 +355,11 @@ public class NoticeMessageTaskService {
String platform = robotMap.get(messageTask.getProjectRobotId()).getPlatform();
String defaultSubject;
if (StringUtils.equalsIgnoreCase(platform, ProjectRobotPlatform.MAIL.toString())) {
defaultSubject = "MeterSphere " + defaultTemplateTitleMap.get(messageTaskTypeDTO.taskType + "_" + messageTaskDetailDTO.getEvent());
defaultSubject = "MeterSphere " + defaultTemplateSubjectMap.get(messageTaskTypeDTO.getTaskType() + "_" + messageTaskDetailDTO.getEvent());
} else {
defaultSubject = defaultTemplateTitleMap.get(messageTaskTypeDTO.taskType + "_" + messageTaskDetailDTO.getEvent());
defaultSubject = defaultTemplateSubjectMap.get(messageTaskTypeDTO.getTaskType() + "_" + messageTaskDetailDTO.getEvent());
}
ProjectRobotConfigDTO projectRobotConfigDTO = getProjectRobotConfigDTO(defaultTemplate, defaultSubject, robotMap, messageTask, messageTaskBlob);
ProjectRobotConfigDTO projectRobotConfigDTO = getProjectRobotConfigDTO(defaultTemplate, defaultSubject, robotMap.get(messageTask.getProjectRobotId()), messageTask, messageTaskBlob);
projectRobotConfigMap.put(messageTask.getProjectRobotId(), projectRobotConfigDTO);
}
}
@ -372,11 +372,10 @@ public class NoticeMessageTaskService {
return messageTaskDTOList;
}
private ProjectRobotConfigDTO getProjectRobotConfigDTO(String defaultTemplate, String defaultSubject, Map<String, ProjectRobot> robotMap, MessageTask messageTask, MessageTaskBlob messageTaskBlob) {
private ProjectRobotConfigDTO getProjectRobotConfigDTO(String defaultTemplate, String defaultSubject, ProjectRobot projectRobot, MessageTask messageTask, MessageTaskBlob messageTaskBlob) {
ProjectRobotConfigDTO projectRobotConfigDTO = new ProjectRobotConfigDTO();
ProjectRobot projectRobot = robotMap.get(messageTask.getProjectRobotId());
projectRobotConfigDTO.setRobotName(projectRobot.getName());
projectRobotConfigDTO.setRobotId(messageTask.getProjectRobotId());
projectRobotConfigDTO.setRobotId(projectRobot.getId());
projectRobotConfigDTO.setPlatform(projectRobot.getPlatform());
projectRobotConfigDTO.setDingType(projectRobot.getType());
projectRobotConfigDTO.setEnable(messageTask.getEnable());
@ -408,4 +407,35 @@ public class NoticeMessageTaskService {
public List<OptionDTO> getUserList(String projectId, String keyword) {
return extProjectUserRoleMapper.getProjectUserSelectList(projectId, keyword);
}
public MessageTemplateConfigDTO getTemplateDetail(String projectId, String taskType, String event, String robotId) {
MessageTaskExample messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andProjectIdEqualTo(projectId).andTaskTypeEqualTo(taskType).andEventEqualTo(event);
List<MessageTask> messageTasks = messageTaskMapper.selectByExample(messageTaskExample);
List<String> receiverIds = messageTasks.stream().map(MessageTask::getReceiver).distinct().toList();
Map<String, List<MessageTask>> messageRobotMap = messageTasks.stream().collect(Collectors.groupingBy(MessageTask::getProjectRobotId));
MessageTask messageTask;
if (CollectionUtils.isNotEmpty(messageRobotMap.get(robotId))) {
messageTask = messageRobotMap.get(robotId).get(0);
} else {
messageTask = messageTasks.get(0);
messageTask.setEnable(false);
messageTask.setUseDefaultTemplate(true);
messageTask.setUseDefaultSubject(true);
}
MessageTaskBlob messageTaskBlob = messageTaskBlobMapper.selectByPrimaryKey(messageTask.getId());
Map<String, String> defaultTemplateMap = MessageTemplateUtils.getDefaultTemplateMap();
Map<String, String> defaultTemplateSubjectMap = MessageTemplateUtils.getDefaultTemplateSubjectMap();
String defaultTemplate = defaultTemplateMap.get(messageTask.getTaskType() + "_" + messageTask.getEvent());
String defaultSubject = defaultTemplateSubjectMap.get(messageTask.getTaskType() + "_" + messageTask.getEvent());
ProjectRobot projectRobot = projectRobotMapper.selectByPrimaryKey(robotId);
if (projectRobot == null) {
throw new MSException(Translator.get("robot_is_null"));
}
ProjectRobotConfigDTO projectRobotConfigDTO = getProjectRobotConfigDTO(defaultTemplate, defaultSubject, projectRobot, messageTask, messageTaskBlob);
MessageTemplateConfigDTO messageTemplateConfigDTO = new MessageTemplateConfigDTO();
BeanUtils.copyBean(messageTemplateConfigDTO,projectRobotConfigDTO);
messageTemplateConfigDTO.setReceiverIds(receiverIds);
return messageTemplateConfigDTO;
}
}

View File

@ -7,6 +7,7 @@ import io.metersphere.project.domain.MessageTaskExample;
import io.metersphere.project.dto.MessageTaskDTO;
import io.metersphere.project.dto.MessageTaskDetailDTO;
import io.metersphere.project.dto.MessageTemplateConfigDTO;
import io.metersphere.project.dto.ProjectRobotConfigDTO;
import io.metersphere.project.mapper.MessageTaskBlobMapper;
import io.metersphere.project.mapper.MessageTaskMapper;
@ -483,4 +484,54 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
List<OptionDTO> userDtoList = JSON.parseArray(JSON.toJSONString(resultHolder.getData()), OptionDTO.class);
Assertions.assertEquals(0, userDtoList.size());
}
@Test
@Order(19)
public void getTemplateDetailWithRobot() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/notice/message/template/detail/project-message-test")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.param("taskType", NoticeConstants.TaskType.API_DEFINITION_TASK)
.param("event", NoticeConstants.Event.CREATE)
.param("robotId", "test_message_robot1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder resultHolder = JSON.parseObject(contentAsString, ResultHolder.class);
MessageTemplateConfigDTO messageTemplateConfigDTO = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), MessageTemplateConfigDTO.class);
Assertions.assertTrue(messageTemplateConfigDTO.getReceiverIds().size()>0);
}
@Test
@Order(20)
public void getTemplateDetailWithOutRobot() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/notice/message/template/detail/project-message-test")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.param("taskType", NoticeConstants.TaskType.API_DEFINITION_TASK)
.param("event", NoticeConstants.Event.CREATE)
.param("robotId", "test_message_robot3")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder resultHolder = JSON.parseObject(contentAsString, ResultHolder.class);
MessageTemplateConfigDTO messageTemplateConfigDTO = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), MessageTemplateConfigDTO.class);
Assertions.assertTrue(messageTemplateConfigDTO.getReceiverIds().size()>0);
}
@Test
@Order(21)
public void getTemplateDetailNotExistRobot() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/notice/message/template/detail/project-message-test")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.param("taskType", NoticeConstants.TaskType.API_DEFINITION_TASK)
.param("event", NoticeConstants.Event.CREATE)
.param("robotId", "test_message_robotX")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is5xxServerError())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
}
}

View File

@ -35,7 +35,8 @@ INSERT INTO user_role_relation (id, user_id, role_id, source_id, organization_id
INSERT INTO project_robot(id, project_id, name, platform, webhook, type, app_key, app_secret, enable, create_user, create_time, update_user, update_time, description) VALUES ('test_message_robot1', 'project-message-test-1', '测试机器人1', 'IN_SITE', 'NONE', null, null, null, true, 'admin', unix_timestamp() * 1000,'admin', unix_timestamp() * 1000, null),
('test_message_robot2', 'project-message-test-1', '测试机器人2', 'MAIL', 'NONE', null, null, null, true, 'admin', unix_timestamp() * 1000,'admin', unix_timestamp() * 1000, null);
('test_message_robot2', 'project-message-test-1', '测试机器人2', 'MAIL', 'NONE', null, null, null, true, 'admin', unix_timestamp() * 1000,'admin', unix_timestamp() * 1000, null),
('test_message_robot3', 'project-message-test-1', '测试机器人3', 'WE_COM', 'NONE', null, null, null, true, 'admin', unix_timestamp() * 1000,'admin', unix_timestamp() * 1000, null);

View File

@ -290,7 +290,7 @@ public interface NoticeConstants {
String JENKINS_TASK_EXECUTE_FAILED = "JENKINS_TASK_EXECUTE_FAILED";//Jenkins执行了测试计划:${name}
}
interface TemplateTitle {
interface TemplateSubject {
@Schema(description = "message.title.test_plan_task_create")
String TEST_PLAN_TASK_CREATE = "TEST_PLAN_TASK_CREATE"; // 测试计划创建通知
@Schema(description = "message.title.test_plan_task_update")

View File

@ -56,7 +56,7 @@ public class AfterReturningNoticeSendService {
}
private String getSubject(SendNotice sendNotice) {
Map<String, String> defaultTemplateTitleMap = MessageTemplateUtils.getDefaultTemplateTitleMap();
Map<String, String> defaultTemplateTitleMap = MessageTemplateUtils.getDefaultTemplateSubjectMap();
return defaultTemplateTitleMap.get(sendNotice.taskType() + "_" + sendNotice.event());
}

View File

@ -77,11 +77,11 @@ public class MessageTemplateUtils {
*
* @return Map<String, String> defaultTemplateTitleMap
*/
public static Map<String, String> getDefaultTemplateTitleMap() {
Map<String, String> defaultTemplateTitleMap = new HashMap<>();
Field[] defaultTemplateTitleFields = FieldUtils.getAllFields(NoticeConstants.TemplateTitle.class);
MessageTemplateUtils.setFieldNameMap(defaultTemplateTitleFields, defaultTemplateTitleMap);
return defaultTemplateTitleMap;
public static Map<String, String> getDefaultTemplateSubjectMap() {
Map<String, String> defaultTemplateSubjectMap = new HashMap<>();
Field[] defaultTemplateSubjectFields = FieldUtils.getAllFields(NoticeConstants.TemplateSubject.class);
MessageTemplateUtils.setFieldNameMap(defaultTemplateSubjectFields, defaultTemplateSubjectMap);
return defaultTemplateSubjectMap;
}
/**

View File

@ -127,7 +127,7 @@ public class MessageDetailService {
}
private String getSubject(String taskType, String event) {
Map<String, String> defaultTemplateTitleMap = MessageTemplateUtils.getDefaultTemplateTitleMap();
Map<String, String> defaultTemplateTitleMap = MessageTemplateUtils.getDefaultTemplateSubjectMap();
return "MeterSphere " + defaultTemplateTitleMap.get(taskType + "_" + event);
}