Skip to content

Commit

Permalink
Roll back to dev version : String Notification ID 적용 버전
Browse files Browse the repository at this point in the history
  • Loading branch information
yummygyudon committed Dec 5, 2023
1 parent d926f2a commit 3cedfcc
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public Notification findNotification(User user, String notificationId) {
return notificationRepository.findByNotificationIdAndUserId(notificationId, user.getId())
.orElseThrow(() -> new BadRequestException(ErrorCode.NOTIFICATION_NOT_FOUND.getMessage()));
}
@Transactional(readOnly = true)
@Deprecated
public Notification findNotificationDeprecated(User user, Long notificationId) {
return notificationRepository.findByIdAndUserId(notificationId, user.getId())
.orElseThrow(() -> new BadRequestException(ErrorCode.NOTIFICATION_NOT_FOUND.getMessage()));
}

@Transactional(readOnly = true)
public List<Notification> findNotificationList(User user, Pageable pageable) {
Expand Down Expand Up @@ -78,6 +84,30 @@ public void updateNotificationIsRead(User user, String notificationId) {
updateSingleNotificationIsRead(user, notificationId);
}
}
@Transactional
@Deprecated
public void updateNotificationIsReadDeprecated(User user, Long notificationId) {
if (Objects.isNull(notificationId)) {
updateAllNotificationIsRead(user);
} else {
updateSingleNotificationIsReadDeprecated(user, notificationId);
}
}

private void updateSingleNotificationIsRead(User user, String notificationId) {
val notification = notificationRepository.findByNotificationIdAndUserId(notificationId, user.getId())
.orElseThrow(() -> new BadRequestException(ErrorCode.NOTIFICATION_NOT_FOUND.getMessage()));
notification.updateIsRead();
notificationRepository.save(notification);
}

@Deprecated
private void updateSingleNotificationIsReadDeprecated(User user, Long notificationId) {
val notification = notificationRepository.findByIdAndUserId(notificationId, user.getId())
.orElseThrow(() -> new BadRequestException(ErrorCode.NOTIFICATION_NOT_FOUND.getMessage()));
notification.updateIsRead();
notificationRepository.save(notification);
}

