feat: redesigned DB schema with users, work_types, days, events
This commit is contained in:
@@ -127,32 +127,53 @@ func parseAllowedUsers(raw string) map[int64]bool {
|
||||
}
|
||||
|
||||
func dailyReportScheduler(h *bot.Handler, store *db.Store, stop chan struct{}) {
|
||||
for {
|
||||
now := time.Now()
|
||||
next := time.Date(now.Year(), now.Month(), now.Day(), 23, 0, 0, 0, now.Location())
|
||||
if now.After(next) {
|
||||
next = next.AddDate(0, 0, 1)
|
||||
}
|
||||
slog.Info("daily report scheduler", "next_run", next)
|
||||
ticker := time.NewTicker(30 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
case <-time.After(time.Until(next)):
|
||||
case <-ticker.C:
|
||||
checkDailyReports(h, store)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func checkDailyReports(h *bot.Handler, store *db.Store) {
|
||||
users, err := store.GetAllUsers()
|
||||
if err != nil {
|
||||
slog.Error("report scheduler: get users", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
|
||||
for _, u := range users {
|
||||
if !u.ReportEnabled || !u.IsActive {
|
||||
continue
|
||||
}
|
||||
|
||||
chatIDStr, err := store.GetSetting(0, "report_chat_id")
|
||||
loc, err := time.LoadLocation(u.Timezone)
|
||||
if err != nil {
|
||||
slog.Error("daily report: chat_id not found")
|
||||
continue
|
||||
}
|
||||
chatID, err := strconv.ParseInt(chatIDStr, 10, 64)
|
||||
if err != nil {
|
||||
slog.Error("daily report: invalid chat_id", "value", chatIDStr)
|
||||
localNow := now.In(loc)
|
||||
today := localNow.Format("2006-01-02")
|
||||
currentHHMM := localNow.Format("15:04")
|
||||
|
||||
if currentHHMM != u.ReportTime {
|
||||
continue
|
||||
}
|
||||
slog.Info("sending daily report", "chat_id", chatID)
|
||||
h.SendDailyReport(chatID)
|
||||
|
||||
if u.LastReportDate != nil && *u.LastReportDate == today {
|
||||
continue
|
||||
}
|
||||
|
||||
slog.Info("sending daily report",
|
||||
"user_id", u.ID, "chat_id", u.ChatID, "timezone", u.Timezone,
|
||||
)
|
||||
h.SendDailyReport(u.ID, u.ChatID)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user