Skip to content
51 changes: 31 additions & 20 deletions src/main/java/com/mr/domain/playing/service/PlayingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.mr.domain.user.entity.User;
import com.mr.domain.user.repository.UserRepository;
import com.mr.global.apipayload.exception.GeneralException;
import com.mr.global.event.PlayingCompletedEvent;
import com.mr.global.file.s3.dto.ValidatedS3Object;
import com.mr.global.file.s3.dto.req.RecordingPresignedUrlRequest;
import com.mr.global.file.s3.dto.res.RecordingPresignedUrlResponse;
Expand Down Expand Up @@ -120,29 +121,12 @@ public MidiEventSaveResponse saveMidiEvents(
recording.fileUrl()
);

int intervalHours = 10; // 10시간 단위로 알림
int intervalSeconds = intervalHours * 3600;
publishPracticeMilestoneNotification(userId, playing);

LocalDateTime weekStart = LocalDate.now(clock).with(DayOfWeek.MONDAY).atStartOfDay();

// 방금 끝낸 연주를 제외한 이전 누적 시간
Long previousWeeklySeconds = playingRepository.sumDurationSecExcludeCurrent(
userId, playing.getStatus(), weekStart, playing.getId()
eventPublisher.publishEvent(
PlayingCompletedEvent.of(userId)
);

Long currentDuration = playing.getDurationSec() != null ? playing.getDurationSec() : 0L;
Long totalWeeklySeconds = previousWeeklySeconds + currentDuration;

Long previousMilestones = previousWeeklySeconds / intervalSeconds;
Long currentMilestones = totalWeeklySeconds / intervalSeconds;

if (currentMilestones > previousMilestones) {
int achievedHours = (int)(currentMilestones * intervalHours); // 달성한 시간: 10 시간 단위
eventPublisher.publishEvent(
NotificationEvent.forPractice(userId, playing.getUser().getNickname(), achievedHours)
);
}

return MidiEventSaveResponse.of(
playing.getId(),
playing.getMidiData().size()
Expand Down Expand Up @@ -203,4 +187,31 @@ private void validateBackingTrackAccessible(Long userId, BackingTrack backingTra
throw new GeneralException(PlayingErrorStatus.BACKING_TRACK_ACCESS_FORBIDDEN);
}
}

private void publishPracticeMilestoneNotification(
Long userId, Playing playing
) {
int intervalHours = 10; // 10시간 단위로 알림
int intervalSeconds = intervalHours * 3600;

LocalDateTime weekStart = LocalDate.now(clock).with(DayOfWeek.MONDAY).atStartOfDay();

// 방금 끝낸 연주를 제외한 이전 누적 시간
Long previousWeeklySeconds = playingRepository.sumDurationSecExcludeCurrent(
userId, playing.getStatus(), weekStart, playing.getId()
);

Comment thread
on1yoneprivate marked this conversation as resolved.
Resolved
Long currentDuration = playing.getDurationSec() != null ? playing.getDurationSec() : 0L;
Long totalWeeklySeconds = previousWeeklySeconds + currentDuration;

Long previousMilestones = previousWeeklySeconds / intervalSeconds;
Long currentMilestones = totalWeeklySeconds / intervalSeconds;

if (currentMilestones > previousMilestones) {
int achievedHours = (int)(currentMilestones * intervalHours); // 달성한 시간: 10 시간 단위
eventPublisher.publishEvent(
NotificationEvent.forPractice(userId, playing.getUser().getNickname(), achievedHours)
);
}
}
}
Loading
Loading