Skip to content

Commit

Permalink
Merge pull request #2783 from bcgov/507-bug-emission-allocation-fails…
Browse files Browse the repository at this point in the history
…-with-other

507 bug emission allocation fails with other
  • Loading branch information
acatchpole authored Feb 6, 2025
2 parents d8cc0b8 + 2c45726 commit 5bdda8c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Migration(migrations.Migration):
(
'allocation_methodology',
models.CharField(
choices=[('Calculator', 'Calculator'), ('other', 'Other')],
choices=[('Calculator', 'Calculator'), ('Other', 'Other')],
default='Calculator',
db_comment='The methodology used to calculate the allocated emissions',
max_length=255,
Expand Down Expand Up @@ -126,12 +126,12 @@ class Migration(migrations.Migration):
model_name='reportproductemissionallocation',
constraint=models.CheckConstraint(
check=models.Q(
('allocation_methodology', 'other'),
('allocation_methodology', 'Other'),
('allocation_other_methodology_description__isnull', True),
_negated=True,
),
name='allocation_other_methodology_must_have_description',
violation_error_message="A value for allocation_other_methodology_description must be provided if the allocation_methodology is 'other'",
violation_error_message="A value for allocation_other_methodology_description must be provided if the allocation_methodology is 'Other'",
),
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ReportProductEmissionAllocation(TimeStampedModel):

class AllocationMethodologyChoices(models.TextChoices):
CALCULATOR = ("Calculator",)
OTHER = "other"
OTHER = "Other"

report_version = models.ForeignKey(
report_version.ReportVersion,
Expand Down Expand Up @@ -69,7 +69,7 @@ class Meta(TimeStampedModel.Meta):
),
models.CheckConstraint(
name="allocation_other_methodology_must_have_description",
check=~Q(allocation_methodology="other", allocation_other_methodology_description__isnull=True),
violation_error_message="A value for allocation_other_methodology_description must be provided if the allocation_methodology is 'other'",
check=~Q(allocation_methodology="Other", allocation_other_methodology_description__isnull=True),
violation_error_message="A value for allocation_other_methodology_description must be provided if the allocation_methodology is 'Other'",
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setUpTestData(cls):
report_product=report_product,
emission_category=emission_category,
allocated_quantity=Decimal('300.4151'),
allocation_methodology='other',
allocation_methodology='Other',
allocation_other_methodology_description='Test description',
)
cls.field_data = [
Expand Down Expand Up @@ -56,7 +56,7 @@ def test_allow_null_description_if_methodology_is_not_other(self):

with pytest.raises(
ValidationError,
match="A value for allocation_other_methodology_description must be provided if the allocation_methodology is 'other'",
match="A value for allocation_other_methodology_description must be provided if the allocation_methodology is 'Other'",
):
make(
ReportProductEmissionAllocation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,23 +160,23 @@ def setUp(self):
"allocated_quantity": self.ALLOCATION_AMOUNT_2,
},
],
"allocation_methodology": "other",
"allocation_methodology": "Other",
"allocation_other_methodology_description": "test",
}

self.post_payload = ReportProductEmissionAllocationsSchemaIn(
report_product_emission_allocations=self.create_mock_allocation_array(
report_product1=report_product_1, report_product2=report_product_2, test_stage=2
),
allocation_methodology="other",
allocation_methodology="Other",
allocation_other_methodology_description="test",
)

self.post_payload2 = ReportProductEmissionAllocationsSchemaIn(
report_product_emission_allocations=self.create_mock_allocation_array(
report_product1=report_product_1, report_product2=report_product_2, test_stage=3
),
allocation_methodology="other",
allocation_methodology="Other",
allocation_other_methodology_description="test",
)

Expand All @@ -200,7 +200,7 @@ def test_get_report_emission_allocation(self):
report_product=product,
emission_category_id=data["category"],
allocated_quantity=data["quantity"],
allocation_methodology="other",
allocation_methodology="Other",
allocation_other_methodology_description="test",
)
# Arrange: Create an emission allocation to an excluded category
Expand All @@ -211,7 +211,7 @@ def test_get_report_emission_allocation(self):
report_product=report_products[1],
emission_category_id=self.WOODY_BIOMASS_CATEGORY_ID,
allocated_quantity=self.ALLOCATION_AMOUNT_3,
allocation_methodology="other",
allocation_methodology="Other",
allocation_other_methodology_description="test",
)

Expand Down Expand Up @@ -345,7 +345,6 @@ def test_get_report_emission_allocation(self):
)

def test_save_emission_allocation(self):

# Act: Use the service to save some emission allocation data (payload includes data for 2 allocations)
ReportEmissionAllocationService.save_emission_allocation_data(
self.test_infrastructure.report_version.pk,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,43 @@ const validateEmissions = (formData: FormData): boolean => {
});
};

const validateMethodology = (formData: FormData): boolean => {
return (
formData.allocation_methodology !== undefined &&
formData.allocation_methodology !== ""
);
};

const validateMethodologyOther = (formData: FormData): boolean => {
return formData.allocation_methodology !== "Other"
? true
: formData.allocation_other_methodology_description !== undefined &&
formData.allocation_other_methodology_description !== "";
};

const validateFormData = (formData: FormData) => {
const errorMismatch =
"All emissions must be allocated to 100% before saving and continuing";
const errorMethodology =
"A methodology must be selected before saving and continuing";
const errorMethodologyOther =
"A description must be provided for the selected methodology";

const newErrors: string[] = [];

if (!validateEmissions(formData)) {
newErrors.push(errorMismatch);
}
if (!validateMethodology(formData)) {
newErrors.push(errorMethodology);
}
if (!validateMethodologyOther(formData)) {
newErrors.push(errorMethodologyOther);
}

return newErrors;
};

export default function FacilityEmissionAllocationForm({
version_id,
facility_id,
Expand All @@ -78,8 +115,7 @@ export default function FacilityEmissionAllocationForm({
}));

// State for submit button disable
const errorMismatch =
"All emissions must be allocated to 100% before saving and continuing";

const [errors, setErrors] = useState<string[] | undefined>();
const [submitButtonDisabled, setSubmitButtonDisabled] = useState(true);

Expand All @@ -97,10 +133,9 @@ export default function FacilityEmissionAllocationForm({

// 🔄 Check for allocation mismatch on page load to prevent submit
useEffect(() => {
if (!validateEmissions(formData)) {
setErrors([errorMismatch]);
}
setSubmitButtonDisabled(!validateEmissions(formData));
const newErrors = validateFormData(formData);
setErrors(newErrors);
setSubmitButtonDisabled(newErrors.length > 0);
}, [formData]);

// 🛠️ Handle changes to the form data, validates emissions, and updates the error state and submit button state.
Expand Down Expand Up @@ -151,14 +186,10 @@ export default function FacilityEmissionAllocationForm({
}),
);
}

// Validate the updated form data and set an error message if validation fails
if (!validateEmissions(updatedFormData)) {
setErrors([errorMismatch]);
setSubmitButtonDisabled(true);
} else {
setSubmitButtonDisabled(false);
}
const newErrors = validateFormData(updatedFormData);
setErrors(newErrors);
setSubmitButtonDisabled(newErrors.length > 0);

// Update the form data state
setFormData(updatedFormData);
Expand Down Expand Up @@ -205,7 +236,6 @@ export default function FacilityEmissionAllocationForm({
return false;
}

setErrors(undefined);
return true;
};

Expand Down

0 comments on commit 5bdda8c

Please sign in to comment.