private void updateAllNotificationIsRead(User user) {
val notificationList = notificationRepository.findAllByUserId(user.getId());
Expand All @@ -90,13 +120,6 @@ private void updateAllNotificationIsRead(User user) {
notificationRepository.saveAll(readNotificationList);
}

private void updateSingleNotificationIsRead(User user, String notificationId) {
val notification = notificationRepository.findByNotificationIdAndUserId(notificationId, user.getId())
.orElseThrow(() -> new BadRequestException(ErrorCode.NOTIFICATION_NOT_FOUND.getMessage()));
notification.updateIsRead();
notificationRepository.save(notification);
}

@Transactional(readOnly = true)
public Boolean getNotificationConfirmStatus(User user) {
val notificationList = notificationRepository.findAllByUserId(user.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public class PushTokenService {
private String apiKey;


@Transactional(readOnly = true)
public boolean isExistDeviceToken(Long userId, String token) {
return pushTokenRepository.existsByUserIdAndToken(userId, token);
}

@Transactional(readOnly = true)
public PushToken getDeviceToken(Long userId, String token) {
return pushTokenRepository.findByUserIdAndToken(userId, token)
Expand Down Expand Up @@ -98,19 +103,16 @@ public PushTokenResponse.StatusResponse deleteDeviceToken(PushToken pushToken) {
}

@Transactional
public Integer deleteAllDeviceTokenOf(User user) {
public void deleteAllDeviceTokenOf(User user) {
// 기존에 저장되어 있던 Tokens -> 알림 서버에 삭제 요청
List<PushToken> userTokens = pushTokenRepository.findAllByUserId(user.getId());
int failedCount = 0;
for (PushToken token : userTokens) {
PushTokenResponse.StatusResponse statusResponse = deleteDeviceToken(token);
if (!statusResponse.getSuccess()) {
failedCount += 1;
if (!userTokens.isEmpty()) {
for (PushToken token : userTokens) {
deleteDeviceToken(token);
}
// 우선 서비스 DB에 있는 모든 토큰 지워버리기
pushTokenRepository.deleteAll(userTokens);
}
// 우선 서비스 DB에 있는 모든 토큰 지워버리기
pushTokenRepository.deleteAll(userTokens);
return failedCount;
}

private HttpHeaders createHeadersFor(String action, String platform) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public interface NotificationRepository extends JpaRepository<Notification, Long

List<Notification> findAllByUserId(Long userId, Pageable pageable);

Optional<Notification> findByIdAndUserId(Long id, Long userId);
Optional<Notification> findByNotificationIdAndUserId(String notificationId, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class NotificationController {
@ApiResponse(responseCode = "200", description = "success"),
@ApiResponse(responseCode = "500", description = "server error", content = @Content)
})
@GetMapping(value = "")
@GetMapping(value = "/all")
public ResponseEntity<List<NotificationResponse.NotificationSimple>> findNotificationList(
@AuthenticationPrincipal User user,
@PageableDefault(size = 10, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable
Expand All @@ -62,16 +62,16 @@ public ResponseEntity<List<NotificationResponse.NotificationSimple>> findNotific
@ApiResponse(responseCode = "200", description = "success"),
@ApiResponse(responseCode = "500", description = "server error", content = @Content)
})
@GetMapping(value = "/{notificationId}")
@GetMapping(value = "/detail/{notificationId}")
public ResponseEntity<NotificationResponse.NotificationDetail> findNotificationDetail(
@AuthenticationPrincipal User user,
@PathVariable("notificationId") String notificationId
) {
val result = notificationService.findNotification(user, notificationId);
return ResponseEntity.status(HttpStatus.OK).body(
NotificationResponse.NotificationDetail.of(
notificationId,
user.getId(),
result.getNotificationId(),
result.getUserId(),
result.getTitle(),
result.getContent(),
result.getDeepLink(),
Expand Down Expand Up @@ -103,7 +103,7 @@ public ResponseEntity registerNotification(
@ApiResponse(responseCode = "500", description = "server error", content = @Content)
})
@PatchMapping(value = {
"/{notificationId}", ""
"/read/{notificationId}", "/read"
})
public ResponseEntity<NotificationDetail> updateNotificationIsRead(
@AuthenticationPrincipal User user,
Expand All @@ -113,4 +113,72 @@ public ResponseEntity<NotificationDetail> updateNotificationIsRead(
return ResponseEntity.status(HttpStatus.OK).body(null);
}



@Operation(summary = "알림 목록 조회 - DEPRECATED")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "success"),
@ApiResponse(responseCode = "500", description = "server error", content = @Content)
})
@GetMapping(value = "")
@Deprecated
public ResponseEntity<List<NotificationResponse.NotificationSimpleDeprecated>> findNotificationListDeprecated(
@AuthenticationPrincipal User user,
@PageableDefault(size = 10, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable
) {
val result = notificationService.findNotificationList(user, pageable);
return ResponseEntity.status(HttpStatus.OK).body(
result.stream()
.map((notification) -> NotificationResponse.NotificationSimpleDeprecated.of(
notification.getId()
, notification.getUserId()
, notification.getTitle()
, notification.getContent()
, notification.getCategory().name()
, notification.getIsRead()
, notification.getCreatedAt()
)).toList());
}
@Operation(summary = "알림 상세 조회 - DEPRECATED")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "success"),
@ApiResponse(responseCode = "500", description = "server error", content = @Content)
})
@GetMapping(value = "/{notificationId}")
@Deprecated
public ResponseEntity<NotificationResponse.NotificationDetailDeprecated> findNotificationDetailDeprecated(
@AuthenticationPrincipal User user,
@PathVariable("notificationId") Long notificationId
) {
val result = notificationService.findNotificationDeprecated(user, notificationId);
return ResponseEntity.status(HttpStatus.OK).body(
NotificationResponse.NotificationDetailDeprecated.of(
result.getId(),
result.getUserId(),
result.getTitle(),
result.getContent(),
result.getDeepLink(),
result.getWebLink(),
result.getCreatedAt(),
result.getUpdatedAt()
)
);
}

@Operation(summary = "알림 읽음 여부 변경 - DEPRECATED")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "success"),
@ApiResponse(responseCode = "500", description = "server error", content = @Content)
})
@PatchMapping(value = {
"/{notificationId}", ""
})
@Deprecated
public ResponseEntity<NotificationDetail> updateNotificationIsReadDeprecated(
@AuthenticationPrincipal User user,
@PathVariable(name = "notificationId", required = false) Long notificationId
) {
notificationService.updateNotificationIsReadDeprecated(user, notificationId);
return ResponseEntity.status(HttpStatus.OK).body(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,89 @@ public static NotificationConfirmStatus of(
);
}
}

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@ToString
@Deprecated
public static class NotificationSimpleDeprecated {

@Schema(description = "알림 아이디", example = "1")
private Long id;

@Schema(description = "앱 유저 아이디", example = "1")
private Long userId;

@Schema(description = "알림 제목", example = "공지다!")
private String title;

@Schema(description = "알림 내용", example = "공지 내용은 앱팀 최고입니다.")
private String content;

@Schema(description = "알림 카테고리", example = "NOTICE")
private String category;

@Schema(description = "알림 읽음 여부", example = "true")
private Boolean isRead;

@Schema(description = "알림 생성 일시", example = "2023-03-29T18:39:42.106369")
private LocalDateTime createdAt;

public static NotificationSimpleDeprecated of(
Long notificationId
, Long userId
, String title
, String content
, String category
, Boolean isRead
, LocalDateTime createdAt
) {
return new NotificationSimpleDeprecated(
notificationId, userId, title, content, category, isRead, createdAt
);
}
}

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@ToString
@Deprecated
public static class NotificationDetailDeprecated {

@Schema(description = "알림 아이디", example = "1")
private Long id;
@Schema(description = "유저 아이디", example = "1")
private Long userId;
@Schema(description = "알림 제목", example = "공지다!")
private String title;
@Schema(description = "알림 내용", example = "공지 내용은 앱팀 최고입니다.")
private String content;
@Schema(description = "알림 첨부 딥링크")
private String deepLink;
@Schema(description = "알림 첨부 웹링크")
private String webLink;
@Schema(description = "알림 생성 일시", example = "2023-03-29T18:39:42.106369")
private LocalDateTime createdAt;
@Schema(description = "알림 수정 일시", example = "2023-03-29T18:39:42.106369")
private LocalDateTime updatedAt;

public static NotificationDetailDeprecated of(
Long notificationId
, Long userId
, String title
, String content
, String deepLink
, String webLink
, LocalDateTime createdAt
, LocalDateTime updatedAt
) {
return new NotificationDetailDeprecated(
notificationId, userId, title, content, deepLink, webLink, createdAt, updatedAt
);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ public ResponseEntity<PushTokenResponse.StatusResponse> deletePushToken(
@AuthenticationPrincipal User user,
@Valid @RequestBody PushTokenRequest.DeleteRequest deletePushTokenRequest
) {
PushToken targetPushToken = pushTokenService.getDeviceToken(
user.getId(), deletePushTokenRequest.getPushToken()
);
val result = pushTokenService.deleteDeviceToken(targetPushToken);
val response = pushTokenResponseMapper.ofStatus(result);
return ResponseEntity.status(HttpStatus.OK).body(response);
if (pushTokenService.isExistDeviceToken(user.getId(), deletePushTokenRequest.getPushToken())) {
PushToken targetPushToken = pushTokenService.getDeviceToken(
user.getId(), deletePushTokenRequest.getPushToken()
);
pushTokenService.deleteDeviceToken(targetPushToken);
}
return ResponseEntity.status(HttpStatus.OK).body(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public ResponseEntity<PushTokenResponse.StatusResponse> logout(
@AuthenticationPrincipal User user,
@Valid @RequestBody PushTokenRequest.DeleteRequest deleteRequest
) {
PushToken targetPushToken = pushTokenService.getDeviceToken(
user.getId(), deleteRequest.getPushToken()
);
// val result =
pushTokenService.deleteDeviceToken(targetPushToken);
// val response = pushTokenResponseMapper.ofStatus(result);
if (pushTokenService.isExistDeviceToken(user.getId(), deleteRequest.getPushToken())) {
PushToken targetPushToken = pushTokenService.getDeviceToken(
user.getId(), deleteRequest.getPushToken()
);
pushTokenService.deleteDeviceToken(targetPushToken);
}
return ResponseEntity.status(HttpStatus.OK).body(null);
}

Expand Down

0 comments on commit 3cedfcc

Please sign in to comment.