-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #205 from PawWithU/feat/204-scheduling-notification
- Loading branch information
Showing
10 changed files
with
315 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
src/main/java/com/pawwithu/connectdog/domain/scheduler/SchedulerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package com.pawwithu.connectdog.domain.scheduler; | ||
|
||
import com.pawwithu.connectdog.domain.application.entity.Application; | ||
import com.pawwithu.connectdog.domain.application.repository.CustomApplicationRepository; | ||
import com.pawwithu.connectdog.domain.fcm.entity.IntermediaryFcm; | ||
import com.pawwithu.connectdog.domain.fcm.entity.VolunteerFcm; | ||
import com.pawwithu.connectdog.domain.fcm.repository.IntermediaryFcmRepository; | ||
import com.pawwithu.connectdog.domain.fcm.repository.VolunteerFcmRepository; | ||
import com.pawwithu.connectdog.domain.fcm.service.FcmService; | ||
import com.pawwithu.connectdog.domain.notification.entity.NotificationType; | ||
import com.pawwithu.connectdog.domain.post.entity.Post; | ||
import com.pawwithu.connectdog.domain.post.repository.CustomPostRepository; | ||
import com.pawwithu.connectdog.domain.volunteer.entity.Volunteer; | ||
import com.pawwithu.connectdog.domain.volunteer.repository.CustomVolunteerRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
import static com.pawwithu.connectdog.domain.fcm.dto.NotificationMessage.*; | ||
|
||
@Slf4j | ||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class SchedulerService { | ||
|
||
private final CustomPostRepository customPostRepository; | ||
private final CustomApplicationRepository customApplicationRepository; | ||
private final FcmService fcmService; | ||
private final VolunteerFcmRepository volunteerFcmRepository; | ||
private final IntermediaryFcmRepository intermediaryFcmRepository; | ||
private final CustomVolunteerRepository customVolunteerRepository; | ||
|
||
// 매일 00시 - 공고 모집 마감 업데이트, 신청 자동 반려 | ||
@Scheduled(cron = "0 0 0 * * *") | ||
public void updateExpiredPostsAndApplications() { | ||
LocalDate today = LocalDate.now(); | ||
// 공고 모집 마감 업데이트 | ||
customPostRepository.updateExpiredPosts(today); | ||
// 신청 자동 반려 | ||
customApplicationRepository.updateExpiredApplications(today); | ||
} | ||
|
||
// 매일 9시 - 모집 마감된 공고를 신청했던 봉사자에게 반려 알림 전송 및 모집자에게 공고 모집 기간 만료 알림 전송 | ||
@Scheduled(cron = "0 0 9 * * *") | ||
public void sendRejectNotification() { | ||
LocalDate today = LocalDate.now(); | ||
LocalDate yesterday = today.minusDays(1); | ||
// 모집 마감된 공고를 신청했던 봉사자에게 반려 알림 전송 | ||
List<Application> applications = customApplicationRepository.getYesterdayExpiredApplications(yesterday); | ||
for (Application application : applications) { | ||
VolunteerFcm volunteerFcm = volunteerFcmRepository.findByVolunteerId(application.getVolunteer().getId()).orElse(null); | ||
if (volunteerFcm != null) { | ||
fcmService.sendMessageToVolunteer(volunteerFcm.getFcmToken(), application.getVolunteer(), | ||
application.getPost().getMainImage().getImage(), NotificationType.REJECTED, EXPIRED_REJECT.getTitle(), EXPIRED_REJECT.getBody()); | ||
} else { | ||
log.info("----------모집 마감 공고 신청 반려 알림 전송 실패----------"); | ||
} | ||
} | ||
// 모집자에게 모집 기간 만료 알림 전송 | ||
List<Post> posts = customPostRepository.getYesterdayExpiredPosts(yesterday); | ||
for (Post post : posts) { | ||
IntermediaryFcm intermediaryFcm = intermediaryFcmRepository.findByIntermediaryId(post.getIntermediary().getId()).orElse(null); | ||
if (intermediaryFcm != null) { | ||
fcmService.sendMessageToIntermediary(intermediaryFcm.getFcmToken(), post.getIntermediary(), post.getMainImage().getImage(), | ||
NotificationType.EXPIRED, EXPIRED.getTitle(), EXPIRED.getBody()); | ||
} else { | ||
log.info("----------공고 마감 사전 알림 전송 실패----------"); | ||
} | ||
} | ||
|
||
} | ||
|
||
// 매일 오후 12시 - 공고 모집 마감 알림 12시간 전 알림 | ||
@Scheduled(cron = "0 0 12 * * *") | ||
public void sendBeforeExpiredNotification() { | ||
LocalDate today = LocalDate.now(); | ||
// 모집 마감 하루 전 모집중 공고 알림 전송 | ||
List<Post> recruitingPosts = customPostRepository.getBeforeExpiredRecruitingPosts(today); | ||
for (Post post : recruitingPosts) { | ||
IntermediaryFcm intermediaryFcm = intermediaryFcmRepository.findByIntermediaryId(post.getIntermediary().getId()).orElse(null); | ||
if (intermediaryFcm != null) { | ||
fcmService.sendMessageToIntermediary(intermediaryFcm.getFcmToken(), post.getIntermediary(), post.getMainImage().getImage(), | ||
NotificationType.BEFORE_EXPIRED, BEFORE_EXPIRED.getTitle(), BEFORE_EXPIRED.getBodyWithContent(" 아직 봉사자를 구하지 못했다면 기간을 조정해보세요!")); | ||
} else { | ||
log.info("----------공고 마감 사전 알림 전송 실패----------"); | ||
} | ||
} | ||
// 모집 마감 하루 전 승인대기중 공고 알림 전송 | ||
List<Post> waitingPosts = customPostRepository.getBeforeExpiredWaitingPosts(today); | ||
for (Post post : waitingPosts) { | ||
IntermediaryFcm intermediaryFcm = intermediaryFcmRepository.findByIntermediaryId(post.getIntermediary().getId()).orElse(null); | ||
if (intermediaryFcm != null) { | ||
fcmService.sendMessageToIntermediary(intermediaryFcm.getFcmToken(), post.getIntermediary(), post.getMainImage().getImage(), | ||
NotificationType.BEFORE_EXPIRED, BEFORE_EXPIRED.getTitle(), BEFORE_EXPIRED.getBodyWithContent(" 신청자가 있으니 빠르게 확인해주세요!")); | ||
} else { | ||
log.info("----------공고 마감 사전 알림 전송 실패----------"); | ||
} | ||
} | ||
} | ||
|
||
// 매일 오전 10시 - 이동봉사 진행 완료 요청 알림 | ||
@Scheduled(cron = "0 0 10 * * *") | ||
public void sendCompleteRequestNotification() { | ||
LocalDate today = LocalDate.now(); | ||
LocalDate yesterday = today.minusDays(1); | ||
List<Application> applications = customApplicationRepository.getExpiredProgressingPosts(yesterday); | ||
for (Application application : applications) { | ||
IntermediaryFcm intermediaryFcm = intermediaryFcmRepository.findByIntermediaryId(application.getPost().getIntermediary().getId()).orElse(null); | ||
if (intermediaryFcm != null) { | ||
fcmService.sendMessageToIntermediary(intermediaryFcm.getFcmToken(), application.getIntermediary(), application.getPost().getMainImage().getImage(), | ||
NotificationType.COMPLETED_REQUEST, COMPLETED_REQUEST.getTitle(), COMPLETED_REQUEST.getBody()); | ||
} else { | ||
log.info("----------이동봉사 진행 완료 요청 알림 전송 실패----------"); | ||
} | ||
} | ||
} | ||
|
||
// 매일 15시 - 이동봉사 가이드 알림 | ||
@Scheduled(cron = "0 0 15 * * *") | ||
public void sendGuideNotification() { | ||
LocalDate today = LocalDate.now(); | ||
LocalDate yesterday = today.minusDays(1); | ||
List<Volunteer> volunteers = customVolunteerRepository.getYesterdaySignUpVolunteers(yesterday); | ||
for (Volunteer volunteer : volunteers) { | ||
VolunteerFcm volunteerFcm = volunteerFcmRepository.findByVolunteerId(volunteer.getId()).orElse(null); | ||
if (volunteerFcm != null) { | ||
fcmService.sendMessageToVolunteer(volunteerFcm.getFcmToken(), volunteer, | ||
volunteer.getProfileImageNum() + "", NotificationType.GUIDE, GUIDE.getTitle(), GUIDE.getBody()); | ||
} else { | ||
log.info("----------이동봉사 가이드 알림 전송 실패----------"); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...n/java/com/pawwithu/connectdog/domain/volunteer/repository/CustomVolunteerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.pawwithu.connectdog.domain.volunteer.repository; | ||
|
||
import com.pawwithu.connectdog.domain.volunteer.entity.Volunteer; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
public interface CustomVolunteerRepository { | ||
|
||
List<Volunteer> getYesterdaySignUpVolunteers(LocalDate date); | ||
} |
32 changes: 32 additions & 0 deletions
32
...m/pawwithu/connectdog/domain/volunteer/repository/impl/CustomVolunteerRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.pawwithu.connectdog.domain.volunteer.repository.impl; | ||
|
||
import com.pawwithu.connectdog.domain.volunteer.entity.Volunteer; | ||
import com.pawwithu.connectdog.domain.volunteer.repository.CustomVolunteerRepository; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.util.List; | ||
|
||
import static com.pawwithu.connectdog.domain.volunteer.entity.QVolunteer.volunteer; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class CustomVolunteerRepositoryImpl implements CustomVolunteerRepository { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public List<Volunteer> getYesterdaySignUpVolunteers(LocalDate date) { | ||
LocalDateTime startOfDay = date.atStartOfDay(); | ||
LocalDateTime endOfDay = date.atTime(LocalTime.MAX); | ||
return queryFactory.selectFrom(volunteer) | ||
.where(volunteer.createdDate.between(startOfDay, endOfDay)) | ||
.fetch(); | ||
} | ||
} |
Oops, something went wrong.