From e258abca25012353067785a11c370758a6f7bd72 Mon Sep 17 00:00:00 2001 From: wayangalihpratama Date: Fri, 21 Jun 2024 18:40:33 +0800 Subject: [PATCH 1/4] [#350] Update benchmark query --- backend/db/crud_living_income_benchmark.py | 7 ++++++- backend/routes/living_income_benchmark.py | 3 ++- backend/tests/test_070_benchmark.py | 10 +++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) 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." + } From 5e8c9f0d5bb1687a7d7578841fa9aa84e8b62309 Mon Sep 17 00:00:00 2001 From: wayangalihpratama Date: Fri, 21 Jun 2024 18:41:15 +0800 Subject: [PATCH 2/4] [#350] Fix benchmark notification error in frontend --- .../cases/components/IncomeDriverTarget.js | 48 ++++++++++++------- frontend/src/setupProxy.js | 4 +- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/frontend/src/pages/cases/components/IncomeDriverTarget.js b/frontend/src/pages/cases/components/IncomeDriverTarget.js index ddd040cb..342d39db 100644 --- a/frontend/src/pages/cases/components/IncomeDriverTarget.js +++ b/frontend/src/pages/cases/components/IncomeDriverTarget.js @@ -105,6 +105,23 @@ const IncomeDriverTarget = ({ } }, [segmentItem, currentSegmentId, form, regionOptions]); + const resetBenchmark = ({ regionData }) => { + form.setFieldsValue({ + household_adult: null, + household_children: null, + }); + setHouseholdSize(0); + setIncomeTarget(0); + updateFormValues({ + ...regionData, + target: 0, + benchmark: {}, + adult: null, + child: null, + }); + setBenchmark("NA"); + }; + const fetchBenchmark = useCallback( ({ region }) => { const regionData = { region: region }; @@ -115,6 +132,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,26 +192,10 @@ 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; + const { statusText, data } = e.response; let content = data?.detail || statusText; - if (status === 404) { - content = "Benchmark value not found."; - } messageApi.open({ type: "error", content: content, diff --git a/frontend/src/setupProxy.js b/frontend/src/setupProxy.js index 4da925e5..a55e650e 100644 --- a/frontend/src/setupProxy.js +++ b/frontend/src/setupProxy.js @@ -4,7 +4,7 @@ module.exports = function (app) { app.use( ["/api"], createProxyMiddleware({ - target: "http://localhost:5000", + target: "http://127.0.0.1:5000", changeOrigin: true, pathRewrite: { "^/api/": "/", @@ -14,7 +14,7 @@ module.exports = function (app) { app.use( ["/config.js"], createProxyMiddleware({ - target: "http://localhost:5000", + target: "http://127.0.0.1:5000", changeOrigin: true, pathRewrite: { "^/api": "/", From 2ca4a6c451d212ec83e298eea7be0302f610f9a7 Mon Sep 17 00:00:00 2001 From: wayangalihpratama Date: Fri, 21 Jun 2024 19:37:31 +0800 Subject: [PATCH 3/4] [#350] Fix yarn lint --- .../cases/components/IncomeDriverTarget.js | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/frontend/src/pages/cases/components/IncomeDriverTarget.js b/frontend/src/pages/cases/components/IncomeDriverTarget.js index 342d39db..d52d6c95 100644 --- a/frontend/src/pages/cases/components/IncomeDriverTarget.js +++ b/frontend/src/pages/cases/components/IncomeDriverTarget.js @@ -105,22 +105,25 @@ const IncomeDriverTarget = ({ } }, [segmentItem, currentSegmentId, form, regionOptions]); - const resetBenchmark = ({ regionData }) => { - form.setFieldsValue({ - household_adult: null, - household_children: null, - }); - setHouseholdSize(0); - setIncomeTarget(0); - updateFormValues({ - ...regionData, - target: 0, - benchmark: {}, - adult: null, - child: null, - }); - setBenchmark("NA"); - }; + 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 }) => { @@ -195,14 +198,21 @@ const IncomeDriverTarget = ({ resetBenchmark({ regionData }); // show notification const { statusText, data } = e.response; - let content = data?.detail || statusText; + const content = data?.detail || statusText; messageApi.open({ type: "error", content: content, }); }); }, - [currentCase, form, messageApi, updateFormValues, setBenchmark] + [ + currentCase, + form, + messageApi, + updateFormValues, + setBenchmark, + resetBenchmark, + ] ); useEffect(() => { From 8724d2a8730a4ae8a3cb09e0f9e1483ab7be7bb2 Mon Sep 17 00:00:00 2001 From: wayangalihpratama Date: Fri, 21 Jun 2024 19:42:41 +0800 Subject: [PATCH 4/4] [#350] Revert setupProxy.js --- frontend/src/setupProxy.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/setupProxy.js b/frontend/src/setupProxy.js index a55e650e..4da925e5 100644 --- a/frontend/src/setupProxy.js +++ b/frontend/src/setupProxy.js @@ -4,7 +4,7 @@ module.exports = function (app) { app.use( ["/api"], createProxyMiddleware({ - target: "http://127.0.0.1:5000", + target: "http://localhost:5000", changeOrigin: true, pathRewrite: { "^/api/": "/", @@ -14,7 +14,7 @@ module.exports = function (app) { app.use( ["/config.js"], createProxyMiddleware({ - target: "http://127.0.0.1:5000", + target: "http://localhost:5000", changeOrigin: true, pathRewrite: { "^/api": "/",