Skip to content

refactor(api): run seed-admin through the shared script context - #733

Merged
jovandyaz merged 1 commit into
mainfrom
fix/seed-admin-script-context
Nov 19, 2026
Merged

jovandyaz merged 1 commit into
mainfrom
fix/seed-admin-script-context

Conversation

@jovandyaz

@jovandyaz jovandyaz commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Why

Two leftovers from #731:

  • seed-admin.ts kept its own copy of script-context.ts's Nest module: the same ConfigModule options and DatabaseModule, plus its own context lifecycle.
  • It read ADMIN_EMAIL and ADMIN_PASSWORD before anything loaded apps/api/.env. .env.example says to put them there, but that only worked through nx run, because Nx loads the project's .env itself. A direct tsx run reported them missing. ConfigService can't supply them either: validateEnv strips keys outside the app schema.

What changed

  • script-context.ts: withScriptDatabase also hands work the ConfigService. Existing callers pass one-argument callbacks and are unchanged.
  • seed-admin.ts:
    • It runs through withScriptDatabase, so its own module and context handling are gone.
    • It loads .env.local and .env with dotenv before reading the two credentials. They are still checked before any database connection.
    • Hashing is unchanged: BcryptPasswordHasher with BCRYPT_ROUNDS.

Verification

Every run used a direct tsx call against the local knowtis_test, with DATABASE_URL overridden.

  • Credentials only in .env.local: the admin was created, and the hash starts with $2b$12$. A second run printed "already an admin" and changed nothing.
  • ADMIN_EMAIL blank: the usage message and exit 1.
  • BCRYPT_ROUNDS=9: it aborts with BCRYPT_ROUNDS: Too small: expected number to be >=10.
  • The test users were deleted afterwards. The dev knowtis database has no users table, so nothing reached it.
  • nx run-many -t typecheck,lint -p api and Prettier: clean.

Summary by CodeRabbit

  • Chores
    • Admin account setup now reads local environment settings and trims the configured email address.
    • The setup process leaves an existing admin unchanged, promotes an existing non-admin account, or creates a new account with a hashed password.
    • Missing credentials and setup failures continue to report an error and exit unsuccessfully.

@jovandyaz jovandyaz added tech-debt tooling Developer tooling, scripts and agent harness labels Nov 19, 2026
@jovandyaz jovandyaz self-assigned this Nov 19, 2026
@jovandyaz

Copy link
Copy Markdown
Owner Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The script database helper now provides ConfigService to its callback. The admin seed script loads credentials with dotenv and uses the helper to seed an account.

Changes

Admin seed script

Layer / File(s) Summary
Pass configuration to script callbacks
apps/api/src/scripts/script-context.ts
withScriptDatabase now passes both the database connection and ConfigService to its callback.
Load credentials and seed the admin
apps/api/src/scripts/seed-admin.ts
The script loads .env.local and .env, trims ADMIN_EMAIL, and reads ADMIN_PASSWORD. It leaves existing admins unchanged, promotes other existing users, and creates missing users with a hashed password.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Merge Risk: Minimal · up to 10cb7

This change moves the admin seed script onto the shared script database helper and loads credentials from env files. No merge-blocking risk was found, and the reported manual checks cover the main paths.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: seed-admin.ts now runs through the shared withScriptDatabase helper instead of its own Nest module and context lifecycle. The raw summary confirms this, and the object…
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.
Full details: Title check

Explanation

The title names the main change: seed-admin.ts now runs through the shared withScriptDatabase helper instead of its own Nest module and context lifecycle. The raw summary confirms this, and the objectives describe the same refactor. The title is specific and matches the changeset.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

apps/api/src/scripts/script-context.ts

ESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox.

apps/api/src/scripts/seed-admin.ts

ESLint skipped: the matched ESLint configuration already failed (missing-dependency).

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 8f925c7 and 10cb7cb.

Files selected for processing: 2

  • apps/api/src/scripts/script-context.ts
  • apps/api/src/scripts/seed-admin.ts

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Action performed

Full review finished.

@jovandyaz
jovandyaz merged commit 60170ca into main Nov 19, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tech-debt tooling Developer tooling, scripts and agent harness

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant