diff --git a/api/src/pcapi/core/finance/api.py b/api/src/pcapi/core/finance/api.py index b469d4ce6df..947ebd36ea9 100644 --- a/api/src/pcapi/core/finance/api.py +++ b/api/src/pcapi/core/finance/api.py @@ -2605,6 +2605,7 @@ def create_deposit( expirationDate=granted_deposit.expiration_date, ) db.session.add(deposit) + db.session.flush() # Edge-cases: Validation of the registration occurred over a birthday # Then we need to add recredit to compensate diff --git a/api/tests/core/finance/test_api.py b/api/tests/core/finance/test_api.py index 0e8ec2d4d36..03f01f6f0fc 100644 --- a/api/tests/core/finance/test_api.py +++ b/api/tests/core/finance/test_api.py @@ -3621,6 +3621,27 @@ def test_create_underage_deposit_and_recredit_sum_of_15_17(self, age, expected_a assert deposit.amount == expected_amount assert deposit.user.id == beneficiary.id + def test_create_underage_deposit_with_birthday_since_registration(self): + sixteen_years_ago = datetime.datetime.utcnow() - relativedelta(years=16) + beneficiary = users_factories.UserFactory(validatedBirthDate=sixteen_years_ago.date()) + fraud_factories.BeneficiaryFraudCheckFactory( + user=beneficiary, + status=fraud_models.FraudCheckStatus.OK, + type=fraud_models.FraudCheckType.EDUCONNECT, + eligibilityType=users_models.EligibilityType.UNDERAGE, + resultContent=fraud_factories.EduconnectContentFactory( + registration_datetime=datetime.datetime.utcnow() - relativedelta(days=1) + ), + ) + + with db.session.no_autoflush: + deposit = api.create_deposit( + beneficiary, "created by test", beneficiary.eligibility, age_at_registration=15 + ) + + assert deposit.type == models.DepositType.GRANT_15_17 + assert deposit.amount == Decimal(20 + 30) + @time_machine.travel("2022-09-05 09:00:00") def test_create_18_years_old_deposit(self): beneficiary = users_factories.UserFactory()