From 08df0f8e1f64fdf0782704828a4883266115948d Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Mon, 5 Feb 2024 21:02:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BF=9D=E5=AD=98=E9=80=9A=E7=9F=A5=E4=BA=BA?= =?UTF-8?q?=E6=97=B6=E6=B6=88=E6=81=AF=E9=80=9A=E7=9F=A5=E4=BC=9A=E5=85=B3?= =?UTF-8?q?=E9=97=AD=E4=BB=A5=E5=8F=8A=E6=A8=A1=E7=89=88=E4=BC=9A=E6=9B=B4?= =?UTF-8?q?=E6=94=B9=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --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= --- .../NoticeMessageTaskController.java | 3 ++ .../service/NoticeMessageTaskService.java | 44 ++++++++----------- .../NoticeMessageTaskControllerTests.java | 25 +++++------ 3 files changed, 34 insertions(+), 38 deletions(-) diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/controller/NoticeMessageTaskController.java b/backend/services/project-management/src/main/java/io/metersphere/project/controller/NoticeMessageTaskController.java index f696b237b4..5ee2d95d32 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/controller/NoticeMessageTaskController.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/controller/NoticeMessageTaskController.java @@ -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 getMessageList(@PathVariable String projectId) throws IOException { return noticeMessageTaskService.getMessageList(projectId); } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/NoticeMessageTaskService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/NoticeMessageTaskService.java index 795903ee69..a98255052b 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/NoticeMessageTaskService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/NoticeMessageTaskService.java @@ -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 messageTasks = updateMessageTasks(messageTaskRequest, userId, mapper, blobMapper, existUserIds); //保存消息任务 List 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 existUserIds, List messageTaskReceivers) { - List 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 messageTasks = messageTaskMapper.selectByExample(messageTaskExample); List messageTaskIds = messageTasks.stream().map(MessageTask::getId).toList(); if (CollectionUtils.isEmpty(messageTasks)) { @@ -192,11 +181,15 @@ public class NoticeMessageTaskService { for (MessageTask messageTask : messageTasks) { messageTask.setUpdateTime(System.currentTimeMillis()); messageTask.setUpdateUser(userId); - messageTask.setEnable(enable); - messageTask.setUseDefaultSubject(useDefaultSubject); - messageTask.setUseDefaultTemplate(useDefaultTemplate); - if (!useDefaultSubject) { - messageTask.setSubject(messageTaskRequest.getSubject()); + if (StringUtils.isNotBlank(messageTaskRequest.getRobotId())) { + messageTask.setEnable(enable); + messageTask.setUseDefaultSubject(useDefaultSubject); + messageTask.setUseDefaultTemplate(useDefaultTemplate); + if (!messageTask.getUseDefaultSubject()) { + messageTask.setSubject(messageTaskRequest.getSubject()); + } + } else { + useDefaultTemplate = messageTask.getUseDefaultTemplate(); } mapper.updateByPrimaryKeySelective(messageTask); } @@ -204,9 +197,11 @@ public class NoticeMessageTaskService { messageTaskBlobExample.createCriteria().andIdIn(messageTaskIds); List messageTaskBlobs = messageTaskBlobMapper.selectByExample(messageTaskBlobExample); for (MessageTaskBlob messageTaskBlob : messageTaskBlobs) { - if (!useDefaultTemplate) { - messageTaskBlob.setTemplate(messageTaskRequest.getTemplate()); - blobMapper.updateByPrimaryKeySelective(messageTaskBlob); + 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 */ public List getMessageList(String projectId) throws IOException { - checkProjectExist(projectId); //获取返回数据结构 StringBuilder jsonStr = new StringBuilder(); try{ diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/NoticeMessageTaskControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/NoticeMessageTaskControllerTests.java index 03b86dcbb6..2c3596587e 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/NoticeMessageTaskControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/NoticeMessageTaskControllerTests.java @@ -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) @@ -132,10 +134,17 @@ public class NoticeMessageTaskControllerTests extends BaseTest { .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); - resultHolder = JSON.parseObject(contentAsString, ResultHolder.class); + resultHolder = JSON.parseObject(contentAsString, ResultHolder.class); 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 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 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 {