Skip to content
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
Expand Up @@ -393,6 +393,9 @@ public enum SwaggerResponseDescription {
NOTIFICATION_NOT_FOUND,
NOTIFICATION_ACCESS_FORBIDDEN
))),
NOTIFICATION_MARK_ALL_TO_CHECKED(new LinkedHashSet<>(Set.of(

))),

;
private final Set<ErrorCode> errorCodeList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,16 @@ public BaseResponse<NotificationMarkToCheckedResponse> markNotificationToChecked
@Parameter(hidden = true) @UserId final Long userId) {
return BaseResponse.ok(notificationMarkUseCase.markToChecked(request.notificationId(), userId));
}

@Operation(
summary = "유저의 모든 알림 읽음 처리",
description = "유저의 읽지 않은 모든 알림을 읽음 처리합니다."
)
@ExceptionDescription(NOTIFICATION_MARK_ALL_TO_CHECKED)
@PostMapping("/notifications/check-all")
public BaseResponse<Void> markAllNotificationsToChecked(
@Parameter(hidden = true) @UserId final Long userId) {
notificationMarkUseCase.markAllToChecked(userId);
return BaseResponse.ok(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ public void update(Notification notification) {

notificationJpaEntity.updateFrom(notification);
}

@Override
public void markAllAsCheckedByUserId(Long userId) {
notificationJpaRepository.markAllAsCheckedByUserId(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

import konkuk.thip.notification.adapter.out.jpa.NotificationJpaEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface NotificationJpaRepository extends JpaRepository<NotificationJpaEntity, Long>, NotificationQueryRepository {

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query("UPDATE NotificationJpaEntity n SET n.isChecked = true " +
"WHERE n.userJpaEntity.userId = :userId AND n.isChecked = false")
int markAllAsCheckedByUserId(@Param("userId") Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
public interface NotificationMarkUseCase {

NotificationMarkToCheckedResponse markToChecked(Long notificationId, Long userId);

void markAllToChecked(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ default Notification getByIdOrThrow(Long id) {
}

void update(Notification notification);

void markAllAsCheckedByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ public NotificationMarkToCheckedResponse markToChecked(Long notificationId, Long
notification.getRedirectSpec().params()
);
}

@Override
@Transactional
public void markAllToChecked(Long userId) {
notificationCommandPort.markAllAsCheckedByUserId(userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package konkuk.thip.notification.adapter.in.web;

import konkuk.thip.common.util.TestEntityFactory;
import konkuk.thip.notification.adapter.out.jpa.NotificationJpaEntity;
import konkuk.thip.notification.adapter.out.persistence.repository.NotificationJpaRepository;
import konkuk.thip.notification.domain.value.NotificationCategory;
import konkuk.thip.user.adapter.out.jpa.UserJpaEntity;
import konkuk.thip.user.adapter.out.persistence.repository.UserJpaRepository;
import konkuk.thip.user.domain.value.Alias;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@ActiveProfiles("test")
@Transactional
@AutoConfigureMockMvc(addFilters = false)
@DisplayName("[통합] 알림 모두 읽음 처리 api 통합 테스트")
class NotificationMarkAllToCheckedApiTest {

@Autowired private MockMvc mockMvc;

@Autowired private UserJpaRepository userJpaRepository;
@Autowired private NotificationJpaRepository notificationJpaRepository;

@Test
@DisplayName("본인의 읽지 않은 모든 알림이 읽음 처리되고, 다른 유저의 알림은 영향받지 않는다.")
void mark_all_notifications_to_checked_success() throws Exception {
// given
UserJpaEntity owner = userJpaRepository.save(TestEntityFactory.createUser(Alias.WRITER));
UserJpaEntity other = userJpaRepository.save(TestEntityFactory.createUser(Alias.WRITER));

NotificationJpaEntity unchecked1 = notificationJpaRepository.save(
TestEntityFactory.createNotification(owner, "알림1", NotificationCategory.FEED));
NotificationJpaEntity unchecked2 = notificationJpaRepository.save(
TestEntityFactory.createNotification(owner, "알림2", NotificationCategory.FEED));
NotificationJpaEntity otherUserNotification = notificationJpaRepository.save(
TestEntityFactory.createNotification(other, "다른 유저 알림", NotificationCategory.FEED));

// when & then
mockMvc.perform(post("/notifications/check-all")
.requestAttr("userId", owner.getUserId()))
.andExpect(status().isOk());

assertThat(notificationJpaRepository.findById(unchecked1.getNotificationId()).orElseThrow().isChecked()).isTrue();
assertThat(notificationJpaRepository.findById(unchecked2.getNotificationId()).orElseThrow().isChecked()).isTrue();
assertThat(notificationJpaRepository.findById(otherUserNotification.getNotificationId()).orElseThrow().isChecked()).isFalse();
}

@Test
@DisplayName("읽지 않은 알림이 없는 경우에도 에러 없이 성공한다.")
void mark_all_notifications_to_checked_when_none_unchecked() throws Exception {
// given
UserJpaEntity owner = userJpaRepository.save(TestEntityFactory.createUser(Alias.WRITER));

// when & then
mockMvc.perform(post("/notifications/check-all")
.requestAttr("userId", owner.getUserId()))
.andExpect(status().isOk());
}
}
Loading