Recipient Management Refactoring
Overview
This document describes the refactoring of the data models to separate the concepts of "User" (gift seeker) and "Recipient" (gift receiver), establishing a one-to-many relationship where a User can have multiple Recipients.
Changes Made
1. Database Models (app/models.py)
New Recipient Model
class Recipient(TimestampMixin, Base):
__tablename__ = "recipients"
id: UUID (primary key)
user_id: UUID (foreign key to users.id)
name: Optional[str]
relation: Optional[str] # friend, partner, etc.
gender: Optional[str]
age: Optional[int]
birth_date: Optional[datetime]
interests: List[str] (JSONB)
New Interaction Model
class Interaction(TimestampMixin, Base):
__tablename__ = "interactions"
id: UUID (primary key)
recipient_id: UUID (foreign key to recipients.id)
session_id: str
action_type: str # like, dislike, view, purchase
target_type: str # hypothesis, product
target_id: str
value: Optional[str]
metadata_json: Optional[dict] (JSONB)
2. Pydantic Models (recommendations/models.py)
- Renamed:
UserProfile→RecipientProfile - Added fields:
id: Optional[str]owner_id: Optional[str]-
name: Optional[str] -
Updated:
RecommendationSession.profile→RecommendationSession.recipient
3. Services
New RecipientService (app/services/recipient_service.py)
Handles CRUD operations for recipients and their interactions:
- create_recipient(user_id, profile) - Create a new recipient
- get_recipient(recipient_id) - Get recipient by ID
- get_user_recipients(user_id) - Get all recipients for a user
- update_recipient(recipient_id, ...) - Update recipient info
- save_interaction(recipient_id, session_id, interaction) - Save interaction
- get_recipient_interactions(recipient_id, limit) - Get interaction history
Updated DialogueManager (app/services/dialogue_manager.py)
- Added
db: AsyncSessionparameter to constructor - Added
user_id: Optional[UUID]parameter toinit_session() - Automatically creates
Recipientin database when session is initialized - All references to
session.profileupdated tosession.recipient
4. API Endpoints
New Recipients Router (routes/recipients.py)
GET /recipients/?user_id={uuid}- List all recipients for a userGET /recipients/{recipient_id}- Get specific recipientPUT /recipients/{recipient_id}- Update recipient informationGET /recipients/{recipient_id}/history- Get interaction history
Updated Recommendations Router
POST /recommendations/initnow accepts optionaluser_idparameter- When
user_idis provided, aRecipientrecord is created in the database
5. Configuration (app/config.py)
Added Docker-aware URL properties:
- db_url - Automatically uses postgres host when running in Docker
- redis_connection_url - Automatically uses redis host when running in Docker
- rabbitmq_connection_url - Automatically uses rabbitmq host when running in Docker
This allows the same .env file to work both locally and in Docker containers.
Database Migration
Migration file: alembic/versions/345ee831ac2a_add_recipients_and_interactions.py
To apply:
Usage Example
Creating a Session with Recipient Persistence
# POST /recommendations/init
{
"user_id": "123e4567-e89b-12d3-a456-426614174000",
"quiz": {
"recipient_age": 30,
"interests": ["coffee", "books"],
"budget": 5000,
"language": "ru"
}
}
This will:
1. Create a RecipientProfile in the session (Redis)
2. Create a Recipient record in PostgreSQL
3. Link the recipient to the user
4. Return a session with the recipient profile
Viewing Recipient History
Returns all interactions for this recipient across all sessions.
Benefits
- Persistent Recipient Profiles: Recipients are now stored in the database, allowing for long-term tracking
- Multi-Recipient Support: Users can manage multiple gift recipients
- Interaction History: All user interactions are logged per recipient for better personalization
- Future Wishlist: Foundation for implementing recipient-specific wishlists
- Better Analytics: Can track gift-giving patterns per recipient type/relationship
Next Steps
- Implement wishlist functionality per recipient
- Add recipient-specific recommendation tuning
- Implement recipient search/filtering
- Add recipient photo upload
- Create recipient management UI in frontend