diff --git a/core/db/methods/user_hierarchy.go b/core/db/methods/user_hierarchy.go index 29eba12..18db30c 100644 --- a/core/db/methods/user_hierarchy.go +++ b/core/db/methods/user_hierarchy.go @@ -101,7 +101,7 @@ func (u *UserHierarchyDB) CreateUserHierarchyDB(ctx context.Context, tx pgx.Tx, returnObjBool = returnObj[0] } if returnObjBool { - UserHierarchy, _, err := u.GetUserHierarchyListDB(ctx, typescore.ListDbOptions{Filtering: UserHierarchyObj}) + UserHierarchy, _, err := u.GetHierarchiesWithRelations(ctx, typescore.ListDbOptions{Filtering: UserHierarchyObj}) if err != nil { return nil, tx, err } @@ -147,7 +147,7 @@ func (u *UserHierarchyDB) UpdateUserHierarchyDB(ctx context.Context, tx pgx.Tx, if returnObjBool { options := typescore.ListDbOptions{Filtering: &typescore.UserHierarchy{ID: paramsUpdate.ID}} - getInfoUp, _, err := u.GetUserHierarchyListDB(ctx, options) + getInfoUp, _, err := u.GetHierarchiesWithRelations(ctx, options) if err != nil { return nil, nil, err } @@ -235,3 +235,68 @@ func (u *UserHierarchyDB) GetApproversDB(ctx context.Context, subordinateID, dep } return result, nil } + +func (u *UserHierarchyDB) GetHierarchiesWithRelations(ctx context.Context, opts typescore.ListDbOptions) ([]*typescore.UserHierarchy, uint64, error) { + options, filter, err := dbutils.GetOptionsDB[typescore.UserHierarchy](opts) + if err != nil { + return nil, 0, err + } + + query := squirrel.Select("uh.*", + "sub.id", "sub.full_name", "sub.username", // поля Subordinate + "app.id", "app.full_name", "app.username", // поля Approver + "dep.id", "dep.name", // поля Department + "cat.id", "cat.name", // поля Category + "top.id", "top.name", // поля Topic + "COUNT(*) OVER() AS total_count"). + From("user_hierarchy uh"). + LeftJoin("users sub ON uh.subordinate_id = sub.id"). + LeftJoin("users app ON uh.approver_id = app.id"). + LeftJoin("departments dep ON uh.department_id = dep.id"). + LeftJoin("approval_topic_categories cat ON uh.topic_category_id = cat.id"). + LeftJoin("approval_topics top ON uh.topic_id = top.id") + + query = dbutils.SetterLimitAndOffsetQuery(query, options.Offset, options.Limit) + query = dbutils.ApplyFilters(query, filter, options.LikeFields, dbcoretablenames.TableNameUserHierarchy.ToString()) + query = query.OrderBy("created_at DESC") + + sql, args, err := query.PlaceholderFormat(squirrel.Dollar).ToSql() + if err != nil { + return nil, 0, err + } + + rows, err := u.pool.Query(ctx, sql, args...) + if err != nil { + return nil, 0, err + } + defer rows.Close() + + var results []*typescore.UserHierarchy + var totalCount uint64 + + for rows.Next() { + h := &typescore.UserHierarchy{} + h.Subordinate = &typescore.User{} + h.Approver = &typescore.User{} + h.Department = &typescore.Department{} + h.TopicCategory = &typescore.ApprovalTopicCategory{} + h.Topic = &typescore.ApprovalTopic{} + + // Сканируем все поля в соответствующие структуры + err := rows.Scan( + &h.ID, &h.SubordinateID, &h.ApproverID, &h.DepartmentID, &h.TopicCategoryID, &h.TopicID, &h.Priority, &h.CreatedAt, &h.UpdatedAt, + &h.Subordinate.ID, &h.Subordinate.FullName, &h.Subordinate.Username, + &h.Approver.ID, &h.Approver.FullName, &h.Approver.Username, + &h.Department.ID, &h.Department.Name, + &h.TopicCategory.ID, &h.TopicCategory.Name, + &h.Topic.ID, &h.Topic.Name, + &totalCount, + ) + if err != nil { + continue + } + results = append(results, h) + } + + return results, totalCount, nil +} diff --git a/service/rest_admin_service/cmd/app.go b/service/rest_admin_service/cmd/app.go index a56d5a4..2f98b2e 100644 --- a/service/rest_admin_service/cmd/app.go +++ b/service/rest_admin_service/cmd/app.go @@ -171,7 +171,7 @@ func initBaseApiRouter(ipc *typesm.InternalProviderControl) (*chi.Mux, error) { router.Use(corsOptions.Handler) router.Use(middleware.RealIP) - + router.Use(middleware.Recoverer) // Добавляем ограничение по IP router.Use(httprate.LimitByIP(rateLimit, rateWindow)) diff --git a/service/rest_admin_service/handlers/user_hierarchy/handlers.go b/service/rest_admin_service/handlers/user_hierarchy/handlers.go index 211796e..4685006 100644 --- a/service/rest_admin_service/handlers/user_hierarchy/handlers.go +++ b/service/rest_admin_service/handlers/user_hierarchy/handlers.go @@ -3,7 +3,6 @@ package hierarchieshandler import ( "bitrix_balancer_api/core/typescore" "bitrix_balancer_api/core/utilscore" - "context" "encoding/json" "errors" "github.com/sirupsen/logrus" @@ -46,16 +45,11 @@ func (s *HierarchiesReg) GetHierarchiesListHandler(w http.ResponseWriter, r *htt } ctx := r.Context() - hierarchiesDbLst, total, errW := s.ipc.Database.UserHierarchy.GetUserHierarchyListDB(ctx, options) + hierarchiesDbLst, total, errW := s.ipc.Database.UserHierarchy.GetHierarchiesWithRelations(ctx, options) if errW != nil { return nil, errW } - // 🚀 Подгружаем связанные данные - for _, h := range hierarchiesDbLst { - s.enrichHierarchy(ctx, h) - } - return &typescore.ApiResponseBase{ TotalCount: total, Data: hierarchiesDbLst, @@ -86,7 +80,7 @@ func (s *HierarchiesReg) GetHierarchyByIDHandler(w http.ResponseWriter, r *http. } ctx := r.Context() - hierarchies, _, err := s.ipc.Database.UserHierarchy.GetUserHierarchyListDB(ctx, typescore.ListDbOptions{ + hierarchies, _, err := s.ipc.Database.UserHierarchy.GetHierarchiesWithRelations(ctx, typescore.ListDbOptions{ Filtering: &typescore.UserHierarchy{ID: &id}, }) if err != nil { @@ -97,8 +91,6 @@ func (s *HierarchiesReg) GetHierarchyByIDHandler(w http.ResponseWriter, r *http. } hierarchy := hierarchies[0] - s.enrichHierarchy(ctx, hierarchy) - return hierarchy, nil } @@ -197,7 +189,6 @@ func (s *HierarchiesReg) CreateHierarchyHandler(w http.ResponseWriter, r *http.R } } - // 🛡 Проверяем, что такая связь ещё не существует existingHierarchies, _, err := s.ipc.Database.UserHierarchy.GetUserHierarchyListDB(ctx, typescore.ListDbOptions{ Filtering: &typescore.UserHierarchy{ SubordinateID: req.SubordinateID, @@ -225,8 +216,6 @@ func (s *HierarchiesReg) CreateHierarchyHandler(w http.ResponseWriter, r *http.R return nil, err } - s.enrichHierarchy(ctx, createdHierarchy) - return createdHierarchy, nil } @@ -353,8 +342,6 @@ func (s *HierarchiesReg) UpdateHierarchyHandler(w http.ResponseWriter, r *http.R return nil, err } - s.enrichHierarchy(ctx, updatedHierarchy) - return updatedHierarchy, nil } @@ -389,7 +376,6 @@ func (s *HierarchiesReg) DeleteHierarchyHandler(w http.ResponseWriter, r *http.R } ctx := r.Context() - err = s.ipc.Database.UserHierarchy.DeleteUserHierarchyDB(ctx, &req) if err != nil { return nil, err @@ -397,49 +383,3 @@ func (s *HierarchiesReg) DeleteHierarchyHandler(w http.ResponseWriter, r *http.R return true, nil } - -// enrichHierarchy подгружает связанные данные для записи иерархии -func (s *HierarchiesReg) enrichHierarchy(ctx context.Context, h *typescore.UserHierarchy) { - if h == nil { - return - } - - // Подгружаем подчинённого - if h.SubordinateID != nil { - subordinate, err := s.ipc.Database.User.GetUserByIDDB(ctx, *h.SubordinateID) - if err == nil && subordinate != nil { - h.Subordinate = subordinate - } - } - - // Подгружаем согласующего - if h.ApproverID != nil { - approver, err := s.ipc.Database.User.GetUserByIDDB(ctx, *h.ApproverID) - if err == nil && approver != nil { - h.Approver = approver - } - } - - // Подгружаем отдел - if h.DepartmentID != nil { - department, err := s.ipc.Database.Department.GetDepartmentByIDDB(ctx, *h.DepartmentID) - if err == nil && department != nil { - h.Department = department - } - } - - if h.TopicCategoryID != nil { - category, err := s.ipc.Database.ApprovalTopicCategory.GetApprovalTopicCategoryByIDDB(ctx, *h.TopicCategoryID) - if err == nil && category != nil { - h.TopicCategory = category - } - } - - // 🆕 Подгружаем тему - if h.TopicID != nil { - topic, err := s.ipc.Database.ApprovalTopic.GetApprovalTopicByIDDB(ctx, *h.TopicID) - if err == nil && topic != nil { - h.Topic = topic - } - } -}