Skip to content

Commit

Permalink
feat: Retension 이벤트 푸시 알림 (#300)
Browse files Browse the repository at this point in the history
* feat: 이벤트 11시 푸시 알림

* feat: 이벤트 푸시 알림 밤 10시로 변경

* feat: 쿼리 수정

* feat: 알림 제목, 내용 상수화 처리
  • Loading branch information
char-yb authored Feb 9, 2024
1 parent 4255966 commit 76fb898
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.depromeet.domain.common.constants;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PushNotificationConstants {
public static final String PUSH_SERVICE_TITLE = "10MM";
public static final String PUSH_SERVICE_CONTENT = "%s님이 회원님을 팔로우하기 시작했습니다🥳";
public static final String PUSH_NON_COMPLETE_MISSION_SERVICE_CONTENT =
"아직 오늘 미션을 완료하지 않았어요! 10분 동안 빠르게 완료해볼까요?";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.depromeet.domain.follow.application;

import static com.depromeet.domain.common.constants.PushNotificationConstants.*;

import com.depromeet.domain.follow.dao.MemberRelationRepository;
import com.depromeet.domain.follow.domain.MemberRelation;
import com.depromeet.domain.follow.dto.request.FollowCreateRequest;
Expand Down Expand Up @@ -34,9 +36,6 @@ public class FollowService {
private final MemberRelationRepository memberRelationRepository;
private final FcmService fcmService;

private static final String PUSH_SERVICE_TITLE = "10MM";
private static final String PUSH_SERVICE_CONTENT = "%s님이 회원님을 팔로우하기 시작했습니다🥳";

public void createFollow(FollowCreateRequest request) {
final Member currentMember = memberUtil.getCurrentMember();
Member targetMember = getTargetMember(request.targetId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.depromeet.domain.member.application;

import static com.depromeet.domain.common.constants.PushNotificationConstants.*;

import com.depromeet.domain.auth.dao.RefreshTokenRepository;
import com.depromeet.domain.auth.dto.request.UsernameCheckRequest;
import com.depromeet.domain.follow.dao.MemberRelationRepository;
Expand All @@ -14,9 +16,11 @@
import com.depromeet.domain.member.dto.response.MemberFindOneResponse;
import com.depromeet.domain.member.dto.response.MemberSearchResponse;
import com.depromeet.domain.member.dto.response.MemberSocialInfoResponse;
import com.depromeet.global.config.fcm.FcmService;
import com.depromeet.global.error.exception.CustomException;
import com.depromeet.global.error.exception.ErrorCode;
import com.depromeet.global.util.MemberUtil;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Expand All @@ -37,6 +41,7 @@ public class MemberService {
private final RefreshTokenRepository refreshTokenRepository;
private final MemberRelationRepository memberRelationRepository;
private final MemberUtil memberUtil;
private final FcmService fcmService;

@Transactional(readOnly = true)
public MemberFindOneResponse findMemberInfo() {
Expand Down Expand Up @@ -184,6 +189,21 @@ public void updateFcmToken(UpdateFcmTokenRequest updateFcmTokenRequest) {
currentMember.updateFcmToken(currentMember.getFcmInfo(), updateFcmTokenRequest.fcmToken());
}

@Transactional(readOnly = true)
public void pushNotificationMissionRequest() {
LocalDateTime today = LocalDateTime.now();
List<Member> nonMissionNonCompletedMembers =
memberRepository.findMissionNonCompletedMembers(today);
List<String> tokenList =
nonMissionNonCompletedMembers.stream()
.map(member -> member.getFcmInfo().getFcmToken())
.toList();
if (!tokenList.isEmpty()) {
fcmService.sendGroupMessageAsync(
tokenList, PUSH_SERVICE_TITLE, PUSH_NON_COMPLETE_MISSION_SERVICE_CONTENT);
}
}

private String escapeSpecialCharacters(String nickname) {
// 여기서 특수문자를 '_'로 대체할 수 있도록 정규표현식을 활용하여 구현
return nickname == null ? "" : nickname.replaceAll("[^0-9a-zA-Z가-힣 ]", "_");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface MemberRepository extends JpaRepository<Member, Long> {
public interface MemberRepository extends JpaRepository<Member, Long>, MemberRepositoryCustom {

Optional<Member> findByOauthInfoAndStatus(OauthInfo oauthInfo, MemberStatus status);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.depromeet.domain.member.dao;

import com.depromeet.domain.member.domain.Member;
import java.time.LocalDateTime;
import java.util.List;

public interface MemberRepositoryCustom {
List<Member> findMissionNonCompletedMembers(LocalDateTime today);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.depromeet.domain.member.dao;

import static com.depromeet.domain.member.domain.QMember.*;
import static com.depromeet.domain.mission.domain.QMission.*;
import static com.depromeet.domain.missionRecord.domain.QMissionRecord.*;

import com.depromeet.domain.member.domain.Member;
import com.depromeet.domain.missionRecord.domain.ImageUploadStatus;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class MemberRepositoryImpl implements MemberRepositoryCustom {

private final JPAQueryFactory jpaQueryFactory;

@Override
public List<Member> findMissionNonCompletedMembers(LocalDateTime today) {
LocalDateTime start = today.toLocalDate().atStartOfDay();
LocalDateTime end = today.toLocalDate().atTime(23, 59, 59);
return jpaQueryFactory
.selectFrom(member)
.leftJoin(member.missions, mission)
.fetchJoin()
.leftJoin(mission.missionRecords, missionRecord)
.on(missionRecord.createdAt.between(start, end))
.where(
missionRecord
.isNull()
.or(missionRecord.uploadStatus.ne(ImageUploadStatus.COMPLETE)),
member.fcmInfo.fcmToken.isNotNull(),
mission.startedAt.loe(today),
mission.finishedAt.goe(today))
.fetch();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.depromeet.scheduler.member;

import com.depromeet.domain.member.application.MemberService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class MemberBatchScheduler {
private final MemberService memberService;

@Scheduled(cron = "0 0 22 * * *")
public void pushNotificationByMissionRequest() {
log.info("PushNotification MissionRequest execute");
memberService.pushNotificationMissionRequest();
}
}

0 comments on commit 76fb898

Please sign in to comment.