Skip to content

fix(platform): auto-create default org only on truly fresh instances#1702

Merged
larryro merged 1 commit into
mainfrom
fix/default-org-fresh-instance
May 11, 2026
Merged

fix(platform): auto-create default org only on truly fresh instances#1702
larryro merged 1 commit into
mainfrom
fix/default-org-fresh-instance

Conversation

@larryro

@larryro larryro commented May 11, 2026

Copy link
Copy Markdown
Collaborator

Summary

Commit 9b92396 replaced the dashboard's auto-create-default-org flow with an unconditional redirect to the create-organization form. That broke the invariant relied on by many hardcoded orgSlug: 'default' callsites (agents, chat, integrations, providers, login policy, etc.): on a fresh instance, the first user picked a free-form name, ended up with a non-default slug, and every hardcoded reference misfired. This PR restores the auto-create, but only when the instance has zero organizations; multi-org deployments and uninvited users still get the form.

Branching

Scenario Instance orgs User memberships Behavior
A. Fresh instance 0 0 Auto-seed default, set active, init workflows, record switch, navigate to /dashboard/$id
B. Multi-org instance, uninvited user ≥1 0 Redirect to /dashboard/create-organization (current 9b92396 behavior)
C. Existing user ≥1 ≥1 Unchanged — last-active routing

Implementation

  • New instanceHasAnyOrganization query (auth-gated, mirrors the existing hasAnyUsers helper pattern; one-row findMany for fast existence check).
  • Dashboard's 0-membership branch now gates on instanceHasAnyOrg. On auto-create failure (e.g. slug-conflict race against another tab/user), the cached existence query is invalidated and resolvedRef is reset so the effect re-runs and routes the loser through the form branch — no orphan state.

Known follow-up (out of scope)

In scenario B, if the user types "Default" / "default" into the form, the server-side beforeCreateOrganization already rejects with Organization slug "default" is already taken., but the form's generic error toast obscures it. Surfacing the server message verbatim is a separate UX polish.

Pre-merge checklist

  • Ran bun run check (format, lint, typecheck, all tests) — all 38 tasks succeeded, 3377 tests passed, 0 lint errors.
  • Updated services/platform/messages/{en,de,fr}.json for any new/renamed/removed keys — N/A (no user-facing strings changed).
  • Updated docs/, docs/de/, and docs/fr/ for every user-visible change — N/A (internal redirect-branch logic; the user-facing form and dashboard surfaces are unchanged).
  • Ran bun run --filter @tale/docs lint (broken links + oxlint) — N/A (no docs touched).
  • Updated README.md, README.de.md, README.fr.md if the change affects what they document — N/A.

Test plan

  • Scenario A (fresh instance auto-create): wipe the Convex DB, sign up the first user → lands on /dashboard/<orgId> with slug=default. Open a feature that hardcodes 'default' (e.g. integrations or chat) and confirm it works.
  • Scenario B (multi-org instance, uninvited new user): with default already present, sign up a brand-new user → redirected to /dashboard/create-organization. Submit name "Acme" → acme org created, lands on its dashboard. Try name "Default" → friendly server error toast.
  • Scenario C (existing user): sign in as a user who already has orgs → unchanged last-active routing.
  • Race condition: in two private windows, sign up two users on a fresh instance and click submit near-simultaneously → one lands on a dashboard; the other lands on /dashboard/create-organization with no orphan state.

Summary by CodeRabbit

Release Notes

  • New Features
    • Auto-seeding of a default organization with initialized workflows when a new user accesses the dashboard on an empty instance, streamlining initial onboarding.

Review Change Stack

Commit 9b92396 replaced the dashboard's auto-create-default flow with an
unconditional redirect to the create-organization form. That broke the
invariant relied on by many hardcoded `orgSlug: 'default'` callsites
(agents, chat, integrations, providers, login policy, etc.): on a fresh
instance, the first user picked a free-form name, ended up with a
non-`default` slug, and every hardcoded reference misfired.

Restore the auto-create, but only when the instance has zero
organizations. Add an auth-gated `instanceHasAnyOrganization` query
(mirrors the existing `hasAnyUsers` helper pattern), and gate the
dashboard's 0-membership branch on it:

  - instance has 0 orgs                 -> seed `default`, set active,
                                           init workflows, record switch,
                                           navigate to /dashboard/$id
  - instance has >=1 org, user has 0    -> redirect to create-organization
                                           form (current 9b92396 behavior;
                                           multi-org deployments, uninvited
                                           users)
  - user has >=1 membership             -> unchanged (last-active routing)

On auto-create failure (e.g. slug-conflict race against another tab),
invalidate the cached existence query and drop the resolved guard so the
effect re-runs and routes the loser through the form branch.
@coderabbitai

coderabbitai Bot commented May 11, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 57988696-a05e-4141-a15f-7e1c94755d81

📥 Commits

Reviewing files that changed from the base of the PR and between 9b92396 and e4902df.

⛔ Files ignored due to path filters (1)
  • services/platform/convex/_generated/api.d.ts is excluded by !**/_generated/**
📒 Files selected for processing (3)
  • services/platform/app/routes/dashboard/index.tsx
  • services/platform/convex/organizations/has_any_organization.ts
  • services/platform/convex/organizations/queries.ts

📝 Walkthrough

Walkthrough

This PR adds automatic seeding of a default organization for users logging in to empty instances. It introduces a backend Convex query (instanceHasAnyOrganization) that detects whether the instance contains any organizations, then modifies the dashboard route to auto-create and initialize a "default" org when both the user has zero memberships and the instance has no organizations. The seeding flow includes creating the org, activating it, initializing default workflows, recording org-switch auditing, and navigating to the new org's dashboard. If creation fails due to slug conflicts, the flow invalidates the query and falls back to the normal create-organization path.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

  • tale-project/tale#1698: Directly conflicts with this PR—one adds auto-creation and workflow initialization for a seeded default organization, while the other removes that flow and redirects to create-organization instead.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: restoring auto-creation of the default organization but only on truly fresh instances, directly addressing the core issue.
Description check ✅ Passed The description provides a comprehensive summary with clear context, implementation details, and a complete pre-merge checklist with all items appropriately marked. Test plan includes specific scenarios.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/default-org-fresh-instance

Comment @coderabbitai help to get the list of available commands and usage tips.

@larryro larryro merged commit f8de84c into main May 11, 2026
25 checks passed
@larryro larryro deleted the fix/default-org-fresh-instance branch May 11, 2026 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant