From 25fbd5789b0a1b02c4580e00fe05176bff663beb Mon Sep 17 00:00:00 2001 From: armanddidierjean <95971503+armanddidierjean@users.noreply.github.com> Date: Fri, 28 Aug 2026 17:27:57 +0200 Subject: [PATCH] CDR: validates only product constraint based on already validated purchases --- app/modules/cdr/cruds_cdr.py | 12 ++++++++---- app/modules/cdr/endpoints_cdr.py | 5 +++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/modules/cdr/cruds_cdr.py b/app/modules/cdr/cruds_cdr.py index a4b6e7b89d..38cf2cc557 100644 --- a/app/modules/cdr/cruds_cdr.py +++ b/app/modules/cdr/cruds_cdr.py @@ -484,12 +484,16 @@ async def get_purchases_by_ids( db: AsyncSession, user_id: str, product_variant_id: list[UUID], + validated: bool | None = None, ) -> Sequence[models_cdr.Purchase]: + constraints = [ + models_cdr.Purchase.user_id == user_id, + models_cdr.Purchase.product_variant_id.in_(product_variant_id), + ] + if validated is not None: + constraints.append(models_cdr.Purchase.validated == validated) result = await db.execute( - select(models_cdr.Purchase).where( - models_cdr.Purchase.user_id == user_id, - models_cdr.Purchase.product_variant_id.in_(product_variant_id), - ), + select(models_cdr.Purchase).where(*constraints), ) return result.scalars().all() diff --git a/app/modules/cdr/endpoints_cdr.py b/app/modules/cdr/endpoints_cdr.py index 0bf21a9920..ee067f7fba 100644 --- a/app/modules/cdr/endpoints_cdr.py +++ b/app/modules/cdr/endpoints_cdr.py @@ -1964,12 +1964,17 @@ async def mark_purchase_as_validated( minimal_end_date=date(datetime.now(UTC).year, 9, 5), ) for product_constraint in product.product_constraints: + # When we validate a product, we want to check for already existing memberships + # If someone purchase a product that gives the required membership, this product must be validated first. purchases = await cruds_cdr.get_purchases_by_ids( db=db, user_id=user_id, product_variant_id=[ variant.id for variant in product_constraint.variants ], + # In case we want to allow validating products + # considering non-validated purchases we may remove this additional filter + validated=True, ) if not purchases: if product_constraint.related_membership: