ruspet_portal_back/service/telegram_service/scheduler/delegation_checker.go

140 lines
4.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package scheduler
import (
"bitrix_balancer_api/core/typescore"
"bitrix_balancer_api/telegram_service/types"
"context"
"fmt"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/sirupsen/logrus"
)
type DelegationChecker struct {
ipc *types.InternalProviderControl
}
func NewDelegationChecker(ipc *types.InternalProviderControl) *DelegationChecker {
return &DelegationChecker{ipc: ipc}
}
func (dc *DelegationChecker) Start() {
ticker := time.NewTicker(30 * time.Minute)
go func() {
for range ticker.C {
dc.checkDelegations()
}
}()
// Первая проверка сразу
go dc.checkDelegations()
}
func (dc *DelegationChecker) checkDelegations() {
ctx := context.Background()
bot := dc.ipc.TGBotClient.Bot()
isActive := true
delegations, _, err := dc.ipc.Database.ApprovalDelegation.GetApprovalDelegationsListDB(ctx, typescore.ListDbOptions{
Filtering: &typescore.ApprovalDelegation{
IsActive: &isActive,
},
})
if err != nil {
logrus.Errorf("DelegationChecker: GetApprovalDelegationsListDB err=%v", err)
return
}
now := time.Now()
for _, deleg := range delegations {
if deleg.ValidUntil == nil {
continue
}
timeLeft := deleg.ValidUntil.Sub(now)
// Истекло - деактивируем
if timeLeft <= 0 {
inactive := false
deleg.IsActive = &inactive
dc.ipc.Database.ApprovalDelegation.UpdateApprovalDelegationDB(ctx, nil, deleg) //nolint:errcheck
dc.notifyExpired(bot, ctx, deleg)
continue
}
// За 24 часа
if timeLeft <= 24*time.Hour && (deleg.NotifiedDay == nil || !*deleg.NotifiedDay) {
dc.notifyExpiringDay(bot, ctx, deleg)
notified := true
deleg.NotifiedDay = &notified
dc.ipc.Database.ApprovalDelegation.UpdateApprovalDelegationDB(ctx, nil, deleg) //nolint:errcheck
continue
}
// За 2 часа
if timeLeft <= 2*time.Hour && (deleg.Notified2h == nil || !*deleg.Notified2h) {
dc.notifyExpiring2h(bot, ctx, deleg)
notified := true
deleg.Notified2h = &notified
dc.ipc.Database.ApprovalDelegation.UpdateApprovalDelegationDB(ctx, nil, deleg) //nolint:errcheck
}
}
}
func (dc *DelegationChecker) notifyExpiringDay(bot *tgbotapi.BotAPI, ctx context.Context, deleg *typescore.ApprovalDelegation) {
dc.sendExpiringNotification(bot, ctx, deleg, "⏰ <b>Делегирование истекает через 24 часа</b>")
}
func (dc *DelegationChecker) notifyExpiring2h(bot *tgbotapi.BotAPI, ctx context.Context, deleg *typescore.ApprovalDelegation) {
dc.sendExpiringNotification(bot, ctx, deleg, "⏰ <b>Делегирование истекает через 2 часа</b>")
}
func (dc *DelegationChecker) notifyExpired(bot *tgbotapi.BotAPI, ctx context.Context, deleg *typescore.ApprovalDelegation) {
dc.sendExpiringNotification(bot, ctx, deleg, "⏱ <b>Срок делегирования истёк</b>")
}
func (dc *DelegationChecker) sendExpiringNotification(bot *tgbotapi.BotAPI, ctx context.Context, deleg *typescore.ApprovalDelegation, title string) {
if deleg.DelegatorID == nil || deleg.DelegateID == nil || deleg.TopicID == nil || deleg.ValidUntil == nil {
return
}
topic, _ := dc.ipc.Database.ApprovalTopic.GetApprovalTopicByIDDB(ctx, *deleg.TopicID)
topicName := "—"
if topic != nil && topic.Name != nil {
topicName = *topic.Name
}
text := fmt.Sprintf(
"%s\n\n"+
"📋 <b>Тема:</b> %s\n"+
"📅 <b>Окончание:</b> %s",
title, topicName, deleg.ValidUntil.Format("02.01.2006 15:04"),
)
// Уведомляем делегатора
delegator, _ := dc.ipc.Database.User.GetUserByIDDB(ctx, *deleg.DelegatorID)
if delegator != nil && delegator.TelegramID != nil {
delegate, _ := dc.ipc.Database.User.GetUserByIDDB(ctx, *deleg.DelegateID)
delegateName := "—"
if delegate != nil && delegate.FullName != nil {
delegateName = *delegate.FullName
}
msg := tgbotapi.NewMessage(*delegator.TelegramID, text+fmt.Sprintf("\n👤 <b>Делегат:</b> %s", delegateName))
msg.ParseMode = "HTML"
bot.Send(msg) //nolint:errcheck
}
// Уведомляем делегата
delegate, _ := dc.ipc.Database.User.GetUserByIDDB(ctx, *deleg.DelegateID)
if delegate != nil && delegate.TelegramID != nil {
delegatorName := "—"
if delegator != nil && delegator.FullName != nil {
delegatorName = *delegator.FullName
}
msg := tgbotapi.NewMessage(*delegate.TelegramID, text+fmt.Sprintf("\n👤 <b>От:</b> %s", delegatorName))
msg.ParseMode = "HTML"
bot.Send(msg) //nolint:errcheck
}
}