Skip to content

Commit

Permalink
fix celine feedback review
Browse files Browse the repository at this point in the history
  • Loading branch information
madjid-asa committed Dec 2, 2021
1 parent 7d998fb commit b6d5274
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
27 changes: 11 additions & 16 deletions itou/approvals/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ def clean(self):
"start_at": (
f"Pour la date de début de suspension, vous pouvez remonter "
f"{self.MAX_RETROACTIVITY_DURATION_DAYS} jours avant la date du jour."
f"Date de début minimum: {next_min_start_at.strftime('%d/%m/%Y')}."
f"Date de début minimum : {next_min_start_at.strftime('%d/%m/%Y')}."
)
}
)
Expand Down Expand Up @@ -626,25 +626,20 @@ def next_min_start_at(approval):
"""
Returns the minimum date on which a suspension can begin.
"""
# by default the next min start is the date of last hiring start of user
min_suspension_start_at = approval.user.last_accepted_job_application.hiring_start_at
today = datetime.date.today()
# We set the next min to the date of last old suspension if suspension exist
# Default starting date.
start_at = approval.user.last_accepted_job_application.hiring_start_at
start_at_threshold = today - datetime.timedelta(days=Suspension.MAX_RETROACTIVITY_DURATION_DAYS)

# Start at overrides to handle edge cases.
if approval.last_old_suspension:
min_suspension_start_at = approval.last_old_suspension.end_at + relativedelta(days=1)
# else we set the next min to the date of last accepted job from the pe approval
start_at = approval.last_old_suspension.end_at + relativedelta(days=1)
elif approval.user.last_accepted_job_application.created_from_pe_approval:
min_suspension_start_at = today

is_old_than_max_retroactivity = today - min_suspension_start_at > datetime.timedelta(
days=Suspension.MAX_RETROACTIVITY_DURATION_DAYS
)
start_at = today

return (
today - relativedelta(days=Suspension.MAX_RETROACTIVITY_DURATION_DAYS)
if is_old_than_max_retroactivity
else min_suspension_start_at
)
if start_at < start_at_threshold:
return start_at_threshold
return start_at


class ProlongationQuerySet(models.QuerySet):
Expand Down
16 changes: 9 additions & 7 deletions itou/approvals/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,23 +948,25 @@ class SuspensionModelTest(TestCase):

def test_clean(self):
today = timezone.now().date()
start_at = timezone.now().date() - relativedelta(months=2)
start_at = today - relativedelta(days=Suspension.MAX_RETROACTIVITY_DURATION_DAYS * 2)
end_at = start_at + relativedelta(months=2)
approval = ApprovalFactory(start_at=start_at, end_at=end_at)
suspension = SuspensionFactory(approval=approval)
approval = ApprovalFactory.build(start_at=start_at, end_at=end_at)

# test the when suspension is out of boundaries
suspension.start_at = start_at - relativedelta(months=1)
# Suspension.start_date is too old.
suspension = SuspensionFactory.build(approval=approval)
suspension.start_at = start_at - relativedelta(days=Suspension.MAX_RETROACTIVITY_DURATION_DAYS + 1)
with self.assertRaises(ValidationError):
suspension.clean()

# the case when end_at < start_at
# suspension.end_at < suspension.start_at
suspension = SuspensionFactory.build(approval=approval)
suspension.start_at = start_at
suspension.end_at = start_at - relativedelta(months=1)
with self.assertRaises(ValidationError):
suspension.clean()

# declare suspension in the future
# Suspension.start_at is in the future.
suspension = SuspensionFactory.build(approval=approval)
suspension.start_at = today + relativedelta(days=2)
suspension.end_at = end_at
with self.assertRaises(ValidationError):
Expand Down

0 comments on commit b6d5274

Please sign in to comment.