Skip to content

Commit

Permalink
Display source of supply in view licence page summary tab (#732)
Browse files Browse the repository at this point in the history
* Display source of supply in view licence page summary tab

https://eaflood.atlassian.net/browse/WATER-4327

Migrating the licence summary page from the water-abstraction-ui to the water-abstraction-system.

This PR adds the source of the supply onto the summary page
  • Loading branch information
robertparkinson authored Feb 22, 2024
1 parent 3aaaa59 commit 0490b87
Show file tree
Hide file tree
Showing 12 changed files with 357 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/models/licence.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ class LicenceModel extends BaseModel {
from: 'licences.id',
to: 'workflows.licenceId'
}
},
permitLicence: {
relation: Model.HasOneRelation,
modelClass: 'permit-licence.model',
join: {
from: 'licences.licenceRef',
to: 'permitLicences.licenceRef'
}
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions app/models/permit-licence.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

/**
* Model for PermitLicence (permit.licence)
* @module PermitLicenceModel
*/

const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class PermitLicenceModel extends BaseModel {
static get tableName () {
return 'permitLicences'
}

// Defining which fields contain json allows us to insert an object without needing to stringify it first
static get jsonAttributes () {
return [
'licence_value_data'
]
}

static get relationMappings () {
return {
permitLicence: {
relation: Model.HasOneRelation,
modelClass: 'licence.model',
join: {
from: 'permitLicences.licenceRef',
to: 'licences.licenceRef'
}
}
}
}
}

module.exports = PermitLicenceModel
16 changes: 16 additions & 0 deletions app/presenters/licences/view-licence.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function go (licence) {
licenceName,
licenceRef,
licenceVersions,
permitLicence,
region,
registeredTo,
startDate
Expand Down Expand Up @@ -54,6 +55,7 @@ function go (licence) {
region: region.displayName,
registeredTo,
startDate: formatLongDate(startDate),
sourceOfSupply: _generateSourceOfSupply(permitLicence),
warning: _generateWarningMessage(ends)
}
}
Expand Down Expand Up @@ -117,6 +119,20 @@ function _generatePurposes (licenceVersions) {
}
}

function _generateSourceOfSupply (permitLicence) {
if (!permitLicence ||
permitLicence?.purposes === undefined ||
permitLicence.purposes.length === 0 ||
permitLicence.purposes[0]?.purposePoints === undefined ||
permitLicence.purposes[0]?.purposePoints.length === 0 ||
permitLicence.purposes[0]?.purposePoints[0]?.point_source?.NAME === undefined
) {
return null
}

return permitLicence.purposes[0].purposePoints[0].point_source.NAME
}

