|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | +) |
| 7 | + |
| 8 | +// NavItem is a single sidebar entry with an explicit position. |
| 9 | +// |
| 10 | +// This type lives in the middleware package (not client/models) following the |
| 11 | +// same convention as OIDCOrganizationSettings: the endpoint is simple enough |
| 12 | +// (two fields, a GET/PUT pair) that a hand-defined type plus GenericRequest is |
| 13 | +// more direct than swagger-driven codegen for it. |
| 14 | +type NavItem struct { |
| 15 | + // Type is either "native" (a built-in section, identified by name, e.g. |
| 16 | + // "dashboard") or "plugin_widget" (identified by the widget's plugin ID as |
| 17 | + // a string). |
| 18 | + Type string `json:"type"` |
| 19 | + Key string `json:"key"` |
| 20 | + // Position is 1-indexed; positions must be unique within a NavConfig. |
| 21 | + Position uint32 `json:"position"` |
| 22 | +} |
| 23 | + |
| 24 | +// NavConfig is the per-organization sidebar nav ordering configuration. |
| 25 | +// Items is empty when no ordering has been saved — the console falls back to |
| 26 | +// its default ordering in that case. |
| 27 | +type NavConfig struct { |
| 28 | + Items []*NavItem `json:"items"` |
| 29 | +} |
| 30 | + |
| 31 | +// GetOrgNav returns the org's sidebar nav ordering config. |
| 32 | +func (m *middleware) GetOrgNav(org string) (*NavConfig, *http.Response, error) { |
| 33 | + var result *NavConfig |
| 34 | + resp, err := m.GenericRequest(Request{ |
| 35 | + Method: "GET", |
| 36 | + Organization: &org, |
| 37 | + Route: []string{"organizations", org, "nav"}, |
| 38 | + }, &result) |
| 39 | + if err != nil { |
| 40 | + return nil, resp, fmt.Errorf("failed to get organization nav ordering: %w", err) |
| 41 | + } |
| 42 | + return result, resp, nil |
| 43 | +} |
| 44 | + |
| 45 | +// UpdateOrgNav creates or replaces the org's sidebar nav ordering config. |
| 46 | +// Passing an empty (or nil) items slice resets the ordering to defaults. |
| 47 | +func (m *middleware) UpdateOrgNav(org string, items []*NavItem) (*NavConfig, *http.Response, error) { |
| 48 | + body := &NavConfig{Items: items} |
| 49 | + if body.Items == nil { |
| 50 | + body.Items = []*NavItem{} |
| 51 | + } |
| 52 | + |
| 53 | + var result *NavConfig |
| 54 | + resp, err := m.GenericRequest(Request{ |
| 55 | + Method: "PUT", |
| 56 | + Organization: &org, |
| 57 | + Route: []string{"organizations", org, "nav"}, |
| 58 | + Body: body, |
| 59 | + }, &result) |
| 60 | + if err != nil { |
| 61 | + return nil, resp, fmt.Errorf("failed to update organization nav ordering: %w", err) |
| 62 | + } |
| 63 | + return result, resp, nil |
| 64 | +} |
0 commit comments