55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
"""Account repository"""
|
|
|
|
from typing import Optional, List
|
|
from sqlalchemy.orm import Session
|
|
from app.db.models import Account
|
|
from app.db.repositories.base import BaseRepository
|
|
|
|
|
|
class AccountRepository(BaseRepository[Account]):
|
|
"""Account data access operations"""
|
|
|
|
def __init__(self, session: Session):
|
|
super().__init__(session, Account)
|
|
|
|
def get_family_accounts(self, family_id: int) -> List[Account]:
|
|
"""Get all accounts for a family"""
|
|
return (
|
|
self.session.query(Account)
|
|
.filter(Account.family_id == family_id, Account.is_active == True)
|
|
.all()
|
|
)
|
|
|
|
def get_user_accounts(self, user_id: int) -> List[Account]:
|
|
"""Get all accounts owned by user"""
|
|
return (
|
|
self.session.query(Account)
|
|
.filter(Account.owner_id == user_id, Account.is_active == True)
|
|
.all()
|
|
)
|
|
|
|
def get_account_if_accessible(self, account_id: int, family_id: int) -> Optional[Account]:
|
|
"""Get account only if it belongs to family"""
|
|
return (
|
|
self.session.query(Account)
|
|
.filter(
|
|
Account.id == account_id,
|
|
Account.family_id == family_id,
|
|
Account.is_active == True
|
|
)
|
|
.first()
|
|
)
|
|
|
|
def update_balance(self, account_id: int, amount: float) -> Optional[Account]:
|
|
"""Update account balance by delta"""
|
|
account = self.get_by_id(account_id)
|
|
if account:
|
|
account.balance += amount
|
|
self.session.commit()
|
|
self.session.refresh(account)
|
|
return account
|
|
|
|
def archive_account(self, account_id: int) -> Optional[Account]:
|
|
"""Archive account"""
|
|
return self.update(account_id, is_archived=True)
|