Skip to content

Commit

Permalink
Match returns to chargePurpose (#356)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4046

Assign matched returns directly to the `chargePurpose` instead of grouping them and assigning them to the `chargeElement`.

We are now a little unsure which is required but this makes it easier to see why we matched the return.
  • Loading branch information
Jozzey authored Aug 16, 2023
1 parent 96e4ab3 commit 140e8c9
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions app/services/check/two-part.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,28 @@ async function _fetchAndApplyReturns (billingPeriod, chargeVersion) {
const { licenceRef, chargeElements } = chargeVersion

for (const chargeElement of chargeElements) {
const purposeUseLegacyIds = _extractPurposeUseLegacyIds(chargeElement)

chargeElement.returns = await ReturnModel.query()
.select([
'returnId',
'returnRequirement',
'startDate',
'endDate',
'metadata'
])
.where('licenceRef', 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)
.whereIn(ref('metadata:purposes[0].tertiary.code').castInt(), purposeUseLegacyIds)
const { chargePurposes } = chargeElement
for (const chargePurpose of chargePurposes) {
const legacyId = chargePurpose.purposesUse.legacyId
chargePurpose.returns = await ReturnModel.query()
.select([
'returnId',
'returnRequirement',
'startDate',
'endDate',
'metadata'
])
.where('licenceRef', 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)
.where(ref('metadata:purposes[0].tertiary.code').castInt(), legacyId)
}
}
}

function _extractPurposeUseLegacyIds (chargeElement) {
return chargeElement.chargePurposes.map((chargePurpose) => {
return chargePurpose.purposesUse.legacyId
})
}

function _matchChargeVersions (chargeVersions) {
const allLicenceIds = chargeVersions.map((chargeVersion) => {
return chargeVersion.licenceId
Expand Down

0 comments on commit 140e8c9

Please sign in to comment.