diff --git a/src/main/java/com/dreamypatisiel/devdevdev/domain/repository/notification/NotificationRepository.java b/src/main/java/com/dreamypatisiel/devdevdev/domain/repository/notification/NotificationRepository.java index a8959b78..31d71ca5 100644 --- a/src/main/java/com/dreamypatisiel/devdevdev/domain/repository/notification/NotificationRepository.java +++ b/src/main/java/com/dreamypatisiel/devdevdev/domain/repository/notification/NotificationRepository.java @@ -13,4 +13,6 @@ public interface NotificationRepository extends JpaRepository findAllByMemberId(Long memberId); Long countByMemberAndIsReadIsFalse(Member member); + + void deleteAllByMember(Member member); } diff --git a/src/main/java/com/dreamypatisiel/devdevdev/domain/service/notification/NotificationService.java b/src/main/java/com/dreamypatisiel/devdevdev/domain/service/notification/NotificationService.java index d01f99c9..f95ffd14 100644 --- a/src/main/java/com/dreamypatisiel/devdevdev/domain/service/notification/NotificationService.java +++ b/src/main/java/com/dreamypatisiel/devdevdev/domain/service/notification/NotificationService.java @@ -367,4 +367,17 @@ public Long getUnreadNotificationCount(Authentication authentication) { // 회원이 읽지 않은 알림 개수 조회 return notificationRepository.countByMemberAndIsReadFalse(findMember); } + + /** + * @Note: 회원의 모든 알림 삭제 + * @Author: 유소영 + */ + @Transactional + public void deleteAllByMember(Authentication authentication) { + // 회원 조회 + Member findMember = memberProvider.getMemberByAuthentication(authentication); + + // 회원의 모든 알림 삭제 + notificationRepository.deleteAllByMember(findMember); + } } diff --git a/src/main/java/com/dreamypatisiel/devdevdev/test/TestController.java b/src/main/java/com/dreamypatisiel/devdevdev/test/TestController.java index a84f0a7a..294e7929 100644 --- a/src/main/java/com/dreamypatisiel/devdevdev/test/TestController.java +++ b/src/main/java/com/dreamypatisiel/devdevdev/test/TestController.java @@ -1,9 +1,14 @@ package com.dreamypatisiel.devdevdev.test; import com.dreamypatisiel.devdevdev.domain.repository.pick.PickRepository; +import com.dreamypatisiel.devdevdev.domain.service.notification.NotificationService; +import com.dreamypatisiel.devdevdev.global.utils.AuthenticationMemberUtils; import com.dreamypatisiel.devdevdev.openai.embeddings.EmbeddingRequestHandler; import com.dreamypatisiel.devdevdev.openai.embeddings.EmbeddingsService; import java.util.List; + +import com.dreamypatisiel.devdevdev.web.dto.response.BasicResponse; +import io.swagger.v3.oas.annotations.Operation; import lombok.Data; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -12,6 +17,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -27,6 +33,15 @@ public class TestController { private final EmbeddingRequestHandler embeddingRequestHandler; private final EmbeddingsService embeddingsService; private final PickRepository pickRepository; + private final NotificationService notificationService; + + @Operation(summary = "알림 제거", description = "회원에게 생성된 모든 알림을 제거") + @DeleteMapping("/notifications") + public ResponseEntity> delete() { + Authentication authentication = AuthenticationMemberUtils.getAuthentication(); + notificationService.deleteAllByMember(authentication); + return ResponseEntity.ok(com.dreamypatisiel.devdevdev.web.dto.response.BasicResponse.success()); + } @GetMapping("/members") public BasicResponse getMembers() { diff --git a/src/main/java/com/dreamypatisiel/devdevdev/web/controller/notification/NotificationController.java b/src/main/java/com/dreamypatisiel/devdevdev/web/controller/notification/NotificationController.java index 4eb9250e..d42a4a61 100644 --- a/src/main/java/com/dreamypatisiel/devdevdev/web/controller/notification/NotificationController.java +++ b/src/main/java/com/dreamypatisiel/devdevdev/web/controller/notification/NotificationController.java @@ -25,8 +25,6 @@ import org.springframework.web.bind.annotation.*; import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.security.core.Authentication; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; diff --git a/src/test/java/com/dreamypatisiel/devdevdev/domain/service/notification/NotificationServiceTest.java b/src/test/java/com/dreamypatisiel/devdevdev/domain/service/notification/NotificationServiceTest.java index 7fcad7b4..5ba792d3 100644 --- a/src/test/java/com/dreamypatisiel/devdevdev/domain/service/notification/NotificationServiceTest.java +++ b/src/test/java/com/dreamypatisiel/devdevdev/domain/service/notification/NotificationServiceTest.java @@ -609,6 +609,53 @@ void getUnreadNotificationCount() { assertThat(response).isEqualTo(7); } + @Test + @DisplayName("회원이 알림을 전체 삭제한다.") + void deleteAllByMember() { + // given + SocialMemberDto socialMemberDto = createSocialDto( + "dreamy", "꿈빛파티시엘", "행복한 꿈빛", "pass123", "dreamy@kakao.com", "KAKAO", "ROLE_USER" + ); + Member member = Member.createMemberBy(socialMemberDto); + memberRepository.save(member); + + UserPrincipal principal = UserPrincipal.createByMember(member); + SecurityContext context = SecurityContextHolder.getContext(); + context.setAuthentication(new OAuth2AuthenticationToken( + principal, principal.getAuthorities(), principal.getSocialType().name() + )); + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + + // 알림 10개 저장 + Company company = createCompany("꿈빛 파티시엘", "https://example.com/company.png", "https://example.com", + "https://example.com"); + companyRepository.save(company); + + List techArticles = new ArrayList<>(); + List notifications = new ArrayList<>(); + + for (int i = 0; i < 10; i++) { + TechArticle techArticle = TechArticle.createTechArticle(new Title("기술블로그 제목 "+i), new Url("https://example.com"), + new Count(1L), new Count(1L), new Count(1L), new Count(1L), null, company); + + techArticles.add(techArticle); + notifications.add(createNotification(member, "알림 메시지 " + i, NotificationType.SUBSCRIPTION, false, techArticle)); + } + + techArticleRepository.saveAll(techArticles); + notificationRepository.saveAll(notifications); + + em.flush(); + em.clear(); + + // when + notificationService.deleteAllByMember(authentication); + + // then + long response = notificationService.getUnreadNotificationCount(authentication); + assertThat(response).isEqualTo(0); + } + private static Subscription createSubscription(Company company, Member member) { return Subscription.builder() .company(company)