Skip to content

Commit

Permalink
Merge pull request #200 from bankidz/dev
Browse files Browse the repository at this point in the history
v0.6.0 버전 배포
  • Loading branch information
sanbonai06 authored Sep 7, 2022
2 parents e102aa1 + 36b1947 commit 97251ce
Show file tree
Hide file tree
Showing 14 changed files with 4,384 additions and 3,947 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import com.ceos.bankids.config.CommonResponse;
import com.ceos.bankids.constant.ChallengeStatus;
import com.ceos.bankids.constant.ErrorCode;
import com.ceos.bankids.domain.Challenge;
import com.ceos.bankids.domain.ChallengeUser;
import com.ceos.bankids.domain.FamilyUser;
import com.ceos.bankids.domain.Notification;
import com.ceos.bankids.domain.User;
import com.ceos.bankids.dto.AllSendNotificationDTO;
import com.ceos.bankids.dto.NotificationDTO;
import com.ceos.bankids.dto.NotificationListDTO;
import com.ceos.bankids.exception.ForbiddenException;
import com.ceos.bankids.repository.NotificationRepository;
import com.ceos.bankids.repository.UserRepository;
import com.ceos.bankids.service.ExpoNotificationServiceImpl;
import com.ceos.bankids.service.NoticeServiceImpl;
Expand All @@ -25,6 +30,7 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
Expand All @@ -36,33 +42,44 @@ public class NotificationController {
private final ExpoNotificationServiceImpl expoNotificationService;
private final NoticeServiceImpl noticeService;
private final UserRepository userRepository;
private final NotificationRepository notificationRepository;

@ApiOperation(value = "모든 유저에게 알림")
@PostMapping(value = "/all_user", produces = "application/json; charset=utf-8")
public CommonResponse<String> allSendNotification(
@RequestBody AllSendNotificationDTO allSendNotificationRequest,
@AuthenticationPrincipal User authUser) {

if (authUser.getId() != 1L) {
throw new ForbiddenException(ErrorCode.NOTICE_AUTH_ERROR.getErrorCode());
}

String title = allSendNotificationRequest.getTitle();
String message = allSendNotificationRequest.getMessage();
userRepository.findAll().stream()
.filter(user -> user.getExpoToken() != null && !Objects.equals(user.getExpoToken(),
"web"))
.forEach(user -> {
expoNotificationService.sendMessage(user, title, message,
allSendNotificationRequest.getNewMap());
if (user.getNoticeOptIn()) {
expoNotificationService.sendMessage(user, title, message,
allSendNotificationRequest.getNewMap());
} else {
Notification notification = Notification.builder().user(user).title(title)
.message(message).build();
notificationRepository.save(notification);
}
});
return CommonResponse.onSuccess("NOTIFICATION SUCCESS");
}

@ApiOperation(value = "유저 알림 리스트 가져오기")
@GetMapping(produces = "application/json; charset=utf-8")
public CommonResponse<List<NotificationDTO>> getNotificationList(
@AuthenticationPrincipal User authUser) {
public CommonResponse<NotificationListDTO> getNotificationList(
@AuthenticationPrincipal User authUser, @RequestParam(required = false) Long lastId) {

log.info("api = 유저 알림 리스트 가져오기 user = {}", authUser.getUsername());
List<NotificationDTO> notificationListDTOS = expoNotificationService.readNotificationList(
authUser);
NotificationListDTO notificationListDTOS = expoNotificationService.readNotificationList(
authUser, lastId);
return CommonResponse.onSuccess(notificationListDTOS);
}

Expand Down Expand Up @@ -92,7 +109,10 @@ public void notification(Challenge challenge, User authUser) {
HashMap<String, Object> newMap = new HashMap<>();
newMap.put("challengeId", challenge.getId());
newMap.put("userId", authUser.getId());
expoNotificationService.sendMessage(authUser, title, notificationBody, newMap);
Boolean checkServiceOptIn = checkServiceOptIn(authUser, title, notificationBody);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(authUser, title, notificationBody, newMap);
}
log.info("유저 {}의 돈길 {}의 {} 상태변경 알림", authUser.getId(), challenge.getId(),
challenge.getChallengeStatus());
}
Expand All @@ -105,7 +125,10 @@ public void userLevelUpMinusOne(User authUser) {
String notificationBody = "레벨업하기까지 \uD83D\uDD381 개\uD83D\uDD38의 돈길만 완주하면 돼요";
HashMap<String, Object> newMap = new HashMap<>();
newMap.put("userId", authUser.getId());
expoNotificationService.sendMessage(authUser, title, notificationBody, newMap);
Boolean checkServiceOptIn = checkServiceOptIn(authUser, title, notificationBody);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(authUser, title, notificationBody, newMap);
}
log.info("유저 id = {}의 레벨업 직전 알림", authUser.getId());
}

