Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,12 @@ export default class CommonSelectLandParcelPageController extends LandGrantsQues
const landParcelMetadata = selectedParcelIds.map((id) => {
const parcel = parcelMap.get(id)
const rawArea = parcel?.area?.value
return { parcelId: id, areaHa: rawArea == null ? null : Number(rawArea) }
const areaHa = rawArea == null ? null : (Number(rawArea) * 10000) / 10000
return { parcelId: id, areaHa }
})

const totalHectaresForSelectedParcels =
Math.round(landParcelMetadata.reduce((sum, { areaHa }) => sum + (areaHa ?? 0), 0) * 10000) / 10000
(landParcelMetadata.reduce((sum, { areaHa }) => sum + (areaHa ?? 0), 0) * 10000) / 10000

if (this.minimumAreaHa !== null && totalHectaresForSelectedParcels < this.minimumAreaHa) {
const parcels = mapParcelsToViewModel(fetchedParcels)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ describe('CommonSelectLandParcelPageController', () => {
expect(controller.setState).not.toHaveBeenCalled()
})

it('rounds totalHectaresForSelectedParcels to 4dp to avoid float precision issues', async () => {
it('passes parcel area sum to GAS', async () => {
fetchParcels.mockResolvedValue([
{ sheetId: 'S1', parcelId: 'P1', area: { value: 25.3874 } },
{ sheetId: 'S2', parcelId: 'P2', area: { value: 169.8586 } }
Expand All @@ -260,12 +260,13 @@ describe('CommonSelectLandParcelPageController', () => {

await controller.handlePost(request, context, h)

const expectedTotal = 25.3874 + 169.8586
expect(controller.mergeState).toHaveBeenCalledWith(
request,
context.state,
expect.objectContaining({
totalHectaresForSelectedParcels: 195.246,
additionalAnswers: { totalHectaresForSelectedParcels: 195.246 }
totalHectaresForSelectedParcels: expectedTotal,
additionalAnswers: { totalHectaresForSelectedParcels: expectedTotal }
})
)
})
Expand Down
5 changes: 2 additions & 3 deletions src/server/woodland/woodland-hectares-page.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ const HECTARES_OVER_TEN_FIELD_NAME = 'hectaresTenOrOverYearsOld'
const HECTARES_UNDER_TEN_FIELD_NAME = 'hectaresUnderTenYearsOld'

const ERROR_BELOW_MINIMUM = `The total area of woodland must be at least ${MIN_WOODLAND_TOTAL_AREA_HA}ha`
const truncate4dp = (/** @type {number} */ n) => Math.floor(n * 10000) / 10000

const errorExceedsMax = (/** @type {number} */ max) =>
`Total area of woodland cannot be more than total area of selected land parcels (${truncate4dp(max)}ha)`
`Total area of woodland cannot be more than total area of selected land parcels (${max}ha)`

/**
* @param {string} fieldName
Expand Down Expand Up @@ -53,7 +52,7 @@ export default class WoodlandHectaresPageController extends TaskPageController {

getViewModel(request, context) {
const { state } = context
const totalHectaresForSelectedParcels = truncate4dp(Number(state['totalHectaresForSelectedParcels'] ?? 0))
const totalHectaresForSelectedParcels = Number(state['totalHectaresForSelectedParcels'] ?? 0)
const viewModel = /** @type {Record<string, any>} */ (super.getViewModel(request, context))

const guidanceIndex = viewModel.components?.findIndex(
Expand Down
Loading