Skip to content

Commit

Permalink
Fetch conditions from licence summary service (#1149)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4322

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

The `ViewLicenceSummaryService` is calling `FetchLicenceAbstractionConditionsService` and `FetchLicenceSummaryService` and then passing both results to the presenter.

Having checked, it is the only service using `FetchLicenceAbstractionConditionsService` and we don't really need the data preparation it is doing.

If the relationship was added between `LicenceVersionPurposeModel` and `LicenceVersionPurposeConditionModel` we could get everything we need from the `FetchLicenceSummaryService`!

So, this change makes that so.
  • Loading branch information
Cruikshanks authored Jun 30, 2024
1 parent 4e851ae commit e087bb8
Show file tree
Hide file tree
Showing 14 changed files with 443 additions and 350 deletions.
12 changes: 10 additions & 2 deletions app/models/licence-version-purpose-condition.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ class LicenceVersionPurposeConditionModel extends BaseModel {

static get relationMappings () {
return {
licenceVersionPurposeConditionTypes: {
relation: Model.HasManyRelation,
licenceVersionPurpose: {
relation: Model.BelongsToOneRelation,
modelClass: 'licence-version-purpose.model',
join: {
from: 'licenceVersionPurposeConditions.licenceVersionPurposeId',
to: 'licenceVersionPurposes.id'
}
},
licenceVersionPurposeConditionType: {
relation: Model.HasOneRelation,
modelClass: 'licence-version-purpose-condition-type.model',
join: {
from: 'licenceVersionPurposeConditions.licenceVersionPurposeConditionTypeId',
Expand Down
8 changes: 8 additions & 0 deletions app/models/licence-version-purpose.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class LicenceVersionPurposeModel extends BaseModel {
to: 'licenceVersions.id'
}
},
licenceVersionPurposeConditions: {
relation: Model.HasManyRelation,
modelClass: 'licence-version-purpose-condition.model',
join: {
from: 'licenceVersionPurposes.id',
to: 'licenceVersionPurposeConditions.licenceVersionPurposeId'
}
},
primaryPurpose: {
relation: Model.BelongsToOneRelation,
modelClass: 'primary-purpose.model.js',
Expand Down
43 changes: 27 additions & 16 deletions app/presenters/licences/view-licence-summary.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const { generateAbstractionPointDetail } = require('../../lib/general.lib.js')
/**
* Formats data for the `/licences/{id}/summary` page's summary tab
*
* @param {module:LicenceModel} licence - The licence where the data will be extracted for from
* @param {module:LicenceModel} licence - The licence the summary data will be extracted from
*
* @returns {Object} The data formatted for the view template
*/
function go (licence, licenceAbstractionConditions) {
function go (licence) {
const {
expiredDate,
id,
Expand All @@ -30,12 +30,7 @@ function go (licence, licenceAbstractionConditions) {

const purposes = _generatePurposes(licenceVersionPurposes)
const monitoringStations = _generateMonitoringStation(licenceGaugingStations)
const abstractionData = _abstractionWrapper(
licenceAbstractionConditions,
licenceVersionPurposes,
purposes,
permitLicence
)
const abstractionData = _abstractionWrapper(licenceVersionPurposes, purposes, permitLicence)

return {
...abstractionData,
Expand All @@ -51,21 +46,22 @@ function go (licence, licenceAbstractionConditions) {
}
}

function _abstractionWrapper (licenceAbstractionConditions, licenceVersionPurposes, purposes, permitLicence) {
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'

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

const abstractionDetails = _parseAbstractionsAndSourceOfSupply(permitLicence)
const abstractionConditionDetails = _abstractionConditionDetails(licenceAbstractionConditions)
const abstractionConditions = _abstractionConditions(licenceVersionPurposes)

return {
abstractionConditionDetails,
abstractionConditions,
abstractionPeriods,
abstractionPeriodsAndPurposesLinkText,
abstractionPointLinkText: abstractionDetails.pointLinkText,
Expand Down Expand Up @@ -99,13 +95,27 @@ function _abstractionAmountDetails (purpose) {
return abstractionAmountDetails
}

function _abstractionConditionDetails (licenceAbstractionConditions) {
const { conditions, numberOfConditions } = licenceAbstractionConditions
function _abstractionConditions (licenceVersionPurposes) {
const allConditions = []

return {
conditions,
numberOfConditions
if (!licenceVersionPurposes) {
return allConditions
}

for (const licenceVersionPurpose of licenceVersionPurposes) {
const { licenceVersionPurposeConditions } = licenceVersionPurpose

for (const licenceVersionPurposeCondition of licenceVersionPurposeConditions) {
const { displayTitle } = licenceVersionPurposeCondition.licenceVersionPurposeConditionType

allConditions.push(displayTitle)
}
}

const uniqueConditions = [...new Set(allConditions)]

// Sort them alphabetically
return uniqueConditions.sort()
}

function _endDate (expiredDate) {
Expand Down Expand Up @@ -192,6 +202,7 @@ function _parseAbstractionsAndSourceOfSupply (permitLicence) {
permitLicence.purposes.forEach((purpose) => {
purpose.purposePoints.forEach((point) => {
const pointDetail = point.point_detail

if (pointDetail) {
abstractionPoints.push(generateAbstractionPointDetail(pointDetail))
}
Expand Down

This file was deleted.

21 changes: 16 additions & 5 deletions app/services/licences/fetch-licence-summary.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const LicenceModel = require('../../models/licence.model.js')
* @returns {Promise<module:LicenceModel>} the data needed to populate the view licence page summary tab
*/
async function go (licenceId) {
return _fetchLicence(licenceId)
return _fetch(licenceId)
}

async function _fetchLicence (licenceId) {
const result = await LicenceModel.query()
async function _fetch (licenceId) {
return LicenceModel.query()
.findById(licenceId)
.select([
'id',
Expand Down Expand Up @@ -62,6 +62,19 @@ async function _fetchLicence (licenceId) {
'description'
])
})
.withGraphFetched('licenceVersions.licenceVersionPurposes.licenceVersionPurposeConditions')
.modifyGraph('licenceVersions.licenceVersionPurposes.licenceVersionPurposeConditions', (builder) => {
builder.select([
'id'
])
})
.withGraphFetched('licenceVersions.licenceVersionPurposes.licenceVersionPurposeConditions.licenceVersionPurposeConditionType')
.modifyGraph('licenceVersions.licenceVersionPurposes.licenceVersionPurposeConditions.licenceVersionPurposeConditionType', (builder) => {
builder.select([
'id',
'displayTitle'
])
})
.withGraphFetched('licenceGaugingStations')
.modifyGraph('licenceGaugingStations', (builder) => {
builder.select([
Expand All @@ -78,8 +91,6 @@ async function _fetchLicence (licenceId) {
})
.modify('licenceHolder')
.modify('registeredToAndLicenceName')

return result
}

module.exports = {
Expand Down
7 changes: 1 addition & 6 deletions app/services/licences/view-licence-summary.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @module ViewLicenceSummaryService
*/

const FetchLicenceAbstractionConditionsService = require('./fetch-licence-abstraction-conditions.service.js')
const FetchLicenceSummaryService = require('./fetch-licence-summary.service.js')
const ViewLicenceSummaryPresenter = require('../../presenters/licences/view-licence-summary.presenter.js')
const ViewLicenceService = require('./view-licence.service.js')
Expand All @@ -23,11 +22,7 @@ async function go (licenceId, auth) {

const summaryLicenceData = await FetchLicenceSummaryService.go(licenceId)

const currentLicenceVersionId = summaryLicenceData?.licenceVersions[0]?.id

const abstractionConditions = await FetchLicenceAbstractionConditionsService.go(currentLicenceVersionId)

const pageData = ViewLicenceSummaryPresenter.go(summaryLicenceData, abstractionConditions)
const pageData = ViewLicenceSummaryPresenter.go(summaryLicenceData)

return {
...pageData,
Expand Down
10 changes: 5 additions & 5 deletions app/views/licences/tabs/summary.njk
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@
<dt class="govuk-summary-list__key">Abstraction conditions</dt>
<dd class="govuk-summary-list__value">
<div>
{% if abstractionConditionDetails.numberOfConditions > 5 %}
There are {{ abstractionConditionDetails.numberOfConditions }} abstraction conditions
{% elif abstractionConditionDetails.numberOfConditions > 1 %}
{% if abstractionConditions.length > 5 %}
There are {{ abstractionConditions.length }} abstraction conditions
{% elif abstractionConditions.length > 1 %}
<ul class="govuk-list govuk-!-margin-bottom-0">
{% for condition in abstractionConditionDetails.conditions %}
{% for condition in abstractionConditions %}
<li>{{ condition }}</li>
{% endfor %}
</ul>
{% else %}
{{ abstractionConditionDetails.conditions[0] }}
{{ abstractionConditions[0] }}
{% endif %}
</div>
<a href="/licences/{{ documentId }}/conditions">View details of the abstraction conditions</a>
Expand Down
55 changes: 45 additions & 10 deletions test/models/licence-version-purpose-condition.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const { expect } = Code
// Test helpers
const DatabaseSupport = require('../support/database.js')
const LicenceVersionPurposeConditionHelper = require('../support/helpers/licence-version-purpose-condition.helper.js')
const LicenceVersionPurposeConditionTypesHelper = require('../support/helpers/licence-version-purpose-condition-type.helper.js')
const LicenceVersionPurposeHelper = require('../support/helpers/licence-version-purpose.helper.js')
const LicenceVersionPurposeModel = require('../../app/models/licence-version-purpose.model.js')
const LicenceVersionPurposeConditionTypeHelper = require('../support/helpers/licence-version-purpose-condition-type.helper.js')
const LicenceVersionPurposeConditionTypeModel = require('../../app/models/licence-version-purpose-condition-type.model.js')

// Thing under test
Expand All @@ -21,11 +23,13 @@ describe('Licence Version Purposes model', () => {

beforeEach(async () => {
await DatabaseSupport.clean()

testRecord = await LicenceVersionPurposeConditionHelper.add()
})

describe('Basic query', () => {
beforeEach(async () => {
testRecord = await LicenceVersionPurposeConditionHelper.add()
})

it('can successfully run a basic query', async () => {
const result = await LicenceVersionPurposeConditionModel.query().findById(testRecord.id)

Expand All @@ -35,34 +39,65 @@ describe('Licence Version Purposes model', () => {
})

describe('Relationships', () => {
describe('when linking to licence version purpose condition', () => {
describe('when linking to licence version purpose', () => {
let testLicenceVersionPurpose

beforeEach(async () => {
testLicenceVersionPurpose = await LicenceVersionPurposeHelper.add()

testRecord = await LicenceVersionPurposeConditionHelper.add({
licenceVersionPurposeId: testLicenceVersionPurpose.id
})
})

it('can successfully run a related query', async () => {
const query = await LicenceVersionPurposeConditionModel.query()
.innerJoinRelated('licenceVersionPurpose')

expect(query).to.exist()
})

it('can eager load the licence version purpose', async () => {
const result = await LicenceVersionPurposeConditionModel.query()
.findById(testRecord.id)
.withGraphFetched('licenceVersionPurpose')

expect(result).to.be.instanceOf(LicenceVersionPurposeConditionModel)
expect(result.id).to.equal(testRecord.id)

expect(result.licenceVersionPurpose).to.be.an.instanceOf(LicenceVersionPurposeModel)
expect(result.licenceVersionPurpose).to.equal(testLicenceVersionPurpose)
})
})

describe('when linking to licence version purpose condition type', () => {
let testLicenceVersionPurposeConditionType

beforeEach(async () => {
testLicenceVersionPurposeConditionType = await LicenceVersionPurposeConditionTypesHelper.add()
testLicenceVersionPurposeConditionType = await LicenceVersionPurposeConditionTypeHelper.add()

testRecord = await LicenceVersionPurposeConditionHelper.add({
licenceVersionPurposeConditionTypeId: testLicenceVersionPurposeConditionType.id
})
})

it('can successfully run a related query', async () => {
const query = await LicenceVersionPurposeConditionModel.query()
.innerJoinRelated('licenceVersionPurposeConditionTypes')
.innerJoinRelated('licenceVersionPurposeConditionType')

expect(query).to.exist()
})

it('can eager load the licence version purpose condition type', async () => {
const result = await LicenceVersionPurposeConditionModel.query()
.findById(testRecord.id)
.withGraphFetched('licenceVersionPurposeConditionTypes')
.withGraphFetched('licenceVersionPurposeConditionType')

expect(result).to.be.instanceOf(LicenceVersionPurposeConditionModel)
expect(result.id).to.equal(testRecord.id)

expect(result.licenceVersionPurposeConditionTypes[0])
.to.be.an.instanceOf(LicenceVersionPurposeConditionTypeModel)
expect(result.licenceVersionPurposeConditionTypes[0].id).to.equal(testLicenceVersionPurposeConditionType.id)
expect(result.licenceVersionPurposeConditionType).to.be.an.instanceOf(LicenceVersionPurposeConditionTypeModel)
expect(result.licenceVersionPurposeConditionType).to.equal(testLicenceVersionPurposeConditionType)
})
})
})
Expand Down
Loading

0 comments on commit e087bb8

Please sign in to comment.