Skip to content

Commit

Permalink
Add validation in quantity for different schedules.
Browse files Browse the repository at this point in the history
  • Loading branch information
areyeslo committed Dec 13, 2024
1 parent b8d4595 commit 8350dca
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 92 deletions.
4 changes: 1 addition & 3 deletions backend/lcfs/web/api/fuel_supply/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ class FuelSupplyCreateUpdateSchema(BaseSchema):
fuel_category_id: int
end_use_id: Optional[int] = None
provision_of_the_act_id: int
quantity: int = Field(
..., gt=0, description="Quantity supplied must be greater than 0"
)
quantity: int
units: str
fuel_type_other: Optional[str] = None
fuel_code_id: Optional[int] = None
Expand Down
4 changes: 1 addition & 3 deletions backend/lcfs/web/api/notional_transfer/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ class NotionalTransferCreateSchema(BaseSchema):
address_for_service: str
fuel_category: str
received_or_transferred: ReceivedOrTransferredEnumSchema
quantity: int = Field(
..., gt=0, description="Quantity supplied must be greater than 0"
)
quantity: int
notional_transfer_id: Optional[int] = None
compliance_report_id: int
deleted: Optional[bool] = None
Expand Down
4 changes: 1 addition & 3 deletions backend/lcfs/web/api/other_uses/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ class OtherUsesCreateSchema(BaseSchema):
fuel_type: str
fuel_category: str
provision_of_the_act: str
quantity_supplied: int = Field(
..., gt=0, description="Quantity supplied must be greater than 0"
)
quantity_supplied: int
units: str
expected_use: str
fuel_code: Optional[str] = None
Expand Down
4 changes: 1 addition & 3 deletions backend/lcfs/web/api/transaction/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ class TransactionViewSchema(BaseSchema):
transaction_type: str
from_organization: Optional[str] = None
to_organization: str
quantity: int = Field(
..., gt=0, description="Quantity supplied must be greater than 0"
)
quantity: int
price_per_unit: Optional[float] = None
status: str
create_date: datetime
Expand Down
4 changes: 1 addition & 3 deletions backend/lcfs/web/api/transfer/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ class TransferSchema(BaseSchema):
from_organization: TransferOrganizationSchema
to_organization: TransferOrganizationSchema
agreement_date: date
quantity: int = Field(
..., gt=0, description="Quantity supplied must be greater than 0"
)
quantity: int
price_per_unit: float
comments: Optional[List[TransferCommentSchema]] = None
from_org_comment: Optional[str] = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,22 @@ export const AddEditAllocationAgreements = () => {
}
}, [location.state?.message, location.state?.severity])

