From a9c6eb13a2c9d61af947e50b1d727fed33c4519c Mon Sep 17 00:00:00 2001 From: romanskijdev Date: Tue, 13 Oct 2026 16:16:07 +0300 Subject: [PATCH] Update task delegation info message --- .../handlers/approval/absense_delegation.go | 116 +++++++++++------- .../handlers/approval/selection_approver.go | 2 - 2 files changed, 70 insertions(+), 48 deletions(-) diff --git a/service/telegram_service/handlers/approval/absense_delegation.go b/service/telegram_service/handlers/approval/absense_delegation.go index 51bb186..841d14d 100644 --- a/service/telegram_service/handlers/approval/absense_delegation.go +++ b/service/telegram_service/handlers/approval/absense_delegation.go @@ -256,28 +256,28 @@ func (h *Handler) createPendingDelegation(ctx context.Context, initiator *typesc inactive := false notifiedFalse := false - // 1. Создаем отложенное делегирование ЗАЯВОК + // 1. Создаем отложенное делегирование ЗАЯВОК (только по тем категориям, где инициатор — согласующий) if flowState.SelectedDelegateID != 0 { - categories, _, _ := h.ipc.Database.ApprovalTopicCategory.GetApprovalTopicCategoryListDB(ctx, typescore.ListDbOptions{ - Filtering: &typescore.ApprovalTopicCategory{IsActive: utilscore.PointerToBool(true)}, - }) - - for _, cat := range categories { - if cat.ID == nil { - continue + // Получаем реальные категории, где этот пользователь является согласующим + approverCategories, err := h.getApproverCategories(ctx, *initiator.ID) + if err == nil { + for _, cat := range approverCategories { + if cat == nil || cat.ID == nil { + continue + } + catID := *cat.ID + delegation := &typescore.ApprovalDelegation{ + DelegatorID: initiator.ID, + DelegateID: &flowState.SelectedDelegateID, + CategoryID: &catID, + ApprovalID: approval.ID, + ValidUntil: &validUntil, + IsActive: &inactive, + NotifiedDay: ¬ifiedFalse, + Notified2h: ¬ifiedFalse, + } + h.ipc.Database.ApprovalDelegation.CreateApprovalDelegationDB(ctx, nil, delegation) //nolint:errcheck } - catID := *cat.ID - delegation := &typescore.ApprovalDelegation{ - DelegatorID: initiator.ID, - DelegateID: &flowState.SelectedDelegateID, - CategoryID: &catID, - ApprovalID: approval.ID, - ValidUntil: &validUntil, - IsActive: &inactive, - NotifiedDay: ¬ifiedFalse, - Notified2h: ¬ifiedFalse, - } - h.ipc.Database.ApprovalDelegation.CreateApprovalDelegationDB(ctx, nil, delegation) //nolint:errcheck } } @@ -301,7 +301,6 @@ func (h *Handler) activatePendingDelegation(bot *tgbotapi.BotAPI, ctx context.Co return } - // Получаем имя инициатора (кто уходит в отпуск) initiatorName := "Сотрудник" if approval.InitiatorID != nil { if u, _ := h.ipc.Database.User.GetUserByIDDB(ctx, *approval.InitiatorID); u != nil && u.FullName != nil { @@ -311,14 +310,10 @@ func (h *Handler) activatePendingDelegation(bot *tgbotapi.BotAPI, ctx context.Co active := true - // ========================================== - // 1. Активируем заявки и собираем категории - // ========================================== apprDelegations, _, _ := h.ipc.Database.ApprovalDelegation.GetApprovalDelegationsListDB(ctx, typescore.ListDbOptions{ Filtering: &typescore.ApprovalDelegation{ApprovalID: approval.ID}, }) - // Словари для группировки категорий по делегатам delegateCategories := make(map[uint64]map[string]struct{}) delegateValidUntil := make(map[uint64]string) @@ -354,7 +349,6 @@ func (h *Handler) activatePendingDelegation(bot *tgbotapi.BotAPI, ctx context.Co } } - // Рассылаем уведомления по заявкам for delegateID, categoriesMap := range delegateCategories { delegateUser, _ := h.ipc.Database.User.GetUserByIDDB(ctx, delegateID) if delegateUser != nil && delegateUser.TelegramID != nil { @@ -380,41 +374,71 @@ func (h *Handler) activatePendingDelegation(bot *tgbotapi.BotAPI, ctx context.Co } // ========================================== - // 2. Активируем задачи + // 2. Активируем задачи и собираем их области (Категории/Темы) // ========================================== taskDelegations, _, _ := h.ipc.Database.TaskDelegation.GetTaskDelegationsListDB(ctx, typescore.ListDbOptions{ Filtering: &typescore.TaskDelegation{ApprovalID: approval.ID}, }) - notifiedTaskExecutors := make(map[uint64]bool) + taskDelegateScopes := make(map[uint64]map[string]struct{}) + taskDelegateValidUntil := make(map[uint64]string) + for _, td := range taskDelegations { td.IsActive = &active h.ipc.Database.TaskDelegation.UpdateTaskDelegationDB(ctx, nil, td) //nolint:errcheck - // Уведомляем делегата задач (один раз) - if td.DelegateID != nil && !notifiedTaskExecutors[*td.DelegateID] { - notifiedTaskExecutors[*td.DelegateID] = true - delegateUser, _ := h.ipc.Database.User.GetUserByIDDB(ctx, *td.DelegateID) + if td.DelegateID != nil { + if taskDelegateScopes[*td.DelegateID] == nil { + taskDelegateScopes[*td.DelegateID] = make(map[string]struct{}) + } - if delegateUser != nil && delegateUser.TelegramID != nil { - validStr := "—" - if td.ValidUntil != nil { - validStr = td.ValidUntil.Format("02.01.2006") + scopeName := "🌍 Все задачи (Глобально)" + if td.TopicID != nil { + if topic, _ := h.ipc.Database.ApprovalTopic.GetApprovalTopicByIDDB(ctx, *td.TopicID); topic != nil && topic.Name != nil { + scopeName = "Тема: " + *topic.Name } - msg := tgbotapi.NewMessage(*delegateUser.TelegramID, fmt.Sprintf( - "🔄 Вам делегировано исполнение задач\n\n"+ - "👤 От кого: %s\n"+ - "📅 Действует до: %s\n"+ - "📋 Объем: Все задачи\n\n"+ - "Заявка на отсутствие сотрудника согласована, делегирование вступило в силу. Теперь все новые задачи этого сотрудника будут поступать вам.", - initiatorName, validStr, - )) - msg.ParseMode = "HTML" - bot.Send(msg) //nolint:errcheck + } else if td.CategoryID != nil { + if category, _ := h.ipc.Database.ApprovalTopicCategory.GetApprovalTopicCategoryByIDDB(ctx, *td.CategoryID); category != nil && category.Name != nil { + scopeName = "Категория: " + *category.Name + } + } + + taskDelegateScopes[*td.DelegateID][scopeName] = struct{}{} + + if td.ValidUntil != nil { + taskDelegateValidUntil[*td.DelegateID] = td.ValidUntil.Format("02.01.2006") + } else if taskDelegateValidUntil[*td.DelegateID] == "" { + taskDelegateValidUntil[*td.DelegateID] = "—" } } } + + // Рассылаем детальные уведомления по задачам + for delegateID, scopesMap := range taskDelegateScopes { + delegateUser, _ := h.ipc.Database.User.GetUserByIDDB(ctx, delegateID) + if delegateUser != nil && delegateUser.TelegramID != nil { + var scopeNames []string + for scopeName := range scopesMap { + scopeNames = append(scopeNames, "• "+scopeName) + } + + scopeListStr := strings.Join(scopeNames, "\n") + validStr := taskDelegateValidUntil[delegateID] + + msg := tgbotapi.NewMessage(*delegateUser.TelegramID, fmt.Sprintf( + "🔄 Вам делегировано исполнение задач\n\n"+ + "👤 От кого: %s\n"+ + "📅 Действует до: %s\n\n"+ + "📋 Переданные области задач:\n%s\n\n"+ + "Заявка на отсутствие сотрудника согласована, делегирование вступило в силу.", + initiatorName, validStr, scopeListStr, + )) + msg.ParseMode = "HTML" + bot.Send(msg) //nolint:errcheck + } + } } + func (h *Handler) deactivatePendingDelegation(ctx context.Context, approval *typescore.Approval) { if approval == nil || approval.ID == nil { return diff --git a/service/telegram_service/handlers/approval/selection_approver.go b/service/telegram_service/handlers/approval/selection_approver.go index 804cf7c..64737cb 100644 --- a/service/telegram_service/handlers/approval/selection_approver.go +++ b/service/telegram_service/handlers/approval/selection_approver.go @@ -578,8 +578,6 @@ func (h *Handler) createTasksForApproval( taskCounter := 0 - // ... (начало метода createTasksForApproval остается без изменений) ... - for _, system := range systemsToProcess { for _, topicExec := range topicExecutors { if topicExec.UserID == nil { -- 2.43.0