Skip to content

Commit

Permalink
Propagate domain ID changes to objects (#301)
Browse files Browse the repository at this point in the history
* fix(api) propagate domain id change

* Update model.go

* fix(api) err var name

* fix(api) move propagate to repository
  • Loading branch information
helderbetiol authored Nov 23, 2023
1 parent 31fdd4d commit ca46ab5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 45 deletions.
56 changes: 11 additions & 45 deletions API/models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,46 +84,6 @@ func getChildrenCollections(limit int, parentEntStr string) []int {
return rangeEntities
}

// PropagateParentIdChange: search for given parent children and
// update their hierarchyName with new parent name
func PropagateParentIdChange(ctx context.Context, oldParentId, newId string, entityInt int) error {
// Find all objects containing parent name
req := bson.M{"id": primitive.Regex{Pattern: oldParentId + u.HN_DELIMETER, Options: ""}}
// For each object found, replace old name by new
update := bson.D{{
Key: "$set", Value: bson.M{
"id": bson.M{
"$replaceOne": bson.M{
"input": "$id",
"find": oldParentId,
"replacement": newId}}}}}
if entityInt == u.DOMAIN {
_, e := repository.GetDB().Collection(u.EntityToString(u.DOMAIN)).UpdateMany(ctx,
req, mongo.Pipeline{update})
if e != nil {
println(e.Error())
return e
}
} else if entityInt == u.DEVICE {
_, e := repository.GetDB().Collection(u.EntityToString(u.DEVICE)).UpdateMany(ctx,
req, mongo.Pipeline{update})
if e != nil {
println(e.Error())
return e
}
} else {
for i := entityInt + 1; i <= u.GROUP; i++ {
_, e := repository.GetDB().Collection(u.EntityToString(i)).UpdateMany(ctx,
req, mongo.Pipeline{update})
if e != nil {
println(e.Error())
return e
}
}
}
return nil
}

func updateOldObjWithPatch(old map[string]interface{}, patch map[string]interface{}) error {
for k, v := range patch {
switch patchValueCasted := v.(type) {
Expand Down Expand Up @@ -786,15 +746,21 @@ func UpdateObject(entityStr string, id string, updateData map[string]interface{}
}

if oldObj["id"] != updateData["id"] {
// Changes to id should be propagated to its children
err := PropagateParentIdChange(
// Changes to id should be propagated
if err := repository.PropagateParentIdChange(
ctx,
oldObj["id"].(string),
updateData["id"].(string),
entity,
)
if err != nil {
); err != nil {
return nil, err
} else if entity == u.DOMAIN {
if err := repository.PropagateDomainChange(ctx,
oldObj["id"].(string),
updateData["id"].(string),
); err != nil {
return nil, err
}
}
}

Expand Down Expand Up @@ -902,7 +868,7 @@ func SwapEntity(createEnt, deleteEnt, id string, data map[string]interface{}, us
}

// Propagate
if err := PropagateParentIdChange(ctx, id, data["id"].(string),
if err := repository.PropagateParentIdChange(ctx, id, data["id"].(string),
u.EntityStrToInt(data["category"].(string))); err != nil {
return nil, err
}
Expand Down
62 changes: 62 additions & 0 deletions API/repository/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,65 @@ func DeleteObject(ctx context.Context, entity string, filter bson.M) *u.Error {

return nil
}

// PropagateParentIdChange: search for given parent children and
// update their hierarchyName with new parent name
func PropagateParentIdChange(ctx context.Context, oldParentId, newId string, entityInt int) error {
// Find all objects containing parent name
req := bson.M{"id": primitive.Regex{Pattern: oldParentId + u.HN_DELIMETER, Options: ""}}
// For each object found, replace old name by new
update := bson.D{{
Key: "$set", Value: bson.M{
"id": bson.M{
"$replaceOne": bson.M{
"input": "$id",
"find": oldParentId,
"replacement": newId}}}}}
if entityInt == u.DOMAIN {
_, err := GetDB().Collection(u.EntityToString(u.DOMAIN)).UpdateMany(ctx,
req, mongo.Pipeline{update})
if err != nil {
println(err.Error())
return err
}
} else if entityInt == u.DEVICE {
_, err := GetDB().Collection(u.EntityToString(u.DEVICE)).UpdateMany(ctx,
req, mongo.Pipeline{update})
if err != nil {
println(err.Error())
return err
}
} else {
for i := entityInt + 1; i <= u.GROUP; i++ {
_, err := GetDB().Collection(u.EntityToString(i)).UpdateMany(ctx,
req, mongo.Pipeline{update})
if err != nil {
println(err.Error())
return err
}
}
}
return nil
}

// PropagateDomainChange: search for all objects with reference to the modified domain
func PropagateDomainChange(ctx context.Context, oldDomainId, newDomainId string) error {
// Find all objects containing this domain
req := bson.M{"domain": primitive.Regex{Pattern: "^" + oldDomainId + "(\\" + u.HN_DELIMETER + "|$)", Options: ""}}
// For each object found, replace old domain by new
update := bson.D{{
Key: "$set", Value: bson.M{
"domain": bson.M{
"$replaceOne": bson.M{
"input": "$domain",
"find": oldDomainId,
"replacement": newDomainId}}}}}
for i := u.STRAYOBJ; i <= u.GROUP; i++ {
_, err := GetDB().Collection(u.EntityToString(i)).UpdateMany(ctx,
req, mongo.Pipeline{update})
if err != nil {
return err
}
}
return nil
}

0 comments on commit ca46ab5

Please sign in to comment.