Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Return Status as part of Matching Algorithm #360

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.returnStatus = [...new Set(cumulativeReturnStatuses)]
Jozzey marked this conversation as resolved.
Show resolved Hide resolved
}

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

function _matchChargeVersions (chargeVersions) {
Expand Down