31 lines
994 B
Python
31 lines
994 B
Python
from repositories.addons import AddonsRepository
|
|
from repositories.pricing import PricingRepository
|
|
from schemas.plans import AddonData, OrderDetails, PricingPlans
|
|
from services.subscriptions import calculate_order_total
|
|
|
|
|
|
async def get_pricing_model(addons_repo: AddonsRepository, pricing_repo: PricingRepository):
|
|
pricing = await pricing_repo.get()
|
|
if not pricing:
|
|
return None
|
|
|
|
addons = await addons_repo.get_all()
|
|
addons_data = [
|
|
AddonData(id=a.id, name=a.name, price=a.price, free_threshold=a.free_threshold)
|
|
for a in addons
|
|
]
|
|
|
|
return PricingPlans(device_price=pricing.device_price, addons=addons_data)
|
|
|
|
|
|
async def calculate_price(
|
|
*, addons_repo: AddonsRepository, order: OrderDetails, pricing: PricingPlans
|
|
) -> float:
|
|
addons = [await addons_repo.get_by_id(a) for a in order.addons]
|
|
return calculate_order_total(
|
|
pricing,
|
|
order.devices,
|
|
[addon.id for addon in addons],
|
|
order.duration_days,
|
|
)
|