diff --git a/core/db/methods/approval_topic_executor.go b/core/db/methods/approval_topic_executor.go index 17c69d2..d824c93 100644 --- a/core/db/methods/approval_topic_executor.go +++ b/core/db/methods/approval_topic_executor.go @@ -29,6 +29,48 @@ type ApprovalTopicExecutorDBI interface { GetExecutorsByTopicIDDB(ctx context.Context, topicID uint64) ([]*typescore.ApprovalTopicExecutor, error) 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) +} + +func (d *ApprovalTopicExecutorDB) GetExecutorCategoriesDB(ctx context.Context, userID uint64) ([]*typescore.ApprovalTopicCategory, error) { + fields := dbutils.GetStructFieldsDB(&typescore.ApprovalTopicCategory{}, "c") + + query := squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar). + Select(fields...). + From(dbcoretablenames.TableNameApprovalTopicExecutor.ToString() + " ate"). + Join(dbcoretablenames.TableNameApprovalTopics.ToString() + " t ON t.id = ate.topic_id"). + Join(dbcoretablenames.TableNameApprovalTopicCategories.ToString() + " c ON c.id = t.category_id"). + Where(squirrel.Eq{ + "ate.user_id": userID, + "ate.is_active": true, + "c.is_active": true, + }). + GroupBy("c.id"). + OrderBy("c.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("GetExecutorCategoriesDB Query error: %v", err) + return nil, err + } + defer rows.Close() + + var categories []*typescore.ApprovalTopicCategory + for rows.Next() { + cat := &typescore.ApprovalTopicCategory{} + if err := dbutils.ScanRowsToStructRows(rows, cat, nil); err != nil { + logrus.Warn("GetExecutorCategoriesDB:ScanRowsToStructRows", err) + continue + } + categories = append(categories, cat) + } + + return categories, nil } func (d *ApprovalTopicExecutorDB) GetApprovalTopicExecutorListByCategoryDB(ctx context.Context, opts typescore.ListDbOptions, categoryID uint64) ([]*typescore.ApprovalTopicExecutor, uint64, error) { diff --git a/service/telegram_service/handlers/approval/handler_delegation_tasks.go b/service/telegram_service/handlers/approval/handler_delegation_tasks.go index 6166ec4..e5bf145 100644 --- a/service/telegram_service/handlers/approval/handler_delegation_tasks.go +++ b/service/telegram_service/handlers/approval/handler_delegation_tasks.go @@ -248,7 +248,7 @@ func (h *Handler) HandleTaskDelegationMenuPage(bot *tgbotapi.BotAPI, chatID int6 return } - categories, err := h.getExecutorCategories(ctx, *user.ID) + categories, err := h.ipc.Database.ApprovalTopicExecutor.GetExecutorCategoriesDB(ctx, *user.ID) if err != nil || len(categories) == 0 { sendText(bot, chatID, "У вас нет категорий с настроенными задачами для делегирования.") return