From aeaa4f8e0dfacbca09457f82e7bde220a7600b93 Mon Sep 17 00:00:00 2001 From: Fringg Date: Wed, 15 Apr 2026 04:13:43 +0300 Subject: [PATCH] fix: allow clearing all period discounts from promo groups Empty period_discounts dict was normalized to None by the schema, making it indistinguishable from "field absent" (don't update). Now empty dict passes through to CRUD which correctly sets period_discounts=None in DB, clearing all discounts. --- app/database/crud/promo_group.py | 2 +- app/webapi/schemas/promo_groups.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/database/crud/promo_group.py b/app/database/crud/promo_group.py index 0da9712c..39a97dc0 100644 --- a/app/database/crud/promo_group.py +++ b/app/database/crud/promo_group.py @@ -166,7 +166,7 @@ async def update_promo_group( group.device_discount_percent = max(0, min(100, device_discount_percent)) if period_discounts is not None: normalized_period_discounts = _normalize_period_discounts(period_discounts) - group.period_discounts = normalized_period_discounts or None + group.period_discounts = normalized_period_discounts if normalized_period_discounts else None if auto_assign_total_spent_kopeks is not None: value = max(0, auto_assign_total_spent_kopeks) group.auto_assign_total_spent_kopeks = value if value > 0 else None diff --git a/app/webapi/schemas/promo_groups.py b/app/webapi/schemas/promo_groups.py index ccd55242..0c2a0109 100644 --- a/app/webapi/schemas/promo_groups.py +++ b/app/webapi/schemas/promo_groups.py @@ -18,7 +18,9 @@ def _normalize_period_discounts(value: dict[object, object] | None) -> dict[int, except (TypeError, ValueError): continue - return normalized or None + # Return empty dict (not None) so the backend can distinguish + # "clear all discounts" ({}) from "don't touch discounts" (None/absent). + return normalized class PromoGroupResponse(BaseModel):