Expand All @@ -118,7 +141,10 @@ public void userLevelUpHalf(User authUser) {

HashMap<String, Object> newMap = new HashMap<>();
newMap.put("userId", authUser.getId());
expoNotificationService.sendMessage(authUser, title, notificationBody, newMap);
Boolean checkServiceOptIn = checkServiceOptIn(authUser, title, notificationBody);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(authUser, title, notificationBody, newMap);
}
log.info("유저 id = {}의 레벨업 절반 달성 알림", authUser.getId());
}

Expand All @@ -132,7 +158,10 @@ public void createPendingChallengeNotification(User contractUser, ChallengeUser
HashMap<String, Object> newMap = new HashMap<>();
newMap.put("user", challengeUser.getUser().getId());
newMap.put("challenge", challengeUser.getChallenge().getId());
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap);
Boolean checkServiceOptIn = checkServiceOptIn(contractUser, title, notificationBody);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap);
}
log.info("부모 유저 id = {}에게 유저 id = {} 돈길 id = {} 의 돈길 제안", contractUser.getId(),
challengeUser.getUser().getId(), challengeUser.getChallenge().getId());
}
Expand All @@ -147,7 +176,10 @@ public void runProgressNotification(User contractUser, ChallengeUser challengeUs
HashMap<String, Object> newMap = new HashMap<>();
newMap.put("user", challengeUser.getUser().getId());
newMap.put("challenge", challengeUser.getChallenge().getId());
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap);
Boolean checkServiceOptIn = checkServiceOptIn(contractUser, title, notificationBody);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap);
}
log.info("부모 유저 id = {}에게 유저 id = {}의 돈길 id = {} 돈길 걷기 알림 전송", contractUser.getId(),
challengeUser.getUser().getId(), challengeUser.getChallenge().getId());
}
Expand All @@ -163,7 +195,10 @@ public void achieveChallengeNotification(User contractUser, ChallengeUser challe
HashMap<String, Object> newMap = new HashMap<>();
newMap.put("user", challengeUser.getUser().getId());
newMap.put("challenge", challengeUser.getChallenge().getId());
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap);
Boolean checkServiceOptIn = checkServiceOptIn(contractUser, title, notificationBody);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap);
}
log.info("부모 유저 id = {}에게 유저 id = {}의 돈길 id = {} 돈길 완주 알림 전송", contractUser.getId(),
challengeUser.getUser().getId(), challengeUser.getChallenge().getId());
}
Expand All @@ -177,7 +212,10 @@ public void kidLevelUpNotification(User contractUser, User user, Long level, Lon
user.getUsername() + "님이 레벨" + level + "에서 레벨" + afterLevel + "로 올랐어요! 확인해볼까요?";
HashMap<String, Object> newMap = new HashMap<>();
newMap.put("user", user.getId());
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap);
Boolean checkServiceOptIn = checkServiceOptIn(contractUser, title, notificationBody);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap);
}
log.info("부모 유저 id = {}에게 유저 id = {}의 레벨업 알림 전송", contractUser.getId(), user.getId());
}

Expand All @@ -190,7 +228,10 @@ public void challengeFailedNotification(User contractUser, ChallengeUser challen
HashMap<String, Object> newMap = new HashMap<>();
newMap.put("user", challengeUser.getUser().getId());
newMap.put("challenge", challengeUser.getChallenge().getId());
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap);
Boolean checkServiceOptIn = checkServiceOptIn(contractUser, title, notificationBody);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap);
}
log.info("부모 유저 id = {}에게 유저 id = {}의 돈길 id = {} 돈길 실패 알림 전송", contractUser.getId(),
challengeUser.getChallenge().getId(), challengeUser.getChallenge().getId());
}
Expand All @@ -204,9 +245,22 @@ public void newFamilyUserNotification(User newFamilyUser, List<FamilyUser> famil
// newMap.put("user", newFamilyUser.getId());
familyUserList.forEach(familyUser -> {
User user = familyUser.getUser();
expoNotificationService.sendMessage(user, title, notificationBody, newMap);
Boolean checkServiceOptIn = checkServiceOptIn(user, title, notificationBody);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(user, title, notificationBody, newMap);
}
log.info("기존 가족 구성원 id = {}에게 유저 id = {}의 가족 참여 알림 전송", familyUser.getUser().getId(),
newFamilyUser.getId());
});
}

