Description
Snapshot creation logic is duplicated in two places, violating DRY and the repository pattern. Both locations use direct db.add() instead of a repository method.
Steps to Reproduce
- Open
backend/core/user_manager.py:64-74
- Open
backend/services/profile_service.py:70-79
- Compare the snapshot creation code - nearly identical
Expected Behavior
Snapshot creation should be a single method in UserProfileRepository:
await repo.create_snapshot(profile)
Actual Behavior
Two separate locations create snapshots with duplicated code:
Location 1: core/user_manager.py:64-74 (registration)
- Creates
UserProfileSnapshot with hardcoded version=1
- Uses
db.add(snapshot) directly
Location 2: services/profile_service.py:70-79 (profile update)
- Creates
UserProfileSnapshot with updated_profile.version
- Uses
self.db.add(snapshot) directly
Root Cause
- Service layer (
profile_service.py) bypasses repository for snapshot creation
user_manager.py has no access to repositories (fastapi-users hook limitation)
- No
create_snapshot() method exists in UserProfileRepository
Possible Solutions
-
Option A: Add create_snapshot() to UserProfileRepository
- Add method to
repositories/user_profile_repository.py
- Update
profile_service.py to use self.repo.create_snapshot()
- Update
user_manager.py to instantiate repo and use it
-
Option B: Create standalone snapshot utility
- Create
utils/snapshot.py with create_snapshot(db, profile) function
- Both locations import and use the utility
- Less pure but simpler given fastapi-users constraints
Description
Snapshot creation logic is duplicated in two places, violating DRY and the repository pattern. Both locations use direct
db.add()instead of a repository method.Steps to Reproduce
backend/core/user_manager.py:64-74backend/services/profile_service.py:70-79Expected Behavior
Snapshot creation should be a single method in
UserProfileRepository:await repo.create_snapshot(profile)Actual Behavior
Two separate locations create snapshots with duplicated code:
Location 1:
core/user_manager.py:64-74(registration)UserProfileSnapshotwith hardcodedversion=1db.add(snapshot)directlyLocation 2:
services/profile_service.py:70-79(profile update)UserProfileSnapshotwithupdated_profile.versionself.db.add(snapshot)directlyRoot Cause
profile_service.py) bypasses repository for snapshot creationuser_manager.pyhas no access to repositories (fastapi-users hook limitation)create_snapshot()method exists inUserProfileRepositoryPossible Solutions
Option A: Add
create_snapshot()toUserProfileRepositoryrepositories/user_profile_repository.pyprofile_service.pyto useself.repo.create_snapshot()user_manager.pyto instantiate repo and use itOption B: Create standalone snapshot utility
utils/snapshot.pywithcreate_snapshot(db, profile)function