From 4eeb48c3f38e1361a821a7f4edbd8f82c20e7675 Mon Sep 17 00:00:00 2001 From: yummygyudon Date: Sat, 18 Jan 2025 19:56:20 +0900 Subject: [PATCH] =?UTF-8?q?[CHORE]=20=EC=BD=94=EB=93=9C=20=EC=8A=A4?= =?UTF-8?q?=ED=83=80=EC=9D=BC=20=EB=A6=AC=EB=B7=B0=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [참고 PR](https://github.com/sopt-makers/sopt-operation-backend/pull/306) --- .../web/alarm/service/AlarmServiceImpl.java | 6 ++++-- .../lecture/service/WebLectureServiceImpl.java | 10 ++++++---- .../client/alarm/InstantAlarmSender.java | 18 +++++++++--------- .../client/alarm/ScheduleAlarmSender.java | 9 +++++---- .../client/alarm/dto/InstantAlarmRequest.java | 1 + 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/operation-api/src/main/java/org/sopt/makers/operation/web/alarm/service/AlarmServiceImpl.java b/operation-api/src/main/java/org/sopt/makers/operation/web/alarm/service/AlarmServiceImpl.java index e90a6a1e..908f5d29 100644 --- a/operation-api/src/main/java/org/sopt/makers/operation/web/alarm/service/AlarmServiceImpl.java +++ b/operation-api/src/main/java/org/sopt/makers/operation/web/alarm/service/AlarmServiceImpl.java @@ -47,12 +47,13 @@ public class AlarmServiceImpl implements AlarmService { public AlarmCreateResponse sendInstantAlarm(AlarmInstantSendRequest request) { val alarm = request.toEntity(); val alarmTarget = alarm.getTarget(); + alarmTarget.setTargetIds(extractTargetIds(alarmTarget)); - val savedAlarm = alarmRepository.save(alarm); + val savedAlarm = alarmRepository.save(alarm); val alarmRequest = InstantAlarmRequest.of(savedAlarm); - alarmManager.sendInstant(alarmRequest); + alarmManager.sendInstant(alarmRequest); return AlarmCreateResponse.of(savedAlarm); } @@ -93,6 +94,7 @@ public void deleteAlarm(long alarmId) { } @Override + @Transactional public void updateScheduleAlarm(long alarmId, AlarmScheduleStatusUpdateRequest request) { val alarm = findAlarm(alarmId); alarm.updateStatusToComplete(request.sendAt()); diff --git a/operation-api/src/main/java/org/sopt/makers/operation/web/lecture/service/WebLectureServiceImpl.java b/operation-api/src/main/java/org/sopt/makers/operation/web/lecture/service/WebLectureServiceImpl.java index 4208070e..21b9360a 100644 --- a/operation-api/src/main/java/org/sopt/makers/operation/web/lecture/service/WebLectureServiceImpl.java +++ b/operation-api/src/main/java/org/sopt/makers/operation/web/lecture/service/WebLectureServiceImpl.java @@ -40,6 +40,8 @@ @RequiredArgsConstructor @Transactional(readOnly = true) public class WebLectureServiceImpl implements WebLectureService { + public static final String NULL = "null"; + public static final String WHITE_SPACE = " "; private final LectureRepository lectureRepository; private final SubLectureRepository subLectureRepository; @@ -47,7 +49,7 @@ public class WebLectureServiceImpl implements WebLectureService { private final SubAttendanceRepository subAttendanceRepository; private final MemberRepository memberRepository; - private final AlarmManager alarmSender; + private final AlarmManager alarmManager; private final ValueConfig valueConfig; @Override @@ -177,14 +179,14 @@ private Lecture getLectureReadyToEnd(long lectureId) { } private void sendAlarm(Lecture lecture) { - val alarmMessageTitle = lecture.getName() + " " + valueConfig.getALARM_MESSAGE_TITLE(); + val alarmMessageTitle = String.join(WHITE_SPACE, lecture.getName(), valueConfig.getALARM_MESSAGE_TITLE()); val alarmMessageContent = valueConfig.getALARM_MESSAGE_CONTENT(); val targets = lecture.getAttendances().stream() .map(attendance -> String.valueOf(attendance.getMember().getPlaygroundId())) - .filter(id -> !id.equals("null")) + .filter(id -> !id.equals(NULL)) .toList(); val alarmRequest = InstantAlarmRequest.of(alarmMessageTitle, alarmMessageContent, targets); - alarmSender.sendInstant(alarmRequest); + alarmManager.sendInstant(alarmRequest); } private Lecture getLectureToDelete(long lectureId) { diff --git a/operation-external/src/main/java/org/sopt/makers/operation/client/alarm/InstantAlarmSender.java b/operation-external/src/main/java/org/sopt/makers/operation/client/alarm/InstantAlarmSender.java index 24ad1698..db24b6b0 100644 --- a/operation-external/src/main/java/org/sopt/makers/operation/client/alarm/InstantAlarmSender.java +++ b/operation-external/src/main/java/org/sopt/makers/operation/client/alarm/InstantAlarmSender.java @@ -31,7 +31,7 @@ class InstantAlarmSender implements AlarmSender{ @Override public void sendAlarm(AlarmRequest alarmRequest) { - InstantAlarmRequest instantRequest = (InstantAlarmRequest) alarmRequest; + val instantRequest = (InstantAlarmRequest) alarmRequest; try { val host = valueConfig.getNOTIFICATION_URL(); val body = generateBody(instantRequest); @@ -46,10 +46,8 @@ public void sendAlarm(AlarmRequest alarmRequest) { private Map generateBody(InstantAlarmRequest instantRequest) { val body = new HashMap<>(); - putRequiredAttributes(instantRequest, body); putOptionalAttributes(instantRequest, body); - return body; } @@ -60,13 +58,16 @@ private static void putRequiredAttributes(InstantAlarmRequest instantRequest, Ha } private static void putOptionalAttributes(InstantAlarmRequest instantRequest, HashMap body) { - if (!instantRequest.targetType().equals(AlarmTargetType.ALL)) { + val isTargetAll = instantRequest.targetType().equals(AlarmTargetType.ALL); + val isWebLink = instantRequest.linkType().equals(AlarmLinkType.WEB); + val isAppLink = instantRequest.linkType().equals(AlarmLinkType.APP); + + if (!isTargetAll) { body.put("userIds", instantRequest.targets()); } - - if (instantRequest.linkType().equals(AlarmLinkType.WEB)) { + if (isWebLink) { body.put("webLink", instantRequest.link()); - } else if (instantRequest.linkType().equals(AlarmLinkType.APP)) { + } else if (isAppLink) { body.put("appLink", instantRequest.link()); } } @@ -74,16 +75,15 @@ private static void putOptionalAttributes(InstantAlarmRequest instantRequest, Ha private HttpHeaders generateHeader(InstantAlarmRequest instantRequest) { val headers = new HttpHeaders(); val apiKey = valueConfig.getNOTIFICATION_KEY(); + val actionValue = instantRequest.targetType().getAction().getValue(); headers.setContentType(APPLICATION_JSON); headers.setAccept(Collections.singletonList(APPLICATION_JSON)); - String actionValue = instantRequest.targetType().getAction().getValue(); headers.add("action", actionValue); headers.add("transactionId", randomUUID().toString()); headers.add("service", "operation"); headers.add("x-api-key", apiKey); - return headers; } diff --git a/operation-external/src/main/java/org/sopt/makers/operation/client/alarm/ScheduleAlarmSender.java b/operation-external/src/main/java/org/sopt/makers/operation/client/alarm/ScheduleAlarmSender.java index fca21f99..5b2979c3 100644 --- a/operation-external/src/main/java/org/sopt/makers/operation/client/alarm/ScheduleAlarmSender.java +++ b/operation-external/src/main/java/org/sopt/makers/operation/client/alarm/ScheduleAlarmSender.java @@ -28,6 +28,9 @@ import java.time.format.DateTimeFormatter; import java.util.UUID; +import static org.sopt.makers.operation.constant.AlarmConstant.ALARM_REQUEST_DATE_FORMAT; +import static org.sopt.makers.operation.constant.AlarmConstant.ALARM_REQUEST_TIME_FORMAT; + @Component @RequiredArgsConstructor class ScheduleAlarmSender implements AlarmSender{ @@ -52,7 +55,6 @@ public void sendAlarm(AlarmRequest alarmRequest) { try { val name = generateEventName(scheduleRequest); val cronExpression = generateScheduleCronExpression(scheduleRequest); - val eventJson = generateEventJson(scheduleRequest); val target = generateEventTarget(eventJson); @@ -85,9 +87,8 @@ private Target generateEventTarget(String eventJson) { } private String generateEventName(ScheduleAlarmRequest request) { - val dateData = request.scheduleDateTime().toLocalDate().format(DateTimeFormatter.ISO_DATE); - val timeData = request.scheduleDateTime().toLocalTime().format(DateTimeFormatter.ofPattern("HH-mm")); - + val dateData = request.scheduleDateTime().toLocalDate().format(DateTimeFormatter.ofPattern(ALARM_REQUEST_DATE_FORMAT)); + val timeData = request.scheduleDateTime().toLocalTime().format(DateTimeFormatter.ofPattern(ALARM_REQUEST_TIME_FORMAT)); return String.format("%s_%s_%d", dateData, timeData, request.alarmId()); } diff --git a/operation-external/src/main/java/org/sopt/makers/operation/client/alarm/dto/InstantAlarmRequest.java b/operation-external/src/main/java/org/sopt/makers/operation/client/alarm/dto/InstantAlarmRequest.java index 848f995c..aeaec3da 100644 --- a/operation-external/src/main/java/org/sopt/makers/operation/client/alarm/dto/InstantAlarmRequest.java +++ b/operation-external/src/main/java/org/sopt/makers/operation/client/alarm/dto/InstantAlarmRequest.java @@ -23,6 +23,7 @@ public record InstantAlarmRequest( AlarmLinkType linkType ) implements AlarmRequest { + public static InstantAlarmRequest of(Alarm alarm) { val content = alarm.getContent(); return InstantAlarmRequest.builder()