diff --git a/service/telegram_service/handlers/approval/helper.go b/service/telegram_service/handlers/approval/helper.go index 9c9575f..ecdfaa6 100644 --- a/service/telegram_service/handlers/approval/helper.go +++ b/service/telegram_service/handlers/approval/helper.go @@ -710,17 +710,6 @@ func (h *Handler) buildApprovalCard(ctx context.Context, approval *typescore.App filesLabel, createdAt, status, ) - // Добавляем информацию о текущем согласующем если заявка в процессе - if approval.Status != nil && *approval.Status == "pending" { - activeStep, _ := h.ipc.Database.ApprovalStep.GetActiveStepDB(ctx, approvalID) - if activeStep != nil && activeStep.ApproverID != nil { - approver, _ := h.ipc.Database.User.GetUserByIDDB(ctx, *activeStep.ApproverID) - if approver != nil && approver.FullName != nil { - card += fmt.Sprintf("\n🔄 У согласующего: %s", *approver.FullName) - } - } - } - // Добавляем информацию о дополнительных согласующих additionalApprovers, _, _ := h.ipc.Database.ApprovalAdditionalApprover.GetApprovalAdditionalApproversListDB(ctx, typescore.ListDbOptions{ Filtering: &typescore.ApprovalAdditionalApprover{ diff --git a/service/telegram_service/handlers/approval/notify.go b/service/telegram_service/handlers/approval/notify.go index 73c8c86..5a397ff 100644 --- a/service/telegram_service/handlers/approval/notify.go +++ b/service/telegram_service/handlers/approval/notify.go @@ -2,6 +2,7 @@ package approval import ( "bitrix_balancer_api/core/typescore" + "bitrix_balancer_api/core/utilscore" "bitrix_balancer_api/telegram_service/approvalflow" "bitrix_balancer_api/telegram_service/keyboard" "bitrix_balancer_api/telegram_service/types" @@ -9,6 +10,7 @@ import ( "encoding/base64" "fmt" "github.com/sirupsen/logrus" + "time" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) @@ -22,6 +24,65 @@ func NewNotifier(ipc *types.InternalProviderControl, topic approvalflow.Topic) * return &Notifier{ipc: ipc, topic: topic} } +// getDisplayName проверяет активные делегирования и формирует строку "Делегат (Делегат от Оригинал)", +// если права были переданы, либо просто "Оригинал", если делегирования нет. +func (n *Notifier) getDisplayName(ctx context.Context, originalApproverID uint64, topicID *uint64) string { + originalApprover, err := n.ipc.Database.User.GetUserByIDDB(ctx, originalApproverID) + if err != nil || originalApprover == nil { + return "—" + } + + origName := "—" + if originalApprover.FullName != nil { + origName = *originalApprover.FullName + } + + if topicID == nil { + return origName + } + + // 1. Проверяем делегирование по теме + delegations, _, err := n.ipc.Database.ApprovalDelegation.GetApprovalDelegationsListDB(ctx, typescore.ListDbOptions{ + Filtering: &typescore.ApprovalDelegation{ + DelegatorID: &originalApproverID, + TopicID: topicID, + IsActive: utilscore.PointerToBool(true), + }, + }) + if err == nil { + for _, d := range delegations { + if d.ValidUntil != nil && d.ValidUntil.After(time.Now()) && d.DelegateID != nil { + if delegate, _ := n.ipc.Database.User.GetUserByIDDB(ctx, *d.DelegateID); delegate != nil && delegate.FullName != nil { + return fmt.Sprintf("%s (Временно исполняющий обязанности от: %s)", *delegate.FullName, origName) + } + } + } + } + + // 2. Проверяем делегирование по категории + topic, _ := n.ipc.Database.ApprovalTopic.GetApprovalTopicByIDDB(ctx, *topicID) + if topic != nil && topic.CategoryID != nil { + delegationsCat, _, err := n.ipc.Database.ApprovalDelegation.GetApprovalDelegationsListDB(ctx, typescore.ListDbOptions{ + Filtering: &typescore.ApprovalDelegation{ + DelegatorID: &originalApproverID, + CategoryID: topic.CategoryID, + IsActive: utilscore.PointerToBool(true), + }, + }) + if err == nil { + for _, d := range delegationsCat { + if d.ValidUntil != nil && d.ValidUntil.After(time.Now()) && d.DelegateID != nil { + if delegate, _ := n.ipc.Database.User.GetUserByIDDB(ctx, *d.DelegateID); delegate != nil && delegate.FullName != nil { + return fmt.Sprintf("%s (Делегат от %s)", *delegate.FullName, origName) + } + } + } + } + } + + return origName +} + func (h *Handler) notifyExecutor(bot *tgbotapi.BotAPI, ctx context.Context, executor *typescore.User, initiator *typescore.User, approval *typescore.Approval, task *typescore.ApprovalTask) { if executor.TelegramID == nil || task.ID == nil || approval.ID == nil { return @@ -178,8 +239,8 @@ func (n *Notifier) NotifyInitiatorApproved( } approverName := "—" - if lastApprover != nil && lastApprover.FullName != nil { - approverName = *lastApprover.FullName + if lastApprover != nil && lastApprover.ID != nil { + approverName = n.getDisplayName(context.Background(), *lastApprover.ID, approval.TopicID) } status := "" @@ -233,8 +294,9 @@ func (n *Notifier) NotifyInitiatorRejected( } approverName := "—" - if approver != nil && approver.FullName != nil { - approverName = *approver.FullName + if approver != nil && approver.ID != nil { + ctx := context.Background() + approverName = n.getDisplayName(ctx, *approver.ID, approval.TopicID) } initiatorCollected := make(map[string]string, len(collected)) @@ -324,8 +386,8 @@ func (n *Notifier) NotifyObserversDecision( card := n.buildApprovalCard(ctx, approval, initiator, collected) approverName := "—" - if approver != nil && approver.FullName != nil { - approverName = *approver.FullName + if approver != nil && approver.ID != nil { + approverName = n.getDisplayName(ctx, *approver.ID, approval.TopicID) } header := fmt.Sprintf( "✅ %s согласовал(а) заявку\n\n",