Skip to content

Commit

Permalink
Merge pull request #1335 from bcgov/LCFS-LCFS-1280-FuelSupplyValidati…
Browse files Browse the repository at this point in the history
…on_v2

LCFS-1280: Validation for Quantity Supplied
  • Loading branch information
areyeslo authored Dec 17, 2024
2 parents 8edd0af + a14d1ec commit 4dab704
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 14 deletions.
2 changes: 1 addition & 1 deletion backend/lcfs/web/api/transaction/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional, List

from pydantic import ConfigDict
from pydantic import ConfigDict, Field
from lcfs.web.api.base import BaseSchema
from datetime import datetime
from enum import Enum
Expand Down
2 changes: 1 addition & 1 deletion backend/lcfs/web/api/transfer/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Optional, List
from datetime import date, datetime
from enum import Enum
from pydantic import ConfigDict
from pydantic import ConfigDict, Field


class TransferRecommendationEnumSchema(str, Enum):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,24 @@ export const AddEditAllocationAgreements = () => {
severity: location.state.severity || 'info'
})
}
}, [location.state])
}, [location.state?.message, location.state?.severity])

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

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
};

const onGridReady = useCallback(
async (params) => {
Expand Down Expand Up @@ -156,6 +173,22 @@ export const AddEditAllocationAgreements = () => {
async (params) => {
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',
);

if (!isValid) {
return
}

if (!isValid) return

params.node.updateData({
...params.node.data,
validationStatus: 'pending'
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/views/AllocationAgreements/_schema.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ export const allocationAgreementColDefs = (optionsData, errors) => [
headerName: i18n.t(
'allocationAgreement:allocationAgreementColLabels.quantity'
),
valueFormatter,
editor: NumberEditor,
valueFormatter: (params) => valueFormatter({ value: params.value }),
cellEditor: NumberEditor,
cellEditorParams: {
precision: 0,
Expand Down
35 changes: 33 additions & 2 deletions frontend/src/views/FuelSupplies/AddEditFuelSupplies.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,30 @@ export const AddEditFuelSupplies = () => {
)

useEffect(() => {
if (location.state?.message) {
if (location?.state?.message) {
alertRef.current?.triggerAlert({
message: location.state.message,
severity: location.state.severity || 'info'
})
}
}, [location.state])
}, [location?.state?.message, location?.state?.severity]);

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

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
};

const onGridReady = useCallback(
async (params) => {
Expand Down Expand Up @@ -152,6 +169,20 @@ export const AddEditFuelSupplies = () => {
async (params) => {
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',
);

if (!isValid) {
return
}

params.node.updateData({
...params.node.data,
validationStatus: 'pending'
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/views/FuelSupplies/_schema.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,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 @@ -178,7 +177,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 Expand Up @@ -382,7 +380,7 @@ export const fuelSupplyColDefs = (optionsData, errors, warnings) => [
field: 'quantity',
headerComponent: RequiredHeader,
headerName: i18n.t('fuelSupply:fuelSupplyColLabels.quantity'),
valueFormatter,
valueFormatter: (params) => valueFormatter({ value: params.value }),
cellEditor: NumberEditor,
cellEditorParams: {
precision: 0,
Expand Down
35 changes: 33 additions & 2 deletions frontend/src/views/NotionalTransfers/AddEditNotionalTransfers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,30 @@ export const AddEditNotionalTransfers = () => {
const navigate = useNavigate()

useEffect(() => {
if (location.state?.message) {
if (location?.state?.message) {
alertRef.triggerAlert({
message: location.state.message,
severity: location.state.severity || 'info'
})
}
}, [location.state])
}, [location?.state?.message, location?.state?.severity]);

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

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
};

const onGridReady = (params) => {
const ensureRowIds = (rows) => {
Expand Down Expand Up @@ -98,6 +115,20 @@ export const AddEditNotionalTransfers = () => {
async (params) => {
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',
);

if (!isValid) {
return
}

// Initialize updated data with 'pending' status
params.node.updateData({
...params.node.data,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/views/NotionalTransfers/_schema.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const notionalTransferColDefs = (optionsData, errors) => [
min: 0,
showStepperButtons: false
},
valueFormatter,
valueFormatter: (params) => valueFormatter({ value: params.value }),
cellStyle: (params) => StandardCellErrors(params, errors)
}
]
Expand Down
32 changes: 32 additions & 0 deletions frontend/src/views/OtherUses/AddEditOtherUses.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ export const AddEditOtherUses = () => {
return ciOfFuel
}, [])

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

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
};

const onGridReady = (params) => {
const ensureRowIds = (rows) => {
return rows.map((row) => {
Expand Down Expand Up @@ -201,6 +218,21 @@ export const AddEditOtherUses = () => {
const onCellEditingStopped = useCallback(
async (params) => {
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,
'quantitySupplied',
);

if (!isValid) {
return
}

params.data.complianceReportId = complianceReportId
params.data.validationStatus = 'pending'

Expand Down
3 changes: 1 addition & 2 deletions frontend/src/views/OtherUses/_schema.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,10 @@ export const otherUsesColDefs = (optionsData, errors) => [
headerName: i18n.t('otherUses:otherUsesColLabels.quantitySupplied'),
headerComponent: RequiredHeader,
cellEditor: NumberEditor,
valueFormatter,
valueFormatter: (params) => valueFormatter({ value: params.value }),
type: 'numericColumn',
cellEditorParams: {
precision: 0,
min: 0,
showStepperButtons: false
},
cellStyle: (params) => StandardCellErrors(params, errors),
Expand Down

0 comments on commit 4dab704

Please sign in to comment.