Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.
calculate_discount.py · 2.3 KB · 70 lines
| 1 | |
| 2 | #!/usr/bin/env python3 |
| 3 | """ |
| 4 | AI-Generated Service: calculate_discount |
| 5 | Description: Calculate discount based on customer loyalty level and order amount. Premium customers get 15%, Gold get 10%, Silver get 5%. Additional 5% for orders over $500. |
| 6 | Generated: 2025-09-18T14:18:22.752042 |
| 7 | """ |
| 8 | |
| 9 | from typing import Dict, Any, Optional |
| 10 | import json |
| 11 | import asyncio |
| 12 | from datetime import datetime |
| 13 | |
| 14 | # AI-Generated Implementation |
| 15 | async def calculate_discount(data: Dict[str, Any]) -> Dict[str, Any]: |
| 16 | """ |
| 17 | Calculate discount based on customer loyalty level and order amount. Premium customers get 15%, Gold get 10%, Silver get 5%. Additional 5% for orders over $500. |
| 18 | |
| 19 | Inputs: customer_type, order_amount |
| 20 | Outputs: discount_amount, final_price, discount_percentage, discount_reason |
| 21 | """ |
| 22 | |
| 23 | try: |
| 24 | # Extract inputs |
| 25 | customer_type = data.get('customer_type', None) |
| 26 | order_amount = data.get('order_amount', None) |
| 27 | |
| 28 | # AI-Generated Business Logic |
| 29 | # Calculate discount based on rules |
| 30 | discount_amount = 0 |
| 31 | discount_reason = [] |
| 32 | |
| 33 | # Loyalty discount |
| 34 | if customer_type == 'premium': |
| 35 | discount_amount += order_total * 0.1 |
| 36 | discount_reason.append('10% premium member discount') |
| 37 | |
| 38 | # Volume discount |
| 39 | if quantity >= 10: |
| 40 | discount_amount += order_total * 0.05 |
| 41 | discount_reason.append('5% bulk discount') |
| 42 | |
| 43 | result = { |
| 44 | 'original_total': order_total, |
| 45 | 'discount_amount': round(discount_amount, 2), |
| 46 | 'final_total': round(order_total - discount_amount, 2), |
| 47 | 'discount_reasons': discount_reason |
| 48 | } |
| 49 | |
| 50 | # Return results |
| 51 | return { |
| 52 | "success": True, |
| 53 | "timestamp": datetime.now().isoformat(), |
| 54 | "data": result |
| 55 | } |
| 56 | |
| 57 | except Exception as e: |
| 58 | return { |
| 59 | "success": False, |
| 60 | "error": str(e), |
| 61 | "timestamp": datetime.now().isoformat() |
| 62 | } |
| 63 | |
| 64 | # Synchronous wrapper for compatibility |
| 65 | def calculate_discount_sync(data: Dict[str, Any]) -> Dict[str, Any]: |
| 66 | """Synchronous wrapper for the async function""" |
| 67 | return asyncio.run(calculate_discount(data)) |
| 68 | |
| 69 | # Export the service |
| 70 | __all__ = ['calculate_discount', 'calculate_discount_sync'] |