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: