From 62f1154740f21a49ff7cb4071a393f8a46680348 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Wed, 25 Oct 2023 17:58:23 +0900 Subject: [PATCH 1/5] feat: add is-allow-notifications field --- .../java/org/swmaestro/repl/gifthub/auth/entity/User.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/entity/User.java b/src/main/java/org/swmaestro/repl/gifthub/auth/entity/User.java index b0b274b3..5e5060df 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/auth/entity/User.java +++ b/src/main/java/org/swmaestro/repl/gifthub/auth/entity/User.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Collection; +import org.hibernate.annotations.ColumnDefault; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; @@ -49,13 +50,18 @@ public class User extends BaseTimeEntity implements UserDetails { @Column(columnDefinition = "TINYINT", nullable = false) private Role role; + @Column(columnDefinition = "TINYINT", nullable = false) + @ColumnDefault("1") + private boolean isAllowNotifications; + @Builder - public User(Long id, String username, String password, String nickname, Role role) { + public User(Long id, String username, String password, String nickname, Role role, boolean isAllowNotifications) { this.id = id; this.username = username; this.password = password; this.nickname = nickname; this.role = role; + this.isAllowNotifications = isAllowNotifications; } @Override From cd8443388c3c83b09db439ff2f0948a19a3c2eee Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Wed, 25 Oct 2023 18:05:47 +0900 Subject: [PATCH 2/5] fix: set inital true --- .../java/org/swmaestro/repl/gifthub/auth/entity/User.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/entity/User.java b/src/main/java/org/swmaestro/repl/gifthub/auth/entity/User.java index 5e5060df..2a275b08 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/auth/entity/User.java +++ b/src/main/java/org/swmaestro/repl/gifthub/auth/entity/User.java @@ -55,13 +55,13 @@ public class User extends BaseTimeEntity implements UserDetails { private boolean isAllowNotifications; @Builder - public User(Long id, String username, String password, String nickname, Role role, boolean isAllowNotifications) { + public User(Long id, String username, String password, String nickname, Role role) { this.id = id; this.username = username; this.password = password; this.nickname = nickname; this.role = role; - this.isAllowNotifications = isAllowNotifications; + this.isAllowNotifications = true; } @Override From 56f91da0b28da23461a6c634dfed38d92be4658d Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Wed, 25 Oct 2023 18:06:40 +0900 Subject: [PATCH 3/5] feat: implement allow and deny expiration notifications method --- .../gifthub/auth/service/UserService.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java b/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java index 190026f8..b4e87df5 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java @@ -191,16 +191,27 @@ public UserInfoResponseDto readInfo(String username) { .username(username) .nickname(user.getNickname()) .oauth(oAuthService.list(user)) - .allowNotifications(isExistDeviceToken(user)) + .allowNotifications(user.isAllowNotifications()) .anonymous(user.isAnonymous()) .build(); return userInfoResponseDto; } /** - * DeviceToken 조회 메서드 (user) + * 알림 동의 여부 허용 메서드 (user) */ - public boolean isExistDeviceToken(User user) { - return !deviceTokenRepository.findAllByUser(user).isEmpty(); + public void allowNotifications(String username) { + User user = read(username); + user.setAllowNotifications(true); + userRepository.save(user); + } + + /** + * 알림 동의 여부 거부 메서드 (user) + */ + public void denyNotifications(String username) { + User user = read(username); + user.setAllowNotifications(false); + userRepository.save(user); } } \ No newline at end of file From 2412d4202a0820b1bee293566660c65aadf40bdf Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Wed, 25 Oct 2023 18:07:17 +0900 Subject: [PATCH 4/5] feat: implement allow and deny expiration norifications api --- .../controller/NotificationController.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/main/java/org/swmaestro/repl/gifthub/notifications/controller/NotificationController.java b/src/main/java/org/swmaestro/repl/gifthub/notifications/controller/NotificationController.java index eaa28dea..2564a6be 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/notifications/controller/NotificationController.java +++ b/src/main/java/org/swmaestro/repl/gifthub/notifications/controller/NotificationController.java @@ -129,4 +129,38 @@ public ResponseEntity sendNotification( .path(request.getRequestURI()) .build()); } + + @PostMapping("/expiration/allow") + @Operation(summary = "만료 알림 수신 허용 메서드", description = "만료 알림 수신을 허용하기 위한 메서드입니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "만료 알림 수신 허용 성공"), + @ApiResponse(responseCode = "400(404)", description = "존재하지 않는 회원") + }) + public ResponseEntity allowExpirationNotification( + HttpServletRequest request, + @RequestHeader("Authorization") String accessToken) { + String username = jwtProvider.getUsername(accessToken.substring(7)); + userService.allowNotifications(username); + return ResponseEntity.ok( + SuccessMessage.builder() + .path(request.getRequestURI()) + .build()); + } + + @PostMapping("/expiration/deny") + @Operation(summary = "만료 알림 수신 거부 메서드", description = "만료 알림 수신을 거부하기 위한 메서드입니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "만료 알림 수신 거부 성공"), + @ApiResponse(responseCode = "400(404)", description = "존재하지 않는 회원") + }) + public ResponseEntity denyExpirationNotification( + HttpServletRequest request, + @RequestHeader("Authorization") String accessToken) { + String username = jwtProvider.getUsername(accessToken.substring(7)); + userService.denyNotifications(username); + return ResponseEntity.ok( + SuccessMessage.builder() + .path(request.getRequestURI()) + .build()); + } } From 0a655255d9d1f674c65507e619341e436c0ac4c6 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Wed, 25 Oct 2023 18:07:50 +0900 Subject: [PATCH 5/5] test: implement allow and deny expiration notifications api test --- .../NotificationControllerTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/test/java/org/swmaestro/repl/gifthub/notifications/controller/NotificationControllerTest.java b/src/test/java/org/swmaestro/repl/gifthub/notifications/controller/NotificationControllerTest.java index 77542fbf..c4b01131 100644 --- a/src/test/java/org/swmaestro/repl/gifthub/notifications/controller/NotificationControllerTest.java +++ b/src/test/java/org/swmaestro/repl/gifthub/notifications/controller/NotificationControllerTest.java @@ -142,4 +142,41 @@ void deleteDeviceToken() throws Exception { .content(objectMapper.writeValueAsString(deviceTokenRequestDto))) .andExpect(status().isOk()); } + + /** + * 만료 알림 수신 허용 테스트 + */ + @Test + @WithMockUser(username = "이진우", roles = "USER") + void allowNotifications() throws Exception { + String accessToken = "my.access.token"; + String username = "이진우"; + User user = User.builder().username(username).build(); + + when(jwtProvider.resolveToken(any())).thenReturn(accessToken); + when(jwtProvider.getUsername(anyString())).thenReturn(username); + when(userService.read(username)).thenReturn(user); + + mockMvc.perform(post("/notifications/expiration/allow").header("Authorization", "Bearer " + accessToken)) + .andExpect(status().isOk()); + } + + /** + * 만료 알림 수신 거부 테스트 + */ + @Test + @WithMockUser(username = "이진우", roles = "USER") + void denyNotifications() throws Exception { + String accessToken = "my.access.token"; + String username = "이진우"; + User user = User.builder().username(username).build(); + + when(jwtProvider.resolveToken(any())).thenReturn(accessToken); + when(jwtProvider.getUsername(anyString())).thenReturn(username); + when(userService.read(username)).thenReturn(user); + + mockMvc.perform(post("/notifications/expiration/deny").header("Authorization", "Bearer " + accessToken)) + .andExpect(status().isOk()); + } + }