Skip to content

Commit

Permalink
Refactor to use new view based RETURNS models (#568)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4057

As part of the work we have been doing on two-part tariff, we will be creating all our new tables in the default `public` schema.

We have also decided that when there is a legacy table that we are still going to need we will create a [View](https://www.postgresql.org/docs/current/sql-createview.html) of it in the `public` schema. This allows us to correct any issues with naming conventions, strip out unused fields, and join entities currently sat in different schemas. The first example of this approach was done in PR #531  in which views were created for the RETURNS schema.

We then created the new models and helpers that use them in [Build models for returns based on new Views](#533).

This is the final step in the process, we are refactoring any use of the legacy models to use the new ones and deleting all the legacy-based code.
  • Loading branch information
Jozzey authored Dec 6, 2023
1 parent 0813a92 commit 6143767
Show file tree
Hide file tree
Showing 18 changed files with 288 additions and 943 deletions.
1 change: 0 additions & 1 deletion app/models/legacy-base.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class LegacyBaseModel extends BaseModel {
static get modelPaths () {
const currentPath = __dirname
return [
path.join(currentPath, 'returns'),
path.join(currentPath, 'water'),
path.join(currentPath, 'crm-v2'),
path.join(currentPath, 'idm')
Expand Down
46 changes: 0 additions & 46 deletions app/models/returns/line.model.js

This file was deleted.

46 changes: 0 additions & 46 deletions app/models/returns/return.model.js

This file was deleted.

16 changes: 0 additions & 16 deletions app/models/returns/returns-base.model.js

This file was deleted.

54 changes: 0 additions & 54 deletions app/models/returns/version.model.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
'use strict'

/**
* Fetches SROC Returns linked to licences flagged for inclusion in next SROC 2PT billing
* @module FetchReturnsForLicenceService
* Fetches SROC return logs linked to licences flagged for inclusion in next SROC 2PT billing
* @module FetchReturnLogsForLicenceService
*/

const { ref } = require('objection')

const ReturnModel = require('../../../models/returns/return.model')
const ReturnLogModel = require('../../../models/return-log.model.js')

/**
* Fetch all SROC returns to be processed as part of two-part-tariff billing
* Fetch all SROC return logs to be processed as part of two-part-tariff billing
* *
* @param {String} licenceRef The reference of the licence that the return relates to
* @param {String} licenceRef The reference of the licence that the return log relates to
* @param {Object} billingPeriod Object with a `startDate` and `endDate` property representing the period being billed
*
* @returns {Object} Contains an array of Returns and the associated current Version, and Lines if they exist
* @returns {Object} Contains an array of `returnLogs` and the associated current `returnSubmissions`, and
* `returnSubmissionLines` if they exist
*/
async function go (licenceRef, billingPeriod) {
return _fetch(licenceRef, billingPeriod)
}

async function _fetch (licenceRef, billingPeriod) {
const returns = await ReturnModel.query()
const returnLogs = await ReturnLogModel.query()
.select([
'returnId',
'id',
'returnRequirement',
ref('metadata:description').castText().as('description'),
'startDate',
Expand All @@ -40,37 +41,37 @@ async function _fetch (licenceRef, billingPeriod) {
ref('metadata:purposes').as('purposes')
])
.where('licenceRef', licenceRef)
// water-abstraction-service filters out old returns in this way: see `src/lib/services/returns/api-connector.js`
// water-abstraction-service filters out old return logs in this way: see `src/lib/services/returns/api-connector.js`
.where('startDate', '>=', '2008-04-01')
.where('startDate', '<=', billingPeriod.endDate)
.where('endDate', '>=', billingPeriod.startDate)
.whereJsonPath('metadata', '$.isTwoPartTariff', '=', true)
.orderBy('startDate', 'ASC')
.orderBy('returnRequirement', 'ASC')
.withGraphFetched('versions')
.modifyGraph('versions', builder => {
.withGraphFetched('returnSubmissions')
.modifyGraph('returnSubmissions', builder => {
builder
.select([
'versionId',
'id',
'nilReturn'
])
.where('versions.current', true)
.where('returnSubmissions.current', true)
})
.withGraphFetched('versions.lines')
.modifyGraph('versions.lines', builder => {
.withGraphFetched('returnSubmissions.returnSubmissionLines')
.modifyGraph('returnSubmissions.returnSubmissionLines', builder => {
builder
.select([
'lineId',
'id',
'startDate',
'endDate',
'quantity'
])
.where('lines.quantity', '>', 0)
.where('lines.startDate', '<=', billingPeriod.endDate)
.where('lines.endDate', '>=', billingPeriod.startDate)
.where('returnSubmissionLines.quantity', '>', 0)
.where('returnSubmissionLines.startDate', '<=', billingPeriod.endDate)
.where('returnSubmissionLines.endDate', '>=', billingPeriod.startDate)
})

return returns
return returnLogs
}

module.exports = {
Expand Down
18 changes: 9 additions & 9 deletions app/services/check/allocate-returns-volumes.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@

function go (chargeReference) {
let returnVolumeInMegalitres
// Loop through each return
chargeReference.returns.forEach((returnData) => {
// The volumes on the return are in Cubic Metres so we convert to Megalitres to match the charge version data
returnVolumeInMegalitres = returnData.volumes.total / 1000
// Loop through each return log
chargeReference.returnLogs.forEach((returnLogData) => {
// The volumes on the return log are in Cubic Metres so we convert to Megalitres to match the charge version data
returnVolumeInMegalitres = returnLogData.volumes.total / 1000
// Loop through each charge element
chargeReference.chargeElements.forEach((chargeElement) => {
if (!chargeElement.allocatedReturnVolume) {
chargeElement.allocatedReturnVolume = 0
}
// Check the chargeElement is not already fully allocated
if (chargeElement.allocatedReturnVolume < chargeElement.authorisedAnnualQuantity) {
// Check if the return's purpose and abstraction period match the charge element
if (_matchReturnToElement(returnData.metadata, chargeElement)) {
// Check if the return log's purpose and abstraction period match the charge element
if (_matchReturnToElement(returnLogData.metadata, chargeElement)) {
// Calculate how much is left to allocated to the ChargeElement from the return
let volumeLeftToAllocate = chargeElement.authorisedAnnualQuantity - chargeElement.allocatedReturnVolume
// Check for the case that the return does not cover the full allocation
// Check for the case that the return log does not cover the full allocation
if (returnVolumeInMegalitres < volumeLeftToAllocate) {
volumeLeftToAllocate = returnVolumeInMegalitres
}
Expand All @@ -35,9 +35,9 @@ function go (chargeReference) {

if (returnVolumeInMegalitres > 0) {
// Convert any remaining volume back to Cubic Metres and add it to the volumes object
returnData.volumes.unallocated = returnVolumeInMegalitres * 1000
returnLogData.volumes.unallocated = returnVolumeInMegalitres * 1000
} else {
returnData.volumes.unallocated = 0
returnLogData.volumes.unallocated = 0
}
})
}
Expand Down
Loading

0 comments on commit 6143767

Please sign in to comment.