Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hotfix]: 푸시알림 전송 로직 변경 #131

Merged
merged 1 commit into from
May 1, 2024
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 @@ -49,6 +49,7 @@ public void sendNotificationByToken(PushNotificationRequest request) {
)
.putData("title", request.title())
.putData("body", request.body())
.putData("type", request.type())
.build();

sendMessageToFirebase(message, user.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public record PushNotificationRequest(
String title,
String body,
String image,
Map<String, String> data
Map<String, String> data,
String type
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import io.sobok.SobokSobok.notice.domain.NoticeType;
import io.sobok.SobokSobok.notice.infrastructure.NoticeQueryRepository;
import io.sobok.SobokSobok.notice.infrastructure.NoticeRepository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -57,66 +59,67 @@ public AddFriendResponse addFriend(Long userId, AddFriendRequest request) {
User receiver = UserServiceUtil.findUserById(userRepository, request.memberId());

if (friendRepository.countBySenderId(sender.getId()) >= 5 ||
friendRepository.countBySenderId(receiver.getId()) >= 5) {
friendRepository.countBySenderId(receiver.getId()) >= 5) {
throw new ConflictException(ErrorCode.EXCEEDED_FRIEND_COUNT);
}

if (noticeQueryRepository.isAlreadyFriendRequestFromSender(sender.getId(), receiver.getId())
|| noticeQueryRepository.isAlreadyFriendRequestFromSender(receiver.getId(),
sender.getId())) {
|| noticeQueryRepository.isAlreadyFriendRequestFromSender(receiver.getId(),
sender.getId())) {
throw new ConflictException(ErrorCode.ALREADY_FRIEND);
}

Notice notice = noticeRepository.save(
Notice.newInstance(
sender.getId(),
receiver.getId(),
NoticeType.FRIEND,
NoticeStatus.WAITING
)
Notice.newInstance(
sender.getId(),
receiver.getId(),
NoticeType.FRIEND,
NoticeStatus.WAITING
)
);

sendFriendRepository.save(
SendFriend.newInstance(
notice.getId(),
request.friendName()
)
SendFriend.newInstance(
notice.getId(),
request.friendName()
)
);

fcmPushService.sendNotificationByToken(PushNotificationRequest.builder()
.userId(receiver.getId())
.title(sender.getUsername() + "님이 친구를 신청했어요")
.type("notice")
.build());

return AddFriendResponse.builder()
.noticeId(notice.getId())
.senderName(sender.getUsername())
.memberName(receiver.getUsername())
.isOkay(NoticeStatus.WAITING)
.build();
.noticeId(notice.getId())
.senderName(sender.getUsername())
.memberName(receiver.getUsername())
.isOkay(NoticeStatus.WAITING)
.build();
}

@Transactional(readOnly = true)
public List<FriendListResponse> getFriendList(Long userId) {
UserServiceUtil.existsUserById(userRepository, userId);

return friendRepository.findAllBySenderId(userId)
.stream().map(friend ->
FriendListResponse.builder()
.friendId(friend.getId())
.memberId(friend.getReceiverId())
.friendName(friend.getFriendName())
.build()
).collect(Collectors.toList());
.stream().map(friend ->
FriendListResponse.builder()
.friendId(friend.getId())
.memberId(friend.getReceiverId())
.friendName(friend.getFriendName())
.build()
).collect(Collectors.toList());
}

@Transactional(noRollbackFor = {ConflictException.class})
public HandleFriendRequestResponse updateNoticeStatus(Long userId, Long noticeId,
HandleFriendRequest request) {
HandleFriendRequest request) {
UserServiceUtil.existsUserById(userRepository, userId);

Notice notice = noticeRepository.findById(noticeId)
.orElseThrow(() -> new BadRequestException(ErrorCode.BAD_REQUEST_EXCEPTION));
.orElseThrow(() -> new BadRequestException(ErrorCode.BAD_REQUEST_EXCEPTION));

if (!userId.equals(notice.getReceiverId())) {
throw new ForbiddenException(ErrorCode.FORBIDDEN_EXCEPTION);
Expand All @@ -126,7 +129,7 @@ public HandleFriendRequestResponse updateNoticeStatus(Long userId, Long noticeId
User receiver = UserServiceUtil.findUserById(userRepository, userId);

if (friendRepository.countBySenderId(userId) >= 5 ||
friendRepository.countBySenderId(sender.getId()) >= 5) {
friendRepository.countBySenderId(sender.getId()) >= 5) {
notice.updateIsOkay(NoticeStatus.REFUSE);
throw new ConflictException(ErrorCode.EXCEEDED_FRIEND_COUNT);
}
Expand All @@ -136,42 +139,44 @@ public HandleFriendRequestResponse updateNoticeStatus(Long userId, Long noticeId
if (request.isOkay() == NoticeStatus.ACCEPT) {
SendFriend sendFriend = sendFriendRepository.findByNoticeId(noticeId);
friendRepository.save(Friend.newInstance(
sender.getId(),
userId,
sendFriend.getFriendName()
sender.getId(),
userId,
sendFriend.getFriendName()
));

friendRepository.save(Friend.newInstance(
userId,
sender.getId(),
sender.getUsername()
userId,
sender.getId(),
sender.getUsername()
));

fcmPushService.sendNotificationByToken(PushNotificationRequest.builder()
.userId(sender.getId())
.title(receiver.getUsername() + "님이 친구를 수락했어요")
.type("notice")
.build());
} else {
fcmPushService.sendNotificationByToken(PushNotificationRequest.builder()
.userId(sender.getId())
.title(receiver.getUsername() + "님이 친구를 거절했어요")
.type("notice")
.build());
}

return HandleFriendRequestResponse.builder()
.noticeId(notice.getId())
.memberName(sender.getUsername())
.isOkay(request.isOkay())
.updatedAt(LocalDateTime.now())
.build();
.noticeId(notice.getId())
.memberName(sender.getUsername())
.isOkay(request.isOkay())
.updatedAt(LocalDateTime.now())
.build();
}

@Transactional
public UpdateFriendNameResponse updateFriendName(Long userId, Long friendId, UpdateFriendName request) {
UserServiceUtil.existsUserById(userRepository, userId);

Friend friend = friendRepository.findById(friendId)
.orElseThrow(() -> new ForbiddenException(ErrorCode.FORBIDDEN_EXCEPTION));
.orElseThrow(() -> new ForbiddenException(ErrorCode.FORBIDDEN_EXCEPTION));

if (!friend.getSenderId().equals(userId)) {
throw new ForbiddenException(ErrorCode.FORBIDDEN_EXCEPTION);
Expand All @@ -180,11 +185,11 @@ public UpdateFriendNameResponse updateFriendName(Long userId, Long friendId, Upd
friend.updateFriendName(request.friendName());

return UpdateFriendNameResponse.builder()
.friendId(friendId)
.userId(userId)
.memberId(friend.getReceiverId())
.friendName(request.friendName())
.build();
.friendId(friendId)
.userId(userId)
.memberId(friend.getReceiverId())
.friendName(request.friendName())
.build();
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public void completePillNotice(Long userId, Long pillId, NoticeStatus isOkay) {
fcmPushService.sendNotificationByToken(PushNotificationRequest.builder()
.userId(sender.getId())
.title(receiver.getUsername() + "님이 약 일정을 거절했어요")
.type("notice")
.build());
}

Expand All @@ -113,6 +114,7 @@ public void completePillNotice(Long userId, Long pillId, NoticeStatus isOkay) {
fcmPushService.sendNotificationByToken(PushNotificationRequest.builder()
.userId(sender.getId())
.title(receiver.getUsername() + "님이 약 일정을 수락했어요")
.type("notice")
.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public void sendPill(Long userId, Long friendId, PillRequest request) {
.userId(friendId)
.title(receiverUsername + "님이 보낸 약 일정을 확인해보세요!")
.body(pillNameSentence.toString())
.type("notice")
.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private void notifyPillSchedule(LocalDate date, String time) {
.userId(pill.getUserId())
.title("[" + pill.getPillName() + "]" + DateUtil.getKoreanTime(time))
.body("소중한 '" + user.getUsername() + "'님, 약 드실 시간이에요\n다 드시면 앱에서 체크 버튼을 눌러주세요")
.type("main")
.build()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import io.sobok.SobokSobok.sticker.ui.dto.ReceivedStickerResponse;
import io.sobok.SobokSobok.sticker.ui.dto.StickerActionResponse;
import io.sobok.SobokSobok.sticker.ui.dto.StickerResponse;

import java.util.List;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -47,18 +49,18 @@ public class StickerService {
@Transactional
public List<StickerResponse> getStickerList() {
return stickerRepository.findAll().stream().map(
sticker -> StickerResponse.builder()
.stickerId(sticker.getId())
.stickerImg(sticker.getStickerImg())
.build()
sticker -> StickerResponse.builder()
.stickerId(sticker.getId())
.stickerImg(sticker.getStickerImg())
.build()
).collect(Collectors.toList());
}

@Transactional
public StickerActionResponse sendSticker(Long userId, Long scheduleId, Long stickerId) {
UserServiceUtil.existsUserById(userRepository, userId);
PillSchedule pillSchedule = PillScheduleServiceUtil.findPillScheduleById(
pillScheduleRepository, scheduleId);
pillScheduleRepository, scheduleId);
StickerServiceUtil.existsStickerById(stickerRepository, stickerId);

if (!pillSchedule.getIsCheck()) {
Expand All @@ -77,35 +79,36 @@ public StickerActionResponse sendSticker(Long userId, Long scheduleId, Long stic
}

LikeSchedule likeSchedule = likeScheduleRepository.save(
LikeSchedule.builder()
.scheduleId(scheduleId)
.senderId(userId)
.stickerId(stickerId)
.build()
LikeSchedule.builder()
.scheduleId(scheduleId)
.senderId(userId)
.stickerId(stickerId)
.build()
);

fcmPushService.sendNotificationByToken(PushNotificationRequest.builder()
.userId(receiver.getId())
.title(senderUsername + "님이 " + pill.getPillName() + " 복약에 반응을 남겼어요!")
.body("받은 스티커를 확인해보세요")
.type("main")
.build());

return StickerActionResponse.builder()
.likeScheduleId(likeSchedule.getId())
.scheduleId(likeSchedule.getScheduleId())
.senderId(likeSchedule.getSenderId())
.stickerId(likeSchedule.getStickerId())
.createdAt(likeSchedule.getCreatedAt())
.updatedAt(likeSchedule.getUpdatedAt())
.build();
.likeScheduleId(likeSchedule.getId())
.scheduleId(likeSchedule.getScheduleId())
.senderId(likeSchedule.getSenderId())
.stickerId(likeSchedule.getStickerId())
.createdAt(likeSchedule.getCreatedAt())
.updatedAt(likeSchedule.getUpdatedAt())
.build();
}

@Transactional
public StickerActionResponse updateSendSticker(Long userId, Long likeScheduleId,
Long stickerId) {
Long stickerId) {
UserServiceUtil.existsUserById(userRepository, userId);
LikeSchedule likeSchedule = likeScheduleRepository.findById(likeScheduleId)
.orElseThrow(() -> new NotFoundException(ErrorCode.UNREGISTERED_LIKE_SCHEDULE));
.orElseThrow(() -> new NotFoundException(ErrorCode.UNREGISTERED_LIKE_SCHEDULE));
StickerServiceUtil.existsStickerById(stickerRepository, stickerId);

if (!likeSchedule.isLikeScheduleSender(userId)) {
Expand All @@ -115,20 +118,20 @@ public StickerActionResponse updateSendSticker(Long userId, Long likeScheduleId,
likeSchedule.changeSticker(stickerId);

return StickerActionResponse.builder()
.likeScheduleId(likeSchedule.getId())
.scheduleId(likeSchedule.getScheduleId())
.senderId(likeSchedule.getSenderId())
.stickerId(likeSchedule.getStickerId())
.createdAt(likeSchedule.getCreatedAt())
.updatedAt(likeSchedule.getUpdatedAt())
.build();
.likeScheduleId(likeSchedule.getId())
.scheduleId(likeSchedule.getScheduleId())
.senderId(likeSchedule.getSenderId())
.stickerId(likeSchedule.getStickerId())
.createdAt(likeSchedule.getCreatedAt())
.updatedAt(likeSchedule.getUpdatedAt())
.build();
}

@Transactional
public List<ReceivedStickerResponse> getReceivedStickerList(Long userId, Long scheduleId) {
UserServiceUtil.existsUserById(userRepository, userId);
PillSchedule pillSchedule = PillScheduleServiceUtil.findPillScheduleById(
pillScheduleRepository, scheduleId);
pillScheduleRepository, scheduleId);
Pill pill = PillServiceUtil.findPillById(pillRepository, pillSchedule.getPillId());

if (!pill.isPillUser(userId) && !friendQueryRepository.isAlreadyFriend(userId, pill.getUserId())) {
Expand Down
Loading