Files
orion/app/modules/prospecting/services/interaction_service.py
Samir Boulahtit 22ae63b414
Some checks failed
CI / validate (push) Has been cancelled
CI / ruff (push) Successful in 10s
CI / dependency-scanning (push) Has been cancelled
CI / docs (push) Has been cancelled
CI / deploy (push) Has been cancelled
CI / pytest (push) Has started running
refactor(prospecting): migrate SVC-006 transaction control to endpoint level
Move db.commit() from services to API endpoints and Celery tasks.
Services now use db.flush() only; endpoints own the transaction.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-28 16:40:09 +01:00

77 lines
2.2 KiB
Python

# app/modules/prospecting/services/interaction_service.py
"""
Interaction tracking service.
Manages logging of all touchpoints with prospects:
calls, emails, meetings, visits, notes, etc.
"""
import logging
from datetime import date
from sqlalchemy.orm import Session
from app.modules.prospecting.models import ProspectInteraction
logger = logging.getLogger(__name__)
class InteractionService:
"""Service for prospect interaction management."""
def create(
self,
db: Session,
prospect_id: int,
user_id: int,
data: dict,
) -> ProspectInteraction:
"""Log a new interaction."""
interaction = ProspectInteraction(
prospect_id=prospect_id,
interaction_type=data["interaction_type"],
subject=data.get("subject"),
notes=data.get("notes"),
outcome=data.get("outcome"),
next_action=data.get("next_action"),
next_action_date=data.get("next_action_date"),
created_by_user_id=user_id,
)
db.add(interaction)
db.flush()
logger.info("Interaction logged for prospect %d: %s", prospect_id, data["interaction_type"])
return interaction
def get_for_prospect(
self,
db: Session,
prospect_id: int,
) -> list[ProspectInteraction]:
"""Get all interactions for a prospect, newest first."""
return (
db.query(ProspectInteraction)
.filter(ProspectInteraction.prospect_id == prospect_id)
.order_by(ProspectInteraction.created_at.desc())
.all()
)
def get_upcoming_actions(
self,
db: Session,
*,
before_date: date | None = None,
) -> list[ProspectInteraction]:
"""Get interactions with upcoming follow-up actions."""
query = db.query(ProspectInteraction).filter(
ProspectInteraction.next_action.isnot(None),
ProspectInteraction.next_action_date.isnot(None),
)
if before_date:
query = query.filter(ProspectInteraction.next_action_date <= before_date)
return query.order_by(ProspectInteraction.next_action_date.asc()).all()
interaction_service = InteractionService()