diff --git a/itou/eligibility/tasks.py b/itou/eligibility/tasks.py index 7a32d81b35..5835a2c3eb 100644 --- a/itou/eligibility/tasks.py +++ b/itou/eligibility/tasks.py @@ -1,3 +1,4 @@ +import datetime import logging from json import JSONDecodeError @@ -77,8 +78,11 @@ def certify_criteria(eligibility_diagnosis): criterion.certified_at = timezone.now() criterion.data_returned_by_api = data["raw_response"] criterion.certification_period = None - start_at, end_at = data["start_at"], data["end_at"] - if start_at and end_at: + if criterion.certified: + start_at = data["start_at"] + end_at = timezone.localdate(criterion.certified_at) + datetime.timedelta( + days=criterion.CERTIFICATION_GRACE_PERIOD_DAYS + ) criterion.certification_period = InclusiveDateRange(start_at, end_at) SelectedAdministrativeCriteria.objects.bulk_update( criteria, diff --git a/itou/utils/apis/api_particulier.py b/itou/utils/apis/api_particulier.py index 0a66967cb2..a06bd045c4 100644 --- a/itou/utils/apis/api_particulier.py +++ b/itou/utils/apis/api_particulier.py @@ -15,12 +15,6 @@ def client(): ) -def _parse_date(date: str) -> datetime.date | None: - if date: - return datetime.date.fromisoformat(date) - return None - - def _build_params_from(job_seeker): jobseeker_profile = job_seeker.jobseeker_profile params = { @@ -58,9 +52,10 @@ def has_required_info(job_seeker): def revenu_solidarite_active(client, job_seeker): response = _request(client, "/v2/revenu-solidarite-active", job_seeker) + certified = response["status"] == "beneficiaire" return { - "start_at": _parse_date(response["dateDebut"]), - "end_at": _parse_date(response["dateFin"]), - "is_certified": response["status"] == "beneficiaire", + "start_at": datetime.date.fromisoformat(response["dateDebut"]) if certified else None, + # Do not expose end_at, it’s always null. + "is_certified": certified, "raw_response": response, } diff --git a/itou/utils/mocks/api_particulier.py b/itou/utils/mocks/api_particulier.py index e295c574fe..0397af369f 100644 --- a/itou/utils/mocks/api_particulier.py +++ b/itou/utils/mocks/api_particulier.py @@ -1,6 +1,6 @@ def rsa_certified_mocker(): # https://github.com/etalab/siade_staging_data/blob/develop/payloads/api_particulier_v2_cnav_revenu_solidarite_active/200_beneficiaire_majoration.yaml - return {"status": "beneficiaire", "majoration": True, "dateDebut": "2024-08-01", "dateFin": "2024-10-31"} + return {"status": "beneficiaire", "majoration": True, "dateDebut": "2024-08-01", "dateFin": None} def rsa_not_certified_mocker(): diff --git a/tests/eligibility/test_iae.py b/tests/eligibility/test_iae.py index d52d24bba5..4db20b98d8 100644 --- a/tests/eligibility/test_iae.py +++ b/tests/eligibility/test_iae.py @@ -557,7 +557,7 @@ def test_eligibility_diagnosis_certify_criteria(mocker, EligibilityDiagnosisFact assert criterion.certified is True assert criterion.certified_at == timezone.now() assert criterion.data_returned_by_api == rsa_certified_mocker() - assert criterion.certification_period == InclusiveDateRange(datetime.date(2024, 8, 1), datetime.date(2024, 10, 31)) + assert criterion.certification_period == InclusiveDateRange(datetime.date(2024, 8, 1), datetime.date(2024, 12, 13)) @pytest.mark.parametrize( @@ -589,7 +589,7 @@ def test_eligibility_diagnosis_certify_criteria_missing_info(respx_mock, Eligibi pytest.param( partial(IAEEligibilityDiagnosisFactory, from_employer=True), { - "certification_period": InclusiveDateRange(datetime.date(2024, 8, 1), datetime.date(2024, 10, 31)), + "certification_period": InclusiveDateRange(datetime.date(2024, 8, 1), datetime.date(2024, 12, 13)), "certified": True, "certified_at": datetime.datetime(2024, 9, 12, tzinfo=datetime.UTC), "data_returned_by_api": rsa_certified_mocker(), @@ -601,7 +601,7 @@ def test_eligibility_diagnosis_certify_criteria_missing_info(respx_mock, Eligibi pytest.param( partial(GEIQEligibilityDiagnosisFactory, from_geiq=True), { - "certification_period": InclusiveDateRange(datetime.date(2024, 8, 1), datetime.date(2024, 10, 31)), + "certification_period": InclusiveDateRange(datetime.date(2024, 8, 1), datetime.date(2024, 12, 13)), "certified": True, "certified_at": datetime.datetime(2024, 9, 12, tzinfo=datetime.UTC), "data_returned_by_api": rsa_certified_mocker(), diff --git a/tests/eligibility/test_tasks.py b/tests/eligibility/test_tasks.py index 7efaec5232..75c5eeac14 100644 --- a/tests/eligibility/test_tasks.py +++ b/tests/eligibility/test_tasks.py @@ -36,6 +36,7 @@ def geiq_eligibility_factory(): ], ) class TestCertifyCriteria: + @freeze_time("2025-01-06") def test_queue_task(self, factory, respx_mock): eligibility_diagnosis = factory() respx_mock.get(f"{settings.API_PARTICULIER_BASE_URL}v2/revenu-solidarite-active").respond( @@ -54,7 +55,7 @@ def test_queue_task(self, factory, respx_mock): assert criterion.certified_at is not None assert criterion.data_returned_by_api == rsa_certified_mocker() assert criterion.certification_period == InclusiveDateRange( - datetime.date(2024, 8, 1), datetime.date(2024, 10, 31) + datetime.date(2024, 8, 1), datetime.date(2025, 4, 8) ) def test_retry_task_rate_limits(self, factory, respx_mock): diff --git a/tests/www/apply/test_process.py b/tests/www/apply/test_process.py index 1971fdbdd6..cd66128c7f 100644 --- a/tests/www/apply/test_process.py +++ b/tests/www/apply/test_process.py @@ -2569,7 +2569,7 @@ def test_accept_iae__criteria_can_be_certified(self, client, mocker): assert criterion.certified assert criterion.data_returned_by_api == rsa_certified_mocker() assert criterion.certification_period == InclusiveDateRange( - datetime.date(2024, 8, 1), datetime.date(2024, 10, 31) + datetime.date(2024, 8, 1), datetime.date(2024, 12, 12) ) assert criterion.certified_at @@ -2634,7 +2634,7 @@ def test_accept_geiq__criteria_can_be_certified(self, client, mocker): assert criterion.certified assert criterion.data_returned_by_api == rsa_certified_mocker() assert criterion.certification_period == InclusiveDateRange( - datetime.date(2024, 8, 1), datetime.date(2024, 10, 31) + datetime.date(2024, 8, 1), datetime.date(2024, 12, 12) ) assert criterion.certified_at