|
@@ -2,49 +2,89 @@ package com.ffii.tsms.modules.common.mail.service |
|
|
|
|
|
|
|
|
import com.ffii.tsms.modules.common.SecurityUtils |
|
|
import com.ffii.tsms.modules.common.SecurityUtils |
|
|
import com.ffii.tsms.modules.common.SettingNames |
|
|
import com.ffii.tsms.modules.common.SettingNames |
|
|
|
|
|
import com.ffii.tsms.modules.common.holiday.service.HolidayService |
|
|
import com.ffii.tsms.modules.common.mail.pojo.MailRequest |
|
|
import com.ffii.tsms.modules.common.mail.pojo.MailRequest |
|
|
|
|
|
import com.ffii.tsms.modules.data.entity.StaffRepository |
|
|
|
|
|
import com.ffii.tsms.modules.data.service.StaffsService |
|
|
|
|
|
import com.ffii.tsms.modules.project.web.models.MilestoneInfo |
|
|
import com.ffii.tsms.modules.settings.service.SettingsService |
|
|
import com.ffii.tsms.modules.settings.service.SettingsService |
|
|
|
|
|
import com.ffii.tsms.modules.timesheet.entity.TimesheetRepository |
|
|
|
|
|
import com.ffii.tsms.modules.timesheet.service.TimesheetsService |
|
|
import com.ffii.tsms.modules.user.service.UserService |
|
|
import com.ffii.tsms.modules.user.service.UserService |
|
|
import jakarta.mail.internet.InternetAddress |
|
|
import jakarta.mail.internet.InternetAddress |
|
|
import org.apache.commons.logging.Log |
|
|
import org.apache.commons.logging.Log |
|
|
import org.apache.commons.logging.LogFactory |
|
|
import org.apache.commons.logging.LogFactory |
|
|
import org.springframework.stereotype.Service |
|
|
import org.springframework.stereotype.Service |
|
|
|
|
|
import java.time.DayOfWeek |
|
|
import java.time.LocalDate |
|
|
import java.time.LocalDate |
|
|
import java.time.format.DateTimeFormatter |
|
|
import java.time.format.DateTimeFormatter |
|
|
|
|
|
import java.time.temporal.TemporalAdjuster |
|
|
|
|
|
import java.time.temporal.TemporalAdjusters |
|
|
|
|
|
|
|
|
@Service |
|
|
@Service |
|
|
open class MailReminderService ( |
|
|
|
|
|
|
|
|
open class MailReminderService( |
|
|
val mailService: MailService, |
|
|
val mailService: MailService, |
|
|
val userService: UserService, |
|
|
val userService: UserService, |
|
|
val settingsService: SettingsService, |
|
|
val settingsService: SettingsService, |
|
|
|
|
|
val holidayService: HolidayService, |
|
|
|
|
|
val timesheetsService: TimesheetsService, |
|
|
|
|
|
val timesheetRepository: TimesheetRepository, |
|
|
|
|
|
val staffsService: StaffsService, |
|
|
|
|
|
val staffRepository: StaffRepository, |
|
|
) { |
|
|
) { |
|
|
protected val logger: Log = LogFactory.getLog(javaClass) |
|
|
protected val logger: Log = LogFactory.getLog(javaClass) |
|
|
|
|
|
|
|
|
private val dateFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") |
|
|
private val dateFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") |
|
|
|
|
|
|
|
|
// @Scheduled(cron = "0 0 6 * * ?") // Runs at 06:00 AM every day |
|
|
|
|
|
|
|
|
// @Scheduled(cron = "0 0 6 * * ?") // Runs at 06:00 AM every day |
|
|
open fun sendTimesheetReminder() { |
|
|
open fun sendTimesheetReminder() { |
|
|
val inputDate = LocalDate.now().minusDays(4).format(dateFormat) |
|
|
|
|
|
|
|
|
val inputDate = LocalDate.now().minusDays(4) |
|
|
|
|
|
val holidayList = holidayService.commonHolidayList().map { it.date } |
|
|
|
|
|
val dayOfWeek = inputDate.dayOfWeek |
|
|
|
|
|
|
|
|
val subject = settingsService.findByName(SettingNames.TIMESHEET_MAIL_SUBJECT).orElseThrow().value |
|
|
|
|
|
val template = settingsService.findByName(SettingNames.TIMESHEET_MAIL_TEMPLATE).orElseThrow().value |
|
|
|
|
|
val cc = settingsService.findByName(SettingNames.TIMESHEET_MAIL_CC).orElseThrow().value.split(",") |
|
|
|
|
|
val bcc = settingsService.findByName(SettingNames.TIMESHEET_MAIL_BCC).orElseThrow().value.split(",") |
|
|
|
|
|
val mailRequest = MailRequest.Builder() |
|
|
|
|
|
.subject(subject) |
|
|
|
|
|
.template("mail/TimesheetNotification") |
|
|
|
|
|
.args(mapOf( |
|
|
|
|
|
Pair("date", inputDate), |
|
|
|
|
|
Pair("template", template) |
|
|
|
|
|
)) |
|
|
|
|
|
.addTo(InternetAddress("[email protected]")) |
|
|
|
|
|
.addCc(cc) |
|
|
|
|
|
.addBcc(bcc) |
|
|
|
|
|
.build() |
|
|
|
|
|
|
|
|
if (!holidayList.contains(inputDate) && dayOfWeek != DayOfWeek.SATURDAY && dayOfWeek != DayOfWeek.SUNDAY) { |
|
|
|
|
|
// Timesheet Data |
|
|
|
|
|
val timesheet = timesheetRepository.findAllByDeletedFalseAndRecordDate(inputDate) |
|
|
|
|
|
.groupBy { it.staff?.id } |
|
|
|
|
|
.mapValues {(_, t) -> |
|
|
|
|
|
t.map { (it.normalConsumed ?: 0.0) } |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
val mailRequestList = mutableListOf<MailRequest>() |
|
|
|
|
|
mailRequestList += mailRequest |
|
|
|
|
|
mailService.send(mailRequestList) |
|
|
|
|
|
|
|
|
// Staff Data |
|
|
|
|
|
val staffs = staffRepository.findAllByDeletedFalse().orElseThrow() |
|
|
|
|
|
|
|
|
|
|
|
val toList = mutableListOf<String>() |
|
|
|
|
|
staffs.forEach { staff -> |
|
|
|
|
|
val sum = timesheet[staff.id]?.sum() ?: 0.0 |
|
|
|
|
|
|
|
|
|
|
|
if (sum < 8) { |
|
|
|
|
|
toList += staff.email |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val subject = settingsService.findByName(SettingNames.TIMESHEET_MAIL_SUBJECT).orElseThrow().value |
|
|
|
|
|
val template = settingsService.findByName(SettingNames.TIMESHEET_MAIL_TEMPLATE).orElseThrow().value |
|
|
|
|
|
val cc = settingsService.findByName(SettingNames.TIMESHEET_MAIL_CC).orElseThrow().value.split(",") |
|
|
|
|
|
val bcc = settingsService.findByName(SettingNames.TIMESHEET_MAIL_BCC).orElseThrow().value.split(",") |
|
|
|
|
|
val mailRequest = MailRequest.Builder() |
|
|
|
|
|
.subject(subject) |
|
|
|
|
|
.template("mail/TimesheetNotification") |
|
|
|
|
|
.args( |
|
|
|
|
|
mapOf( |
|
|
|
|
|
Pair("date", inputDate.format(dateFormat)), |
|
|
|
|
|
Pair("template", template) |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
// .addTo(InternetAddress("[email protected]")) |
|
|
|
|
|
.addTo(toList) |
|
|
|
|
|
.addCc(cc) |
|
|
|
|
|
.addBcc(bcc) |
|
|
|
|
|
.build() |
|
|
|
|
|
|
|
|
|
|
|
val mailRequestList = mutableListOf<MailRequest>() |
|
|
|
|
|
mailRequestList += mailRequest |
|
|
|
|
|
mailService.send(mailRequestList) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
open fun sendTimesheetReminderTest() { |
|
|
open fun sendTimesheetReminderTest() { |
|
@@ -59,10 +99,12 @@ open class MailReminderService ( |
|
|
.subject(subject) |
|
|
.subject(subject) |
|
|
// .template("mail/TimesheetNotification") |
|
|
// .template("mail/TimesheetNotification") |
|
|
.templateContent(template) |
|
|
.templateContent(template) |
|
|
.args(mapOf( |
|
|
|
|
|
Pair("date", inputDate), |
|
|
|
|
|
|
|
|
.args( |
|
|
|
|
|
mapOf( |
|
|
|
|
|
Pair("date", inputDate), |
|
|
// Pair("template", template) |
|
|
// Pair("template", template) |
|
|
)) |
|
|
|
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
.addTo(InternetAddress(SecurityUtils.getUser().orElseThrow().email)) |
|
|
.addTo(InternetAddress(SecurityUtils.getUser().orElseThrow().email)) |
|
|
.addCc(cc) |
|
|
.addCc(cc) |
|
|
.addBcc(bcc) |
|
|
.addBcc(bcc) |
|
|