Skip to content

Commit

Permalink
feat: 알림을 확인 할 수 있는 알림센터 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kdomo committed May 9, 2024
1 parent e1a1f37 commit 2d8b72e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
package com.depromeet.domain.notification.api;

import com.depromeet.domain.notification.application.PushService;
import com.depromeet.domain.notification.application.NotificationService;
import com.depromeet.domain.notification.dto.NotificationFindAllResponse;
import com.depromeet.domain.notification.dto.request.PushMissionRemindRequest;
import com.depromeet.domain.notification.dto.request.PushUrgingSendRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "8. [알림]", description = "알림 관련 API")
@RestController
@RequestMapping("/notifications")
@RequiredArgsConstructor
public class PushController {
private final PushService pushService;
public class NotificationController {
private final NotificationService notificationService;

@Operation(summary = "알림센터 조회", description = "알림센터 목록을 조회합니다.")
@GetMapping
public List<NotificationFindAllResponse> notificationFindAll() {
return notificationService.findAllNotification();
}

@Operation(summary = "재촉하기", description = "당일 미션을 완료하지 않은 친구에게 재촉하기 Push Message를 발송합니다.")
@PostMapping("/urging")
public void urgingSend(@Valid @RequestBody PushUrgingSendRequest request) {
pushService.sendUrgingPush(request);
notificationService.sendUrgingPush(request);
}

@Operation(summary = "미션 타이머 리마인드 알림", description = "인증을 놓치는 경우에 대비하여 리마인드 알림을 전송합니다.")
@PostMapping("/missions/remind")
public void missionRemindSend(@Valid @RequestBody PushMissionRemindRequest request) {
pushService.sendMissionRemindPush(request);
notificationService.sendMissionRemindPush(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
import com.depromeet.domain.notification.dao.NotificationRepository;
import com.depromeet.domain.notification.domain.Notification;
import com.depromeet.domain.notification.domain.NotificationType;
import com.depromeet.domain.notification.dto.NotificationFindAllResponse;
import com.depromeet.domain.notification.dto.request.PushMissionRemindRequest;
import com.depromeet.domain.notification.dto.request.PushUrgingSendRequest;
import com.depromeet.global.common.constants.PushNotificationConstants;
import com.depromeet.global.error.exception.CustomException;
import com.depromeet.global.error.exception.ErrorCode;
import com.depromeet.global.util.MemberUtil;
import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.stereotype.Service;
Expand All @@ -23,7 +26,7 @@
@Service
@RequiredArgsConstructor
@Transactional
public class PushService {
public class NotificationService {
private final MemberUtil memberUtil;
private final FcmService fcmService;
private final MissionRepository missionRepository;
Expand Down Expand Up @@ -68,6 +71,17 @@ public void sendMissionRemindPush(PushMissionRemindRequest request) {
Instant.now().plusSeconds(request.seconds()));
}

public List<NotificationFindAllResponse> findAllNotification() {
final Member member = memberUtil.getCurrentMember();
return notificationRepository.findAllByTargetMemberId(member.getId()).stream()
.map(
notification ->
NotificationFindAllResponse.of(
notification.getNotificationType(),
notification.getCreatedAt()))
.collect(Collectors.toList());
}

private void validateFinishedMission(Mission mission) {
if (mission.isFinished()) {
throw new CustomException(ErrorCode.FINISHED_MISSION_URGING_NOT_ALLOWED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.depromeet.domain.notification.domain.Notification;
import com.depromeet.domain.notification.domain.NotificationType;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface NotificationRepository extends JpaRepository<Notification, Long> {
Optional<Notification> findBySourceMemberIdAndTargetMemberIdAndNotificationType(
Long sourceId, Long targetId, NotificationType notificationType);

List<Notification> findAllByTargetMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.depromeet.domain.notification.dto;

import com.depromeet.domain.mission.domain.*;
import com.depromeet.domain.notification.domain.NotificationType;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;

public record NotificationFindAllResponse(
@Schema(description = "알림 타입") NotificationType notificationType,
@Schema(description = "알림 날짜") LocalDateTime createdAt) {

public static NotificationFindAllResponse of(
NotificationType notificationType, LocalDateTime createdAt) {
return new NotificationFindAllResponse(notificationType, createdAt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

@SpringBootTest
@ActiveProfiles("test")
class PushServiceTest {
class NotificationServiceTest {
@Autowired private DatabaseCleaner databaseCleaner;

@Autowired private MemberUtil memberUtil;
Expand All @@ -56,7 +56,7 @@ class PushServiceTest {

@Autowired private MissionRecordRepository missionRecordRepository;

@Autowired private PushService pushService;
@Autowired private NotificationService notificationService;

@BeforeEach
void setUp() {
Expand All @@ -76,7 +76,7 @@ class 친구에게_미션을_재촉할_때 {
PushUrgingSendRequest request = new PushUrgingSendRequest(1L);

// when, then
assertThatThrownBy(() -> pushService.sendUrgingPush(request))
assertThatThrownBy(() -> notificationService.sendUrgingPush(request))
.isInstanceOf(CustomException.class)
.hasMessage(ErrorCode.MEMBER_NOT_FOUND.getMessage());
}
Expand All @@ -90,7 +90,7 @@ class 친구에게_미션을_재촉할_때 {
Profile.createProfile("testNickname1", "testImageUrl1")));

// when, then
assertThatThrownBy(() -> pushService.sendUrgingPush(request))
assertThatThrownBy(() -> notificationService.sendUrgingPush(request))
.isInstanceOf(CustomException.class)
.hasMessage(ErrorCode.MISSION_NOT_FOUND.getMessage());
}
Expand Down Expand Up @@ -119,7 +119,7 @@ class 친구에게_미션을_재촉할_때 {
currentMember));

// when, then
assertThatThrownBy(() -> pushService.sendUrgingPush(request))
assertThatThrownBy(() -> notificationService.sendUrgingPush(request))
.isInstanceOf(CustomException.class)
.hasMessage(ErrorCode.SELF_SENDING_NOT_ALLOWED.getMessage());
}
Expand Down Expand Up @@ -152,7 +152,7 @@ class 친구에게_미션을_재촉할_때 {
targetMember));

// when, then
assertThatThrownBy(() -> pushService.sendUrgingPush(request))
assertThatThrownBy(() -> notificationService.sendUrgingPush(request))
.isInstanceOf(CustomException.class)
.hasMessage(ErrorCode.FINISHED_MISSION_URGING_NOT_ALLOWED.getMessage());
}
Expand Down Expand Up @@ -202,7 +202,7 @@ class 친구에게_미션을_재촉할_때 {
missionRecordRepository.save(missionRecord);

// when, then
assertThatThrownBy(() -> pushService.sendUrgingPush(request))
assertThatThrownBy(() -> notificationService.sendUrgingPush(request))
.isInstanceOf(CustomException.class)
.hasMessage(ErrorCode.TODAY_COMPLETED_MISSION_SENDING_NOT_ALLOWED.getMessage());
}
Expand Down Expand Up @@ -240,7 +240,7 @@ class 친구에게_미션을_재촉할_때 {
targetMember));

// when
pushService.sendUrgingPush(request);
notificationService.sendUrgingPush(request);

// then
Optional<Notification> optionalNotification = notificationRepository.findById(1L);
Expand Down

0 comments on commit 2d8b72e

Please sign in to comment.