Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/main/kotlin/com/moa/repository/NotificationLogRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.moa.entity.notification.NotificationLog
import com.moa.entity.notification.NotificationStatus
import com.moa.entity.notification.NotificationType
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import java.time.LocalDate
import java.time.LocalTime

Expand Down Expand Up @@ -33,14 +34,16 @@ interface NotificationLogRepository : JpaRepository<NotificationLog, Long> {
notificationTypes: Collection<NotificationType>,
): List<NotificationLog>

fun existsByMemberIdAndScheduledDateAndStatus(
memberId: Long,
scheduledDate: LocalDate,
status: NotificationStatus,
): Boolean

fun existsByScheduledDateAndNotificationType(
@Query(
"select distinct n.memberId " +
"from NotificationLog n " +
"where n.scheduledDate = :scheduledDate " +
"and n.notificationType = :notificationType " +
"and n.memberId in :memberIds"
)
fun findMemberIdsByScheduledDateAndNotificationTypeAndMemberIdIn(
scheduledDate: LocalDate,
notificationType: NotificationType,
): Boolean
memberIds: Collection<Long>,
): List<Long>
Comment on lines +37 to +48
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JPQL query can return duplicate memberIds if there are multiple NotificationLog rows for the same member/date/type (e.g., retries). Since callers immediately convert to a Set, consider changing the query to select distinct n.memberId to reduce DB row count and network/memory usage. If possible, also consider narrowing the query by candidate memberIds (IN clause), since the services already have the member list and only need the intersection.

Copilot uses AI. Check for mistakes.
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.moa.service.notification

import com.moa.entity.Workday
import com.moa.entity.WorkPolicyVersion
import com.moa.entity.Workday
import com.moa.entity.notification.NotificationLog
import com.moa.entity.notification.NotificationSettingType
import com.moa.entity.notification.NotificationType
Expand Down Expand Up @@ -30,8 +30,6 @@ class NotificationBatchService(

@Transactional
fun generateNotificationsForDate(date: LocalDate) {
if (isAlreadyGenerated(date)) return

if (publicHolidayService.isHoliday(date)) {
generateHolidayNotifications(date)
} else {
Expand All @@ -43,13 +41,21 @@ class NotificationBatchService(
val allPolicies = workPolicyVersionRepository.findLatestEffectivePoliciesPerMember(date)
if (allPolicies.isEmpty()) return

val memberIds = allPolicies.map { it.memberId }
val allMemberIds = allPolicies.map { it.memberId }
val alreadyGeneratedMemberIds = notificationLogRepository
.findMemberIdsByScheduledDateAndNotificationTypeAndMemberIdIn(date, NotificationType.PUBLIC_HOLIDAY, allMemberIds)
.toSet()

val targetPolicies = allPolicies.filter { it.memberId !in alreadyGeneratedMemberIds }
if (targetPolicies.isEmpty()) return

val memberIds = targetPolicies.map { it.memberId }
val requiredTermCodes = notificationEligibilityService.findRequiredTermCodes()
val context = notificationEligibilityService.loadContext(memberIds, date)

log.info("Generating public holiday notifications for {} members on {}", memberIds.size, date)

val notifications = allPolicies.mapNotNull { policy ->
val notifications = targetPolicies.mapNotNull { policy ->
createHolidayNotificationIfEligible(policy.memberId, date, requiredTermCodes, context)
}

Expand All @@ -74,29 +80,28 @@ class NotificationBatchService(
val workdayPolicies = findWorkdayPolicies(date)
if (workdayPolicies.isEmpty()) return

val memberIds = workdayPolicies.map { it.memberId }
val workdayMemberIds = workdayPolicies.map { it.memberId }
val alreadyGeneratedMemberIds = notificationLogRepository
.findMemberIdsByScheduledDateAndNotificationTypeAndMemberIdIn(date, NotificationType.CLOCK_IN, workdayMemberIds)
.toSet()

val targetPolicies = workdayPolicies.filter { it.memberId !in alreadyGeneratedMemberIds }
if (targetPolicies.isEmpty()) return

val memberIds = targetPolicies.map { it.memberId }
val requiredTermCodes = notificationEligibilityService.findRequiredTermCodes()
val context = notificationEligibilityService.loadContext(memberIds, date)

log.info("Generating notifications for {} members on {}", memberIds.size, date)

val notifications = workdayPolicies.mapNotNull { policy ->
val notifications = targetPolicies.mapNotNull { policy ->
createNotificationsIfEligible(policy, date, requiredTermCodes, context)
}.flatten()

notificationLogRepository.saveAll(notifications)
log.info("Created {} notification logs for {}", notifications.size, date)
}

private fun isAlreadyGenerated(date: LocalDate): Boolean {
val exists = notificationLogRepository
.existsByScheduledDateAndNotificationType(date, NotificationType.CLOCK_IN)
|| notificationLogRepository
.existsByScheduledDateAndNotificationType(date, NotificationType.PUBLIC_HOLIDAY)
if (exists) log.info("Notifications already generated for date: {}", date)
return exists
}

private fun findWorkdayPolicies(date: LocalDate): List<WorkPolicyVersion> {
val todayWorkday = Workday.from(date)
return workPolicyVersionRepository.findLatestEffectivePoliciesPerMember(date)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,31 @@ class PaydayNotificationBatchService(

@Transactional
fun generateNotificationsForDate(date: LocalDate) {
if (isAlreadyGenerated(date)) return

val profiles = findPaydayProfiles(date)
if (profiles.isEmpty()) return

val memberIds = profiles.map { it.memberId }
val profileMemberIds = profiles.map { it.memberId }
val alreadyGeneratedMemberIds = notificationLogRepository
.findMemberIdsByScheduledDateAndNotificationTypeAndMemberIdIn(date, NotificationType.PAYDAY, profileMemberIds)
.toSet()

val targetProfiles = profiles.filter { it.memberId !in alreadyGeneratedMemberIds }
if (targetProfiles.isEmpty()) return

val memberIds = targetProfiles.map { it.memberId }
val requiredTermCodes = notificationEligibilityService.findRequiredTermCodes()
val context = notificationEligibilityService.loadContext(memberIds)

log.info("Generating payday notifications for {} members on {}", memberIds.size, date)

val notifications = profiles.mapNotNull { profile ->
val notifications = targetProfiles.mapNotNull { profile ->
createNotificationIfEligible(profile.memberId, date, requiredTermCodes, context)
}

notificationLogRepository.saveAll(notifications)
log.info("Created {} payday notification logs for {}", notifications.size, date)
}

private fun isAlreadyGenerated(date: LocalDate): Boolean {
val exists = notificationLogRepository
.existsByScheduledDateAndNotificationType(date, NotificationType.PAYDAY)
if (exists) log.info("Payday notifications already generated for date: {}", date)
return exists
}

private fun findPaydayProfiles(date: LocalDate): List<Profile> {
val candidatePaydayDays = PaydayDay.resolvingTo(date)
.map { it.value }
Expand Down
Loading