From f95ecfa02c749679db096443c590f77ccb2ee961 Mon Sep 17 00:00:00 2001 From: romanskijdev Date: Tue, 13 Oct 2026 16:39:44 +0300 Subject: [PATCH] Filter tasks categories in delegation menu --- core/db/methods/approval_topic_executor.go | 41 +++++++++++++++++++ .../approval/handler_delegation_tasks.go | 13 ++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/core/db/methods/approval_topic_executor.go b/core/db/methods/approval_topic_executor.go index d824c93..377b1dd 100644 --- a/core/db/methods/approval_topic_executor.go +++ b/core/db/methods/approval_topic_executor.go @@ -30,8 +30,49 @@ type ApprovalTopicExecutorDBI interface { GetApprovalTopicExecutorListWithParamsDB(ctx context.Context, opts typescore.ListDbOptions) ([]*typescore.ApprovalTopicExecutor, uint64, error) GetApprovalTopicExecutorListByCategoryDB(ctx context.Context, opts typescore.ListDbOptions, categoryID uint64) ([]*typescore.ApprovalTopicExecutor, uint64, error) GetExecutorCategoriesDB(ctx context.Context, userID uint64) ([]*typescore.ApprovalTopicCategory, error) + GetExecutorTopicsByCategoryDB(ctx context.Context, userID uint64, categoryID uint64) ([]*typescore.ApprovalTopic, error) } +func (d *ApprovalTopicExecutorDB) GetExecutorTopicsByCategoryDB(ctx context.Context, userID uint64, categoryID uint64) ([]*typescore.ApprovalTopic, error) { + fields := dbutils.GetStructFieldsDB(&typescore.ApprovalTopic{}, "t") + + query := squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar). + Select(fields...). + From(dbcoretablenames.TableNameApprovalTopicExecutor.ToString() + " ate"). + Join(dbcoretablenames.TableNameApprovalTopics.ToString() + " t ON t.id = ate.topic_id"). + Where(squirrel.Eq{ + "ate.user_id": userID, + "ate.is_active": true, + "t.category_id": categoryID, + "t.is_active": true, + }). + GroupBy("t.id"). + OrderBy("t.name ASC") + + sql, args, err := query.ToSql() + if err != nil { + return nil, err + } + + rows, err := d.pool.Query(ctx, sql, args...) + if err != nil { + logrus.Errorf("GetExecutorTopicsByCategoryDB Query error: %v", err) + return nil, err + } + defer rows.Close() + + var topics []*typescore.ApprovalTopic + for rows.Next() { + topic := &typescore.ApprovalTopic{} + if err := dbutils.ScanRowsToStructRows(rows, topic, nil); err != nil { + logrus.Warn("GetExecutorTopicsByCategoryDB:ScanRowsToStructRows", err) + continue + } + topics = append(topics, topic) + } + + return topics, nil +} func (d *ApprovalTopicExecutorDB) GetExecutorCategoriesDB(ctx context.Context, userID uint64) ([]*typescore.ApprovalTopicCategory, error) { fields := dbutils.GetStructFieldsDB(&typescore.ApprovalTopicCategory{}, "c") diff --git a/service/telegram_service/handlers/approval/handler_delegation_tasks.go b/service/telegram_service/handlers/approval/handler_delegation_tasks.go index e5bf145..71edf58 100644 --- a/service/telegram_service/handlers/approval/handler_delegation_tasks.go +++ b/service/telegram_service/handlers/approval/handler_delegation_tasks.go @@ -276,20 +276,25 @@ func (h *Handler) HandleTaskDelegationCategoryCallback(bot *tgbotapi.BotAPI, cq func (h *Handler) HandleTaskDelegationCategoryCallbackPage(bot *tgbotapi.BotAPI, cq *tgbotapi.CallbackQuery, categoryID uint64, page int) { ctx := context.Background() chatID := cq.Message.Chat.ID + telegramID := cq.From.ID bot.Request(tgbotapi.NewCallback(cq.ID, "")) //nolint:errcheck bot.Request(tgbotapi.NewDeleteMessage(chatID, cq.Message.MessageID)) //nolint:errcheck + user, err := h.ipc.Database.User.GetUserByTelegramIDDB(ctx, telegramID) + if err != nil || user == nil || user.ID == nil { + return + } + category, _ := h.ipc.Database.ApprovalTopicCategory.GetApprovalTopicCategoryByIDDB(ctx, categoryID) categoryName := "—" if category != nil && category.Name != nil { categoryName = *category.Name } - topics, _, err := h.ipc.Database.ApprovalTopic.GetApprovalTopicsListDB(ctx, typescore.ListDbOptions{ - Filtering: &typescore.ApprovalTopic{CategoryID: &categoryID}, - }) - if err != nil { + topics, err := h.ipc.Database.ApprovalTopicExecutor.GetExecutorTopicsByCategoryDB(ctx, *user.ID, categoryID) + if err != nil || len(topics) == 0 { + sendText(bot, chatID, "В этой категории нет доступных вам тем с задачами.") return } -- 2.43.0