private Boolean checkServiceOptIn(User user, String title, String body) {
if (!user.getServiceOptIn()) {
Notification notification = Notification.builder().user(user).title(title).message(body)
.build();
notificationRepository.save(notification);
return false;
}
return true;
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/ceos/bankids/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.ceos.bankids.dto.KidBackupDTO;
import com.ceos.bankids.dto.LoginDTO;
import com.ceos.bankids.dto.MyPageDTO;
import com.ceos.bankids.dto.OptInDTO;
import com.ceos.bankids.dto.ParentBackupDTO;
import com.ceos.bankids.dto.UserDTO;
import com.ceos.bankids.service.ChallengeServiceImpl;
Expand Down Expand Up @@ -148,4 +149,26 @@ public CommonResponse<UserDTO> patchExpoToken(@AuthenticationPrincipal User auth
userService.setNewCookie(user, response);
return CommonResponse.onSuccess(null);
}

@ApiOperation(value = "유저 공지 및 이벤트 알림 동의")
@PatchMapping(value = "/notice", produces = "application/json; charset=utf-8")
@ResponseBody
public CommonResponse<OptInDTO> patchNoticeOptIn(@AuthenticationPrincipal User authUser) {

log.info("api = 유저 공지 및 이벤트 알림 동의, user = {}", authUser.getUsername());
OptInDTO optInDTO = userService.updateNoticeOptIn(authUser);

return CommonResponse.onSuccess(optInDTO);
}

@ApiOperation(value = "가족 활동 알림 동의")
@PatchMapping(value = "/action", produces = "application/json; charset=utf-8")
@ResponseBody
public CommonResponse<OptInDTO> patchActionOptIn(@AuthenticationPrincipal User authUser) {

log.info("api = 가족 활동 알림 동의, user = {}", authUser.getUsername());
OptInDTO optInDTO = userService.updateServiceOptIn(authUser);

return CommonResponse.onSuccess(optInDTO);
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/ceos/bankids/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
Expand Down Expand Up @@ -63,6 +64,14 @@ public class User extends AbstractTimestamp implements UserDetails {
@Column(columnDefinition = "TEXT")
private String expoToken;

@Column(nullable = false)
@ColumnDefault("false")
private Boolean noticeOptIn;

@Column(nullable = false)
@ColumnDefault("false")
private Boolean serviceOptIn;

@OneToOne(mappedBy = "user", fetch = FetchType.LAZY)
private Kid kid;

Expand Down Expand Up @@ -93,6 +102,8 @@ public User(
Boolean isKid,
String refreshToken,
String expoToken,
Boolean noticeOptIn,
Boolean serviceOptIn,
Parent parent,
Kid kid
) {
Expand All @@ -119,6 +130,8 @@ public User(
this.isKid = isKid;
this.refreshToken = refreshToken;
this.expoToken = expoToken;
this.noticeOptIn = noticeOptIn;
this.serviceOptIn = serviceOptIn;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ceos/bankids/dto/NotificationDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import lombok.Getter;
import lombok.ToString;

@ApiModel(value = "알림 리스트로 줄 때 DTO")
@ApiModel(value = "알림 줄 때 DTO")
@Getter
@ToString
@EqualsAndHashCode
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/ceos/bankids/dto/NotificationListDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.ceos.bankids.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

@ApiModel(value = "알림 리스트로 줄 때 DTO")
@Getter
@ToString
@EqualsAndHashCode
public class NotificationListDTO {

@ApiModelProperty(example = "6")
private Long lastId;

@ApiModelProperty(example = "list")
private List<NotificationDTO> notificationList;

public NotificationListDTO(Long lastId, List<NotificationDTO> notificationList) {
this.lastId = lastId;
this.notificationList = notificationList;
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/ceos/bankids/dto/OptInDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.ceos.bankids.dto;

import com.ceos.bankids.domain.User;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import java.sql.Timestamp;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
@EqualsAndHashCode
public class OptInDTO {

@ApiModelProperty(example = "false")
private Boolean noticeOptIn;
@ApiModelProperty(example = "true")
private Boolean serviceOptIn;
@ApiModelProperty(example = "2022/07/05 05:05:05")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy/MM/dd hh:mm:ss", timezone = "Asia/Seoul")
private Timestamp updatedAt;

public OptInDTO(User user) {
this.noticeOptIn = user.getNoticeOptIn();
this.serviceOptIn = user.getServiceOptIn();
this.updatedAt = user.getUpdatedAt();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

import com.ceos.bankids.domain.Notification;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;


public interface NotificationRepository extends
JpaRepository<Notification, Long> {

public List<Notification> findAllByUserId(Long userId);

// @Query("select n from Notification n where n.id < ")
public Page<Notification> findByIdLessThanAndUserIdOrderByIdDesc(Long id, Long userId,
Pageable pageRequest);

public Page<Notification> findByUserIdOrderByIdDesc(Long userId, Pageable pageRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import com.ceos.bankids.domain.User;
import com.ceos.bankids.dto.NotificationDTO;
import java.util.List;
import com.ceos.bankids.dto.NotificationListDTO;
import org.springframework.stereotype.Service;

@Service
public interface ExpoNotificationService {

public List<NotificationDTO> readNotificationList(User user);
public NotificationListDTO readNotificationList(User user, Long lastId);

public NotificationDTO updateNotification(User user, Long notificationId);
}
Loading

0 comments on commit 97251ce

Please sign in to comment.