fix: new message problem#136
Conversation
WalkthroughThe pull request refactors user character resolution to decouple the User dependency from the ResolvedUserCharacter DTO. The UserCharacterResolver is updated to obtain user information through the provider entity instead. Tenant_id filtering is added to provider lookups across multiple locations, and comprehensive unit tests validate the new behavior. Changes
Sequence DiagramsequenceDiagram
participant Client
participant Resolver as UserCharacterResolver
participant Provider as Provider<br/>(Entity)
participant Character as Character<br/>(Entity)
Client->>Resolver: resolve(provider, providerId, username, tenantId)
alt Existing Provider Found
Resolver->>Provider: Query by tenant_id, model_type,<br/>provider, provider_id
Provider-->>Resolver: providerEntity
Resolver->>Character: Fetch character from<br/>providerEntity->character
Character-->>Resolver: character
Note over Resolver: isNewUser = false
Resolver-->>Client: ResolvedUserCharacter(provider,<br/>character, false)
else New Provider Required
Resolver->>Provider: Query returns empty
Resolver->>Provider: Create new provider
Resolver->>Character: Create new character<br/>for tenant
Character-->>Resolver: character (experience=0)
Resolver->>Character: Refresh character
Resolver->>Provider: Associate character<br/>to provider
Note over Resolver: isNewUser = true
Resolver-->>Client: ResolvedUserCharacter(provider,<br/>character, true)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Tip 📝 Customizable high-level summaries are now available in beta!You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.
Example instruction:
Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
app-modules/bot-discord/src/Actions/UserCharacterResolver.php (1)
22-27: Provider query now correctly scoped by tenant and model typeAdding
tenant_idandmodel_type = User::classto the provider query makes the resolver tenant-safe and ensures only user-backed providers are considered. This directly addresses cross-tenant collisions for the sameproviderId.If provider lookups on
(tenant_id, model_type, provider, provider_id)are frequent, consider a composite DB index on those columns for better performance.app-modules/bot-discord/tests/Feature/Actions/UserCharacterResolverTest.php (1)
42-78: Solid existing-user test; consider adding a cross-tenant regression caseThe existing-user scenario is well covered: you assert that the resolver reuses the existing provider and character and doesn’t create extra records, which matches the intended behavior.
Given the bug this PR addresses, it may be worth adding a third test where:
- A provider exists for
(provider, providerId)in a different tenant, andresolve()is called with a newtenantId.That test should assert that a new user/character/provider is created for the second tenant, guarding the tenant-scoping behavior against future regressions.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
app-modules/bot-discord/src/Actions/UserCharacterResolver.php(2 hunks)app-modules/bot-discord/src/DTO/ResolvedUserCharacter.php(0 hunks)app-modules/bot-discord/tests/Feature/Actions/UserCharacterResolverTest.php(1 hunks)app-modules/user/src/Actions/UpdateProfile.php(1 hunks)
💤 Files with no reviewable changes (1)
- app-modules/bot-discord/src/DTO/ResolvedUserCharacter.php
🧰 Additional context used
🧬 Code graph analysis (2)
app-modules/bot-discord/src/Actions/UserCharacterResolver.php (3)
app-modules/provider/src/Models/Provider.php (1)
user(59-62)app-modules/bot-discord/src/DTO/ResolvedUserCharacter.php (1)
ResolvedUserCharacter(10-17)app-modules/character/src/Models/Character.php (1)
Character(28-128)
app-modules/bot-discord/tests/Feature/Actions/UserCharacterResolverTest.php (4)
app-modules/bot-discord/src/Actions/UserCharacterResolver.php (2)
UserCharacterResolver(14-70)resolve(16-69)app-modules/character/src/Models/Character.php (1)
Character(28-128)app-modules/provider/src/Models/Provider.php (1)
Provider(28-96)app-modules/tenant/src/Models/Tenant.php (1)
Tenant(23-104)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Perform Pest Tests / Run
- GitHub Check: Perform Rector Check / Run
🔇 Additional comments (4)
app-modules/user/src/Actions/UpdateProfile.php (1)
17-21: Tenant scoping for provider lookup looks correctAdding
where('tenant_id', $profileDTO->tenantId)aligns this profile update with tenant-aware provider resolution and prevents cross-tenant profile updates. No further issues spotted here.app-modules/bot-discord/src/Actions/UserCharacterResolver.php (2)
40-50: Refreshing character after create ensures DB defaults are loadedCalling
$character->refresh()after creating the character is a good fix: it guarantees attributes with DB defaults (likeexperience = 0) are present on the in-memory model, matching what the tests assert and what downstream code likely expects.
59-62: Assumption thatproviderEntity->useralways existsThe existing-user path now derives the character via:
->where('user_id', $providerEntity->user->id)This is correct as long as every provider with
model_type = User::classhas a valid related user. If there’s any chance users can be deleted while providers remain, this line would throw on a null relation beforefirstOrFail()can run.You may want to either (a) ensure FK + cascade rules prevent orphan providers, or (b) defensively guard for a missing user here (e.g., skip to “new user” behavior or fail with a clearer error) depending on business rules.
app-modules/bot-discord/tests/Feature/Actions/UserCharacterResolverTest.php (1)
12-40: Good coverage for the new-user creation flowThis test thoroughly validates the “no existing provider” path: it checks the resolver flags a new user, wires up provider and character correctly, and asserts counts/tenant IDs. This should catch regressions around character defaults and provider scoping.
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **Bug Fixes** * Improved tenant isolation for user character resolution and profile updates * **Tests** * Added comprehensive unit tests for character resolution scenarios, including new user creation and existing user resolution * **Refactor** * Simplified character resolution model by removing unnecessary dependencies <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Summary by CodeRabbit
Release Notes
Bug Fixes
Tests
Refactor
✏️ Tip: You can customize this high-level summary in your review settings.