Skip to content

Eliminate Duplicate Snapshot Creation Code #8

@gzark1

Description

@gzark1

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

  1. Open backend/core/user_manager.py:64-74
  2. Open backend/services/profile_service.py:70-79
  3. 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

  1. 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
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions