Skip to content

Commit

Permalink
Switch to Charge Version as base for 2PT matching (#340)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4046

Update the existing matching algorithm for matching 2PT returns to charge versions to be based on `charge_versions` as the primary table instead of licences.

For example, a charge version may have 2 charge elements that match and therefore currently cause our results to spit out the same licence twice. Also, we know eventually the data will be used to feed billing which uses charge versions as the primary data source.
  • Loading branch information
Cruikshanks authored Aug 8, 2023
1 parent 1556cdb commit 4e5ebad
Showing 1 changed file with 56 additions and 18 deletions.
74 changes: 56 additions & 18 deletions app/services/check/two-part.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,82 @@
*/

const DetermineBillingPeriodsService = require('../billing/determine-billing-periods.service.js')
const LicenceModel = require('../../models/water/licence.model.js')
const ChargeVersionModel = require('../../models/water/charge-version.model.js')
const ReturnModel = require('../../models/returns/return.model.js')

async function go (naldRegionId) {
const billingPeriods = DetermineBillingPeriodsService.go()

const billingPeriod = billingPeriods[1]

const licences = await LicenceModel.query()
.select('licences.licenceRef', 'licences.licenceId')
.innerJoinRelated('chargeVersions.chargeElements')
const chargeVersions = await _fetchChargeVersions(billingPeriod, naldRegionId)
await _fetchAndApplyReturns(billingPeriod, chargeVersions)

return _response(chargeVersions)
}

async function _fetchChargeVersions (billingPeriod, naldRegionId) {
const chargeVersions = await ChargeVersionModel.query()
.select([
'chargeVersions.chargeVersionId',
'chargeVersions.startDate',
'chargeVersions.endDate',
'chargeVersions.status',
'chargeVersions.licenceId',
'chargeVersions.licenceRef'
])
.innerJoinRelated('chargeElements')
.where('chargeVersions.regionCode', naldRegionId)
.where('chargeVersions.scheme', 'sroc')
.where('chargeVersions.startDate', '<=', billingPeriod.endDate)
.whereJsonPath('chargeVersions:chargeElements.adjustments', '$.s127', '=', true)
.whereNot('chargeVersions.status', 'draft')
.withGraphFetched('chargeVersions.chargeElements.chargePurposes')
.modifyGraph('chargeVersions', builder => {
builder.where('chargeVersions.scheme', 'sroc')
.where('chargeVersions.startDate', '<=', billingPeriod.endDate)
.whereNot('chargeVersions.status', 'draft')
})
.modifyGraph('chargeVersions.chargeElements', builder => {
.whereJsonPath('chargeElements.adjustments', '$.s127', '=', true)
.withGraphFetched('chargeElements')
.modifyGraph('chargeVersions.chargeElements', (builder) => {
builder.whereJsonPath('chargeElements.adjustments', '$.s127', '=', true)
})
.withGraphFetched('chargeElements.chargePurposes')

return chargeVersions
}

for (const licence of licences) {
licence.returns = await ReturnModel.query()
.select('returnId', 'returnRequirement', 'startDate', 'endDate', 'metadata')
.where('licenceRef', licence.licenceRef)
// used in the service to filter out old returns here `src/lib/services/returns/api-connector.js`
async function _fetchAndApplyReturns (billingPeriod, chargeVersions) {
for (const chargeVersion of chargeVersions) {
chargeVersion.returns = await ReturnModel.query()
.select([
'returnId',
'returnRequirement',
'startDate',
'endDate',
'metadata'
])
.where('licenceRef', chargeVersion.licenceRef)
// water-abstraction-service filters out old returns in this way: `src/lib/services/returns/api-connector.js`
.where('startDate', '>=', '2008-04-01')
.where('startDate', '<=', billingPeriod.endDate)
.where('endDate', '>=', billingPeriod.startDate)
.whereJsonPath('metadata', '$.isTwoPartTariff', '=', true)
}
}

function _response (chargeVersions) {
const allLicenceIds = chargeVersions.map((chargeVersion) => {
return chargeVersion.licenceId
})

const uniqueLicenceIds = [...new Set(allLicenceIds)]

return uniqueLicenceIds.map((uniqueLicenceId) => {
const matchedChargeVersions = chargeVersions.filter((chargeVersion) => {
return chargeVersion.licenceId === uniqueLicenceId
})

return { billingPeriod, licences }
return {
licenceId: uniqueLicenceId,
licenceRef: matchedChargeVersions[0].licenceRef,
chargeVersions: matchedChargeVersions
}
})
}

module.exports = {
Expand Down

0 comments on commit 4e5ebad

Please sign in to comment.