Skip to content

Commit

Permalink
Display monitoring stations in view licence page summary tab (#773)
Browse files Browse the repository at this point in the history
* https://eaflood.atlassian.net/browse/WATER-4331

View licence summary monitoring stations
  • Loading branch information
robertparkinson authored Mar 5, 2024
1 parent b39dfcb commit 0d2ca82
Show file tree
Hide file tree
Showing 15 changed files with 503 additions and 0 deletions.
38 changes: 38 additions & 0 deletions app/models/gauging-station.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

/**
* Model for gaugingStations (water.gauging_stations)
* @module GaugingStationsnModel
*/

const { Model } = require('objection')

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

class GaugingStationsnModel extends BaseModel {
static get tableName () {
return 'gaugingStations'
}

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

static get relationMappings () {
return {
licenceGaugingStations: {
relation: Model.HasManyRelation,
modelClass: 'licence-gauging-station.model',
join: {
from: 'gaugingStations.id',
to: 'licenceGaugingStations.gaugingStationId'
}
}
}
}
}

module.exports = GaugingStationsnModel
31 changes: 31 additions & 0 deletions app/models/licence-gauging-station.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

/**
* Model for licenceGauginStations (water.licence_gauging_stations)
* @module GauginStationsnModel
*/

const { Model } = require('objection')

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

class LicenceGauginStationsnModel extends BaseModel {
static get tableName () {
return 'licenceGaugingStations'
}

static get relationMappings () {
return {
licenceGaugingStations: {
relation: Model.HasManyRelation,
modelClass: 'gauging-station.model',
join: {
from: 'licenceGaugingStations.gaugingStationId',
to: 'gaugingStations.id'
}
}
}
}
}

module.exports = LicenceGauginStationsnModel
12 changes: 12 additions & 0 deletions app/models/licence.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ class LicenceModel extends BaseModel {
from: 'licences.licenceRef',
to: 'permitLicences.licenceRef'
}
},
licenceGaugingStations: {
relation: Model.ManyToManyRelation,
modelClass: 'gauging-station.model',
join: {
from: 'licences.id',
through: {
from: 'licenceGaugingStations.licenceId',
to: 'licenceGaugingStations.gaugingStationId'
},
to: 'gaugingStations.id'
}
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions app/presenters/licences/view-licence.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function go (licence) {
expiredDate,
id,
licenceDocumentHeader,
licenceGaugingStations,
licenceHolder,
licenceName,
licenceRef,
Expand All @@ -42,6 +43,7 @@ function go (licence) {
}

const pointDetails = _parseAbstractionsAndSourceOfSupply(permitLicence)
const monitoringStationDetails = _generateMonitoringStation(licenceGaugingStations)

return {
id,
Expand All @@ -55,6 +57,8 @@ function go (licence) {
licenceHolder: _generateLicenceHolder(licenceHolder),
licenceName,
licenceRef,
monitoringStationCaption: monitoringStationDetails.monitoringStationCaption,
monitoringStations: monitoringStationDetails.monitoringStations,
pageTitle: `Licence ${licenceRef}`,
purposes,
region: region.displayName,
Expand Down Expand Up @@ -104,6 +108,23 @@ function _generateLicenceHolder (licenceHolder) {
return licenceHolder
}

function _generateMonitoringStation (stations) {
let monitoringStations = []
if (stations && stations.length !== undefined) {
const jsonArray = stations.map(JSON.stringify)
monitoringStations = Array.from(new Set(jsonArray)).map(JSON.parse)
}

const monitoringStationCaption = monitoringStations.length > 1
? 'Monitoring stations'
: 'Monitoring station'

return {
monitoringStationCaption,
monitoringStations
}
}

function _generatePurposes (licenceVersions) {
if (!licenceVersions ||
licenceVersions.length === 0 ||
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 @@ -84,6 +84,14 @@ async function _fetchLicence (id) {
})
.modify('licenceHolder')
.modify('registeredToAndLicenceName')
.withGraphFetched('licenceGaugingStations')
.modifyGraph('licenceGaugingStations', (builder) => {
builder.select([
'gaugingStations.id',
'gaugingStations.label'
])
.where('licenceGaugingStations.dateDeleted', null)
})

return result
}
Expand Down
20 changes: 20 additions & 0 deletions app/views/licences/tabs/summary.njk
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@
</dd>
</div>
{% endif %}

<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">{{ monitoringStationCaption }}</dt>
<dd class="govuk-summary-list__value">
{% if monitoringStations.length > 0 %}
<ul class="govuk-list govuk-!-margin-bottom-0">
{% for monitoringStation in monitoringStations %}
<li>
<a href="/monitoring-stations/{{ monitoringStation.gaugingStationId }}">{{ monitoringStation.label }}</a>
</li>
{% endfor %}
</ul>
{% else %}
<span class="govuk-form-group govuk-!-margin-bottom-0">
This licence is not tagged with a station
</span>
<a href="/licences">Search for a monitoring station and tag this licence</a>
{% endif %}
</dd>
</div>
</dl>

<div class="govuk-grid-row">
Expand Down
40 changes: 40 additions & 0 deletions db/migrations/legacy/20240229142820_water-gauging-stations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

const tableName = 'gauging_stations'

exports.up = function (knex) {
return knex
.schema
.withSchema('water')
.createTable(tableName, (table) => {
// Primary Key
table.uuid('gauging_station_id').primary()

// Data
table.string('label').notNullable()
table.decimal('lat').notNullable()
table.decimal('long').notNullable()
table.integer('easting')
table.integer('northing')
table.string('grid_reference').notNullable()
table.string('catchment_name')
table.string('river_name')
table.string('wiski_id')
table.string('station_reference')
table.string('status')
table.jsonb('metadata')
table.uuid('hydrology_station_id').notNullable()
table.boolean('is_test')

// Legacy timestamps
table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now())
table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now())
})
}

exports.down = function (knex) {
return knex
.schema
.withSchema('water')
.dropTableIfExists(tableName)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

const tableName = 'licence_gauging_stations'

exports.up = function (knex) {
return knex
.schema
.withSchema('water')
.createTable(tableName, (table) => {
// Primary Key
table.uuid('licence_gauging_station_id').primary()

// Data
table.uuid('licence_id').notNullable()
table.uuid('gauging_station_id').notNullable()
table.string('source').notNullable()
table.uuid('licence_version_purpose_condition_id').notNullable()
table.smallint('abstraction_period_start_day')
table.smallint('abstraction_period_start_month')
table.smallint('abstraction_period_end_day')
table.smallint('abstraction_period_end_month')
table.string('restriction_type').notNullable()
table.string('threshold_unit').notNullable()
table.decimal('threshold_value').notNullable()
table.string('status').notNullable()
table.timestamp('date_status_updated', { useTz: false })
table.timestamp('date_deleted', { useTz: false })
table.string('alert_type').notNullable()
table.boolean('is_test')

// Legacy timestamps
table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now())
table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now())
})
}

exports.down = function (knex) {
return knex
.schema
.withSchema('water')
.dropTableIfExists(tableName)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const viewName = 'gauging_stations'

exports.up = function (knex) {
return knex
.schema
.createView(viewName, (view) => {
view.as(knex('gauging_stations').withSchema('water').select([
'gauging_station_id AS id',
'label',
'lat',
'long',
'easting',
'northing',
'grid_reference',
'catchment_name ',
'river_name',
'wiski_id',
'station_reference',
'status',
'metadata',
'hydrology_station_id',
// 'is_test'
'date_created AS created_at',
'date_updated AS updated_at'
]))
})
}

exports.down = function (knex) {
return knex
.schema
.dropViewIfExists(viewName)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

const viewName = 'licence_gauging_stations'

exports.up = function (knex) {
return knex
.schema
.createView(viewName, (view) => {
view.as(knex('licence_gauging_stations').withSchema('water').select([
'licence_gauging_station_id AS id',
'licence_id',
'gauging_station_id',
'source',
'licence_version_purpose_condition_id',
'abstraction_period_start_day',
'abstraction_period_start_month ',
'abstraction_period_end_day',
'abstraction_period_end_month',
'restriction_type',
'threshold_unit',
'threshold_value',
'status',
'date_status_updated',
'date_deleted',
'alert_type',
// 'is_test',
'date_created AS created_at',
'date_updated AS updated_at'
]))
})
}

exports.down = function (knex) {
return knex
.schema
.dropViewIfExists(viewName)
}
Loading

0 comments on commit 0d2ca82

Please sign in to comment.