From 8eaabec26d2fa68efdc4251f43b1f7d821bce07a Mon Sep 17 00:00:00 2001 From: Daniel Haselhan Date: Fri, 3 Jan 2025 13:24:16 -0800 Subject: [PATCH] feat: Filter between legacy and not legacy fuel types * Pass compliance period for get other use table options * Add filter on compliance period < 2024 to enable the is_legacy filter. * Add new is_legacy to FuelTypes and default to false --- .../versions/2025-01-03-18-43_ca7200152130.py | 37 +++++++++++++ backend/lcfs/db/models/fuel/FuelType.py | 8 ++- .../tests/other_uses/test_other_uses_repo.py | 5 +- .../other_uses/test_other_uses_services.py | 32 +++++------- .../tests/other_uses/test_other_uses_view.py | 7 ++- .../lcfs/web/api/allocation_agreement/repo.py | 6 ++- .../web/api/allocation_agreement/services.py | 6 ++- .../web/api/allocation_agreement/views.py | 3 +- backend/lcfs/web/api/fuel_code/repo.py | 38 ++++++++------ backend/lcfs/web/api/fuel_export/repo.py | 8 ++- backend/lcfs/web/api/fuel_supply/repo.py | 4 ++ backend/lcfs/web/api/other_uses/repo.py | 32 +++++++----- backend/lcfs/web/api/other_uses/services.py | 18 +++---- backend/lcfs/web/api/other_uses/views.py | 3 +- frontend/src/constants/routes/apiRoutes.js | 2 +- frontend/src/hooks/useFuelSupply.js | 2 +- frontend/src/hooks/useOtherUses.js | 5 +- .../src/views/OtherUses/AddEditOtherUses.jsx | 52 ++++++++++--------- 18 files changed, 172 insertions(+), 96 deletions(-) create mode 100644 backend/lcfs/db/migrations/versions/2025-01-03-18-43_ca7200152130.py diff --git a/backend/lcfs/db/migrations/versions/2025-01-03-18-43_ca7200152130.py b/backend/lcfs/db/migrations/versions/2025-01-03-18-43_ca7200152130.py new file mode 100644 index 000000000..fee67574b --- /dev/null +++ b/backend/lcfs/db/migrations/versions/2025-01-03-18-43_ca7200152130.py @@ -0,0 +1,37 @@ +"""Add is_legacy to fuel_type + +Revision ID: ca7200152130 +Revises: ab04810d4d7c +Create Date: 2025-01-03 18:43:43.638740 + +""" + +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision = "ca7200152130" +down_revision = "ab04810d4d7c" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column( + "fuel_type", + sa.Column( + "is_legacy", + sa.Boolean(), + server_default=sa.text("FALSE"), + nullable=False, + comment="Indicates if the fuel type is legacy and should not be used for new reports", + ), + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column("fuel_type", "is_legacy") + # ### end Alembic commands ### diff --git a/backend/lcfs/db/models/fuel/FuelType.py b/backend/lcfs/db/models/fuel/FuelType.py index 4ebbb7aae..e6d634805 100644 --- a/backend/lcfs/db/models/fuel/FuelType.py +++ b/backend/lcfs/db/models/fuel/FuelType.py @@ -1,6 +1,6 @@ import enum -from sqlalchemy import Column, Integer, Text, Boolean, Enum, Numeric +from sqlalchemy import Column, Integer, Text, Boolean, Enum, Numeric, text from sqlalchemy import ForeignKey from sqlalchemy.orm import relationship @@ -54,6 +54,12 @@ class FuelType(BaseModel, Auditable, DisplayOrder): nullable=False, comment="Indicates if the fuel type is unrecognized", ) + is_legacy = Column( + Boolean, + server_default=text("FALSE"), + nullable=False, + comment="Indicates if the fuel type is legacy and should not be used for new reports", + ) # Relationships fuel_codes = relationship("FuelCode", back_populates="fuel_type") diff --git a/backend/lcfs/tests/other_uses/test_other_uses_repo.py b/backend/lcfs/tests/other_uses/test_other_uses_repo.py index 67ea7d1d5..3906980c8 100644 --- a/backend/lcfs/tests/other_uses/test_other_uses_repo.py +++ b/backend/lcfs/tests/other_uses/test_other_uses_repo.py @@ -36,7 +36,7 @@ def other_uses_repo(mock_db_session): repo.fuel_code_repo.get_expected_use_types = AsyncMock(return_value=[]) # Mock for local get_formatted_fuel_types method - async def mock_get_formatted_fuel_types(): + async def mock_get_formatted_fuel_types(include_legacy=False): mock_result = await mock_db_session.execute(AsyncMock()) return mock_result.unique().scalars().all() @@ -116,7 +116,7 @@ def mock_execute_side_effect(*args, **kwargs): ) # Execute the method under test - result = await other_uses_repo.get_table_options() + result = await other_uses_repo.get_table_options("2024") # Assertions assert isinstance(result, dict) @@ -222,6 +222,7 @@ async def test_get_latest_other_uses_by_group_uuid(other_uses_repo, mock_db_sess assert result.user_type == UserTypeEnum.GOVERNMENT assert result.version == 2 + @pytest.mark.anyio async def test_get_other_use_version_by_user(other_uses_repo, mock_db_session): group_uuid = "test-group-uuid" diff --git a/backend/lcfs/tests/other_uses/test_other_uses_services.py b/backend/lcfs/tests/other_uses/test_other_uses_services.py index 86794426a..e36d9ab7f 100644 --- a/backend/lcfs/tests/other_uses/test_other_uses_services.py +++ b/backend/lcfs/tests/other_uses/test_other_uses_services.py @@ -36,7 +36,7 @@ async def test_get_table_options(other_uses_service): } ) - response = await service.get_table_options() + response = await service.get_table_options("2024") assert isinstance(response, OtherUsesTableOptionsSchema) mock_repo.get_table_options.assert_awaited_once() @@ -66,16 +66,15 @@ async def test_create_other_use(other_uses_service): mock_fuel_code.fuel_code = "FuelCode123" # Mock fuel repository methods - mock_fuel_repo.get_fuel_category_by = AsyncMock( - return_value=mock_fuel_category) - mock_fuel_repo.get_fuel_type_by_name = AsyncMock( - return_value=mock_fuel_type) + mock_fuel_repo.get_fuel_category_by = AsyncMock(return_value=mock_fuel_category) + mock_fuel_repo.get_fuel_type_by_name = AsyncMock(return_value=mock_fuel_type) mock_fuel_repo.get_expected_use_type_by_name = AsyncMock( - return_value=mock_expected_use) + return_value=mock_expected_use + ) mock_fuel_repo.get_provision_of_the_act_by_name = AsyncMock( - return_value=mock_provision_of_the_act) - mock_fuel_repo.get_fuel_code_by_name = AsyncMock( - return_value=mock_fuel_code) + return_value=mock_provision_of_the_act + ) + mock_fuel_repo.get_fuel_code_by_name = AsyncMock(return_value=mock_fuel_code) # Create a mock for the created other use mock_created_use = create_mock_entity({}) @@ -120,8 +119,7 @@ async def test_update_other_use(other_uses_service): mock_existing_use = create_mock_entity({}) # Configure repository methods to return these mocked objects - mock_repo.get_other_use_version_by_user = AsyncMock( - return_value=mock_existing_use) + mock_repo.get_other_use_version_by_user = AsyncMock(return_value=mock_existing_use) # Mock related entities with proper string attributes mock_fuel_type = MagicMock() @@ -140,17 +138,15 @@ async def test_update_other_use(other_uses_service): mock_fuel_code.fuel_code = "NewFuelCode" # Mock fuel repository methods - mock_fuel_repo.get_fuel_type_by_name = AsyncMock( - return_value=mock_fuel_type) - mock_fuel_repo.get_fuel_category_by = AsyncMock( - return_value=mock_fuel_category) + mock_fuel_repo.get_fuel_type_by_name = AsyncMock(return_value=mock_fuel_type) + mock_fuel_repo.get_fuel_category_by = AsyncMock(return_value=mock_fuel_category) mock_fuel_repo.get_expected_use_type_by_name = AsyncMock( - return_value=mock_expected_use) + return_value=mock_expected_use + ) mock_fuel_repo.get_provision_of_the_act_by_name = AsyncMock( return_value=mock_provision_of_the_act ) - mock_fuel_repo.get_fuel_code_by_name = AsyncMock( - return_value=mock_fuel_code) + mock_fuel_repo.get_fuel_code_by_name = AsyncMock(return_value=mock_fuel_code) # Mock the updated use returned after the update mock_updated_use = MagicMock() diff --git a/backend/lcfs/tests/other_uses/test_other_uses_view.py b/backend/lcfs/tests/other_uses/test_other_uses_view.py index 3de83dee8..3f0e92f42 100644 --- a/backend/lcfs/tests/other_uses/test_other_uses_view.py +++ b/backend/lcfs/tests/other_uses/test_other_uses_view.py @@ -6,7 +6,10 @@ from lcfs.db.base import UserTypeEnum, ActionTypeEnum from lcfs.db.models.user.Role import RoleEnum from lcfs.web.api.base import ComplianceReportRequestSchema -from lcfs.web.api.other_uses.schema import PaginatedOtherUsesRequestSchema, OtherUsesSchema +from lcfs.web.api.other_uses.schema import ( + PaginatedOtherUsesRequestSchema, + OtherUsesSchema, +) from lcfs.web.api.other_uses.services import OtherUsesServices from lcfs.web.api.other_uses.validation import OtherUsesValidation from lcfs.tests.other_uses.conftest import create_mock_schema, create_mock_entity @@ -47,7 +50,7 @@ async def test_get_table_options( lambda: mock_other_uses_service ) - response = await client.get(url) + response = await client.get(url + "?compliancePeriod=2024") assert response.status_code == 200 data = response.json() diff --git a/backend/lcfs/web/api/allocation_agreement/repo.py b/backend/lcfs/web/api/allocation_agreement/repo.py index 374f7e6fa..080874369 100644 --- a/backend/lcfs/web/api/allocation_agreement/repo.py +++ b/backend/lcfs/web/api/allocation_agreement/repo.py @@ -34,10 +34,12 @@ def __init__( self.fuel_code_repo = fuel_repo @repo_handler - async def get_table_options(self) -> dict: + async def get_table_options(self, compliance_period: str) -> dict: """Get all table options""" fuel_categories = await self.fuel_code_repo.get_fuel_categories() - fuel_types = await self.fuel_code_repo.get_formatted_fuel_types() + fuel_types = await self.fuel_code_repo.get_formatted_fuel_types( + include_legacy=compliance_period < "2024" + ) units_of_measure = [unit.value for unit in QuantityUnitsEnum] allocation_transaction_types = ( (await self.db.execute(select(AllocationTransactionType))).scalars().all() diff --git a/backend/lcfs/web/api/allocation_agreement/services.py b/backend/lcfs/web/api/allocation_agreement/services.py index 59ada9e41..12954ec63 100644 --- a/backend/lcfs/web/api/allocation_agreement/services.py +++ b/backend/lcfs/web/api/allocation_agreement/services.py @@ -87,11 +87,13 @@ async def convert_to_model( ) @service_handler - async def get_table_options(self) -> AllocationAgreementTableOptionsSchema: + async def get_table_options( + self, compliance_period: str + ) -> AllocationAgreementTableOptionsSchema: """ Gets the list of table options related to allocation agreements. """ - table_options = await self.repo.get_table_options() + table_options = await self.repo.get_table_options(compliance_period) fuel_types = [ { **fuel_type, diff --git a/backend/lcfs/web/api/allocation_agreement/views.py b/backend/lcfs/web/api/allocation_agreement/views.py index bb61a4505..3e30919de 100644 --- a/backend/lcfs/web/api/allocation_agreement/views.py +++ b/backend/lcfs/web/api/allocation_agreement/views.py @@ -48,10 +48,11 @@ @view_handler(["*"]) async def get_table_options( request: Request, + compliancePeriod: str, service: AllocationAgreementServices = Depends(), ): """Endpoint to retrieve table options related to allocation agreements""" - return await service.get_table_options() + return await service.get_table_options(compliancePeriod) @router.post( diff --git a/backend/lcfs/web/api/fuel_code/repo.py b/backend/lcfs/web/api/fuel_code/repo.py index 174ee126b..d05318569 100644 --- a/backend/lcfs/web/api/fuel_code/repo.py +++ b/backend/lcfs/web/api/fuel_code/repo.py @@ -51,23 +51,23 @@ def __init__(self, db: AsyncSession = Depends(get_async_db_session)): self.db = db @repo_handler - async def get_fuel_types(self) -> List[FuelType]: - """Get all fuel type options""" - return ( - ( - await self.db.execute( - select(FuelType).options( - joinedload(FuelType.provision_1), - joinedload(FuelType.provision_2), - ) - ) - ) - .scalars() - .all() + async def get_fuel_types(self, include_legacy=False) -> List[FuelType]: + stmt = select(FuelType).options( + joinedload(FuelType.provision_1), + joinedload(FuelType.provision_2), ) + # Conditionally add the legacy filter + if not include_legacy: + stmt = stmt.where(FuelType.is_legacy == False) + + result = await self.db.execute(stmt) + return result.scalars().all() + @repo_handler - async def get_formatted_fuel_types(self) -> List[Dict[str, Any]]: + async def get_formatted_fuel_types( + self, include_legacy=False + ) -> List[Dict[str, Any]]: """Get all fuel type options with their associated fuel categories and fuel codes""" # Define the filtering conditions for fuel codes current_date = date.today() @@ -77,13 +77,21 @@ async def get_formatted_fuel_types(self) -> List[Dict[str, Any]]: FuelCode.expiration_date == None, FuelCode.expiration_date > current_date ) + conditions = [fuel_code_filters] + + # If we don't want to include legacy fuel types, filter them out + if not include_legacy: + conditions.append(FuelType.is_legacy == False) + + combined_conditions = and_(*conditions) + # Build the query with filtered fuel_codes query = ( select(FuelType) .outerjoin(FuelType.fuel_instances) .outerjoin(FuelInstance.fuel_category) .outerjoin(FuelType.fuel_codes) - .where(fuel_code_filters) + .where(combined_conditions) .options( contains_eager(FuelType.fuel_instances).contains_eager( FuelInstance.fuel_category diff --git a/backend/lcfs/web/api/fuel_export/repo.py b/backend/lcfs/web/api/fuel_export/repo.py index 510af73b6..32598d45c 100644 --- a/backend/lcfs/web/api/fuel_export/repo.py +++ b/backend/lcfs/web/api/fuel_export/repo.py @@ -52,13 +52,13 @@ def __init__(self, db: AsyncSession = Depends(get_async_db_session)): ) @repo_handler - async def get_fuel_export_table_options(self, compliancePeriod: str): + async def get_fuel_export_table_options(self, compliance_period: str): """ Retrieve Fuel Type and other static data to use them while populating fuel supply form. """ subquery_compliance_period_id = ( select(CompliancePeriod.compliance_period_id) - .where(CompliancePeriod.description == compliancePeriod) + .where(CompliancePeriod.description == compliance_period) .scalar_subquery() ) @@ -164,6 +164,10 @@ async def get_fuel_export_table_options(self, compliancePeriod: str): ) ) + include_legacy = compliance_period < "2024" + if not include_legacy: + query = query.where(FuelType.is_legacy == False) + results = (await self.db.execute(query)).all() return results diff --git a/backend/lcfs/web/api/fuel_supply/repo.py b/backend/lcfs/web/api/fuel_supply/repo.py index dca195539..f90df203c 100644 --- a/backend/lcfs/web/api/fuel_supply/repo.py +++ b/backend/lcfs/web/api/fuel_supply/repo.py @@ -184,6 +184,10 @@ async def get_fuel_supply_table_options(self, compliance_period: str): ) ) + include_legacy = compliance_period < "2024" + if not include_legacy: + query.where(FuelType.is_legacy == False) + fuel_type_results = (await self.db.execute(query)).all() return { diff --git a/backend/lcfs/web/api/other_uses/repo.py b/backend/lcfs/web/api/other_uses/repo.py index 8e515b484..86ed3821b 100644 --- a/backend/lcfs/web/api/other_uses/repo.py +++ b/backend/lcfs/web/api/other_uses/repo.py @@ -35,10 +35,12 @@ def __init__( self.fuel_code_repo = fuel_repo @repo_handler - async def get_table_options(self) -> dict: + async def get_table_options(self, compliance_period: str) -> dict: """Get all table options""" fuel_categories = await self.fuel_code_repo.get_fuel_categories() - fuel_types = await self.get_formatted_fuel_types() + fuel_types = await self.get_formatted_fuel_types( + include_legacy=compliance_period < "2024" + ) expected_uses = await self.fuel_code_repo.get_expected_use_types() units_of_measure = [unit.value for unit in QuantityUnitsEnum] provisions_of_the_act = ( @@ -305,28 +307,34 @@ async def get_other_use_version_by_user( return result.scalars().first() @repo_handler - async def get_formatted_fuel_types(self) -> List[Dict[str, Any]]: + async def get_formatted_fuel_types( + self, include_legacy=False + ) -> List[Dict[str, Any]]: """Get all fuel type options with their associated fuel categories and fuel codes for other uses""" - # Define the filtering conditions for fuel codes current_date = date.today() - fuel_code_filters = ( + base_conditions = [ or_( FuelCode.effective_date == None, FuelCode.effective_date <= current_date - ) - & or_( + ), + or_( FuelCode.expiration_date == None, FuelCode.expiration_date > current_date, - ) - & (FuelType.other_uses_fossil_derived == True) - ) + ), + FuelType.other_uses_fossil_derived == True, + ] + + # Conditionally add the is_legacy filter + if not include_legacy: + base_conditions.append(FuelType.is_legacy == False) + + combined_conditions = and_(*base_conditions) - # Build the query with filtered fuel_codes query = ( select(FuelType) .outerjoin(FuelType.fuel_instances) .outerjoin(FuelInstance.fuel_category) .outerjoin(FuelType.fuel_codes) - .where(fuel_code_filters) + .where(combined_conditions) .options( contains_eager(FuelType.fuel_instances).contains_eager( FuelInstance.fuel_category diff --git a/backend/lcfs/web/api/other_uses/services.py b/backend/lcfs/web/api/other_uses/services.py index db5b22f81..ac4bd29f7 100644 --- a/backend/lcfs/web/api/other_uses/services.py +++ b/backend/lcfs/web/api/other_uses/services.py @@ -111,11 +111,13 @@ def model_to_schema(self, model: OtherUses): return updated_schema @service_handler - async def get_table_options(self) -> OtherUsesTableOptionsSchema: + async def get_table_options( + self, compliance_period: str + ) -> OtherUsesTableOptionsSchema: """ Gets the list of table options related to other uses. """ - table_options = await self.repo.get_table_options() + table_options = await self.repo.get_table_options(compliance_period) return OtherUsesTableOptionsSchema( fuel_categories=[ OtherUsesFuelCategorySchema.model_validate(category) @@ -147,8 +149,7 @@ async def get_other_uses(self, compliance_report_id: int) -> OtherUsesListSchema """ other_uses = await self.repo.get_other_uses(compliance_report_id) return OtherUsesAllSchema( - other_uses=[OtherUsesSchema.model_validate( - ou) for ou in other_uses] + other_uses=[OtherUsesSchema.model_validate(ou) for ou in other_uses] ) @service_handler @@ -201,10 +202,8 @@ async def update_other_use( ) if other_use.fuel_category.category != other_use_data.fuel_category: - other_use.fuel_category = ( - await self.fuel_repo.get_fuel_category_by( - category=other_use_data.fuel_category - ) + other_use.fuel_category = await self.fuel_repo.get_fuel_category_by( + category=other_use_data.fuel_category ) if other_use.expected_use.name != other_use_data.expected_use: @@ -291,8 +290,7 @@ async def delete_other_use( # Copy fields from the latest version for the deletion record for field in existing_fuel_supply.__table__.columns.keys(): if field not in OTHER_USE_EXCLUDE_FIELDS: - setattr(deleted_entity, field, getattr( - existing_fuel_supply, field)) + setattr(deleted_entity, field, getattr(existing_fuel_supply, field)) await self.repo.create_other_use(deleted_entity) return DeleteOtherUsesResponseSchema(success=True, message="Marked as deleted.") diff --git a/backend/lcfs/web/api/other_uses/views.py b/backend/lcfs/web/api/other_uses/views.py index 78704e62e..915f691e6 100644 --- a/backend/lcfs/web/api/other_uses/views.py +++ b/backend/lcfs/web/api/other_uses/views.py @@ -42,10 +42,11 @@ # @cache(expire=60 * 60 * 24) # cache for 24 hours async def get_table_options( request: Request, + compliancePeriod: str, service: OtherUsesServices = Depends(), ): """Endpoint to retrieve table options related to other uses""" - return await service.get_table_options() + return await service.get_table_options(compliancePeriod) @router.post( diff --git a/frontend/src/constants/routes/apiRoutes.js b/frontend/src/constants/routes/apiRoutes.js index d79d25464..b8f7a1af5 100644 --- a/frontend/src/constants/routes/apiRoutes.js +++ b/frontend/src/constants/routes/apiRoutes.js @@ -34,7 +34,7 @@ export const apiRoutes = { saveOtherUses: '/other-uses/save', getOtherUses: '/other-uses/list', getAllOtherUses: '/other-uses/list-all', - otherUsesOptions: '/other-uses/table-options', + otherUsesOptions: '/other-uses/table-options?', getComplianceReport: '/reports/:reportID', updateComplianceReport: '/reports/:reportID', getComplianceReportSummary: '/reports/:reportID/summary', diff --git a/frontend/src/hooks/useFuelSupply.js b/frontend/src/hooks/useFuelSupply.js index 33c059847..86606e630 100644 --- a/frontend/src/hooks/useFuelSupply.js +++ b/frontend/src/hooks/useFuelSupply.js @@ -8,7 +8,7 @@ export const useFuelSupplyOptions = (params, options) => { const path = apiRoutes.fuelSupplyOptions + 'compliancePeriod=' + params.compliancePeriod return useQuery({ - queryKey: ['fuel-supply-options'], + queryKey: ['fuel-supply-options', params.compliancePeriod], queryFn: async () => (await client.get(path)).data, ...options }) diff --git a/frontend/src/hooks/useOtherUses.js b/frontend/src/hooks/useOtherUses.js index 49ad999ee..3ff5481ef 100644 --- a/frontend/src/hooks/useOtherUses.js +++ b/frontend/src/hooks/useOtherUses.js @@ -4,9 +4,10 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' export const useOtherUsesOptions = (params, options) => { const client = useApiService() - const path = apiRoutes.otherUsesOptions + const path = + apiRoutes.otherUsesOptions + 'compliancePeriod=' + params.compliancePeriod return useQuery({ - queryKey: ['other-uses-options'], + queryKey: ['other-uses-options', params.compliancePeriod], queryFn: async () => (await client.get(path)).data, ...options }) diff --git a/frontend/src/views/OtherUses/AddEditOtherUses.jsx b/frontend/src/views/OtherUses/AddEditOtherUses.jsx index 944a6ac67..17a625c32 100644 --- a/frontend/src/views/OtherUses/AddEditOtherUses.jsx +++ b/frontend/src/views/OtherUses/AddEditOtherUses.jsx @@ -1,4 +1,3 @@ - import { BCGridEditor } from '@/components/BCDataGrid/BCGridEditor' import Loading from '@/components/Loading' import { @@ -33,7 +32,7 @@ export const AddEditOtherUses = () => { data: optionsData, isLoading: optionsLoading, isFetched - } = useOtherUsesOptions() + } = useOtherUsesOptions({ compliancePeriod }) const { data: otherUses, isLoading: usesLoading } = useGetAllOtherUses(complianceReportId) const { mutateAsync: saveRow } = useSaveOtherUses({ complianceReportId }) @@ -81,22 +80,28 @@ export const AddEditOtherUses = () => { return ciOfFuel }, []) - const validate = (params, validationFn, errorMessage, alertRef, field = null) => { - const value = field ? params.node?.data[field] : params; + const validate = ( + params, + validationFn, + errorMessage, + alertRef, + field = null + ) => { + const value = field ? params.node?.data[field] : params if (field && params.colDef.field !== field) { - return true; + return true } if (!validationFn(value)) { alertRef.current?.triggerAlert({ message: errorMessage, - severity: 'error', - }); - return false; + severity: 'error' + }) + return false } - return true; // Proceed with the update - }; + return true // Proceed with the update + } const onGridReady = (params) => { const ensureRowIds = (rows) => { @@ -184,35 +189,34 @@ export const AddEditOtherUses = () => { if (params.colDef.field === 'fuelType') { const fuelType = optionsData?.fuelTypes?.find( (obj) => params.data.fuelType === obj.fuelType - ); + ) if (fuelType) { // Auto-populate the "units" field if (fuelType.units) { - params.node.setDataValue('units', fuelType.units); + params.node.setDataValue('units', fuelType.units) } else { - params.node.setDataValue('units', ''); + params.node.setDataValue('units', '') } // Auto-populate the "fuelCategory" field const fuelCategoryOptions = fuelType.fuelCategories.map( (item) => item.category - ); + ) - const categoryValue = fuelCategoryOptions.length === 1 - ? fuelCategoryOptions[0] - : null + const categoryValue = + fuelCategoryOptions.length === 1 ? fuelCategoryOptions[0] : null - params.node.setDataValue('fuelCategory', categoryValue); + params.node.setDataValue('fuelCategory', categoryValue) // Auto-populate the "fuelCode" field const fuelCodeOptions = fuelType.fuelCodes.map( (code) => code.fuelCode - ); - params.node.setDataValue('fuelCode', fuelCodeOptions[0] ?? null); + ) + params.node.setDataValue('fuelCode', fuelCodeOptions[0] ?? null) params.node.setDataValue( 'fuelCodeId', fuelType.fuelCodes[0]?.fuelCodeId ?? null - ); + ) } } } @@ -227,12 +231,12 @@ export const AddEditOtherUses = () => { const isValid = validate( params, (value) => { - return value !== null && !isNaN(value) && value > 0; + return value !== null && !isNaN(value) && value > 0 }, 'Quantity supplied must be greater than 0.', alertRef, - 'quantitySupplied', - ); + 'quantitySupplied' + ) if (!isValid) { return