|
| 1 | +package com.whiskels.notifier.slack.reporter.impl; |
| 2 | + |
| 3 | +import com.whiskels.notifier.common.util.Util; |
| 4 | +import com.whiskels.notifier.external.ReportSupplier; |
| 5 | +import com.whiskels.notifier.external.google.customer.CustomerBirthdayInfoDto; |
| 6 | +import com.whiskels.notifier.slack.SlackWebHookExecutor; |
| 7 | +import com.whiskels.notifier.slack.reporter.AbstractCustomerEventReporter; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| 10 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 11 | +import org.springframework.scheduling.annotation.Scheduled; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | +import java.util.function.Predicate; |
| 16 | + |
| 17 | +import static com.whiskels.notifier.common.util.DateTimeUtil.isSameDay; |
| 18 | +import static com.whiskels.notifier.common.util.DateTimeUtil.reportDate; |
| 19 | + |
| 20 | +@Component |
| 21 | +@ConditionalOnProperty("slack.customer.birthday.webhook") |
| 22 | +@ConditionalOnBean(value = CustomerBirthdayInfoDto.class, parameterizedContainer = ReportSupplier.class) |
| 23 | +class CustomerEventReporterBeforeEvent extends AbstractCustomerEventReporter { |
| 24 | + private final String header; |
| 25 | + private final Long daysBefore; |
| 26 | + |
| 27 | + |
| 28 | + public CustomerEventReporterBeforeEvent(@Value("${slack.customer.birthday.webhook}") String webHook, |
| 29 | + @Value("${slack.customer.birthday.before.header:Customer birthdays in ${slack.customer.birthday.before.days} days from}") String header, |
| 30 | + @Value("${slack.customer.birthday.before.days}") Long daysBefore, |
| 31 | + ReportSupplier<CustomerBirthdayInfoDto> provider, |
| 32 | + SlackWebHookExecutor executor) { |
| 33 | + super(webHook, provider, executor); |
| 34 | + this.header = header; |
| 35 | + this.daysBefore = daysBefore; |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + @Scheduled(cron = "${slack.customer.birthday.before.cron:0 0 9 * * *}", zone = "${common.timezone}") |
| 40 | + public void executeScheduled() { |
| 41 | + prepareAndSend(); |
| 42 | + } |
| 43 | + |
| 44 | + public void prepareAndSend() { |
| 45 | + var data = provider.get(); |
| 46 | + var payload = createPayload(header + reportDate(data.getReportDate()), true); |
| 47 | + executor.execute(payload); |
| 48 | + } |
| 49 | + |
| 50 | + protected List<Predicate<CustomerBirthdayInfoDto>> birthdayPredicates() { |
| 51 | + var data = provider.get(); |
| 52 | + return List.of(Util.notNull(CustomerBirthdayInfoDto::getBirthday), |
| 53 | + isSameDay(CustomerBirthdayInfoDto::getBirthday, data.getReportDate().plusDays(daysBefore))); |
| 54 | + } |
| 55 | +} |
0 commit comments