Skip to content

Commit

Permalink
(PC-31244)[API] fix: handle skipped recredit for 16 in recredit
Browse files Browse the repository at this point in the history
when a user used to register at 15 and finishes their credit activation
at 17, then the recredit 16 is to the deposit but the recredit 16 is not
created in the database. see commit e9b5f5e.
  • Loading branch information
dnguyen1-pass committed Sep 2, 2024
1 parent 41bc113 commit 2d8379b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
19 changes: 18 additions & 1 deletion api/src/pcapi/core/finance/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2924,7 +2924,24 @@ def _has_been_recredited(user: users_models.User) -> bool:
if len(user.deposit.recredits) == 0:
return False

return conf.RECREDIT_TYPE_AGE_MAPPING[user.age] in [recredit.recreditType for recredit in user.deposit.recredits]
has_been_recredited = conf.RECREDIT_TYPE_AGE_MAPPING[user.age] in [
recredit.recreditType for recredit in user.deposit.recredits
]
if has_been_recredited:
return True

if user.age == 16:
# This condition handles the following edge case:
# - User started the activation workflow at 15 and finished it at 17.
# This used to create a deposit holding the total amount from 15 to 17 years old without creating
# the deposit for 16 years old in the database.
# - When trying to unlock their 18 years old deposit, we receive a new (correct) value for their age.
# - If this age is 16, no recredit happened (see 1st point) so we need a different check.
#
# This edge case is unlikely, but was introduced in e9b5f5e4.
return user.deposit.amount == sum(conf.RECREDIT_TYPE_AMOUNT_MAPPING.values())

return False


def _get_known_age_at_deposit(user: users_models.User) -> int | None:
Expand Down
10 changes: 10 additions & 0 deletions api/tests/core/finance/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3964,6 +3964,16 @@ def test_cannot_be_recredited_when_recredit_was_already_given(self):
can_be_recredited_for_16 = api._can_be_recredited(user)
assert not can_be_recredited_for_16

def test_cannot_be_recredited_when_recredit_16_was_already_applied_but_not_created(self):
user = users_factories.BeneficiaryFactory(age=15)
factories.RecreditFactory(deposit=user.deposit, recreditType=models.RecreditType.RECREDIT_17)
user.deposit.amount = Decimal(20 + 30 + 30)

next_year = datetime.datetime.utcnow() + relativedelta(years=1)
with time_machine.travel(next_year):
can_be_recredited_for_16 = api._can_be_recredited(user)
assert not can_be_recredited_for_16

def test_can_be_recredited_no_registration_datetime(self):
with time_machine.travel("2020-05-02"):
user = users_factories.UnderageBeneficiaryFactory(
Expand Down

0 comments on commit 2d8379b

Please sign in to comment.