feat(dashboard): global (All Libraries) preferences scope
The dashboard's All Libraries view could render sections across every library but could not persist its own preferences: the PUT required a UUID library_id, and the prefs GET-without-library_id matched an arbitrary library's row through a loose NULL OR-clause. Storage: a dedicated user_dashboard_preferences_global table (one row per user, user_id PK → users CASCADE) instead of a NULL/sentinel library_id — chosen over both because (a) the column's FK to libraries rules out a sentinel UUID without dropping the constraint or minting a fake library row, and (b) intent becomes structural: only the explicit global path writes that table, so a buggy or outside-the-API write can never masquerade as global. System and user collections are user-scoped, so the order/hidden arrays are library-independent and valid in this scope. API: PUT /api/dashboard/preferences binds library_id as *string — JSON null/absent = global, empty string = 400 (a lost value must stay loud, not silently clobber the global scope), valid UUID = per-library (unchanged). GET without library_id now returns the global row instead of an arbitrary one; the per-library query's dead NULL OR-arm is removed. GET /sections keeps requiring library_id for per-library views and aggregates across libraries when absent (unchanged). Tests: global save/update-dedupe/read round-trip, empty-string 400, global-vs-per-library independence. Two suite failures are the handoff's documented pre-existing rot (duplicate system collections in the dev DB; a sections test expecting a 400 the handler never implemented) — reproduced and confirmed unrelated to this change. Live-verified on the dev stack: null save 200 x2 with a single row updated, empty string 400, global read carries the second save.
This commit is contained in:
1 parent
b8db1d109c
commit
25d44452c0
8 files changed
+304
-7
No files matched your search
@@ -376,6 +376,22 @@ func (s *DashboardService) getUserCollectionItems(ctx context.Context, coll data
|
||||
}
|
||||
|
||||
func (s *DashboardService) GetDashboardPreferences(ctx context.Context, userID uuid.UUID, libraryID pgtype.UUID) (database.UserDashboardPreferences, error) {
|
||||
// Absent library = the global (All Libraries) scope: a separate
|
||||
// one-row-per-user table, so an accidental per-library write can never
|
||||
// masquerade as global.
|
||||
if !libraryID.Valid {
|
||||
g, err := s.db.GetGlobalDashboardPreferences(ctx, pgtype.UUID{Bytes: userID, Valid: true})
|
||||
if err != nil {
|
||||
return database.UserDashboardPreferences{}, err
|
||||
}
|
||||
return database.UserDashboardPreferences{
|
||||
UserID: g.UserID,
|
||||
HiddenCollections: g.HiddenCollections,
|
||||
CollectionOrder: g.CollectionOrder,
|
||||
ItemsPerSection: g.ItemsPerSection,
|
||||
UpdatedAt: g.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
return s.db.GetDashboardPreferences(ctx, database.GetDashboardPreferencesParams{
|
||||
UserID: pgtype.UUID{Bytes: userID, Valid: true},
|
||||
LibraryID: libraryID,
|
||||
@@ -383,6 +399,24 @@ func (s *DashboardService) GetDashboardPreferences(ctx context.Context, userID u
|
||||
}
|
||||
|
||||
func (s *DashboardService) UpsertDashboardPreferences(ctx context.Context, params database.UpsertDashboardPreferencesParams) (database.UserDashboardPreferences, error) {
|
||||
if !params.LibraryID.Valid {
|
||||
g, err := s.db.UpsertGlobalDashboardPreferences(ctx, database.UpsertGlobalDashboardPreferencesParams{
|
||||
UserID: params.UserID,
|
||||
HiddenCollections: params.HiddenCollections,
|
||||
CollectionOrder: params.CollectionOrder,
|
||||
ItemsPerSection: params.ItemsPerSection,
|
||||
})
|
||||
if err != nil {
|
||||
return database.UserDashboardPreferences{}, err
|
||||
}
|
||||
return database.UserDashboardPreferences{
|
||||
UserID: g.UserID,
|
||||
HiddenCollections: g.HiddenCollections,
|
||||
CollectionOrder: g.CollectionOrder,
|
||||
ItemsPerSection: g.ItemsPerSection,
|
||||
UpdatedAt: g.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
return s.db.UpsertDashboardPreferences(ctx, params)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user