Skip to content

feat(db): keep integrations per brand, from a pool of the Account's sign-ins (brand access PR 14, 00470) - #2042

Merged
gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/brand-access-integrations
Nov 3, 2026
Merged

gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/brand-access-integrations

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Brand access PR 14 (Integrations 1): every integration becomes per brand (BRAND-ACCESS D5). One Google login can serve several brands without signing in again (D6), so a sign-in lives once in the Account's pool and each brand names the one it uses. Migration 00470. Spec: BRAND-ACCESS §3.3, §4, §5 and §7 (PR 14).

Behaviour-neutral while everyone is 'all': every brand uses the Account's Google connection, as before, and its own CMS site. Nothing on screen changes. The Brand settings cards, "Use the same account as {brand}" and the Account overview come in PR 15.

00470 · the pool and each brand's use of it

  • integration_accounts: the Account's sign-ins. That is one Google login per product (Composio keeps one connected account per product) or one CMS site. Each keeps Composio's entity and connected account ids, never a token, plus a label (the CMS site's name for now) and a status.
  • brand_integrations: the sign-in a brand uses per provider, unique (brand_id, provider). It has a settings jsonb for later; nothing writes it yet.
    • The Search Console and Analytics properties stay on brands.gsc_property / ga_property_id.
    • The CMS site and collection stay on brand_cms_connections.
    • Everything reads them there as before.
  • The switch-over (D7) copies what there is, and nothing disconnects:
    • each integration_connections row (Search Console, Analytics, Merchant Center) becomes a sign-in used by every brand of the Account, the demo brand included, as before;
    • each brand_cms_connections row becomes a sign-in used by its brand.
  • Triggers keep the pool in step while Settings › Integrations and the CMS cards still write the old tables. A new Google row is used by every brand of the Account. A removed one takes its sign-in and the brands' use of it. A brand added later uses the Account's Google sign-ins.
  • A check trigger: a brand uses only a sign-in of its own organization, for the provider it names.
  • RLS:
    • a brand's integrations are read by the people who can access the brand (user_brand_ids());
    • a sign-in is read by the people who can access a brand that uses it;
    • only the server writes them.
  • integration_connections loses its admin/manager write policy. The app never used it, and the API now calls Composio with the account that table names, so only the server may write it.

Server

  • lib/brand-integrations.js is the one resolver: brandConnection(brandId, provider) and connectedBrands(provider, { organizationId, brandId }). Before 00470 is applied, it reads the Account's row as before.
  • The Search Console and Analytics syncs pick the brands whose sign-in is live, with the same plan, active, demo and property filters. They call Composio with that brand's sign-in.
  • The Action Center's Analytics tool and its analytics source use the brand's sign-in.
  • Every Google call (listGscSites, queryGscSearchAnalytics, listGaProperties, runGaReport) names the connected account, as executeWebflowTool does.
  • GET /api/integrations/{gsc,ga}/properties?brandId= lists the properties of the brand's own sign-in. Anyone who can access the brand may call it; the demo brand lists none. Without brandId it lists the Account's, for All brands only, as before. The web doesn't send brandId yet.

Web

  • lib/brand-integrations.ts is the same resolver. The suggestion sources and AI Referral Traffic (getSuggestionSourceStates) and Chat's Google reads (google-reads.ts) use it. Before 00470 it reads the Account's rows.

Composio check (spec §4): several connected accounts under one entity work. connectedAccounts.link/initiate take allowMultiple: true (without it, a second ACTIVE account throws ComposioMultipleConnectedAccountsError). tools.execute and tools.proxyExecute take connectedAccountId. Webflow already uses exactly this (connectWebflowToken, executeWebflowTool). With only the entity, Composio would pick an account itself, so PR 14 names the account on every Google call, and getActiveConnection can look one up by id.

Left for later PRs

  • PR 15: the per-brand connect flow (link with allowMultiple), the Google label (e-mail), the cards.
  • PR 15: getting_started_facts still reads the Account's row, which is the same thing until a brand can have its own.
  • PR 16: Merchant Center and the catalog (catalog/sign-in.js workspaceEntity).

schema.sql, types/supabase.ts, the brand-access policy test, the demo coverage and write guard tests are updated, plus a schema test for 00470.

  • The policy test takes integration_connections off the deferred list: it now holds only the Account's own connection status.
  • integration_accounts reaches its brands through brand_integrations.

Related issue

None. BRAND-ACCESS spec, PR 14.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

Dev database: not applied yet. A verify script runs in a rolled-back transaction (before 00470 is applied, it runs the migration inside that transaction). It checks:

  • every old row was copied with the same ids and status;
  • every brand uses each of its Account's Google rows, and each CMS site its brand;
  • for every brand, nothing changes against the old row: connected per product, the Composio entity and account a call names, and the brands the nightly syncs pick;
  • under RLS, for an admin, an All-brands manager, a Selected-brands manager (with and without a brand shared), another organization's person and anon;
  • signed-in people can't write the three tables;
  • the triggers work on probe rows, and a brand can't use another organization's sign-in or another product's.

Then:

  1. bash supabase/build-schema.sh: schema.sql doesn't change.
  2. cd server && npx vitest run src/lib/brand-integrations.test.js src/lib/brand-access-integrations.test.js src/lib/brand-access-policies.test.js src/lib/composio.test.js src/lib/gsc-sync*.test.js src/lib/ga-sync.test.js src/routes/integrations-*.test.js src/lib/action-center/sources.test.js src/lib/demo/coverage.test.js src/lib/demo/write-guard.test.js
  3. cd web && yarn vitest run src/lib/brand-integrations.test.ts src/lib/mcp/chat-reads.test.ts src/lib/brand-access-functions.test.ts src/lib/supabase/embed-hints.test.ts plus the coverage tests.

Needs migration 00470 on prod (gkhngyk). Apply it before or with the deploy. A server or web deployed first reads the Account's rows as before.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint passes (0 errors; 7 old warnings in files this PR doesn't touch)
  • yarn typecheck passes
  • yarn format:check passes (changed files)
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change)

