Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the 'points' data solution #1352

Merged
merged 47 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
a680861
Update the 'points' data solution
Cruikshanks Sep 24, 2024
925d8b6
Update legacy migrations for existing tables
Cruikshanks Sep 24, 2024
683ec2c
Add the legacy migrations for the new tables
Cruikshanks Sep 24, 2024
42ca4e2
Add the view migrations for the new tables
Cruikshanks Sep 24, 2024
2e4951b
Add alter view migrations for the existing tables
Cruikshanks Sep 24, 2024
05ceb6e
Add the new source model
Cruikshanks Sep 24, 2024
0cc3c6f
Add support for the source model
Cruikshanks Sep 24, 2024
bd404c9
Add the Point model
Cruikshanks Sep 24, 2024
2be16f4
Changes for return requirement points
Cruikshanks Sep 24, 2024
9c3ec9e
Changes for licence version purpose points
Cruikshanks Sep 24, 2024
42bc564
Housekeeping - Fix JSDoc issues in the file
Cruikshanks Sep 24, 2024
9a54446
Correct copy & pasted module comments
Cruikshanks Sep 24, 2024
c139d17
Revert the existing view changes
Cruikshanks Sep 24, 2024
8bee766
Fix broken test
Cruikshanks Sep 24, 2024
e6ed9eb
Merge branch 'main' into switch-to-new-points-spike
Cruikshanks Sep 24, 2024
f0137a0
Fix unexplained failing test
Cruikshanks Sep 24, 2024
20e8bee
Putting the alter migrations back
Cruikshanks Sep 24, 2024
89dce12
Phase 2 of changes
Cruikshanks Sep 24, 2024
8adfb9e
Get view return version (requirements) working
Cruikshanks Sep 24, 2024
fcdfe17
Fix FetchReturnVersion test
Cruikshanks Sep 24, 2024
005c528
Fix Rtn Req. ViewPresenter test
Cruikshanks Sep 24, 2024
bd211c5
Fix Rtn Req. ViewService test
Cruikshanks Sep 24, 2024
885331a
Get returns setup manual journey working
Cruikshanks Sep 24, 2024
ca6bf7d
Fix FetchPointsService test
Cruikshanks Sep 24, 2024
0068c69
Fix Rtn Req. setup points presenter test
Cruikshanks Sep 24, 2024
65551b9
Fix Rtn. Req. setup PointsService test
Cruikshanks Sep 24, 2024
e241e1f
Fix Rtn. Req. SubmitPointsService test
Cruikshanks Sep 24, 2024
a873951
Housekeeping - first pass of points validator tidy
Cruikshanks Sep 24, 2024
d1ea3b4
Housekeeping - 2nd pass of points validator tidy
Cruikshanks Sep 24, 2024
85bfd03
Housekeeping - 3rd pass of points validator tidy
Cruikshanks Sep 24, 2024
9623241
Housekeeping - final pass of points validator tidy
Cruikshanks Sep 24, 2024
e7f2a71
Get returns setup copy existing journey working
Cruikshanks Sep 24, 2024
31433f3
Fix Rtn. Req. setup GenerateFromExisting test
Cruikshanks Sep 24, 2024
388d585
Fix Rtn. Req. ReturnRequirementsPresenter test
Cruikshanks Sep 24, 2024
8fe2ced
Get returns setup use abstraction journey working
Cruikshanks Sep 24, 2024
5e0398b
Fix Rtn Req FetchAbstractionDataService test
Cruikshanks Sep 24, 2024
169f6da
Fix Rtn Req GenerateFromAbstractionData test
Cruikshanks Sep 24, 2024
f0fc0e7
Get the check page working (view rtn reqs)
Cruikshanks Sep 24, 2024
453b880
Get persisting a new return version working
Cruikshanks Sep 24, 2024
3b7952e
Fix Rtn Req GenerateReturnVersionRequirements test
Cruikshanks Sep 24, 2024
6d040c5
Fix Rtn Req PersistReturnVersionService test
Cruikshanks Sep 24, 2024
6e43012
Add points to data loader for acceptance tests
Cruikshanks Sep 24, 2024
77c4fb5
Remove BasePointModel
Cruikshanks Sep 24, 2024
4790b04
Get the FetchReturnRequirementsService working
Cruikshanks Sep 24, 2024
b744459
Fix FetchLicenceReturnsService (no tests!!)
Cruikshanks Sep 24, 2024
009426c
Get GenerateReturnLogsService working
Cruikshanks Sep 24, 2024
20291e6
Housekeeping - fix broken test
Cruikshanks Sep 24, 2024
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
12 changes: 10 additions & 2 deletions app/models/licence-version-purpose-point.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

