Skip to content

Commit

Permalink
Merge pull request #186 from akvo/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
wayangalihpratama authored Jan 3, 2024
2 parents ad16310 + 69ce7bb commit 863b353
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 157 deletions.
8 changes: 6 additions & 2 deletions backend/db/crud_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class PaginatedCaseData(TypedDict):


def add_case(session: Session, payload: CaseBase, user: User) -> CaseDict:
reporting_period = payload.reporting_period
reporting_period = reporting_period if reporting_period else "per-year"
current_datetime = datetime.now()
case = Case(
name=payload.name,
Expand All @@ -33,7 +35,7 @@ def add_case(session: Session, payload: CaseBase, user: User) -> CaseDict:
area_size_unit=payload.area_size_unit,
volume_measurement_unit=payload.volume_measurement_unit,
cost_of_production_unit=payload.cost_of_production_unit,
reporting_period=payload.reporting_period,
reporting_period=reporting_period,
segmentation=1 if payload.segmentation else 0,
living_income_study=payload.living_income_study,
multiple_commodities=1 if payload.multiple_commodities else 0,
Expand Down Expand Up @@ -140,12 +142,14 @@ def update_case(session: Session, id: int, payload: CaseBase) -> CaseDict:
case.area_size_unit = payload.area_size_unit
case.volume_measurement_unit = payload.volume_measurement_unit
case.cost_of_production_unit = payload.cost_of_production_unit
case.reporting_period = payload.reporting_period
case.segmentation = 1 if payload.segmentation else 0
case.living_income_study = payload.living_income_study
case.multiple_commodities = 1 if payload.multiple_commodities else 0
case.logo = payload.logo
case.private = 1 if payload.private else 0
# reporting period
if payload.reporting_period:
case.reporting_period = payload.reporting_period
# handle tag
if payload.tags:
prev_tags = (
Expand Down
16 changes: 11 additions & 5 deletions backend/models/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,19 @@ class Case(Base):
reporting_period = Column(String, nullable=False)
segmentation = Column(SmallInteger, nullable=False, default=0)
living_income_study = Column(
Enum(LivingIncomeStudyEnum, name="case_living_income_study"), nullable=True
Enum(LivingIncomeStudyEnum, name="case_living_income_study"),
nullable=True,
)
multiple_commodities = Column(SmallInteger, nullable=False, default=0)
private = Column(SmallInteger, nullable=False, default=0)
logo = Column(String, nullable=True)
created_by = Column(Integer, ForeignKey("user.id"))
created_at = Column(DateTime, nullable=False, server_default=func.now())
updated_at = Column(
DateTime, nullable=False, server_default=func.now(), onupdate=func.now()
DateTime,
nullable=False,
server_default=func.now(),
onupdate=func.now(),
)

case_commodities = relationship(
Expand Down Expand Up @@ -166,8 +170,8 @@ def __init__(
area_size_unit: str,
volume_measurement_unit: str,
cost_of_production_unit: str,
reporting_period: str,
segmentation: int,
reporting_period: Optional[str],
living_income_study: Optional[str],
description: Optional[str],
multiple_commodities: int,
Expand Down Expand Up @@ -266,7 +270,9 @@ def to_case_detail(self) -> CaseDetailDict:
"created_by": self.created_by_user.email,
"created_at": self.created_at.strftime("%Y-%m-%d %H:%M:%S"),
"updated_at": self.updated_at.strftime("%Y-%m-%d %H:%M:%S"),
"segments": [ps.serialize_with_answers for ps in self.case_segments],
"segments": [
ps.serialize_with_answers for ps in self.case_segments
],
"case_commodities": [pc.simplify for pc in self.case_commodities],
"private": self.private,
"tags": [ct.tag for ct in self.case_tags],
Expand Down Expand Up @@ -299,9 +305,9 @@ class CaseBase(BaseModel):
area_size_unit: str
volume_measurement_unit: str
cost_of_production_unit: str
reporting_period: str
segmentation: bool
multiple_commodities: bool
reporting_period: Optional[str] = None
living_income_study: Optional[LivingIncomeStudyEnum] = None
logo: Optional[str] = None
private: Optional[bool] = False
Expand Down
22 changes: 2 additions & 20 deletions frontend/src/pages/cases/components/CaseProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
commodityOptions,
countryOptions,
currencyOptions,
reportingPeriod,
selectProps,
yesNoOptions,
DebounceSelect,
Expand Down Expand Up @@ -217,23 +216,6 @@ const CaseForm = ({
<AreaUnitFields form={form} disabled={!enableEditCase} />
</Col>
</Row>
<Form.Item
label="Reporting Period"
name="reporting_period"
rules={[
{
required: true,
message: "Reporting Period is required",
},
]}
>
<Radio.Group
options={reportingPeriod}
optionType="button"
buttonStyle="solid"
disabled={!enableEditCase}
/>
</Form.Item>
</Card>
</Col>
</Row>
Expand Down Expand Up @@ -456,9 +438,9 @@ const CaseProfile = ({
currency: values.currency,
area_size_unit: values.area_size_unit,
volume_measurement_unit: values.volume_measurement_unit,
reporting_period: values.reporting_period,
multiple_commodities: secondary || tertiary,
// need to handle below value correctly
reporting_period: "per-year",
// TODO:: need to handle below value correctly
cost_of_production_unit: "cost_of_production_unit",
segmentation: true,
living_income_study: null,
Expand Down
34 changes: 30 additions & 4 deletions frontend/src/pages/cases/components/DashboardScenarioModeling.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,36 @@ const DashboardScenarioModeling = ({
}, [enableEditCase, scenarioData]);

const commodityQuestions = useMemo(() => {
return commodityList.map((c) => ({
...c,
...questionGroups.find((qg) => qg.commodity_id === c.commodity),
}));
return commodityList.map((c) => {
const findQG = questionGroups.find(
(qg) => qg.commodity_id === c.commodity
);
// handle grouped diversified income question
if (c.commodity_type === "diversified") {
return {
...c,
...findQG,
questions: [
{
...findQG.questions[0],
id: "diversified",
default_value: findQG.questions
.map((x) => `#${x.id}`)
.join(" + "),
parent: null,
question_type: "diversified",
text: "Diversified Income",
description: "Custom question",
childrens: findQG.questions,
},
],
};
}
return {
...c,
...findQG,
};
});
}, [commodityList, questionGroups]);

const renameScenario = (index, newName, newDescription) => {
Expand Down
Loading

0 comments on commit 863b353

Please # to comment.