From f3b5f5c19f8ee298ddf22b81e7b9e44f9893f898 Mon Sep 17 00:00:00 2001 From: Kelly Merrick Date: Fri, 13 Sep 2024 14:16:56 -0500 Subject: [PATCH] fix(dasboards): remove deleted dashboards from users list (#1182) * fix(dasboards): remove deleted dashboards from users list * linter fixes * yet another linter fix * swap db delete and user update order --- api/dashboard/delete.go | 24 ++++++++++++++++++++++++ api/types/user.go | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/api/dashboard/delete.go b/api/dashboard/delete.go index 05b14e86b..093f0fb59 100644 --- a/api/dashboard/delete.go +++ b/api/dashboard/delete.go @@ -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()) @@ -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) @@ -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 diff --git a/api/types/user.go b/api/types/user.go index a7d7691c0..4d454526d 100644 --- a/api/types/user.go +++ b/api/types/user.go @@ -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{} }