CI passed on this branch (2af121b), run on the fork (https://github.com/cihan-gyk/ansvisor/actions/runs/41561898315): schema is up to date; web format:check, lint, typecheck, all tests and yarn build pass; server lint, format:check and all tests pass.

🤖 Generated with Claude Code

cihan-gyk and others added 2 commits November 3, 2026 00:56
…ign-ins (brand access PR 14, 00470)

- 00470: integration_accounts (the Account's Google logins and CMS sites,
  Composio's entity and connected account ids, never a token) and
  brand_integrations (the sign-in each brand uses per provider, settings
  jsonb for later). The Account's Google rows are copied as sign-ins used by
  every brand; each CMS connection as a sign-in used by its brand. Triggers
  keep the pool in step while Settings > Integrations and the CMS cards still
  write the old tables, and give a new brand the Account's Google sign-ins.
  RLS: a brand's rows for people who can access it (user_brand_ids), a
  sign-in for people who can access a brand that uses it; the server writes.
  integration_connections loses its unused admin/manager write policy.
- Server: lib/brand-integrations.js resolves a brand's connection; the GSC
  and GA syncs, the Action Center's Analytics tool and sources use it, and
  every Google call names the connected account (several may share a
  Composio entity). The property lists take ?brandId= for the brand's own
  sign-in; without it, the Account's list for All brands as before.
- Web: lib/brand-integrations.ts; suggestion sources, AI Referral Traffic and
  Chat's Google reads ask it. Both resolvers read the Account's row until
  00470 is applied.
- Tests: resolver, schema, policy coverage (integration_connections off the
  deferred list), demo coverage, syncs, routes.

Behaviour-neutral while everyone is 'all': every brand uses the Account's
Google connection, as before.

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

resolveBrandSources reads the brand's Analytics connection through
lib/brand-integrations.js (00470), which asks for one row with maybeSingle;
the generate test's PostgREST stub had no maybeSingle, so every
generateActionsForBrand test threw.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ery brand on them

A brand's property list, its one-brand sync and its property pick now need
access to every brand that uses the brand's Google sign-in (All brands before
00470), since every brand still shares the Account's sign-in. Property picks
move from a direct brands update to a server route that checks the pick
against the brand's own sign-in's list, then starts the brand's sync.
A brand's Search Console site and Analytics property are picked through
the server, which checks the pick against the brand's sign-in. A
trigger now refuses a member's own insert or update of those two
columns, so the check can't be gone around.
gkhngyk
gkhngyk previously approved these changes Nov 3, 2026
00472 is taken by another open PR (report cards).

@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.

LGTM

@gkhngyk
gkhngyk merged commit 1e0b659 into ansvisor:main Nov 3, 2026
5 checks passed
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 4, 2026
Brand access PRs 12 and 14 landed on main as ansvisor#2050 and ansvisor#2042 with review
changes. Main's versions are kept, and PRs 13 and 15 and their fixes are
re-applied on top: the integrations routes keep main's sign-in reach check
(assertSignInReach) and per-brand property PUT routes next to the brand's
own sign-in routes, and the demo guard names both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants