Skip to content

Commit

Permalink
implement functionality to schedule-base notifying group about week e…
Browse files Browse the repository at this point in the history
…vents #10
  • Loading branch information
jonua committed Feb 5, 2024
1 parent e8d7c06 commit 697e1d9
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,55 @@
import lombok.extern.slf4j.Slf4j;
import me.jonua.herrziggy_bot.command.BotCommand;
import me.jonua.herrziggy_bot.command.handlers.GetCalendarCommandHandler;
import me.jonua.herrziggy_bot.service.StorageService;
import org.springframework.beans.factory.annotation.Value;
import me.jonua.herrziggy_bot.model.CalendarNotificationConfiguration;
import me.jonua.herrziggy_bot.service.CalendarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class ScheduledGroupNotifier {
@Value("${bot.chat-id}")
private String sourceId;

private final GetCalendarCommandHandler calendarCommandHandler;
private final StorageService storageService;

@Scheduled(cron = "0 0 10 * * 1") // every monday at 7 o'clock by UTC
private void notifyAlAboutCalendarEvents() {
log.info("Notifying group {} about next week events...", sourceId);
storageService.findCalendarByGroup(sourceId)
.ifPresent(calendar -> calendarCommandHandler
.sendCalendar(calendar.getGoogleCalendarId(), sourceId, BotCommand.THIS_WEEK, false));
private final CalendarService calendarService;
@Lazy
@Autowired
private ScheduledGroupNotifier self;

@Bean
public ScheduledTaskRegistrar scheduledTaskRegistrar() {
return new ScheduledTaskRegistrar();
}

@Scheduled(cron = "0 0 10 * * 1")
public void updatedCalendarNotificationSchedules() {
List<CalendarNotificationConfiguration> activeSchedules = calendarService.findActiveSchedules();
for (CalendarNotificationConfiguration activeSchedule : activeSchedules) {
if (activeSchedule.getCalendar() == null) {
log.warn("Schedule {} has no any linked calendars", activeSchedule.getUuid());
continue;
}

self.notifyCalendarSubscribers(activeSchedule.getUuid());
}
}

@Transactional
public void notifyCalendarSubscribers(String configUuid) {
CalendarNotificationConfiguration config = calendarService.getNotificationConfiguration(configUuid);
log.info("Notifying group {} ({}) about week events from calendar {} ({})...",
config.getTgSource().getSourceId(), config.getTgSource().getTitle(),
config.getCalendar().getGoogleCalendarId(), config.getCalendar().getAdditionalInfo());

calendarCommandHandler
.sendCalendar(config.getCalendar().getGoogleCalendarId(), config.getTgSource().getSourceId(),
BotCommand.THIS_WEEK, false);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package me.jonua.herrziggy_bot.data.jpa.repository;

import me.jonua.herrziggy_bot.model.Calendar;
import me.jonua.herrziggy_bot.model.CalendarNotificationConfiguration;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
Expand All @@ -12,4 +14,10 @@ public interface CalendarRepository extends BaseRepository<Calendar> {
Optional<Calendar> findBySourceId(String sourceId);

Optional<Calendar> findByUuid(String calendarUuid);

@Query("SELECT c FROM CalendarNotificationConfiguration c WHERE c.active = true")
List<CalendarNotificationConfiguration> findActiveSchedules();

@Query("SELECT c FROM CalendarNotificationConfiguration c WHERE c.uuid = :configUuid")
Optional<CalendarNotificationConfiguration> findNotificationConfiguration(String configUuid);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.jonua.herrziggy_bot.model;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.OneToOne;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
public class CalendarNotificationConfiguration extends BaseEntity {
private boolean active = false;
@OneToOne(fetch = FetchType.EAGER)
private Calendar calendar;
@OneToOne(fetch = FetchType.EAGER)
private TgSource tgSource;
}
19 changes: 19 additions & 0 deletions src/main/java/me/jonua/herrziggy_bot/service/CalendarService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package me.jonua.herrziggy_bot.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.jonua.herrziggy_bot.calendar.GoogleCalendarApi;
import me.jonua.herrziggy_bot.calendar.dto.CalendarEventsDto;
import me.jonua.herrziggy_bot.data.jpa.repository.CalendarRepository;
import me.jonua.herrziggy_bot.enums.calendar.CalendarPeriod;
import me.jonua.herrziggy_bot.model.CalendarNotificationConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Service;

import java.time.ZonedDateTime;
import java.util.List;
import java.util.Locale;

import static me.jonua.herrziggy_bot.utils.DateTimeUtils.*;
Expand All @@ -18,6 +22,7 @@
@Service
@RequiredArgsConstructor
public class CalendarService {
private final CalendarRepository calendarRepository;
@Value("${bot.locale}")
private Locale locale;
private final GoogleCalendarApi googleCalendarApi;
Expand Down Expand Up @@ -51,4 +56,18 @@ private Pair<ZonedDateTime, ZonedDateTime> getPeriod(CalendarPeriod period, Zone
case FULL_SEMESTER_TESTS -> Pair.of(getStartOfDay(subjectDate), getEndOfHalfYear(subjectDate));
};
}

@Transactional
public List<CalendarNotificationConfiguration> findActiveSchedules() {
return calendarRepository.findActiveSchedules();
}

@Transactional
public CalendarNotificationConfiguration getNotificationConfiguration(String configUuid) {
return calendarRepository.findNotificationConfiguration(configUuid)
.orElseThrow(() -> {
log.error("No notification configuration with uuid {} was found", configUuid);
return new RuntimeException("No notification configuration found");
});
}
}
1 change: 0 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ bot:
locale: ru
token: "-"
name: "-"
chat-id: "-"
max-allowed-entity-size-bytes: 10_000_000 # 10mb
feedback:
sent-to-user-id: "-"
Expand Down

0 comments on commit 697e1d9

Please sign in to comment.