Skip to content

Commit

Permalink
Fix view licence abs amounts issue plus refactor (#1132)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4322

> Part of our work to replace the legacy view licence page

We found our new licence was crashing when we tried to view a specific licence. When we dug into it, the problem was that even though it had a current version in the `PermitLicenceModel`s `licenceDataValue` field, and that version had purposes, those purposes didn't have abstraction amounts.

It's not common for licences to be in this state, but there is not much we can do as the data comes from NALD. It would have to be corrected there.

So, we need to update the `ViewLicenceSummaryPresenter` code to handle this. But it has been some time since we built the summary tab so we have more experience with the licence abstraction data. For example, we now know these values can be taken from somewhere other than the JSON blob the previous team was using.

This means, along with fixing the issue, we intend to do a little 'housekeeping' along the way to try and simplify how things are working, and to use normalised data wherever we can.
  • Loading branch information
Cruikshanks authored Jul 1, 2024
1 parent e087bb8 commit 9903139
Show file tree
Hide file tree
Showing 6 changed files with 461 additions and 671 deletions.
234 changes: 100 additions & 134 deletions app/presenters/licences/view-licence-summary.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,72 +27,60 @@ function go (licence) {
} = licence

const licenceVersionPurposes = _licenceVersionPurposes(licence)
const points = _points(permitLicence)

const purposes = _generatePurposes(licenceVersionPurposes)
const monitoringStations = _generateMonitoringStation(licenceGaugingStations)
const abstractionData = _abstractionWrapper(licenceVersionPurposes, purposes, permitLicence)
const purposes = _purposes(licenceVersionPurposes)
const abstractionPeriods = _abstractionPeriods(licenceVersionPurposes)
const abstractionPoints = _abstractionPoints(points)

return {
...abstractionData,
abstractionAmounts: _abstractionAmounts(licenceVersionPurposes),
abstractionConditions: _abstractionConditions(licenceVersionPurposes),
abstractionPeriods,
abstractionPeriodsAndPurposesLinkText: _abstractionPeriodsAndPurposesLinkText(abstractionPeriods, purposes),
abstractionPeriodsCaption: _abstractionPeriodsCaption(abstractionPeriods),
abstractionPoints,
abstractionPointsCaption: _abstractionPointsCaption(abstractionPoints),
abstractionPointsLinkText: _abstractionPointsLinkText(abstractionPoints),
activeTab: 'summary',
documentId: licenceDocumentHeader.id,
endDate: _endDate(expiredDate),
id,
licenceHolder: _generateLicenceHolder(licence),
monitoringStations,
licenceHolder: _licenceHolder(licence),
licenceId: id,
monitoringStations: _monitoringStations(licenceGaugingStations),
purposes,
region: region.displayName,
sourceOfSupply: points[0]?.point_source?.NAME ?? null,
startDate: formatLongDate(startDate)
}
}

