Skip to content

Commit

Permalink
Fixes bug with widgets referencing parent records instead of new uniq…
Browse files Browse the repository at this point in the history
…ue ones
  • Loading branch information
RyanStonebraker committed Sep 13, 2024
1 parent c1d2440 commit 0674568
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion api/ws/handlers/screen-configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,54 @@ func writeScreenConfiguration(screenConfig *protos.ScreenConfiguration, hctx wsH
for i, layout := range screenConfig.Layouts {
if layout.TabId == "" {
layout.TabId = hctx.Svcs.IDGen.GenObjectID()
layout.TabName = "Tab " + fmt.Sprint(i+1)
if layout.TabName == "" {
layout.TabName = "Tab " + fmt.Sprint(i+1)
}
}
for _, widget := range layout.Widgets {
if widget.Id == "" {
widget.Id = formWidgetId(widget, screenConfig.Id, i)
if widget.Data != nil {
// We have widget data, but no ID, so write it to the database with a new ID
_, err := hctx.Svcs.MongoDB.Collection(dbCollections.WidgetDataName).UpdateOne(sessCtx, bson.M{
"_id": widget.Id,
}, bson.M{
"$set": widget.Data,
}, options.Update().SetUpsert(true))
if err != nil {
return nil, err
}
}
} else if widget.Id != "" {
// We have a widget ID, but no data, so we'll just copy the respective widget record to a new one
var oldWidgetId = widget.Id
widget.Id = formWidgetId(widget, screenConfig.Id, i)
// Fetch old widget data
result := hctx.Svcs.MongoDB.Collection(dbCollections.WidgetDataName).FindOne(sessCtx, bson.M{
"_id": oldWidgetId,
})
if result.Err() != nil {
// We can't get the data, so we'll just continue
continue
}

widgetData := &protos.WidgetData{}
err = result.Decode(&widgetData)
if err != nil {
continue
}

widgetData.Id = widget.Id

// Write the data to the new widget ID
_, err := hctx.Svcs.MongoDB.Collection(dbCollections.WidgetDataName).UpdateOne(sessCtx, bson.M{
"_id": widget.Id,
}, bson.M{
"$set": widgetData,
}, options.Update().SetUpsert(true))
if err != nil {
return nil, err
}
}
}
}
Expand Down

0 comments on commit 0674568

Please sign in to comment.