Skip to main content

Contract C16 (feature 004).

Saved views API (/v1/orgs/{orgId}/saved-views)

A saved view stores a set of filters for the web Plants screen, so a user can reopen "Karachi residential, battery" with one click. A view is either PERSONAL (only its creator sees it) or TEAM (everyone in the org sees it).

:::note Status Verified against packages/api/src/handlers/saved-views.ts, packages/core/src/views/saved-views.ts and CreateSavedViewInput / UpdateSavedViewInput / SavedView in packages/core/src/schemas.ts. All examples are illustrative. :::

:::caution Not yet executed These endpoints have not been executed against a deployed stage. A call with no token is expected to get API Gateway's own 401 {"message":"Unauthorized"} (illustrative, from the authorizer config). :::

Rules that apply to every route​

  • Auth: Cognito JWT. The handler requires plants ≥ VIEW in the path org.
  • Org: the path {orgId} wins. If X-Org-Id is sent and differs (and the caller is not a platform member) → 400 VALIDATION_FAILED (details.header: "X-Org-Id").
  • Who can write: PERSONAL views, only the owner (another user's personal view is simply 404). TEAM views need plants ≥ EDIT to create, edit or delete.
  • Caps: 50 personal views per user per org, 100 team views per org. Over the cap → 422 LIMIT_EXCEEDED.
  • Name: 1–80 characters, trimmed, unique per visibility (per owner for PERSONAL, per org for TEAM). A clash → 409 ALREADY_EXISTS.
  • filters: the same filter object as the web plant list (GET /v1/plants), strict. Unknown keys, bad ranges, or a sort the chosen list path does not support → 422 VALIDATION_FAILED.
  • externalFilters: optional, opaque to the server, applied by the app against TCP data. Only status (array of up to 20 strings, each 1–40), alertState, communication (strings 1–40) and, since 2026-11-25, model (array of up to 20 device model names, each 1–64) are allowed, total 2048 bytes max.
  • sort: name, -name, createdAt or -createdAt.

SavedView shape​

FieldTypeNotes
viewIdstring (ULID)
screenPLANTSOnly value today
namestring
visibilityPERSONAL | TEAMCannot be changed after create
orgIdstring
filtersobjectAs saved
externalFiltersobject?
sortstring?
countinteger?Approximate number of plants. Present only when filters holds exactly one facet with one value (for example just tagId) and the caller has unscoped access. Otherwise absent — call GET …/{viewId}/count for any other view
stalebooleantrue when a referenced tag is missing or being deleted, or a referenced org no longer exists. Show a "this view is out of date" hint
createdBy, createdAt, updatedAtstringISO-8601 UTC
versionintegerSend as If-Match on PATCH

GET /v1/orgs/{orgId}/saved-views — List​

Query screen=PLANTS (optional). Returns the caller's personal views plus the org's team views as { "items": [SavedView] }. Not paginated: at most 150 items by the caps.

// illustrative example
{ "items": [ { "viewId": "01JB7Q4M2X8K3T5V6W7Y8Z9A0B", "screen": "PLANTS", "name": "Karachi battery",
"visibility": "TEAM", "orgId": "<org-id>", "filters": { "tagId": "<tag-id>" }, "count": 42, "stale": false,
"createdBy": "<sub>", "createdAt": "2026-10-13T08:00:00.000Z", "updatedAt": "2026-10-13T08:00:00.000Z", "version": 1 } ] }

Errors: 400, 401, 403.

POST /v1/orgs/{orgId}/saved-views — Create​

  • Headers: Idempotency-Key required (missing → 428 PRECONDITION_REQUIRED).
  • Body: { screen, name, visibility, filters, externalFilters?, sort? }. Unknown keys → 422.
  • Response: 201 with the SavedView, plus Location: /v1/orgs/{orgId}/saved-views/{viewId} and ETag.
// illustrative request
{ "screen": "PLANTS", "name": "Karachi battery", "visibility": "TEAM", "filters": { "tagId": "<tag-id>" }, "sort": "name" }

Errors: 400, 403 (TEAM without plants ≥ EDIT), 409 ALREADY_EXISTS, 422 VALIDATION_FAILED / LIMIT_EXCEEDED, 428.

PATCH /v1/orgs/{orgId}/saved-views/{viewId} — Update​

  • Headers: If-Match: "<version>" required (428 if missing).
  • Body: any of name, filters, externalFilters, sort (at least one). null clears externalFilters or sort. visibility and screen cannot be changed (sending them → 422).
  • Response: 200 with the updated SavedView and a new ETag.

Errors: 400, 403, 404, 409 VERSION_CONFLICT / ALREADY_EXISTS, 422, 428.

DELETE /v1/orgs/{orgId}/saved-views/{viewId} — Delete​

Same permission as PATCH. No If-Match. Response 204. Errors: 400, 403, 404.

GET /v1/orgs/{orgId}/saved-views/{viewId}/count — Bounded match count​

Added 2026-11-24 (console item 1.8). Counts the plants a view's filters match for the caller, for any filter combination (several values, several facets, q, capacity or date ranges, favorite).

  • How. Runs the view's filters through the GET /v1/plants list path itself — same visibility rules, same indexes, never a Scan — 50 rows per page, and stops at 500 matches or 20 pages, whichever comes first.
  • Response 200. { viewId, count, capped, externalFiltersIgnored? }. capped: true means the count stopped early: show ${count}+. The view's sort does not matter. Client-side externalFilters (telemetry filters) are not applied; externalFiltersIgnored: true says so.
  • Auth. As the list: plants ≥ VIEW in {orgId}; another user's personal view, or a missing view, is 404. No query parameters (any → 400). Stored filters that no longer parse → 422 VALIDATION_FAILED (field filters).
  • Cost. At most 20 Query pages plus 20 BatchGets of up to 50 plant records; a small org needs one of each. Call it for the views on screen (for example when the saved-view menu opens), not for all 150 views up front, and prefer the list's own count when it is present.

Illustrative example (derived from the handler; no execution captured yet):

{ "viewId": "<view-id>", "count": 500, "capped": true }

Tag deletion​

When someone deletes a tag, the tag-dependents check lists team views that filter on that tag (kind SAVED_VIEW). Personal views are not listed (finding them would need a table scan, which is banned). They show stale: true on their owner's list instead.

Known gap​

The app replays a view's filters through GET /v1/plants. Partner-org callers currently get an empty list there, and scoped callers can only sort by name. Saving such a view is allowed.