37 lines
1.1 KiB
Python
37 lines
1.1 KiB
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,
|
|
is_enabled=a.is_enabled,
|
|
)
|
|
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,
|
|
)
|