const validateField = (
params,
field,
validationFn,
errorMessage,
alertRef
) => {
const newValue = params.newValue
const validate = (params, validationFn, errorMessage, alertRef, field = null) => {
const value = field ? params.node?.data[field] : params;

if (params.colDef.field === field) {
if (!validationFn(newValue)) {
alertRef.current?.triggerAlert({
message: errorMessage,
severity: 'error'
})
return false
}
if (field && params.colDef.field !== field) {
return true;
}

return true // Proceed with the update
}
if (!validationFn(value)) {
alertRef.current?.triggerAlert({
message: errorMessage,
severity: 'error',
});
return false;
}
return true; // Proceed with the update
};

const onGridReady = useCallback(
async (params) => {
Expand Down Expand Up @@ -168,17 +163,23 @@ export const AddEditAllocationAgreements = () => {

const onCellEditingStopped = useCallback(
async (params) => {
const isValid = validateField(
if (params.oldValue === params.newValue) return

const isValid = validate(
params,
(value) => {
return value !== null && !isNaN(value) && value > 0;
},
'Quantity supplied must be greater than 0.',
alertRef,
'quantity',
(value) => value !== null && !isNaN(value) && value > 0,
'Quantity must be greater than 0.',
alertRef
)
);

if (!isValid) return
if (!isValid) {
return
}

if (params.oldValue === params.newValue) return
if (!isValid) return

params.node.updateData({
...params.node.data,
Expand Down
39 changes: 22 additions & 17 deletions frontend/src/views/FuelSupplies/AddEditFuelSupplies.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,20 @@ export const AddEditFuelSupplies = () => {
}
}, [location?.state?.message, location?.state?.severity]);

const validateField = (params, field, validationFn, errorMessage, alertRef) => {
const newValue = params.newValue;
const validate = (params, validationFn, errorMessage, alertRef, field = null) => {
const value = field ? params.node?.data[field] : params;

if (params.colDef.field === field) {
if (!validationFn(newValue)) {
alertRef.current?.triggerAlert({
message: errorMessage,
severity: 'error',
});
return false;
}
if (field && params.colDef.field !== field) {
return true;
}

if (!validationFn(value)) {
alertRef.current?.triggerAlert({
message: errorMessage,
severity: 'error',
});
return false;
}
return true; // Proceed with the update
};

Expand Down Expand Up @@ -164,17 +165,21 @@ export const AddEditFuelSupplies = () => {

const onCellEditingStopped = useCallback(
async (params) => {
const isValid = validateField(
if (params.oldValue === params.newValue) return

const isValid = validate(
params,
'quantity',
(value) => value !== null && !isNaN(value) && value > 0,
(value) => {
return value !== null && !isNaN(value) && value > 0;
},
'Quantity supplied must be greater than 0.',
alertRef
alertRef,
'quantity',
);

if (!isValid) return;

if (params.oldValue === params.newValue) return
if (!isValid) {
return
}

params.node.updateData({
...params.node.data,
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/views/FuelSupplies/_schema.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export const fuelSupplyColDefs = (optionsData, errors, warnings) => [
params.data.provisionOfTheAct = null
params.data.fuelCode = null
params.data.fuelCodeId = null
params.data.quantity = 0
params.data.units = fuelType?.unit
params.data.unrecognized = fuelType?.unrecognized
}
Expand Down Expand Up @@ -176,7 +175,6 @@ export const fuelSupplyColDefs = (optionsData, errors, warnings) => [
params.data.eer = null
params.data.provisionOfTheAct = null
params.data.fuelCode = null
params.data.quantity = 0
}
return true
},
Expand Down
39 changes: 22 additions & 17 deletions frontend/src/views/NotionalTransfers/AddEditNotionalTransfers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,20 @@ export const AddEditNotionalTransfers = () => {
}
}, [location?.state?.message, location?.state?.severity]);

const validateField = (params, field, validationFn, errorMessage, alertRef) => {
const newValue = params.newValue;
const validate = (params, validationFn, errorMessage, alertRef, field = null) => {
const value = field ? params.node?.data[field] : params;

if (params.colDef.field === field) {
if (!validationFn(newValue)) {
alertRef.current?.triggerAlert({
message: errorMessage,
severity: 'error',
});
return false;
}
if (field && params.colDef.field !== field) {
return true;
}

if (!validationFn(value)) {
alertRef.current?.triggerAlert({
message: errorMessage,
severity: 'error',
});
return false;
}
return true; // Proceed with the update
};

Expand Down Expand Up @@ -98,17 +99,21 @@ export const AddEditNotionalTransfers = () => {

const onCellEditingStopped = useCallback(
async (params) => {
const isValid = validateField(
if (params.oldValue === params.newValue) return

const isValid = validate(
params,
(value) => {
return value !== null && !isNaN(value) && value > 0;
},
'Quantity supplied must be greater than 0.',
alertRef,
'quantity',
(value) => value !== null && !isNaN(value) && value > 0,
'Quantity must be greater than 0.',
alertRef
);

if (!isValid) return;

if (params.oldValue === params.newValue) return
if (!isValid) {
return
}

// Initialize updated data with 'pending' status
params.node.updateData({
Expand Down
38 changes: 22 additions & 16 deletions frontend/src/views/OtherUses/AddEditOtherUses.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,20 @@ export const AddEditOtherUses = () => {
return ciOfFuel;
}, []);

const validateField = (params, field, validationFn, errorMessage, alertRef) => {
const newValue = params.newValue;
const validate = (params, validationFn, errorMessage, alertRef, field = null) => {
const value = field ? params.node?.data[field] : params;

if (params.colDef.field === field) {
if (!validationFn(newValue)) {
alertRef.current?.triggerAlert({
message: errorMessage,
severity: 'error',
});
return false;
}
if (field && params.colDef.field !== field) {
return true;
}

if (!validationFn(value)) {
alertRef.current?.triggerAlert({
message: errorMessage,
severity: 'error',
});
return false;
}
return true; // Proceed with the update
};

Expand Down Expand Up @@ -185,17 +186,22 @@ export const AddEditOtherUses = () => {

const onCellEditingStopped = useCallback(
async (params) => {
const isValid = validateField(
if (params.oldValue === params.newValue) return

const isValid = validate(
params,
'quantitySupplied',
(value) => value !== null && !isNaN(value) && value > 0,
(value) => {
return value !== null && !isNaN(value) && value > 0;
},
'Quantity supplied must be greater than 0.',
alertRef
alertRef,
'quantitySupplied',
);

if (!isValid) return;
if (!isValid) {
return
}

if (params.oldValue === params.newValue) return
params.data.complianceReportId = complianceReportId
params.data.validationStatus = 'pending'

Expand Down

0 comments on commit 8350dca

Please sign in to comment.