function _generateWarningMessage (ends) {
if (!ends) {
return null
Expand Down
8 changes: 8 additions & 0 deletions app/services/licences/fetch-licence.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @module FetchLicenceService
*/

const { ref } = require('objection')

const LicenceModel = require('../../models/licence.model.js')

/**
Expand Down Expand Up @@ -54,6 +56,12 @@ async function _fetchLicence (id) {
'displayName'
])
})
.withGraphFetched('permitLicence')
.modifyGraph('permitLicence', (builder) => {
builder.select([
ref('licenceDataValue:data.current_version.purposes').as('purposes')
])
})
.withGraphFetched('licenceDocumentHeader')
.modifyGraph('licenceDocumentHeader', (builder) => {
builder.select([
Expand Down
5 changes: 5 additions & 0 deletions app/views/licences/tabs/summary.njk
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
</div>
{% endif %}

<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">Source of supply</dt>
<dd class="govuk-summary-list__value">{{ sourceOfSupply }}</dd>
</div>

<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">Billing region</dt>
<dd class="govuk-summary-list__value">{{ region }}</dd>
Expand Down
38 changes: 38 additions & 0 deletions db/migrations/legacy/20221108005001-permit-licence.js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

const tableName = 'licence'

exports.up = function (knex) {
return knex
.schema
.withSchema('permit')
.createTable(tableName, (table) => {
// Primary Key
table.uuid('licence_id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.integer('licence_status_id').notNullable()
table.integer('licence_type_id').notNullable()
table.integer('licence_regime_id').notNullable()
table.string('licence_search_key')
table.tinyint('is_public_domain')
table.date('licence_start_dt')
table.date('licence_end_dt')
table.string('licence_ref').notNullable()
table.jsonb('licence_data_value')
table.jsonb('licence_summary')
table.jsonb('metadata')
table.dateTime('date_licence_version_purpose_conditions_last_copied')
table.dateTime('date_gauging_station_links_last_copied')

// Constraints
table.unique(['licence_regime_id', 'licence_type_id', 'licence_ref'], { useConstraint: true })
})
}

exports.down = function (knex) {
return knex
.schema
.withSchema('permit')
.dropTableIfExists(tableName)
}
32 changes: 32 additions & 0 deletions db/migrations/public/20240214142212_create-permit-licence-view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

const viewName = 'permit_licences'

exports.up = function (knex) {
return knex
.schema
.createView(viewName, (view) => {
view.as(knex('licence').withSchema('permit').select([
'licence_id AS id',
'licence_status_id',
'licence_type_id',
'licence_regime_id',
'licence_search_key',
'is_public_domain AS public_domain',
'licence_start_dt AS start_date ',
'licence_end_dt AS end_date',
'licence_ref',
'licence_data_value',
'licence_summary',
'metadata',
'date_licence_version_purpose_conditions_last_copied',
'date_gauging_station_links_last_copied'
]))
})
}

exports.down = function (knex) {
return knex
.schema
.dropViewIfExists(viewName)
}
1 change: 1 addition & 0 deletions test/presenters/licences/view-licence.presenter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('View Licence presenter', () => {
purposes: null,
registeredTo: null,
region: 'Narnia',
sourceOfSupply: null,
startDate: '1 April 2019',
warning: null
})
Expand Down
18 changes: 18 additions & 0 deletions test/services/licences/fetch-licence.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const LicenceHolderSeeder = require('../../support/seeders/licence-holder.seeder
const LicenceVersionHelper = require('../../support/helpers/licence-version.helper.js')
const LicenceVersionPurposeHelper = require('../../support/helpers/licence-version-purpose.helper.js')
const PurposeHelper = require('../../support/helpers/purpose.helper.js')
const PermitLicenceHelper = require('../../support/helpers/permit-licence.helper.js')
const RegionHelper = require('../../support/helpers/region.helper.js')

// Thing under test
Expand Down Expand Up @@ -76,6 +77,7 @@ describe('Fetch licence service', () => {
expect(result.region.displayName).to.equal('Avalon')
expect(result.registeredTo).to.equal(null)
expect(result.revokedDate).to.equal(null)
expect(result.permitLicence).to.equal(null)
})
})

Expand Down Expand Up @@ -104,6 +106,19 @@ describe('Fetch licence service', () => {
purposeId: purpose.id
})

await PermitLicenceHelper.add({
licenceRef: licence.licenceRef,
licenceDataValue: {
data: {
current_version: {
purposes: [{
purposePoints: [{ point_source: { NAME: 'Ground water' } }]
}]
}
}
}
})

const licenceRole = await LicenceRoleHelper.add()
const licenceName = 'Test Company Ltd'
const companyEntityId = 'c960a4a1-94f9-4c05-9db1-a70ce5d08738'
Expand Down Expand Up @@ -140,6 +155,9 @@ describe('Fetch licence service', () => {
expect(result.region.displayName).to.equal('Avalon')
expect(result.registeredTo).to.equal('[email protected]')
expect(result.revokedDate).to.equal(null)
expect(result.permitLicence.purposes).to.equal([{
purposePoints: [{ point_source: { NAME: 'Ground water' } }]
}])
})
})
})
125 changes: 125 additions & 0 deletions test/services/licences/view-licence.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('View Licence service', () => {
region: 'South West',
registeredTo: null,
startDate: '7 March 2013',
sourceOfSupply: null,
warning: null
})
})
Expand Down Expand Up @@ -173,6 +174,130 @@ describe('View Licence service', () => {
expect(result.warning).to.be.null()
})
})

describe('and it has a source of supply', () => {
beforeEach(() => {
fetchLicenceResult = _testLicence()
fetchLicenceResult.permitLicence = {
purposes: [{
purposePoints: [{
point_source: {
NAME: 'SURFACE WATER SOURCE OF SUPPLY'
}
}]
}]
}
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the source of supply for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.sourceOfSupply).to.equal('SURFACE WATER SOURCE OF SUPPLY')
})
})

describe('and it does not have a source of supply name', () => {
beforeEach(() => {
fetchLicenceResult = _testLicence()
fetchLicenceResult.permitLicence = {
purposes: [{
purposePoints: [{
point_source: {}
}]
}]
}
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return null for the source of supply', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.sourceOfSupply).to.equal(null)
})
})

describe('and it does not have a source of supply point_source', () => {
beforeEach(() => {
fetchLicenceResult = _testLicence()
fetchLicenceResult.permitLicence = {
purposes: [{
purposePoints: [{}]
}]
}
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return null for the source of supply', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.sourceOfSupply).to.equal(null)
})
})

describe('and it has an empty purposePoints array', () => {
beforeEach(() => {
fetchLicenceResult = _testLicence()
fetchLicenceResult.permitLicence = {
purposes: [{
purposePoints: []
}]
}
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return null for the source of supply', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.sourceOfSupply).to.equal(null)
})
})

describe('and it does not have a purposePoints array', () => {
beforeEach(() => {
fetchLicenceResult = _testLicence()
fetchLicenceResult.permitLicence = {
purposes: [{}]
}
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return null for the source of supply', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.sourceOfSupply).to.equal(null)
})
})

describe('and it has an empty purposes array', () => {
beforeEach(() => {
fetchLicenceResult = _testLicence()
fetchLicenceResult.permitLicence = {
purposes: []
}
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return null for the source of supply', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.sourceOfSupply).to.equal(null)
})
})

describe('and it does not have a purposes array', () => {
beforeEach(() => {
fetchLicenceResult = _testLicence()
fetchLicenceResult.permitLicence = undefined
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return null for the source of supply', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.sourceOfSupply).to.equal(null)
})
})
})
})

Expand Down
2 changes: 1 addition & 1 deletion test/support/helpers/database.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

const { db, dbConfig } = require('../../../db/db.js')

const LEGACY_SCHEMAS = ['crm', 'crm_v2', 'idm', 'water', 'returns']
const LEGACY_SCHEMAS = ['crm', 'crm_v2', 'idm', 'permit', 'returns', 'water']

/**
* Call to clean the database of all data
Expand Down
Loading

0 comments on commit 0490b87

Please sign in to comment.