const { Model } = require('objection')

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

class LicenceVersionPurposePointModel extends BasePointModel {
class LicenceVersionPurposePointModel extends BaseModel {
static get tableName () {
return 'licenceVersionPurposePoints'
}
Expand All @@ -23,6 +23,14 @@ class LicenceVersionPurposePointModel extends BasePointModel {
from: 'licenceVersionPurposePoints.licenceVersionPurposeId',
to: 'licenceVersionPurposes.id'
}
},
point: {
relation: Model.BelongsToOneRelation,
modelClass: 'point.model',
join: {
from: 'licenceVersionPurposePoints.pointId',
to: 'points.id'
}
}
}
}
Expand Down
24 changes: 15 additions & 9 deletions app/models/licence-version-purpose.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ class LicenceVersionPurposeModel extends BaseModel {
to: 'licenceVersionPurposeConditions.licenceVersionPurposeId'
}
},
licenceVersionPurposePoints: {
relation: Model.HasManyRelation,
modelClass: 'licence-version-purpose-point.model',
points: {
relation: Model.ManyToManyRelation,
modelClass: 'point.model',
join: {
from: 'licenceVersionPurposes.id',
to: 'licenceVersionPurposePoints.licenceVersionPurposeId'
through: {
from: 'licenceVersionPurposePoints.licenceVersionPurposeId',
to: 'licenceVersionPurposePoints.pointId'
},
to: 'points.id'
}
},
primaryPurpose: {
Expand Down Expand Up @@ -71,19 +75,21 @@ class LicenceVersionPurposeModel extends BaseModel {
* Modifiers allow us to reuse logic in queries, eg. select the licence version purpose and all related purposes to
* determine if the purpose is electricity generation.
*
* ```javascript
* return LicenceVersionPurposeModel.query()
* .findById(licenceVersionPurposeId)
* .modify('allPurposes')
* ```
*
* See {@link https://vincit.github.io/objection.js/recipes/modifiers.html | Modifiers} for more details
*
* @returns {object}
*/
static get modifiers () {
return {
/**
* allPurposes modifier fetches the purpose plus primary and secondary purposes. Built to support determining if
* the overall purpose is electricity generation or spray irrigation with two-part tariff. These are needed to
* determine what frequency returns should be collected and reported by the licensee.
*/
// allPurposes modifier fetches the purpose plus primary and secondary purposes. Built to support determining if
// the overall purpose is electricity generation or spray irrigation with two-part tariff. These are needed to
// determine what frequency returns should be collected and reported by the licensee
allPurposes (query) {
query
.withGraphFetched('purpose')
Expand Down
53 changes: 47 additions & 6 deletions app/models/base-point.model.js → app/models/point.model.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,56 @@
'use strict'

/**
* @module BasePointModel
* Model for points (water.points)
* @module PointModel
*/

const { Model } = require('objection')

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

/**
* Base class for all 'point' based models, for example, `ReturnRequirementPointModel`
*/
class BasePointModel extends BaseModel {
class PointModel extends BaseModel {
static get tableName () {
return 'points'
}

static get relationMappings () {
return {
licenceVersionPurposes: {
relation: Model.ManyToManyRelation,
modelClass: 'licence-version-purpose.model',
join: {
from: 'points.id',
through: {
from: 'licenceVersionPurposePoints.pointId',
to: 'licenceVersionPurposePoints.licenceVersionPurposeId'
},
to: 'licenceVersionPurposes.id'
}
},
returnRequirements: {
relation: Model.ManyToManyRelation,
modelClass: 'return-requirement.model',
join: {
from: 'points.id',
through: {
from: 'returnRequirementPoints.pointId',
to: 'returnRequirementPoints.returnRequirementId'
},
to: 'returnRequirements.id'
}
},
source: {
relation: Model.HasOneRelation,
modelClass: 'source.model',
join: {
from: 'points.sourceId',
to: 'sources.id'
}
}
}
}

/**
* Generate a string that describes this abstraction point
*
Expand Down Expand Up @@ -45,4 +86,4 @@ class BasePointModel extends BaseModel {
}
}

module.exports = BasePointModel
module.exports = PointModel
12 changes: 10 additions & 2 deletions app/models/return-requirement-point.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@

const { Model } = require('objection')

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

class ReturnRequirementPointModel extends BasePointModel {
class ReturnRequirementPointModel extends BaseModel {
static get tableName () {
return 'returnRequirementPoints'
}

static get relationMappings () {
return {
point: {
relation: Model.BelongsToOneRelation,
modelClass: 'point.model',
join: {
from: 'returnRequirementPoints.pointId',
to: 'points.id'
}
},
returnRequirement: {
relation: Model.BelongsToOneRelation,
modelClass: 'return-requirement.model',
Expand Down
12 changes: 8 additions & 4 deletions app/models/return-requirement.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ class ReturnRequirementModel extends BaseModel {

static get relationMappings () {
return {
returnRequirementPoints: {
relation: Model.HasManyRelation,
modelClass: 'return-requirement-point.model',
points: {
relation: Model.ManyToManyRelation,
modelClass: 'point.model',
join: {
from: 'returnRequirements.id',
to: 'returnRequirementPoints.returnRequirementId'
through: {
from: 'returnRequirementPoints.returnRequirementId',
to: 'returnRequirementPoints.pointId'
},
to: 'points.id'
}
},
returnRequirementPurposes: {
Expand Down
31 changes: 31 additions & 0 deletions app/models/source.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

/**
* Model for sources (water.sources)
* @module SourceModel
*/

const { Model } = require('objection')

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

class SourceModel extends BaseModel {
static get tableName () {
return 'sources'
}

static get relationMappings () {
return {
points: {
relation: Model.HasManyRelation,
modelClass: 'point.model',
join: {
from: 'sources.id',
to: 'points.sourceId'
}
}
}
}
}

module.exports = SourceModel
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ const agreementsExceptionsText = {
* Formats return requirements data for the `/return-requirements/{sessionId}/check` page
*
* @param {object[]} requirements - The existing return requirements in the current session
* @param {module:LicenceVersionPurposePoint[]} licenceVersionPurposePoints - All licence version purpose points linked
* to the current licence version
* @param {module:PointModel[]} points - All points linked to the licence version purposes linked to the current licence
* version
* @param {string} journey - Whether the setup journey is 'no-returns-required' or 'returns-required'
*
* @returns {object} returns requirement data needed by the view template
*/
function go (requirements, licenceVersionPurposePoints, journey) {
function go (requirements, points, journey) {
return {
returnsRequired: journey === 'returns-required',
requirements: _requirements(requirements, licenceVersionPurposePoints)
requirements: _requirements(requirements, points)
}
}

Expand Down Expand Up @@ -63,7 +63,7 @@ function _agreementsExceptions (agreementsExceptions) {
.join(', ') + ', and ' + formattedExceptions[formattedExceptions.length - 1]
}

function _requirements (requirements, licenceVersionPurposePoints) {
function _requirements (requirements, points) {
const completedRequirements = []

for (const [index, requirement] of requirements.entries()) {
Expand All @@ -72,7 +72,7 @@ function _requirements (requirements, licenceVersionPurposePoints) {
// NOTE: We determine a requirement is complete because agreement exceptions is populated and it is the last step in
// the journey
if (agreementsExceptions) {
completedRequirements.push(_mapRequirement(requirement, index, licenceVersionPurposePoints))
completedRequirements.push(_mapRequirement(requirement, index, points))
}
}

Expand All @@ -89,24 +89,24 @@ function _mapPurposes (purposes) {
})
}

function _mapRequirement (requirement, index, licenceVersionPurposePoints) {
function _mapRequirement (requirement, index, points) {
return {
abstractionPeriod: _abstractionPeriod(requirement.abstractionPeriod),
agreementsExceptions: _agreementsExceptions(requirement.agreementsExceptions),
frequencyCollected: returnRequirementFrequencies[requirement.frequencyCollected],
frequencyReported: returnRequirementFrequencies[requirement.frequencyReported],
index,
points: _mapPoints(requirement.points, licenceVersionPurposePoints),
points: _mapPoints(requirement.points, points),
purposes: _mapPurposes(requirement.purposes),
returnsCycle: requirement.returnsCycle === 'summer' ? 'Summer' : 'Winter and all year',
siteDescription: requirement.siteDescription
}
}

function _mapPoints (points, licenceVersionPurposePoints) {
return points.map((point) => {
const matchedPoint = licenceVersionPurposePoints.find((licenceVersionPurposePoint) => {
return licenceVersionPurposePoint.naldPointId.toString() === point
function _mapPoints (selectedPoints, points) {
return selectedPoints.map((selectedPoint) => {
const matchedPoint = points.find((point) => {
return point.id === selectedPoint
})

return matchedPoint.$describe()
Expand Down
18 changes: 9 additions & 9 deletions app/presenters/return-requirements/points.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
*
* @param {module:SessionModel} session - The returns requirements session instance
* @param {string} requirementIndex - The index of the requirement being added or changed
* @param {module:LicenceVersionPurposePoint[]} licenceVersionPurposePoints - All licence version purpose points linked
* to the current licence version
* @param {module:PointModel[]} points - All points linked to the licence version purposes linked to the current licence
* version
*
* @returns {object} - The data formatted for the view template
*/
function go (session, requirementIndex, licenceVersionPurposePoints) {
function go (session, requirementIndex, points) {
const { id: sessionId, licence, requirements } = session
const requirement = requirements[requirementIndex]

return {
backLink: _backLink(session, requirementIndex),
licenceId: licence.id,
licencePoints: _licencePoints(licenceVersionPurposePoints),
licencePoints: _licencePoints(points),
licenceRef: licence.licenceRef,
selectedNaldPointIds: requirement?.points ? requirement.points.join(',') : '',
selectedPointIds: requirement?.points ? requirement.points.join(',') : '',
sessionId
}
}
Expand All @@ -39,12 +39,12 @@ function _backLink (session, requirementIndex) {
return `/system/return-requirements/${id}/purpose/${requirementIndex}`
}

function _licencePoints (licenceVersionPurposePoints) {
function _licencePoints (points) {
// First extract our points from the data, including generating the descriptions
const licencePoints = licenceVersionPurposePoints.map((licenceVersionPurposePoint) => {
const licencePoints = points.map((point) => {
return {
naldPointId: licenceVersionPurposePoint.naldPointId.toString(),
description: licenceVersionPurposePoint.$describe()
id: point.id,
description: point.$describe()
}
})

Expand Down
8 changes: 4 additions & 4 deletions app/presenters/return-requirements/view.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function _mapRequirement (requirement) {
agreementsExceptions: _agreementsExceptions(requirement),
frequencyCollected: returnRequirementFrequencies[requirement.collectionFrequency],
frequencyReported: returnRequirementFrequencies[requirement.reportingFrequency],
points: _points(requirement.returnRequirementPoints),
points: _points(requirement.points),
purposes: _purposes(requirement.returnRequirementPurposes),
returnReference: requirement.legacyId,
returnsCycle: requirement.summer === true ? 'Summer' : 'Winter and all year',
Expand All @@ -129,9 +129,9 @@ function _purposes (returnRequirementPurposes) {
})
}

function _points (returnRequirementPoints) {
return returnRequirementPoints.map((returnRequirementPoint) => {
return returnRequirementPoint.$describe()
function _points (points) {
return points.map((point) => {
return point.$describe()
})
}

Expand Down
2 changes: 2 additions & 0 deletions app/services/data/load/load.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const LicenceVersionPurposePointHelper = require('../../../../test/support/helpe
const LicenceVersionHelper = require('../../../../test/support/helpers/licence-version.helper.js')
const LicenceHelper = require('../../../../test/support/helpers/licence.helper.js')
const ModLogHelper = require('../../../../test/support/helpers/mod-log.helper.js')
const PointHelper = require('../../../../test/support/helpers/point.helper.js')
const PermitLicenceHelper = require('../../../../test/support/helpers/permit-licence.helper.js')
const ReturnLogHelper = require('../../../../test/support/helpers/return-log.helper.js')
const ReturnRequirementPointHelper = require('../../../../test/support/helpers/return-requirement-point.helper.js')
Expand Down Expand Up @@ -101,6 +102,7 @@ const LOAD_HELPERS = {
licenceVersions: { helper: LicenceVersionHelper, test: true, legacy: { schema: 'water', table: 'licence_versions', id: 'licence_version_id' } },
licences: { helper: LicenceHelper, test: true, legacy: { schema: 'water', table: 'licences', id: 'licence_id' } },
modLogs: { helper: ModLogHelper, test: false },
points: { helper: PointHelper, test: false },
permitLicences: { helper: PermitLicenceHelper, test: false },
returnLogs: { helper: ReturnLogHelper, test: true, legacy: { schema: 'returns', table: 'returns', id: 'return_id' } },
returnRequirementPoints: { helper: ReturnRequirementPointHelper, test: false },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ async function _fetchReturnRequirements (licenceReference) {
.modifyGraph('returnVersion.licence.region', (builder) => {
builder.select(['id', 'naldRegionId'])
})
.withGraphFetched('returnRequirementPoints')
.modifyGraph('returnRequirementPoints', (builder) => {
builder.select(['description',
'ngr1',
'ngr2',
'ngr3',
'ngr4'])
.withGraphFetched('points')
.modifyGraph('points', (builder) => {
builder.select([
'points.description',
'points.ngr1',
'points.ngr2',
'points.ngr3',
'points.ngr4'
])
})
.withGraphFetched('returnRequirementPurposes.primaryPurpose')
.modifyGraph('returnRequirementPurposes.primaryPurpose', (builder) => {
Expand Down
Loading
Loading