stack/apps/backend/src
BilalG1 512099ed23
Speed up dummy-project seeding (preview create-project ~15s → ~1.3s) (#1437)
## Summary

The internal `preview/create-project` endpoint was taking ~15s because
`seedDummyProject` created its dummy users one at a time through the
full `usersCrudHandlers.adminCreate` CRUD pipeline (one DB transaction +
config render per user, ~86 users). This reworks the seeding path to use
bulk inserts.

End-to-end, the endpoint's server-side handler time drops from
**~15,100ms → ~1,300ms** (~11× faster).

## Seeding changes (`seed-dummy-data.ts`)

- **`seedDummyUsers` — bulk insert.** Build every row (`ProjectUser`,
`ContactChannel`, `AuthMethod`, `ProjectUserOAuthAccount`,
`OAuthAuthMethod`, default permissions) up front with pre-generated
UUIDs, then insert via one `createMany` per table inside a single
transaction — replacing ~86 sequential `adminCreate` transactions.
Named-user team memberships are bulk-inserted the same way (`TeamMember`
+ `TeamMemberDirectPermission`). Idempotency is preserved with a single
up-front email lookup, so re-runs against an existing project still skip
existing users.
- **Native `randomUUID`.** The seed paths now use `node:crypto`'s
`randomUUID()` instead of stack-shared's `generateUuid()`. The
browser-safe polyfill calls `crypto.getRandomValues` ~31× per UUID (once
per template char, each with a fresh `Uint8Array(1)`); generating
thousands of seed UUIDs made that ~800ms of pure CPU in the
activity-event build alone.
- **`seedBulkSignupsAndActivity`.** Skip the redundant back-date
`UPDATE` for freshly-inserted users (`createMany` already writes correct
`createdAt`/`signedUpAt`), and flush ClickHouse events in larger,
parallel batches.
- **`seedDummyProject`.** Run `seedBulkSignupsAndActivity` concurrently
with the lighter remaining steps, and fold `seedDummyTransactions` into
the emails/activity/replays `Promise.all`.
- Removed the now-unused `syncSeedUserOauthProviders` helper.

The bulk path produces the same rows as the CRUD-handler path (verified
row-count equality during development). Webhooks / soft-limit checks are
intentionally not fired for seed data, consistent with the rest of the
seed.

## Also in this PR — preview-mode 404 fix
(`preview-project-redirect.tsx`)

While testing the above, the dashboard 404'd right after a preview
project was created. In preview mode the `/projects` page renders
`PreviewProjectRedirect`, which `POST`s
`/internal/preview/create-project` and then `router.push()`es to
`/projects/<new-id>` — but it never refreshed the client-side
owned-projects cache, so the `[projectId]` route's `useAdminApp()` read
a stale list, failed to find the just-created project, and called
`notFound()`.

Fixed by refreshing the owned-projects cache before navigating, matching
what the normal create-project flow in `page-client.tsx` already does.
(Pre-existing bug, not caused by the seeding change — but it surfaces
the seeding path, so it's bundled here.)

## Testing

`pnpm typecheck` and `pnpm lint` pass for both backend and dashboard.
The preview endpoint was exercised repeatedly during development (HTTP
200, projects created and populated correctly).

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Performance**
* Much faster bulk user and event seeding via larger, parallelized
batches and optimized backfilling.

* **Refactor**
* Dummy data seeding redesigned to be idempotent, deterministic, and
bulk-oriented; seeding tasks now overlap where safe.

* **Bug Fixes**
* Preview project flow validates client capabilities and refreshes the
local project list to avoid stale navigation.
  * Auto-login guarded to run only once to prevent duplicate sign-ins.

* **UI/UX**
* Walkthrough steps and sidebar behavior improved; walkthrough labels
and search keywords updated.

* **Chore**
* CLI identity command now resolves session authentication more
reliably.

<!-- review_stack_entry_start -->

[![Review Change
Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/hexclave/stack-auth/pull/1437?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack)

<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-05-20 10:10:22 -07:00
..
app Remote dev envs (#1435) 2026-05-19 15:54:18 -07:00
auto-migrations Migration tests 2026-02-17 15:58:06 -08:00
lib Speed up dummy-project seeding (preview create-project ~15s → ~1.3s) (#1437) 2026-05-20 10:10:22 -07:00
oauth Improved StackAssertionError error logging 2026-05-07 13:29:01 -07:00
private Update implementation 2026-05-12 11:14:30 -07:00
route-handlers Remote dev envs (#1435) 2026-05-19 15:54:18 -07:00
scripts [Feat] new scripts on migrate/seed/init run for internal (#1421) 2026-05-12 16:05:45 -07:00
utils Backend fallback (cloud run) (#1306) 2026-04-11 00:57:37 +00:00
analytics.tsx Fix event capture 2024-08-14 12:49:35 -07:00
globals.d.ts Split backend and dashboard (#83) 2024-06-18 15:49:31 +02:00
instrumentation.ts Onboarding app & restricted users (#1069) 2026-01-11 17:22:14 -08:00
polyfills.tsx Unhandled promise rejections no longer kill the whole server if not in development 2026-05-12 13:14:12 -07:00
prisma-client.tsx Fix bigint serialization error on tracing 2026-04-18 14:46:03 -07:00
proxy.tsx External db sync (#1036) 2026-02-05 12:04:31 -08:00
s3.tsx session replays (#1187) 2026-02-16 14:15:17 -08:00
smart-router.tsx Move /api/v1 to /api/latest 2025-02-05 17:24:43 -08:00
stack.tsx Rename STACK_SEED_INTERNAL_PROJECT_SECRET_SERVER_KEY to STACK_INTERNAL_PROJECT_SECRET_SERVER_KEY (#1415) 2026-05-06 10:35:08 -07:00