Skip to content

fix(server): keep costly generation to admins and managers - #1769

Merged
gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:fix/costly-actions-managers
Oct 29, 2026
Merged

gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:fix/costly-actions-managers

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Costly generation is now for admins and managers only, on the server and in the UI. This follows the role model the product owner decided on 2026-10-29: daily work is open to every role, while settings, permanent deletes, adding prompts and costly actions are for admins and managers. An admin can make any number of managers, so a team can share this work.

Server: 403 "Only admins and managers can …" for analysts and agency partners

Route Refusal
POST /api/prompts/suggestions/:brandId/refresh, /suggestions/:brandId/page, /api/prompts/suggest generate prompt suggestions
POST /api/topics/suggest, /api/topics/suggestions/:brandId/refresh generate topic suggestions
POST /api/content/opportunities/generate, /:id/alternative, /prompts/:promptId/suggest, /api/content/generate generate opportunities
POST /api/content/:id/brief generate briefs
POST /api/prompts/:promptId/improve, and sending Improve to the Action Center use Improve

The brief, /content/generate, prompts/suggest and topics/suggest catch blocks turned the 403 into a 500; they now pass it on.

Web: the buttons that start these show only to admins and managers. Where a button is hidden, a one-line note says who can do it (managersOnly in en.json: "Ask an admin or a manager to …").

  • Prompts:
    • Prompt Suggestions: New, Generate Suggestions, Generate new suggestions.
    • Topic suggestions: a note in the empty state.
    • The Improve tab: Generate, Regenerate, Send Actions.
  • Content:
    • Opportunities: New.
    • The opportunity panel: Generate Brief, Generate Content Brief, Another angle.
  • Insights:
    • Opportunities card: Track and Suggest content. Track adds a prompt, so it is for managers like + Track.
  • Mentions drawer and AI Traffic pages: Suggest content, Add prompts.

Row menus hide the item without a note.

Stays open to every role: writing, fixing and rewriting in the Content Editor; Site Audit runs; task updates.

Related issue

None.

Stacked on #1763 (server role checks, assertRole). The diff against it holds only this change.

Type of change

  • fix — Bug fix

How to test

  1. cd server && npx vitest run src/routes/costly-roles.test.js src/routes/prompts-improve-send.test.js: across 12 routes, analysts and agency partners get 403 and nothing is generated or queued; managers pass.
  2. Web tests for the Prompt Suggestions card, Content page, opportunity panel, Insights, Mentions, Improve tab, Traffic Pages and the topic suggestions card.
  3. Signed in as an analyst, none of the buttons above shows, and the notes say who can do it.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Server: format, lint, and 282 test files / 3,931 tests (3 skipped) pass
  • Web: lint (0 errors, 7 warnings), typecheck and build pass. Tests: 555 of 556 files. _before-publish.test.tsx failed on the focus race that test(content): wait for the dialog's first focus before opening the JSON-LD code #1753 fixes; this branch doesn't touch it.
  • Browser check as analyst / agency partner: in the next batched local test

🤖 Generated with Claude Code

cihan-gyk and others added 4 commits October 29, 2026 14:17
…anges

The API writes with the service role, so the database's admin-or-manager
rules on content_opportunities and the suggestion tables never reached
these routes: an analyst could delete opportunities for good, and an agency
partner could dismiss prompt suggestions. A shared assertRole in
lib/access.js now checks the profile's role after the organization check:

- PATCH /api/content/:id/status: any member records progress; dismissing,
  or moving a dismissed one back, is for admins and managers.
- POST /api/content/bulk/status and /bulk/delete: admins and managers.
- POST /api/prompts/suggestions/:id/dismiss: not agency partners, as the
  table's update rule; /accept: admins and managers, who add prompts.
- POST /api/topics/suggestions/:id/accept and /dismiss: admins and
  managers, as the Topics page offers them.

The web hides what the role can't do: the Opportunities bulk bar's Done,
Dismiss All and Delete and the row and panel Dismiss; on Prompt
Suggestions, Add and Add All for analysts, and Dismiss and Clear All for
agency partners too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
An admin can make as many managers as the team needs, so generating with
AI is theirs (2026-10-29): suggestions, opportunities, briefs and Improve.
The routes check the caller's role after the organization check and before
anything is generated or queued, with a 403 that says who can:

