Fixed parent theme searching in approver getting methods

This commit is contained in:
romanskijdev 2026-11-13 11:32:08 +03:00
parent 23ad47e62f
commit a145f659b4
2 changed files with 55 additions and 7 deletions

View File

@ -208,7 +208,7 @@ func (u *UserHierarchyDB) GetApproversDB(
SELECT t.id, LENGTH(t.code) as spec_score
FROM approval_topics t
CROSS JOIN target_topic tt
WHERE strpos(tt.code || '_', t.code || '_') = 1
WHERE string_to_array(t.code, '_') <@ string_to_array(tt.code, '_')
AND t.category_id = tt.category_id
),
ranked AS (
@ -285,7 +285,6 @@ func (u *UserHierarchyDB) GetApproversDB(
return approvers, nil
}
// GetHierarchiesWithRelations(ctx context.Context, opts typescore.ListDbOptions, userStatus string) ([]*typescore.UserHierarchy, uint64, error)
func (u *UserHierarchyDB) GetHierarchiesWithRelations(ctx context.Context, opts typescore.ListDbOptions, userStatuses ...string) ([]*typescore.UserHierarchy, uint64, error) {
options, filter, err := dbutils.GetOptionsDB[typescore.UserHierarchy](opts)
if err != nil {

View File

@ -383,16 +383,65 @@ func (s *FlowNodesReg) SaveEntireTreeHandler(w http.ResponseWriter, r *http.Requ
return nil, errors.New("category not found")
}
// Получаем текущие узлы из БД
oldNodes, _, _ := s.ipc.Database.ApprovalFlowNode.GetApprovalFlowNodesListDB(ctx, typescore.ListDbOptions{
Filtering: &typescore.ApprovalFlowNode{CategoryID: &categoryID},
})
oldNodesMap := make(map[uint64]*typescore.ApprovalFlowNode)
for _, n := range oldNodes {
oldNodesMap[*n.ID] = n
}
if err := s.ipc.Database.ApprovalFlowNode.DeleteFlowNodesByCategoryIDDB(ctx, categoryID); err != nil {
activeIDs := make(map[uint64]bool)
// Рекурсивная функция синхронизации
var syncNodes func(nodes []*typescore.ApprovalFlowNode, parentID *uint64) error
syncNodes = func(nodes []*typescore.ApprovalFlowNode, parentID *uint64) error {
for i, node := range nodes {
node.CategoryID = &categoryID
node.ParentID = parentID
sortOrder := i
node.SortOrder = &sortOrder
var currentID uint64
if node.ID != nil && oldNodesMap[*node.ID] != nil {
// Обновляем существующий (ID сохраняется)
updated, _, err := s.ipc.Database.ApprovalFlowNode.UpdateApprovalFlowNodeDB(ctx, nil, node, true)
if err != nil {
return err
}
currentID = *updated.ID
} else {
// Создаем новый
created, _, err := s.ipc.Database.ApprovalFlowNode.CreateApprovalFlowNodeDB(ctx, nil, node, true)
if err != nil {
return err
}
currentID = *created.ID
}
activeIDs[currentID] = true
if len(node.Children) > 0 {
if err := syncNodes(node.Children, &currentID); err != nil {
return err
}
}
}
return nil
}
// Запускаем синхронизацию
if err := syncNodes(req.Nodes, nil); err != nil {
return nil, err
}
if err := s.saveNodesRecursive(ctx, req.Nodes, categoryID, nil); err != nil {
return nil, err
// Удаляем только те узлы, которых больше нет в новом дереве
for _, oldNode := range oldNodes {
if !activeIDs[*oldNode.ID] {
s.ipc.Database.ApprovalFlowNode.DeleteApprovalFlowNodeDB(ctx, oldNode) //nolint:errcheck
}
}
s.bumpFlowVersion(ctx)
@ -403,11 +452,11 @@ func (s *FlowNodesReg) SaveEntireTreeHandler(w http.ResponseWriter, r *http.Requ
ctx, s.ipc,
*currentAdmin.ID, *currentAdmin.Login,
"update", "flow_tree", &categoryID,
fmt.Sprintf("Пересохранено дерево категории «%s» (было узлов: %d, стало: %d)", categoryName, len(oldNodes), countNodesRecursive(req.Nodes)),
fmt.Sprintf("Пересохранено дерево категории «%s» (было узлов: %d, стало: %d)", categoryName, len(oldNodes), len(activeIDs)),
map[string]interface{}{
"category_id": categoryID,
"nodes_before": len(oldNodes),
"nodes_after": countNodesRecursive(req.Nodes),
"nodes_after": len(activeIDs),
},
)
}