Skip to content

feat: introduce system user for automated changes#37

Merged
GitAddRemote merged 2 commits intomainfrom
feature/system-user
Dec 3, 2025
Merged

feat: introduce system user for automated changes#37
GitAddRemote merged 2 commits intomainfrom
feature/system-user

Conversation

@GitAddRemote
Copy link
Copy Markdown
Owner

Summary

Implements a dedicated system user (ID=1) for all automated changes, ensuring audit trail integrity and consistent foreign key attribution for background jobs and sync operations.

Changes

Database Schema

  • Add isSystemUser boolean column to user table with index
  • Seed system user with ID=1 (username: station-system, email: system@station.internal)
  • Generate unique, unusable password hash to prevent authentication

Application Layer

  • SystemUserService: Cached system user ID at startup with fail-fast behavior
  • UsersService: Automatically exclude system user from all query operations
  • AuthService: Block system user from authentication attempts

Key Features

  • System user ID cached in memory for O(1) lookups
  • All normal user queries exclude system user by default
  • System user cannot authenticate or generate tokens
  • Warning logged if system user attempts to log in
  • Comprehensive unit tests (11 test suites, 101 tests passing)

Testing

pnpm --filter backend test
# All 101 tests passing ✅

Migration Safety

  • Idempotent migrations with proper rollback support
  • System user seeded with ON CONFLICT DO NOTHING
  • Sequence reset to avoid ID conflicts with regular users

Future Usage

System user will be used for:

  • UEX sync jobs (categories, commodities, prices)
  • Automated inventory updates
  • Background data processing tasks
  • Migration-driven data changes

Closes

#16

Test Plan

  • System user seeds successfully
  • System user excluded from user list queries
  • System user cannot authenticate
  • All existing tests pass
  • SystemUserService unit tests pass
  • Service fails if system user missing

@GitAddRemote GitAddRemote self-assigned this Dec 3, 2025
Implement dedicated system user record (ID=1) for automated
  operations

such as sync jobs and background tasks.

Database changes:

- Add isSystemUser column to User entity with index

- Create migration to add is_system_user column

- Seed system user (ID=1, username: station-system)

- Generate unique, unusable password hash for system user

Application layer:

- Implement SystemUserService with ID caching at startup

- Service auto-creates system user in test mode if missing

- Service fails fast if system user missing in production

- Provide isSystemUser() helper for user identification

Query filtering:

- Update UsersService to exclude system user from all queries

- System user excluded from find operations

Authentication protection:

- Block system user from authentication in validateUser

- System user cannot log in or generate tokens

- Log warning if system user attempts authentication

Testing:

- Add comprehensive unit tests for SystemUserService

- Update AuthService tests with SystemUserService mocks

- Add E2E test helper for system user seeding

- All 101 unit tests passing

Closes #16
The 'should throw error if system user is missing' test was failing

because the service auto-creates the system user in test environments,

but the mock repository was missing create/save methods.

Changes:

- Add create and save methods to mock repository

- Set NODE_ENV to 'production' in error test to prevent auto-creation

- Add new test case to verify auto-creation works in test environment

All 102 tests now pass successfully.
@GitAddRemote GitAddRemote merged commit 2387511 into main Dec 3, 2025
9 checks passed
@GitAddRemote GitAddRemote deleted the feature/system-user branch December 3, 2025 22:58
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