fix(项目管理): 修复保存通知人时消息通知会关闭以及模版会更改问题

--bug=1035461 --user=郭雨琦
https://www.tapd.cn/55049933/bugtrace/bugs/view?bug_id=1155049933001035461&from=wxnotification&corpid=ww918354e3468dc0cc&agentid=1000014&jump_count=1&qy_private_corpid=
This commit is contained in:
guoyuqi 2024-02-05 21:02:05 +08:00 committed by Craftsman
parent 01d8747468
commit 08df0f8e1f
3 changed files with 34 additions and 38 deletions

View File

@ -11,6 +11,7 @@ import io.metersphere.system.dto.sdk.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.security.CheckOwner;
import io.metersphere.system.utils.SessionUtils;
import io.metersphere.validation.groups.Created;
import io.metersphere.validation.groups.Updated;
@ -38,6 +39,7 @@ public class NoticeMessageTaskController {
@Operation(summary = "项目管理-消息管理-消息设置-保存消息设置")
@RequiresPermissions(value = {PermissionConstants.PROJECT_MESSAGE_READ_ADD, PermissionConstants.PROJECT_MESSAGE_READ_UPDATE}, logical = Logical.OR)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.addLog(#messageTaskRequest)", msClass = MessageTaskLogService.class)
@CheckOwner(resourceId = "#request.projectId", resourceType = "project")
public ResultHolder saveMessage(@Validated({Created.class, Updated.class}) @RequestBody MessageTaskRequest messageTaskRequest) {
return noticeMessageTaskService.saveMessageTask(messageTaskRequest, SessionUtils.getUserId());
}
@ -45,6 +47,7 @@ public class NoticeMessageTaskController {
@GetMapping("message/task/get/{projectId}")
@Operation(summary = "项目管理-消息管理-消息设置-获取消息设置")
@RequiresPermissions(PermissionConstants.PROJECT_MESSAGE_READ_ADD)
@CheckOwner(resourceId = "#projectId", resourceType = "project")
public List<MessageTaskDTO> getMessageList(@PathVariable String projectId) throws IOException {
return noticeMessageTaskService.getMessageList(projectId);
}

View File

@ -68,11 +68,6 @@ public class NoticeMessageTaskService {
public ResultHolder saveMessageTask(MessageTaskRequest messageTaskRequest, String userId) {
String projectId = messageTaskRequest.getProjectId();
checkProjectExist(projectId);
//如果只选了用户没有选机器人默认机器人为站内信
ProjectRobot projectRobot = getDefaultRobot(messageTaskRequest.getProjectId(), messageTaskRequest.getRobotId());
String robotId = projectRobot.getId();
messageTaskRequest.setRobotId(robotId);
//删除用户数据不在当前传送用户内的数据
deleteUserData(messageTaskRequest, projectId);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
@ -85,6 +80,10 @@ public class NoticeMessageTaskService {
List<MessageTask> messageTasks = updateMessageTasks(messageTaskRequest, userId, mapper, blobMapper, existUserIds);
//保存消息任务
List<String> messageTaskReceivers = CollectionUtils.isEmpty(messageTasks) ? new ArrayList<>() : messageTasks.stream().map(MessageTask::getReceiver).toList();
//如果新增时只选了用户没有选机器人默认机器人为站内信
ProjectRobot projectRobot = getDefaultRobot(messageTaskRequest.getProjectId(), messageTaskRequest.getRobotId());
String robotId = projectRobot.getId();
messageTaskRequest.setRobotId(robotId);
insertMessageTask(messageTaskRequest, userId, mapper, blobMapper, existUserIds, messageTaskReceivers);
sqlSession.flushStatements();
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
@ -111,13 +110,6 @@ public class NoticeMessageTaskService {
}
}
private void checkProjectExist(String projectId) {
Project project = projectMapper.selectByPrimaryKey(projectId);
if (project == null) {
throw new MSException(Translator.get("project_is_not_exist"));
}
}
/**
* 新增MessageTask
*
@ -129,7 +121,6 @@ public class NoticeMessageTaskService {
* @param messageTaskReceivers 更新过后还有多少接收人需要保存
*/
private void insertMessageTask(MessageTaskRequest messageTaskRequest, String userId, MessageTaskMapper mapper, MessageTaskBlobMapper blobMapper, List<String> existUserIds, List<String> messageTaskReceivers) {
List<MessageTask> messageTasks = new ArrayList<>();
for (String receiverId : existUserIds) {
if (CollectionUtils.isNotEmpty(messageTaskReceivers) && messageTaskReceivers.contains(receiverId)) {
continue;
@ -143,7 +134,6 @@ public class NoticeMessageTaskService {
messageTaskBlob.setTemplate(messageTaskRequest.getTemplate());
}
messageTaskBlob.setTemplate(messageTaskRequest.getTemplate());
messageTasks.add(messageTask);
blobMapper.insert(messageTaskBlob);
}
}
@ -182,8 +172,7 @@ public class NoticeMessageTaskService {
boolean useDefaultTemplate = messageTaskRequest.getUseDefaultTemplate() == null || messageTaskRequest.getUseDefaultTemplate();
MessageTaskExample messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andReceiverIn(existUserIds).andProjectIdEqualTo(messageTaskRequest.getProjectId())
.andProjectRobotIdEqualTo(messageTaskRequest.getRobotId()).andTaskTypeEqualTo(messageTaskRequest.getTaskType()).andEventEqualTo(messageTaskRequest.getEvent());
messageTaskExample.createCriteria().andReceiverIn(existUserIds).andProjectIdEqualTo(messageTaskRequest.getProjectId()).andTaskTypeEqualTo(messageTaskRequest.getTaskType()).andEventEqualTo(messageTaskRequest.getEvent());
List<MessageTask> messageTasks = messageTaskMapper.selectByExample(messageTaskExample);
List<String> messageTaskIds = messageTasks.stream().map(MessageTask::getId).toList();
if (CollectionUtils.isEmpty(messageTasks)) {
@ -192,23 +181,29 @@ public class NoticeMessageTaskService {
for (MessageTask messageTask : messageTasks) {
messageTask.setUpdateTime(System.currentTimeMillis());
messageTask.setUpdateUser(userId);
if (StringUtils.isNotBlank(messageTaskRequest.getRobotId())) {
messageTask.setEnable(enable);
messageTask.setUseDefaultSubject(useDefaultSubject);
messageTask.setUseDefaultTemplate(useDefaultTemplate);
if (!useDefaultSubject) {
if (!messageTask.getUseDefaultSubject()) {
messageTask.setSubject(messageTaskRequest.getSubject());
}
} else {
useDefaultTemplate = messageTask.getUseDefaultTemplate();
}
mapper.updateByPrimaryKeySelective(messageTask);
}
MessageTaskBlobExample messageTaskBlobExample = new MessageTaskBlobExample();
messageTaskBlobExample.createCriteria().andIdIn(messageTaskIds);
List<MessageTaskBlob> messageTaskBlobs = messageTaskBlobMapper.selectByExample(messageTaskBlobExample);
for (MessageTaskBlob messageTaskBlob : messageTaskBlobs) {
if (StringUtils.isNotBlank(messageTaskRequest.getRobotId())) {
if (!useDefaultTemplate) {
messageTaskBlob.setTemplate(messageTaskRequest.getTemplate());
blobMapper.updateByPrimaryKeySelective(messageTaskBlob);
}
}
}
return messageTasks;
}
@ -318,7 +313,6 @@ public class NoticeMessageTaskService {
* @return List<MessageTaskDTO>
*/
public List<MessageTaskDTO> getMessageList(String projectId) throws IOException {
checkProjectExist(projectId);
//获取返回数据结构
StringBuilder jsonStr = new StringBuilder();
try{

View File

@ -75,6 +75,8 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
messageTaskRequest.setReceiverIds(userIds);
messageTaskRequest.setRobotId("test_message_robot2");
messageTaskRequest.setEnable(true);
messageTaskRequest.setSubject("看看改不改");
messageTaskRequest.setUseDefaultSubject(false);
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/notice/message/task/save")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
@ -116,6 +118,8 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
ResultHolder resultHolder = JSON.parseObject(contentAsString, ResultHolder.class);
Assertions.assertEquals(100200, resultHolder.getCode());
messageTaskRequest = new MessageTaskRequest();
messageTaskRequest.setProjectId("project-message-test");
messageTaskRequest.setTaskType(NoticeConstants.TaskType.API_DEFINITION_TASK);
@ -123,8 +127,6 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
userIds = new ArrayList<>();
userIds.add("project-message-user-1");
messageTaskRequest.setReceiverIds(userIds);
messageTaskRequest.setRobotId("test_message_robot2");
messageTaskRequest.setEnable(true);
mockMvc.perform(MockMvcRequestBuilders.post("/notice/message/task/save")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
@ -136,6 +138,13 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
Assertions.assertEquals(100200, resultHolder.getCode());
MessageTaskExample messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andReceiverEqualTo("project-message-user-1")
.andProjectIdEqualTo("project-message-test").andProjectRobotIdEqualTo("test_message_robot2").andTaskTypeEqualTo(NoticeConstants.TaskType.API_DEFINITION_TASK).andEventEqualTo(NoticeConstants.Event.CREATE);
List<MessageTask> messageTasks1 = messageTaskMapper.selectByExample(messageTaskExample);
Assertions.assertTrue(messageTasks1.get(0).getEnable());
Assertions.assertTrue(StringUtils.equalsIgnoreCase(messageTasks1.get(0).getSubject(), "看看改不改"));
messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andProjectIdEqualTo("project-message-test").andTaskTypeEqualTo(NoticeConstants.TaskType.API_DEFINITION_TASK).andEventEqualTo(NoticeConstants.Event.CREATE);
List<MessageTask> messageTasks = messageTaskMapper.selectByExample(messageTaskExample);
Assertions.assertEquals(1, messageTasks.size());
@ -314,16 +323,6 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
System.out.println(messageTaskDetailDTOList);
}
@Test
@Order(9)
public void getMessageListProjectFail() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/notice/message/task/get/project-message-test-x")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken))
.andExpect(status().is5xxServerError())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
@Test
@Order(10)
public void getMessageListEmpty() throws Exception {