29 lines
990 B
Python
29 lines
990 B
Python
from fastapi import HTTPException
|
|
from ..db import get_connection
|
|
from ..utils.jwt_handler import verify_token
|
|
import hashlib
|
|
|
|
def apply_coupons(base_price: float, coupon_id: int, duration: float) -> float:
|
|
if not coupon_id:
|
|
return base_price
|
|
connection = get_connection()
|
|
if not connection:
|
|
raise HTTPException(status_code=500, detail="Database connection failed!")
|
|
|
|
|
|
cursor = connection.cursor(dictionary=True)
|
|
cursor.execute("""
|
|
SELECT discount_type, discount_amount
|
|
FROM coupons
|
|
WHERE coupon_id = %s
|
|
""", (coupon_id,))
|
|
coupon = cursor.fetchone()
|
|
|
|
if coupon['discount_type'] == 'percentage':
|
|
return base_price * (1 - coupon['discount_amount']/100)
|
|
elif coupon['discount_type'] == 'fixed':
|
|
return max(base_price - coupon['discount_amount'], 0)
|
|
elif coupon['discount_type'] == 'time':
|
|
return base_price * (duration - coupon['discount_amount'])/duration
|
|
return base_price
|