- POST /api/prompts/suggestions/:brandId/refresh, /suggestions/:brandId/page
  and /suggest
- POST /api/topics/suggest and /suggestions/:brandId/refresh
- POST /api/content/generate, /opportunities/generate, /:id/alternative,
  /prompts/:promptId/suggest and /:id/brief
- POST /api/prompts/:promptId/improve, and the Improve send to the Action
  Center (the dashboard's and an API key's alike)

The web shows these only to admins and managers and tells anyone else who
can, from en.json (managersOnly): Prompt Suggestions' New and Generate, the
Topic suggestions' Generate, Content Opportunities' New, the panel's
Generate Brief and Another angle, the Improve tab's Generate, Regenerate
and Send Actions, Insights' and Mentions' Suggest content, and the AI
Traffic pages' Add prompts and Suggest content.

Writing in the Content Editor, Site Audits and task updates stay open to
every member.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…gers

Track adds prompts, which only admins and managers may do, but the Insights
Opportunities card offered it to everyone. It now shows only to them, as
Suggest content already does; anyone else keeps a topic's Send action and
reads who can track or suggest content (managersOnly, en.json).

Adds tests for the card by role and for Topic Suggestions by role.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good with 94b9b9e8: rebased onto main and fixed two tests

Reviewed this PR's own change (#1763's head 47bfd96 → d16b459: f35d799 and d16b459), read on main 3b7611c.

