Skip to content

Commit

Permalink
Merge pull request #692 from florkbr/add-active-workspace-support
Browse files Browse the repository at this point in the history
RHCLOUD-35156: Add support to chrome service for active workspace on user obj
  • Loading branch information
Hyperkid123 authored Oct 4, 2024
2 parents 83d88a1 + 843a51b commit 2504dc2
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
21 changes: 18 additions & 3 deletions cmd/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"

"github.com/RedHatInsights/chrome-service-backend/rest/database"
"github.com/RedHatInsights/chrome-service-backend/rest/models"
"github.com/joho/godotenv"
Expand All @@ -16,6 +17,7 @@ func main() {

var bundleRes *gorm.DB
var visitedRes *gorm.DB
var activeWorkspaceRes *gorm.DB
tx := database.DB.Begin().Session(&gorm.Session{
Logger: logger.Default.LogMode(logger.Info),
})
Expand Down Expand Up @@ -123,10 +125,18 @@ func main() {

fmt.Println("Seed default value to last visited pages")
visitedRes = tx.Model(&models.UserIdentity{}).Where("last_visited_pages IS NULL").Update("last_visited_pages", []byte(`[]`))
if bundleRes.Error != nil {
fmt.Println("Unable to migrate database!", bundleRes.Error.Error())
if visitedRes.Error != nil {
fmt.Println("Unable to migrate database!", visitedRes.Error.Error())
tx.Rollback()
panic(bundleRes.Error)
panic(visitedRes.Error)
}

fmt.Println("Seed default value to active workspace")
activeWorkspaceRes = tx.Model(&models.UserIdentity{}).Where("active_workspace IS NULL").Update("active_workspace", "default")
if activeWorkspaceRes.Error != nil {
fmt.Println("Unable to migrate database!", activeWorkspaceRes.Error.Error())
tx.Rollback()
panic(activeWorkspaceRes.Error)
}

err := tx.Commit().Error
Expand All @@ -144,5 +154,10 @@ func main() {
if visitedRes.RowsAffected > 0 {
logrus.Infof("Migrated %d user identity visited rows", visitedRes.RowsAffected)
}

if activeWorkspaceRes.RowsAffected > 0 {
logrus.Infof("Migrated %d user identity visited rows", activeWorkspaceRes.RowsAffected)
}

logrus.Info("Migration complete")
}
6 changes: 6 additions & 0 deletions docs/user-identity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# User routes

Store user specific information such as active workspace, favorited pages, visited bundles, UI preview enablement, and more

# Active Workspace
TODO
2 changes: 2 additions & 0 deletions rest/models/UserIdentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type UserIdentity struct {
DashboardTemplates []DashboardTemplate `json:"dashboardTemplates,omitempty"`
UIPreview bool `json:"uiPreview"`
UIPreviewSeen bool `json:"uiPreviewSeen"`
ActiveWorkspace string `json:"activeWorkspace"`
}

type UserIdentityResponse struct {
Expand All @@ -43,4 +44,5 @@ type UserIdentityResponse struct {
VisitedBundles datatypes.JSON `json:"visitedBundles,omitempty" gorm:"type: JSONB"`
UIPreview bool `json:"uiPreview"`
UIPreviewSeen bool `json:"uiPreviewSeen"`
ActiveWorkspace string `json:"activeWorkspace"`
}
27 changes: 27 additions & 0 deletions rest/routes/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func GetUserIdentity(w http.ResponseWriter, r *http.Request) {
VisitedBundles: updatedUser.VisitedBundles,
UIPreview: updatedUser.UIPreview,
UIPreviewSeen: updatedUser.UIPreviewSeen,
ActiveWorkspace: updatedUser.ActiveWorkspace,
}

resp := util.EntityResponse[models.UserIdentityResponse]{
Expand Down Expand Up @@ -155,11 +156,37 @@ func MarkPreviewSeen(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(resp)
}

type UpdateActiveWorkspacePayload struct {
ActiveWorkspace string `json:"activeWorkspace"`
}

func UpdateActiveWorkspace(w http.ResponseWriter, r *http.Request) {
user := r.Context().Value(util.USER_CTX_KEY).(models.UserIdentity)
var request UpdateActiveWorkspacePayload
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
handleIdentityError(err, w)
return
}
err = service.UpdateActiveWorkspace(&user, request.ActiveWorkspace)
if err != nil {
handleIdentityError(err, w)
return
}

resp := util.EntityResponse[models.UserIdentity]{
Data: user,
}

json.NewEncoder(w).Encode(resp)
}

func MakeUserIdentityRoutes(sub chi.Router) {
sub.Get("/", GetUserIdentity)
sub.Get("/intercom", GetIntercomHash)
sub.Post("/update-ui-preview", UpdateUserPreview)
sub.Post("/mark-preview-seen", MarkPreviewSeen)
sub.Post("/update-active-workspace", UpdateActiveWorkspace)
sub.Route("/visited-bundles", func(r chi.Router) {
r.Post("/", AddVisitedBundle)
r.Get("/", GetVisitedBundles)
Expand Down
13 changes: 13 additions & 0 deletions rest/service/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func CreateIdentity(userId string, skipCache bool) (models.UserIdentity, error)
FavoritePages: []models.FavoritePage{},
SelfReport: models.SelfReport{},
VisitedBundles: nil,
ActiveWorkspace: "default",
}
err := json.Unmarshal([]byte(`{}`), &identity.VisitedBundles)
if err != nil {
Expand Down Expand Up @@ -193,3 +194,15 @@ func UpdateUserPreview(identity *models.UserIdentity, preview bool) error {
func MarkPreviewSeen(identity *models.UserIdentity) error {
return database.DB.Model(identity).Updates(models.UserIdentity{UIPreviewSeen: true}).Error
}

func UpdateActiveWorkspace(identity *models.UserIdentity, workspace string) error {
identity.ActiveWorkspace = workspace
err := database.DB.Model(identity).Update("active_workspace", workspace).Error

// set the cache after successful DB operation
if err == nil {
util.UsersCache.Set(identity.AccountId, *identity)
}

return err
}

0 comments on commit 2504dc2

Please sign in to comment.