diff --git a/backend/db/crud_living_income_benchmark.py b/backend/db/crud_living_income_benchmark.py index c872a75f..3d80401e 100644 --- a/backend/db/crud_living_income_benchmark.py +++ b/backend/db/crud_living_income_benchmark.py @@ -6,6 +6,7 @@ LivingIncomeBenchmarkDict, ) from models.cpi import Cpi +from fastapi import HTTPException, status def get_all_lib(session: Session) -> List[LivingIncomeBenchmarkDict]: @@ -69,10 +70,14 @@ def get_by_country_region_year( lib["last_year_cpi"] = last_year_cpi_value lib["cpi_factor"] = cpi_factor return lib + else: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail=f"Benchmark not available for the year {year}." + ) else: lib = lib.serialize lib["case_year_cpi"] = None lib["last_year_cpi"] = None lib["cpi_factor"] = None return lib - return None diff --git a/backend/routes/living_income_benchmark.py b/backend/routes/living_income_benchmark.py index 8e6530eb..d970bfeb 100644 --- a/backend/routes/living_income_benchmark.py +++ b/backend/routes/living_income_benchmark.py @@ -30,6 +30,7 @@ def get_by_country_region_year( ) if not res: raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, detail="Benchmark not found" + status_code=status.HTTP_404_NOT_FOUND, + detail="Benchmark value not found." ) return res diff --git a/backend/tests/test_070_benchmark.py b/backend/tests/test_070_benchmark.py index a2200fae..9c153362 100644 --- a/backend/tests/test_070_benchmark.py +++ b/backend/tests/test_070_benchmark.py @@ -94,6 +94,10 @@ async def test_get_benchmark_by_country_region_year_3( }, ) assert res.status_code == 404 + res = res.json() + assert res == { + "detail": "Benchmark value not found." + } @pytest.mark.asyncio async def test_get_benchmark_by_country_region_year_4( @@ -107,7 +111,11 @@ async def test_get_benchmark_by_country_region_year_4( params={ "country_id": 1, "region_id": 1, - "year": 2012, + "year": 2023, }, ) assert res.status_code == 404 + res = res.json() + assert res == { + "detail": "Benchmark not available for the year 2023." + } diff --git a/frontend/src/pages/cases/components/IncomeDriverTarget.js b/frontend/src/pages/cases/components/IncomeDriverTarget.js index ddd040cb..d52d6c95 100644 --- a/frontend/src/pages/cases/components/IncomeDriverTarget.js +++ b/frontend/src/pages/cases/components/IncomeDriverTarget.js @@ -105,6 +105,26 @@ const IncomeDriverTarget = ({ } }, [segmentItem, currentSegmentId, form, regionOptions]); + const resetBenchmark = useCallback( + ({ regionData }) => { + form.setFieldsValue({ + household_adult: null, + household_children: null, + }); + setHouseholdSize(0); + setIncomeTarget(0); + updateFormValues({ + ...regionData, + target: 0, + benchmark: {}, + adult: null, + child: null, + }); + setBenchmark("NA"); + }, + [form, setBenchmark, updateFormValues] + ); + const fetchBenchmark = useCallback( ({ region }) => { const regionData = { region: region }; @@ -115,6 +135,17 @@ const IncomeDriverTarget = ({ .then((res) => { // data represent LI Benchmark value const { data } = res; + // if data value by currency not found or 0 + // return a NA notif + if (!data.value?.[currentCase.currency.toLowerCase()] === 0) { + resetBenchmark({ regionData }); + messageApi.open({ + type: "error", + content: `Benchmark not available in ${currentCase.currency} for the year ${currentCase.year}.`, + }); + return; + } + // const household_adult = data.nr_adults; const household_children = data.household_size - data.nr_adults; setBenchmark(data); @@ -164,33 +195,24 @@ const IncomeDriverTarget = ({ }) .catch((e) => { // reset field and benchmark value - form.setFieldsValue({ - household_adult: null, - household_children: null, - }); - setHouseholdSize(0); - setIncomeTarget(0); - updateFormValues({ - ...regionData, - target: 0, - benchmark: {}, - adult: null, - child: null, - }); - setBenchmark("NA"); + resetBenchmark({ regionData }); // show notification - const { status, statusText, data } = e.response; - let content = data?.detail || statusText; - if (status === 404) { - content = "Benchmark value not found."; - } + const { statusText, data } = e.response; + const content = data?.detail || statusText; messageApi.open({ type: "error", content: content, }); }); }, - [currentCase, form, messageApi, updateFormValues, setBenchmark] + [ + currentCase, + form, + messageApi, + updateFormValues, + setBenchmark, + resetBenchmark, + ] ); useEffect(() => {