Skip to content

Commit

Permalink
Merge pull request #89 from TelePigeon/feat/88
Browse files Browse the repository at this point in the history
[feat] Worry Cron 추가
  • Loading branch information
minwoo0419 authored Jun 7, 2024
2 parents 56f7a26 + 9e3ef56 commit e24aad9
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/telepigeon/server/domain/Worry.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.telepigeon.server.domain;

import com.telepigeon.server.dto.fcm.FcmMessageDto;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
Expand Down Expand Up @@ -52,4 +53,12 @@ public static Worry create(
) {
return new Worry(name, content, times, profile);
}

public FcmMessageDto toFcmMessageDto() {
return FcmMessageDto.builder()
.title(this.name)
.body(this.content)
.type("worry")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import com.telepigeon.server.domain.Profile;
import com.telepigeon.server.domain.Worry;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface WorryRepository extends JpaRepository<Worry, Long> {
List<Worry> findAllByProfile(Profile profile);

@Query("SELECT w FROM Worry w WHERE w.times LIKE %:time%")
List<Worry> findAllByTime(String time);
}
32 changes: 31 additions & 1 deletion src/main/java/com/telepigeon/server/service/ScheduleService.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
package com.telepigeon.server.service;

import com.telepigeon.server.domain.Profile;
import com.telepigeon.server.domain.Worry;
import com.telepigeon.server.service.external.FcmService;
import com.telepigeon.server.service.question.QuestionService;
import com.telepigeon.server.service.worry.WorryRetriever;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.telepigeon.server.service.profile.ProfileRetriever;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class ScheduleService {
private final QuestionService questionService;
private final ProfileRetriever profileRetriever;
private final WorryRetriever worryRetriever;
private final FcmService fcmService;

@Scheduled(cron="0 0 12 * * *")
public void createSchedule(){
public void createSchedule() {
for (Profile profile : profileRetriever.findAll()) {
try {
questionService.create(profile);
Expand All @@ -25,4 +34,25 @@ public void createSchedule(){
}
}
}

@Scheduled(cron="0 0 * * * *")
public void sendWorries() {
String currentHour = LocalTime.now().format(DateTimeFormatter.ofPattern("HH시"));
List<Worry> worries = worryRetriever.findAllByTime(currentHour);
for (Worry worry : worries) {
try {
sendWorry(worry);
} catch (Exception e) {
log.error("Failed to send worry for worry {}", worry.getId());
}
}
}

private void sendWorry(Worry worry) {
Profile profile = profileRetriever.findByOpponentProfile(worry.getProfile());
fcmService.send(
profile.getUser().getFcmToken(),
worry.toFcmMessageDto()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.telepigeon.server.dto.room.response.RoomStateDto;
import com.telepigeon.server.dto.type.FcmContent;
import com.telepigeon.server.service.external.S3Service;
import com.telepigeon.server.service.fcm.FcmService;
import com.telepigeon.server.service.external.FcmService;
import com.telepigeon.server.service.external.NaverCloudService;
import com.telepigeon.server.service.user.UserRetriever;
import com.telepigeon.server.service.hurry.HurryRetriever;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.telepigeon.server.service.fcm;
package com.telepigeon.server.service.external;

import com.google.firebase.messaging.*;
import com.telepigeon.server.dto.fcm.FcmMessageDto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.telepigeon.server.dto.type.FcmContent;
import com.telepigeon.server.exception.BusinessException;
import com.telepigeon.server.exception.code.BusinessErrorCode;
import com.telepigeon.server.service.fcm.FcmService;
import com.telepigeon.server.service.external.FcmService;
import com.telepigeon.server.service.profile.ProfileRetriever;
import com.telepigeon.server.service.room.RoomRetriever;
import com.telepigeon.server.service.user.UserRetriever;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public Profile findByUserNotAndRoom(final User user, final Room room) {
.orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND));
}

public Profile findByOpponentProfile(final Profile profile) {
return profileRepository.findByUserNotAndRoom(profile.getUser(), profile.getRoom())
.orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND));
}

public boolean existsByUserNotAndRoom(final User user, final Room room) {
return profileRepository.existsByUserNotAndRoom(user, room);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.telepigeon.server.exception.code.BusinessErrorCode;
import com.telepigeon.server.exception.code.NotFoundErrorCode;
import com.telepigeon.server.service.answer.AnswerRetriever;
import com.telepigeon.server.service.fcm.FcmService;
import com.telepigeon.server.service.external.FcmService;
import com.telepigeon.server.service.hurry.HurryRemover;
import com.telepigeon.server.service.hurry.HurryRetriever;
import com.telepigeon.server.service.openAi.OpenAiService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.telepigeon.server.repository.RoomRepository;
import com.telepigeon.server.service.answer.AnswerRemover;
import com.telepigeon.server.service.answer.AnswerRetriever;
import com.telepigeon.server.service.fcm.FcmService;
import com.telepigeon.server.service.external.FcmService;
import com.telepigeon.server.service.profile.ProfileRemover;
import com.telepigeon.server.service.profile.ProfileRetriever;
import com.telepigeon.server.service.profile.ProfileSaver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public Worry findById(Long worryId) {
()-> new NotFoundException(NotFoundErrorCode.WORRY_NOT_FOUND)
);
}

public List<Worry> findAllByTime(String time) {
return worryRepository.findAllByTime(time);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//import com.telepigeon.server.exception.BusinessException;
//import com.telepigeon.server.exception.NotFoundException;
//import com.telepigeon.server.service.answer.AnswerRetriever;
//import com.telepigeon.server.service.fcm.FcmService;
//import com.telepigeon.server.service.external.FcmService;
//import com.telepigeon.server.service.hurry.HurryRetriever;
//import com.telepigeon.server.service.openAi.OpenAiService;
//import com.telepigeon.server.service.profile.ProfileRetriever;
Expand Down

0 comments on commit e24aad9

Please sign in to comment.