-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorder_processing.py
More file actions
119 lines (87 loc) · 3.19 KB
/
order_processing.py
File metadata and controls
119 lines (87 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from typing import Any, Dict, List, Tuple
# Константы
DEFAULT_CURRENCY = "USD"
TAX_RATE = 0.21
COUPON_SAVE10 = "SAVE10"
COUPON_SAVE20 = "SAVE20"
COUPON_VIP = "VIP"
SAVE10_RATE = 0.10
SAVE20_RATE = 0.20
SAVE20_FALLBACK_RATE = 0.05
SAVE20_THRESHOLD = 200
VIP_FIXED_DISCOUNT = 50
VIP_LOW_SUBTOTAL_THRESHOLD = 100
VIP_LOW_SUBTOTAL_DISCOUNT = 10
def parse_request(request: Dict[str, Any]) -> Tuple[Any, Any, Any, Any]:
user_id = request.get("user_id")
items = request.get("items")
coupon = request.get("coupon")
currency = request.get("currency")
return user_id, items, coupon, currency
def validate_basic_fields(user_id: Any, items: Any, currency: Any) -> str:
if user_id is None:
raise ValueError("user_id is required")
if items is None:
raise ValueError("items is required")
if currency is None:
currency = DEFAULT_CURRENCY
return currency
def validate_items(items: Any) -> List[Dict[str, Any]]:
if not isinstance(items, list):
raise ValueError("items must be a list")
if len(items) == 0:
raise ValueError("items must not be empty")
for it in items:
if "price" not in it or "qty" not in it:
raise ValueError("item must have price and qty")
if it["price"] <= 0:
raise ValueError("price must be positive")
if it["qty"] <= 0:
raise ValueError("qty must be positive")
return items
def calculate_subtotal(items: List[Dict[str, Any]]) -> int:
subtotal = 0
for it in items:
subtotal = subtotal + it["price"] * it["qty"]
return subtotal
def calculate_discount(subtotal: int, coupon: Any) -> int:
if coupon is None or coupon == "":
return 0
if coupon == COUPON_SAVE10:
return int(subtotal * SAVE10_RATE)
if coupon == COUPON_SAVE20:
if subtotal >= SAVE20_THRESHOLD:
return int(subtotal * SAVE20_RATE)
return int(subtotal * SAVE20_FALLBACK_RATE)
if coupon == COUPON_VIP:
discount = VIP_FIXED_DISCOUNT
if subtotal < VIP_LOW_SUBTOTAL_THRESHOLD:
discount = VIP_LOW_SUBTOTAL_DISCOUNT
return discount
raise ValueError("unknown coupon")
def calculate_tax(total_after_discount: int) -> int:
return int(total_after_discount * TAX_RATE)
def build_order_id(user_id: Any, items: List[Dict[str, Any]]) -> str:
return str(user_id) + "-" + str(len(items)) + "-" + "X"
def process_checkout(request: Dict[str, Any]) -> Dict[str, Any]:
user_id, items, coupon, currency = parse_request(request)
currency = validate_basic_fields(user_id, items, currency)
items = validate_items(items)
subtotal = calculate_subtotal(items)
discount = calculate_discount(subtotal, coupon)
total_after_discount = subtotal - discount
if total_after_discount < 0:
total_after_discount = 0
tax = calculate_tax(total_after_discount)
total = total_after_discount + tax
order_id = build_order_id(user_id, items)
return {
"order_id": order_id,
"user_id": user_id,
"currency": currency,
"subtotal": subtotal,
"discount": discount,
"tax": tax,
"total": total,
"items_count": len(items),
}