Skip to content

Commit

Permalink
feat: 그룹 기도제목 기도 / 스크랩 시에 알림 발송 추가 (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sion99 authored Apr 15, 2024
2 parents bcbc5f4 + cb10f78 commit 57c4729
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.uspray.uspray.DTO.group.response.GroupListResponseDto;
import com.uspray.uspray.DTO.group.response.GroupMemberResponseDto;
import com.uspray.uspray.exception.SuccessStatus;
import com.uspray.uspray.service.GroupFacade;
import com.uspray.uspray.service.facade.GroupFacade;
import com.uspray.uspray.service.GroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.uspray.uspray.DTO.pray.request.PrayToGroupPrayDto;
import com.uspray.uspray.DTO.pray.response.PrayResponseDto;
import com.uspray.uspray.exception.SuccessStatus;
import com.uspray.uspray.service.GroupPrayFacade;
import com.uspray.uspray.service.facade.GroupPrayFacade;
import com.uspray.uspray.service.GroupPrayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.uspray.uspray.DTO.pray.request.PrayRequestDto;
import com.uspray.uspray.DTO.pray.response.PrayResponseDto;
import com.uspray.uspray.exception.SuccessStatus;
import com.uspray.uspray.service.HistoryFacade;
import com.uspray.uspray.service.facade.HistoryFacade;
import com.uspray.uspray.service.HistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.uspray.uspray.DTO.pray.request.PrayUpdateRequestDto;
import com.uspray.uspray.DTO.pray.response.PrayResponseDto;
import com.uspray.uspray.exception.SuccessStatus;
import com.uspray.uspray.service.PrayFacade;
import com.uspray.uspray.service.facade.PrayFacade;
import com.uspray.uspray.service.PrayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.uspray.uspray.DTO.sharedpray.request.SharedPraySaveRequestDto;
import com.uspray.uspray.DTO.sharedpray.response.SharedPrayResponseDto;
import com.uspray.uspray.exception.SuccessStatus;
import com.uspray.uspray.service.ShareFacade;
import com.uspray.uspray.service.facade.ShareFacade;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.uspray.uspray.domain.Member;
import com.uspray.uspray.exception.ErrorStatus;
import com.uspray.uspray.exception.model.NotFoundException;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

Expand All @@ -17,6 +18,8 @@ public interface GroupMemberRepository extends JpaRepository<GroupMember, Long>

Optional<GroupMember> findByGroupIdAndMemberId(Long groupId, Long memberId);

List<GroupMember> findByGroupId(Long groupId);

default GroupMember getGroupMemberByGroupAndMember(Group group, Member member) {
return this.findByGroupAndMember(group, member).orElseThrow(
() -> new NotFoundException(ErrorStatus.NOT_FOUND_GROUP_MEMBER_EXCEPTION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.uspray.uspray.infrastructure.CategoryRepository;
import com.uspray.uspray.infrastructure.MemberRepository;
import com.uspray.uspray.infrastructure.PrayRepository;
import com.uspray.uspray.service.facade.PrayFacade;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.uspray.uspray.Enums.NotificationType;
import com.uspray.uspray.infrastructure.query.MemberQueryRepository;
import com.uspray.uspray.service.facade.PrayFacade;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uspray.uspray.service;
package com.uspray.uspray.service.facade;

import com.uspray.uspray.DTO.group.request.GroupRequestDto;
import com.uspray.uspray.domain.Group;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uspray.uspray.service;
package com.uspray.uspray.service.facade;

import com.uspray.uspray.DTO.grouppray.GroupPrayRappingDto;
import com.uspray.uspray.DTO.grouppray.GroupPrayRequestDto;
Expand All @@ -12,6 +12,7 @@
import com.uspray.uspray.domain.GroupMember;
import com.uspray.uspray.domain.GroupPray;
import com.uspray.uspray.domain.Member;
import com.uspray.uspray.domain.NotificationLog;
import com.uspray.uspray.domain.Pray;
import com.uspray.uspray.domain.ScrapAndHeart;
import com.uspray.uspray.exception.ErrorStatus;
Expand All @@ -21,8 +22,10 @@
import com.uspray.uspray.infrastructure.GroupPrayRepository;
import com.uspray.uspray.infrastructure.GroupRepository;
import com.uspray.uspray.infrastructure.MemberRepository;
import com.uspray.uspray.infrastructure.NotificationLogRepository;
import com.uspray.uspray.infrastructure.PrayRepository;
import com.uspray.uspray.infrastructure.ScrapAndHeartRepository;
import com.uspray.uspray.service.FCMNotificationService;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Base64;
Expand All @@ -32,9 +35,11 @@
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
public class GroupPrayFacade {
Expand All @@ -46,6 +51,8 @@ public class GroupPrayFacade {
private final CategoryRepository categoryRepository;
private final PrayRepository prayRepository;
private final GroupRepository groupRepository;
private final NotificationLogRepository notificationLogRepository;
private final FCMNotificationService fcmNotificationService;

@Transactional
public void prayToGroupPray(PrayToGroupPrayDto prayToGroupPrayDto, String userId) {
Expand Down Expand Up @@ -183,16 +190,20 @@ public void heartGroupPray(Long groupPrayId, String userId) {
Optional<ScrapAndHeart> scrapAndHeartByGroupPrayAndMember = scrapAndHeartRepository.findScrapAndHeartByGroupPrayAndMember(
groupPray, member);

List<GroupMember> groupMembers = groupMemberRepository.findByGroupId(groupPray.getGroup().getId());

if (scrapAndHeartByGroupPrayAndMember.isEmpty()) {
ScrapAndHeart scrapAndHeart = ScrapAndHeart.builder()
.groupPray(groupPray)
.member(member)
.build();
scrapAndHeart.heartPray();
scrapAndHeartRepository.save(scrapAndHeart);
sendNotificationAndSaveLog(scrapAndHeart, groupPray, groupPray.getAuthor(), true);
return;
}
scrapAndHeartByGroupPrayAndMember.get().heartPray();
sendNotificationAndSaveLog(scrapAndHeartByGroupPrayAndMember.get(), groupPray, groupPray.getAuthor(), true);
}

@Transactional
Expand Down Expand Up @@ -222,11 +233,13 @@ public void scrapGroupPray(ScrapRequestDto scrapRequestDto, String userId) {
prayRepository.save(pray);
scrapAndHeart.scrapPray(pray);
scrapAndHeartRepository.save(scrapAndHeart);
sendNotificationAndSaveLog(scrapAndHeart, groupPray, groupPray.getAuthor(), false);
return;
}
Pray pray = makePray(scrapRequestDto, groupPray, member);
prayRepository.save(pray);
scrapAndHeartByGroupPrayAndMember.get().scrapPray(pray);
sendNotificationAndSaveLog(scrapAndHeartByGroupPrayAndMember.get(), groupPray, groupPray.getAuthor(), false);
}

private Pray makePray(ScrapRequestDto scrapRequestDto, GroupPray groupPray, Member member) {
Expand All @@ -244,4 +257,47 @@ private Pray makePray(ScrapRequestDto scrapRequestDto, GroupPray groupPray, Memb
.prayType(PrayType.SHARED)
.build();
}

private void sendNotificationAndSaveLog(ScrapAndHeart scrapAndHeart, GroupPray groupPray, Member receiver, boolean isHeart) {
String groupName = groupPray.getGroup().getName();
String name = scrapAndHeart.getMember().getName();
if (isHeart) {
try {
fcmNotificationService.sendMessageTo(
receiver.getFirebaseToken(),
groupName + " 💘",
name + "님이 당신의 기도제목을 두고 기도했어요");
} catch (Exception e) {
log.error(e.getMessage());
}
log.error(
"send notification to " + memberRepository.getMemberByUserId(receiver.getUserId())
);
NotificationLog notificationLog = NotificationLog.builder()
.pray(groupPray.getOriginPray())
.member(memberRepository.getMemberByUserId(receiver.getUserId()))
.title(name + "님이 당신의 기도제목을 두고 기도했어요")
.build();
notificationLogRepository.save(notificationLog);
return;
}
try {
fcmNotificationService.sendMessageTo(
receiver.getFirebaseToken(),
groupName + " 💌 ",
name + "님이 당신의 기도제목을 저장했어요");
} catch (Exception e) {
log.error(e.getMessage());
}
log.error(
"send notification to " + memberRepository.getMemberByUserId(receiver.getUserId())
);
NotificationLog notificationLog = NotificationLog.builder()
.pray(groupPray.getOriginPray())
.member(memberRepository.getMemberByUserId(receiver.getUserId()))
.title(name + "님이 당신의 기도제목을 저장했어요")
.build();
notificationLogRepository.save(notificationLog);

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uspray.uspray.service;
package com.uspray.uspray.service.facade;

import com.uspray.uspray.DTO.pray.request.PrayRequestDto;
import com.uspray.uspray.DTO.pray.response.PrayResponseDto;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uspray.uspray.service;
package com.uspray.uspray.service.facade;

import com.uspray.uspray.DTO.pray.PrayListResponseDto;
import com.uspray.uspray.DTO.pray.request.PrayRequestDto;
Expand All @@ -20,6 +20,7 @@
import com.uspray.uspray.infrastructure.NotificationLogRepository;
import com.uspray.uspray.infrastructure.PrayRepository;
import com.uspray.uspray.infrastructure.ScrapAndHeartRepository;
import com.uspray.uspray.service.FCMNotificationService;
import java.time.LocalDate;
import java.util.List;
import javax.transaction.Transactional;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uspray.uspray.service;
package com.uspray.uspray.service.facade;

import com.uspray.uspray.DTO.sharedpray.request.SharedPrayDeleteRequestDto;
import com.uspray.uspray.DTO.sharedpray.request.SharedPrayRequestDto;
Expand All @@ -19,6 +19,7 @@
import com.uspray.uspray.infrastructure.NotificationLogRepository;
import com.uspray.uspray.infrastructure.PrayRepository;
import com.uspray.uspray.infrastructure.SharedPrayRepository;
import com.uspray.uspray.service.FCMNotificationService;
import java.time.LocalDate;
import java.util.Base64;
import java.util.List;
Expand Down

0 comments on commit 57c4729

Please sign in to comment.