Skip to content

Commit

Permalink
Get Return Status as part of Matching Algorithm (#360)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4081

As a billing and data user, I need the status of the returns to be assessed.

Once the charge element is matched to a return, the next step is to assess the status of the return, so that the relevant volumes and status can be recorded or error recorded.

This PR adds a new `returnStatus` array to the `chargeVersions` object displaying the unique statuses of the returns matched to the charge version.
  • Loading branch information
Jozzey authored Aug 17, 2023
1 parent 5cf40c0 commit 0104512
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions app/services/check/two-part.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,42 @@ async function _fetchChargeVersions (billingPeriod, naldRegionId) {

async function _fetchAndApplyReturns (billingPeriod, chargeVersion) {
const { licenceRef, chargeElements } = chargeVersion
const cumulativeReturnStatuses = []

for (const chargeElement of chargeElements) {
const { chargePurposes } = chargeElement

for (const chargePurpose of chargePurposes) {
const legacyId = chargePurpose.purposesUse.legacyId

chargePurpose.returns = await ReturnModel.query()
.select([
'returnId',
'returnRequirement',
'startDate',
'endDate',
'status',
'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)

chargePurpose.returnStatus = chargePurpose.returns.map((matchedReturn) => {
return matchedReturn.status
})
}
const purposeUseLegacyIds = _extractPurposeUseLegacyIds(chargeElement)

chargeElement.returns = await ReturnModel.query()
.select([
'returnId',
'returnRequirement',
'startDate',
'endDate',
'status',
'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 chargeElementReturnStatuses = chargeElement.returns.map((matchedReturn) => {
return matchedReturn.status
})

cumulativeReturnStatuses.push(...chargeElementReturnStatuses)
}

chargeVersion.returnStatuses = [...new Set(cumulativeReturnStatuses)]
}

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

function _matchChargeVersions (chargeVersions) {
Expand Down

0 comments on commit 0104512

Please sign in to comment.