Files
specs/app/repositories/pool_repository.py
2026-10-10 11:30:27 +00:00

35 lines
1.3 KiB
Python
Executable File

"""
Repository for PoolResource operations
"""
from sqlalchemy.orm import Session
from app.models import PoolResource, PoolReceipt, PoolAllocation
class PoolResourceRepository:
def __init__(self, db: Session):
self.db = db
def get_current_balance(self, pool_resource_id: int) -> int:
"""Calculate current balance from receipts and allocations"""
# Get all receipts for this resource
receipts = self.db.query(PoolReceipt).filter(
PoolReceipt.pool_resource_id == pool_resource_id
).all()
# Get all allocations for this resource
allocations = self.db.query(PoolAllocation).filter(
PoolAllocation.pool_resource_id == pool_resource_id
).all()
# Calculate total receipt quantity
total_receipts = sum(r.quantity for r in receipts)
# Calculate total allocation quantity
total_allocations = sum(a.quantity for a in allocations)
# Return balance
return total_receipts - total_allocations
def can_allocate(self, pool_resource_id: int, quantity: int) -> bool:
"""Check if allocation of specified quantity is possible"""
current_balance = self.get_current_balance(pool_resource_id)
return quantity <= current_balance