61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
"""Account service"""
|
|
|
|
from typing import Optional, List
|
|
from sqlalchemy.orm import Session
|
|
from app.db.repositories import AccountRepository
|
|
from app.db.models import Account
|
|
from app.schemas import AccountCreateSchema
|
|
|
|
|
|
class AccountService:
|
|
"""Service for account operations"""
|
|
|
|
def __init__(self, session: Session):
|
|
self.session = session
|
|
self.account_repo = AccountRepository(session)
|
|
|
|
def create_account(self, family_id: int, owner_id: int, data: AccountCreateSchema) -> Account:
|
|
"""Create new account"""
|
|
return self.account_repo.create(
|
|
family_id=family_id,
|
|
owner_id=owner_id,
|
|
name=data.name,
|
|
account_type=data.account_type,
|
|
description=data.description,
|
|
balance=data.initial_balance,
|
|
initial_balance=data.initial_balance,
|
|
)
|
|
|
|
def transfer_between_accounts(
|
|
self, from_account_id: int, to_account_id: int, amount: float
|
|
) -> bool:
|
|
"""Transfer money between accounts"""
|
|
from_account = self.account_repo.update_balance(from_account_id, -amount)
|
|
to_account = self.account_repo.update_balance(to_account_id, amount)
|
|
return from_account is not None and to_account is not None
|
|
|
|
def get_family_total_balance(self, family_id: int) -> float:
|
|
"""Get total balance of all family accounts"""
|
|
accounts = self.account_repo.get_family_accounts(family_id)
|
|
return sum(acc.balance for acc in accounts)
|
|
|
|
def archive_account(self, account_id: int) -> Optional[Account]:
|
|
"""Archive account (hide but keep data)"""
|
|
return self.account_repo.archive_account(account_id)
|
|
|
|
def get_account_summary(self, account_id: int) -> dict:
|
|
"""Get account summary"""
|
|
account = self.account_repo.get_by_id(account_id)
|
|
if not account:
|
|
return {}
|
|
|
|
return {
|
|
"account_id": account.id,
|
|
"name": account.name,
|
|
"type": account.account_type,
|
|
"balance": account.balance,
|
|
"is_active": account.is_active,
|
|
"is_archived": account.is_archived,
|
|
"created_at": account.created_at,
|
|
}
|