From 178d738d99b2c3b4caad146cf949d05a79aa3fff Mon Sep 17 00:00:00 2001 From: skcodes-infra Date: Tue, 7 Apr 2026 01:09:14 +0530 Subject: [PATCH] Fix KeyError in admin_create_coupon when optional fields are omitted The coupon creation endpoint checks for optional fields using body.get() but then accesses them with body["key"], which raises KeyError when the field is not present in the request body. Affected fields: maximum_discount, valid_from, valid_until. Changed all three to use body.get() consistently for both the conditional check and the value access. --- app/api/admin.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/api/admin.py b/app/api/admin.py index 73cf0c9..4cc1c75 100644 --- a/app/api/admin.py +++ b/app/api/admin.py @@ -441,12 +441,12 @@ async def admin_create_coupon( discount_type=body.get("discount_type", "percentage"), discount_value=float(body.get("discount_value", 0)), minimum_order_amount=float(body.get("minimum_order_amount", 0)), - maximum_discount=float(body["maximum_discount"]) if body.get("maximum_discount") is not None else None, + maximum_discount=float(body.get("maximum_discount")) if body.get("maximum_discount") is not None else None, usage_limit=int(body.get("usage_limit", 1)), usage_limit_per_user=int(body.get("usage_limit_per_user", 1)), target_audience=body.get("target_audience", "all"), - valid_from=datetime.fromisoformat(body["valid_from"].replace("Z", "+00:00")) if body.get("valid_from") else now, - valid_until=datetime.fromisoformat(body["valid_until"].replace("Z", "+00:00")) if body.get("valid_until") else now, + valid_from=datetime.fromisoformat(body.get("valid_from", "").replace("Z", "+00:00")) if body.get("valid_from") else now, + valid_until=datetime.fromisoformat(body.get("valid_until", "").replace("Z", "+00:00")) if body.get("valid_until") else now, is_active=body.get("is_active", True), ) db.add(coupon)