Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dasboards): remove deleted dashboards from users list #1182

Merged
merged 7 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions api/dashboard/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func DeleteDashboard(c *gin.Context) {
l := c.MustGet("logger").(*logrus.Entry)
d := dashboard.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

l.Debugf("deleting dashboard %s", d.GetID())

Expand All @@ -70,6 +71,19 @@ func DeleteDashboard(c *gin.Context) {
return
}

// Remove the dashboard ID from the user's dashboards
dashboards := u.GetDashboards()

updatedDashboards := []string{}

for _, id := range dashboards {
if id != d.GetID() {
updatedDashboards = append(updatedDashboards, id)
}
}

u.SetDashboards(updatedDashboards)

err := database.FromContext(c).DeleteDashboard(c, d)
if err != nil {
retErr := fmt.Errorf("error while deleting dashboard %s: %w", d.GetID(), err)
Expand All @@ -80,6 +94,16 @@ func DeleteDashboard(c *gin.Context) {
}

c.JSON(http.StatusOK, fmt.Sprintf("dashboard %s deleted", d.GetName()))

// send API call to update the user
u, err = database.FromContext(c).UpdateUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}
}

// isAdmin is a helper function that iterates through the dashboard admins
Expand Down
2 changes: 1 addition & 1 deletion api/types/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (u *User) GetFavorites() []string {
// When the provided User type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (u *User) GetDashboards() []string {
// return zero value if User type or Favorites field is nil
// return zero value if User type or Dashboard field is nil
if u == nil || u.Dashboards == nil {
return []string{}
}
Expand Down
Loading