Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 알림을 확인 할 수 있는 알림센터 기능 추가 #392

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,19 @@ public void sendMissionRemindPush(PushMissionRemindRequest request) {
Instant.now().plusSeconds(request.seconds()));
}

public List<NotificationFindAllResponse> findAllNotification() {
final Member member = memberUtil.getCurrentMember();
return notificationRepository
.findAllByTargetMemberIdOrderByCreatedAtDesc(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> findAllByTargetMemberIdOrderByCreatedAtDesc(Long targetMemberId);
}
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);
}
}
Comment on lines +8 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

notificationType과 createdAt만 DTO로 정하신 이유가 있나요??
source나 targetId로 필요한 데이터가 있지 않을까해서용

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

알림에 대한 후속 액션이 없고
단순 뷰로 보여주기위한 기획이여서
요대로 작성했숨니당

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
Loading