51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
"""Goal repository"""
|
|
|
|
from typing import List, Optional
|
|
from sqlalchemy.orm import Session
|
|
from app.db.models import Goal
|
|
from app.db.repositories.base import BaseRepository
|
|
|
|
|
|
class GoalRepository(BaseRepository[Goal]):
|
|
"""Goal data access operations"""
|
|
|
|
def __init__(self, session: Session):
|
|
super().__init__(session, Goal)
|
|
|
|
def get_family_goals(self, family_id: int) -> List[Goal]:
|
|
"""Get all active goals for family"""
|
|
return (
|
|
self.session.query(Goal)
|
|
.filter(Goal.family_id == family_id, Goal.is_active == True)
|
|
.order_by(Goal.priority.desc())
|
|
.all()
|
|
)
|
|
|
|
def get_goals_progress(self, family_id: int) -> List[dict]:
|
|
"""Get goals with progress info"""
|
|
goals = self.get_family_goals(family_id)
|
|
return [
|
|
{
|
|
"id": goal.id,
|
|
"name": goal.name,
|
|
"target": goal.target_amount,
|
|
"current": goal.current_amount,
|
|
"progress_percent": (goal.current_amount / goal.target_amount * 100) if goal.target_amount > 0 else 0,
|
|
"is_completed": goal.is_completed
|
|
}
|
|
for goal in goals
|
|
]
|
|
|
|
def update_progress(self, goal_id: int, amount: float) -> Optional[Goal]:
|
|
"""Update goal progress"""
|
|
goal = self.get_by_id(goal_id)
|
|
if goal:
|
|
goal.current_amount += amount
|
|
if goal.current_amount >= goal.target_amount:
|
|
goal.is_completed = True
|
|
from datetime import datetime
|
|
goal.completed_at = datetime.utcnow()
|
|
self.session.commit()
|
|
self.session.refresh(goal)
|
|
return goal
|