function _abstractionWrapper (licenceVersionPurposes, purposes, permitLicence) {
const abstractionPeriods = _generateAbstractionPeriods(licenceVersionPurposes)
let abstractionPeriodsAndPurposesLinkText = null

if (abstractionPeriods) {
const abstractionPeriodsLabel = abstractionPeriods.uniqueAbstractionPeriods.length > 1 ? 'periods' : 'period'
const purposesLabel = purposes.data.length > 1 ? 'purposes' : 'purpose'
function _abstractionAmounts (licenceVersionPurposes) {
const details = []

abstractionPeriodsAndPurposesLinkText = `View details of your ${purposesLabel}, ${abstractionPeriodsLabel} and amounts`
if (!licenceVersionPurposes) {
return details
}

const abstractionDetails = _parseAbstractionsAndSourceOfSupply(permitLicence)
const abstractionConditions = _abstractionConditions(licenceVersionPurposes)
const { annualQuantity, dailyQuantity, hourlyQuantity, instantQuantity } = licenceVersionPurposes[0]

return {
abstractionConditions,
abstractionPeriods,
abstractionPeriodsAndPurposesLinkText,
abstractionPointLinkText: abstractionDetails.pointLinkText,
abstractionPoints: abstractionDetails.points,
abstractionPointsCaption: abstractionDetails.pointsCaption,
abstractionQuantities: abstractionDetails.quantities,
sourceOfSupply: abstractionDetails.sourceOfSupply
if (annualQuantity) {
details.push(`${parseFloat(annualQuantity).toFixed(2)} cubic metres per year`)
}
}

function _abstractionAmountDetails (purpose) {
const abstractionAmountDetails = []
const { ANNUAL_QTY, DAILY_QTY, HOURLY_QTY, INST_QTY } = purpose

if (ANNUAL_QTY !== 'null') {
abstractionAmountDetails.push(`${parseFloat(ANNUAL_QTY).toFixed(2)} cubic metres per year`)
if (dailyQuantity) {
details.push(`${parseFloat(dailyQuantity).toFixed(2)} cubic metres per day`)
}

if (DAILY_QTY !== 'null') {
abstractionAmountDetails.push(`${parseFloat(DAILY_QTY).toFixed(2)} cubic metres per day`)
if (hourlyQuantity) {
details.push(`${parseFloat(hourlyQuantity).toFixed(2)} cubic metres per hour`)
}

if (HOURLY_QTY !== 'null') {
abstractionAmountDetails.push(`${parseFloat(HOURLY_QTY).toFixed(2)} cubic metres per hour`)
if (instantQuantity) {
details.push(`${parseFloat(instantQuantity).toFixed(2)} cubic metres per second`)
}

if (INST_QTY !== 'null') {
abstractionAmountDetails.push(`${parseFloat(INST_QTY).toFixed(2)} litres per second`)
}

return abstractionAmountDetails
return details
}

function _abstractionConditions (licenceVersionPurposes) {
Expand All @@ -118,35 +106,71 @@ function _abstractionConditions (licenceVersionPurposes) {
return uniqueConditions.sort()
}

function _endDate (expiredDate) {
if (!expiredDate || expiredDate < Date.now()) {
return null
}

return formatLongDate(expiredDate)
}

function _generateAbstractionPeriods (licenceVersionPurposes) {
function _abstractionPeriods (licenceVersionPurposes) {
if (!licenceVersionPurposes) {
return null
return []
}

const formattedAbstractionPeriods = licenceVersionPurposes.map((purpose) => {
const abstractionPeriods = licenceVersionPurposes.map((purpose) => {
const startDate = formatAbstractionDate(purpose.abstractionPeriodStartDay, purpose.abstractionPeriodStartMonth)
const endDate = formatAbstractionDate(purpose.abstractionPeriodEndDay, purpose.abstractionPeriodEndMonth)

return `${startDate} to ${endDate}`
})

const uniqueAbstractionPeriods = [...new Set(formattedAbstractionPeriods)]
const uniqueAbstractionPeriods = [...new Set(abstractionPeriods)]

return {
caption: uniqueAbstractionPeriods.length > 1 ? 'Periods of abstraction' : 'Period of abstraction',
uniqueAbstractionPeriods
return uniqueAbstractionPeriods
}

function _abstractionPeriodsAndPurposesLinkText (abstractionPeriods, purposes) {
let abstractionPeriodsAndPurposesLinkText = null

if (abstractionPeriods.length > 0) {
const abstractionPeriodsLabel = abstractionPeriods.length > 1 ? 'periods' : 'period'
const purposesLabel = purposes.data.length > 1 ? 'purposes' : 'purpose'

abstractionPeriodsAndPurposesLinkText = `View details of your ${purposesLabel}, ${abstractionPeriodsLabel} and amounts`
}

return abstractionPeriodsAndPurposesLinkText
}

function _abstractionPeriodsCaption (abstractionPeriods) {
return abstractionPeriods.length > 1 ? 'Periods of abstraction' : 'Period of abstraction'
}

function _abstractionPoints (points) {
const abstractionPoints = []

points.forEach((point) => {
if (point?.point_detail) {
abstractionPoints.push(generateAbstractionPointDetail(point.point_detail))
}
})

const uniqueAbstractionPoints = [...new Set(abstractionPoints)]

return uniqueAbstractionPoints.sort()
}

function _generateLicenceHolder (licence) {
function _abstractionPointsCaption (abstractionPoints) {
return abstractionPoints.length > 1 ? 'Points of abstraction' : 'Point of abstraction'
}

function _abstractionPointsLinkText (abstractionPoints) {
return abstractionPoints.length > 1 ? 'View details of the abstraction points' : 'View details of the abstraction point'
}

function _endDate (expiredDate) {
if (!expiredDate || expiredDate < Date.now()) {
return null
}

return formatLongDate(expiredDate)
}

function _licenceHolder (licence) {
const licenceHolder = licence.$licenceHolder()

if (!licenceHolder) {
Expand All @@ -156,13 +180,29 @@ function _generateLicenceHolder (licence) {
return licenceHolder
}

function _generateMonitoringStation (licenceGaugingStations) {
function _monitoringStations (licenceGaugingStations) {
return licenceGaugingStations.map((licenceGaugingStation) => {
return licenceGaugingStation.gaugingStation
})
}

function _generatePurposes (licenceVersionPurposes) {
function _points (permitLicence) {
const points = []

if (!permitLicence?.purposes?.[0]?.purposePoints) {
return points
}

permitLicence.purposes.forEach((purpose) => {
purpose.purposePoints.forEach((purposePoint) => {
points.push(purposePoint)
})
})

return points
}

function _purposes (licenceVersionPurposes) {
if (!licenceVersionPurposes) {
return null
}
Expand All @@ -179,80 +219,6 @@ function _generatePurposes (licenceVersionPurposes) {
}
}

function _parseAbstractionsAndSourceOfSupply (permitLicence) {
if (!permitLicence ||
!permitLicence.purposes ||
permitLicence.purposes.length === 0 ||
permitLicence.purposes[0]?.purposePoints === undefined ||
permitLicence.purposes[0]?.purposePoints.length === 0
) {
return {
points: null,
pointsCaption: null,
pointLinkText: null,
quantities: null,
quantityCaption: null,
sourceOfSupply: null
}
}

const abstractionPoints = []
let abstractionQuantities

permitLicence.purposes.forEach((purpose) => {
purpose.purposePoints.forEach((point) => {
const pointDetail = point.point_detail

if (pointDetail) {
abstractionPoints.push(generateAbstractionPointDetail(pointDetail))
}
})
abstractionQuantities = _setAbstractionAmountDetails(abstractionQuantities, purpose)
})

const uniqueAbstractionPoints = [...new Set(abstractionPoints)]

const abstractionLinkDefaultText = 'View details of the abstraction point'
const pointLinkText = uniqueAbstractionPoints.length > 1 ? abstractionLinkDefaultText + 's' : abstractionLinkDefaultText

const pointsCaption = uniqueAbstractionPoints.length > 1 ? 'Points of abstraction' : 'Point of abstraction'

return {
points: uniqueAbstractionPoints.length === 0 ? null : uniqueAbstractionPoints,
pointsCaption,
pointLinkText,
quantities: abstractionQuantities && abstractionQuantities.length === 1
? _abstractionAmountDetails(abstractionQuantities[0])
: null,
sourceOfSupply: permitLicence.purposes[0].purposePoints[0]?.point_source?.NAME ?? null
}
}

function _setAbstractionAmountDetails (abstractionAmountSet, purpose) {
const { ANNUAL_QTY, DAILY_QTY, HOURLY_QTY, INST_QTY } = purpose
const purposeAbstractionQuantities = {
ANNUAL_QTY, DAILY_QTY, HOURLY_QTY, INST_QTY
}

if (!abstractionAmountSet &&
(purposeAbstractionQuantities.DAILY_QTY !== 'null' ||
purposeAbstractionQuantities.ANNUAL_QTY !== 'null' ||
purposeAbstractionQuantities.HOURLY_QTY !== 'null' ||
purposeAbstractionQuantities.INST_QTY !== 'null')) {
return [purposeAbstractionQuantities]
}

if (abstractionAmountSet &&
(abstractionAmountSet[0].ANNUAL_QTY !== purposeAbstractionQuantities.ANNUAL_QTY ||
abstractionAmountSet[0].DAILY_QTY !== purposeAbstractionQuantities.DAILY_QTY ||
abstractionAmountSet[0].HOURLY_QTY !== purposeAbstractionQuantities.HOURLY_QTY ||
abstractionAmountSet[0].INST_QTY !== purposeAbstractionQuantities.INST_QTY)) {
return abstractionAmountSet.push(purposeAbstractionQuantities)
}

return abstractionAmountSet
}

function _licenceVersionPurposes (licence) {
const currentVersion = licence.$currentVersion()

Expand Down
6 changes: 5 additions & 1 deletion app/services/licences/fetch-licence-summary.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ async function _fetch (licenceId) {
'abstractionPeriodStartDay',
'abstractionPeriodStartMonth',
'abstractionPeriodEndDay',
'abstractionPeriodEndMonth'
'abstractionPeriodEndMonth',
'annualQuantity',
'dailyQuantity',
'hourlyQuantity',
'instantQuantity'
])
})
.withGraphFetched('licenceVersions.licenceVersionPurposes.purpose')
Expand Down
Loading

0 comments on commit 9903139

Please sign in to comment.