Skip to content

Commit

Permalink
Merge pull request #351 from akvo/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
wayangalihpratama authored Jun 21, 2024
2 parents 7662409 + 8724d2a commit 957c08f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
7 changes: 6 additions & 1 deletion backend/db/crud_living_income_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
LivingIncomeBenchmarkDict,
)
from models.cpi import Cpi
from fastapi import HTTPException, status


def get_all_lib(session: Session) -> List[LivingIncomeBenchmarkDict]:
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion backend/routes/living_income_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 9 additions & 1 deletion backend/tests/test_070_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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."
}
62 changes: 42 additions & 20 deletions frontend/src/pages/cases/components/IncomeDriverTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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);
Expand Down Expand Up @@ -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(() => {
Expand Down

0 comments on commit 957c08f

Please # to comment.