What I changed (branch take-1769, built on main 3b7611c: this PR's two commits cherry-picked onto it, plus two test fixes)

  • The base. As it stands, the branch doesn't merge with main. #1763 was squashed with later changes, so its old copy here conflicts in four web files: content/page.test.tsx, _suggestions-card.tsx, _suggestions-card.test.tsx and opportunity-detail.test.tsx. The PR's own two commits apply to main cleanly.
  • 0002acec, _topic-suggestions-card.test.tsx. The card remembers being open in localStorage. The first test opens it, so in the second test the mount opens it and the test's click closes it again. "tells an analyst or an agency partner who generates them" then failed. The tests now clear storage first. The test still fails if the card's change is removed.
  • 94b9b9e8, prompts-improve-page-checks.test.js. This test came to main with #1724, after this branch was cut. Its user had no role, so with this PR POST /:promptId/improve answered 403 and the test failed. The user is now a manager.

What I checked

  • Placement. Each of the 12 routes checks the role after its own access check, so another organization's id still reads as not found. Each check also runs before quota, idempotency and the job are touched.
    • /content/generate, /opportunities/generate, /:id/alternative, /prompts/:promptId/suggest and /:id/brief
    • /prompts/suggestions/:brandId/refresh and /page, /prompts/suggest, /:promptId/improve and sendImproveAction
    • /topics/suggest and /topics/suggestions/:brandId/refresh
  • sendImproveAction. It serves both the dashboard's Send Actions and the internal route behind the MCP's create_improve_action, so an API key is checked by both layers.
  • Error passing. The catch blocks that used to turn the 403 into a 500 now pass through status and code.
  • Setup. /prompts/suggest and /topics/suggest also run during setup and Add brand. A new organization's first user gets admin by default, and adding a brand is admin/manager in RLS, so setup isn't blocked.
  • Each check is tested. I removed each of the 12 checks one at a time, and costly-roles.test.js failed every time, on that route. Without the server change, prompts-improve-send.test.js fails too.
  • Web. The buttons read canManage from useUserRole, the same flag the RLS-backed UI already uses. The dashboard layout fills it on the server, so it doesn't flicker. Where a button is hidden, a note shows instead, and row menus drop the item.
  • Embeds: none added or changed.
  • Read-only workspace. No new write route or server action. subscription-guard-coverage.test.ts passes on main with this PR and #1770.
  • Tests (on main with this PR and #1770). Full server suite: 284 of 295 files pass. The rest:
    • Eight Framer/CMS files can't load framer-api in my local install, the same on main.
    • The content-people and audit timing tests pass when re-run.
    • The improve page-checks test is fixed above.
    • Web: tsc is clean, and the touched component tests, lib/mcp, api/mcp, lib/agent and subscription-guard-coverage pass: 61 files, 408 tests. The full web suite gives 598 of 600 files; the two timing tests that failed under load (shopping categories, Library paging) pass when re-run. eslint and prettier are clean on the touched files.

Before merging

  • Merge take-1769, not this branch: this branch conflicts with main.
  • Web and API can ship in either order. With the web first, analysts lose the buttons early. With the API first, an analyst's click gets the 403 message.

@gkhngyk
gkhngyk merged commit 5956f4d into ansvisor:main Oct 29, 2026
4 of 5 checks passed
gkhngyk added a commit that referenced this pull request Oct 30, 2026
…gestions (#1808)

* fix(prompts): ask for a topic before adding prompts or generating suggestions

Prompts are grouped under topics, but a brand with none could still add
them: the Add Prompt dialog said "No topics defined yet. Add topics in brand
settings." and saved the prompt without a topic, Track buttons did the same,
and generating suggestions made topics the person never chose from the AI's
labels (findOrCreateTopic).

Server (API):
- POST /api/prompts/suggestions/:brandId/refresh and /:brandId/page answer
  409 { code: 'no_topics' } for a brand with no active topic, after the
  role check and before anything is generated, reserved or written.

Server actions and MCP:
- addPromptToSet (every single add: suggestions, Add All, Insights Track,
  fan-out Track, AI Traffic, People Also Ask) and importPrompts (Add Prompt
  dialog, bulk import) return { error, code: 'no_topics' } and write
  nothing. trackPagePrompts checks before it makes a topic.
- refreshPromptSuggestions returns { ok: false, failure: 'no_topics' } as a
  value; other failures still throw, as before.
- accept_prompt_suggestion says to add a topic first (accept_topic_suggestion
  or Prompts > Topics), and its description says so.
- A failed topics read never counts as "no topics".

UI:
- One TopicFirstDialog / TopicFirstNotice (en.json "topicFirst") with an
  "Add a topic" link to /dashboard/prompts/topics?new=1, which opens Add
  Topic once and clears the param. Buttons stay visible; a click that hits
  no_topics shows it.
- Add Prompt shows the notice instead of the form when the brand has no
  topics; it reads topics on every open, so a topic added meanwhile counts.
- Brand > Manage prompts replaces the old "brand settings" text in both
  cards and asks for a topic instead of adding one without.
- Onboarding and brand setup (savePromptSet) are unchanged: they create the
  chosen topics first. Role gating (#1763, #1769) is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(prompts): add a topic in place on the Prompts page, and say why suggestions failed

Review follow-ups on the topic-first flow.

- Prompts page: "Add a topic" in Add prompt, Suggestions and Query Fan-out
  is a button that closes the dialog, switches to the Topics tab with
  showSection and asks TopicsTab to open Add Topic (openAddTopic /
  onAddTopicOpened). The page reads its tab from the route only when it
  opens, so the ?new=1 link changed the address alone after a tab switch.
  Insights, AI Traffic and the prompt page keep the link.
- Roles: AddTopicLink, TopicFirstNotice and TopicFirstDialog read
  canManage. Since #1790 only admins and managers write topics; anyone else
  gets managersOnly.addTopic and no link, and the dialog's button says Close.
- refreshPromptSuggestions returns every failure as a value, as #1777's
  reads do (apiFailure + isTransientStatus on the body read once, so
  no_topics stays its own): a used-up budget, a read-only workspace or the
  demo brand, a role refusal, an outage, no answer. The card shows them with
  useFailureToast (the upgrade dialog for subscription) instead of a
  message production hides.
- Brand > Manage prompts links to that brand's own topics
  (/dashboard/brands/:id/topics#add-topic, as the brand card does), not the
  active brand's.
- trackPagePrompts reads active topics only; the AI Traffic page dialog
  forgets a Track's no_topics on the next open; the Insights test has its
  own describe block.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* docs(help): say a brand adds a topic before its prompts

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Gökhan Geyik <[email protected]>