Skip to content

Commit

Permalink
refactor(api) fill hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
helderbetiol committed Jul 22, 2024
1 parent 7f23c9a commit 14dc62f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
33 changes: 19 additions & 14 deletions API/models/entity_get_hierarchy.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func getHierarchyByNamespace(namespace u.Namespace, userRoles map[string]Role, f
}

// Fill hierarchy with formatted data
fillHierharchyData(data, namespace, entityName, hierarchy, categories)
fillHierarchyData(data, namespace, entityName, hierarchy, categories)
}

// For the root of VIRTUAL objects we also need to check for devices
Expand Down Expand Up @@ -317,7 +317,7 @@ func getChildrenCollections(limit int, parentEntStr string) []int {
return rangeEntities
}

func fillHierharchyData(data []map[string]any, namespace u.Namespace, entityName string, hierarchy, categories map[string][]string) {
func fillHierarchyData(data []map[string]any, namespace u.Namespace, entityName string, hierarchy, categories map[string][]string) {
for _, obj := range data {
if strings.Contains(u.NamespaceToString(namespace), string(u.Logical)) {
// Logical
Expand All @@ -334,19 +334,24 @@ func fillHierharchyData(data []map[string]any, namespace u.Namespace, entityName
hierarchy[rootIdx+entityName] = append(hierarchy[rootIdx+entityName], objId)
}
}
} else if strings.Contains(obj["id"].(string), ".") {
// Physical or Org Children
categories[entityName] = append(categories[entityName], obj["id"].(string))
fillHierarchyMap(obj["id"].(string), hierarchy)
} else {
// Physical or Org Roots
objId := obj["id"].(string)
categories[entityName] = append(categories[entityName], objId)
if u.EntityStrToInt(entityName) == u.STRAYOBJ {
hierarchy[rootIdx+entityName] = append(hierarchy[rootIdx+entityName], objId)
} else if u.EntityStrToInt(entityName) != u.VIRTUALOBJ {
hierarchy[rootIdx] = append(hierarchy[rootIdx], objId)
}
fillHierarchyDataWithHierarchicalObj(entityName, obj, hierarchy, categories)
}
}
}

func fillHierarchyDataWithHierarchicalObj(entityName string, obj map[string]any, hierarchy, categories map[string][]string) {
objId := obj["id"].(string)
categories[entityName] = append(categories[entityName], objId)
if strings.Contains(objId, ".") {
// Physical or Org Children
fillHierarchyMap(objId, hierarchy)
} else {
// Physical or Org Roots
if u.EntityStrToInt(entityName) == u.STRAYOBJ {
hierarchy[rootIdx+entityName] = append(hierarchy[rootIdx+entityName], objId)
} else if u.EntityStrToInt(entityName) != u.VIRTUALOBJ {
hierarchy[rootIdx] = append(hierarchy[rootIdx], objId)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion API/models/entity_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ func propagateUpdateChanges(ctx mongo.SessionContext, entity int, oldObj, update
entity,
); err != nil {
return err
} else if entity == u.DOMAIN {
}
if entity == u.DOMAIN {
if err := repository.PropagateDomainChange(ctx,
oldObj["id"].(string),
updateData["id"].(string),
Expand Down

0 comments on commit 14dc62f

Please sign in to comment.