From 4e5ebad595a58bc4893918b08ebf3b1f4d2cf05d Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Tue, 8 Aug 2023 10:40:04 +0100 Subject: [PATCH] Switch to Charge Version as base for 2PT matching (#340) 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. --- app/services/check/two-part.service.js | 74 +++++++++++++++++++------- 1 file changed, 56 insertions(+), 18 deletions(-) diff --git a/app/services/check/two-part.service.js b/app/services/check/two-part.service.js index 0903b31c56..896b483757 100644 --- a/app/services/check/two-part.service.js +++ b/app/services/check/two-part.service.js @@ -6,7 +6,7 @@ */ 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) { @@ -14,36 +14,74 @@ async function go (naldRegionId) { 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 = {