-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable 2PT supplementary bill run to be created (#1748)
https://eaflood.atlassian.net/browse/WATER-4201 > Part of the work to support two-part tariff bill runs Prior to this change, we'd updated the set-up bill run to include an option for two-part tariff supplementary, and added all the checks to ensure it was possible. However, we have some temporary code that causes the engine to do nothing when the create button is clicked on the `/check` bill run page. This change removes that. We also add a service to manage the processing of the bill run, and update some of the supporting services to allow this to happen.
- Loading branch information
1 parent
fbee1a9
commit d977580
Showing
11 changed files
with
479 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
app/services/bill-runs/tpt-supplementary/process-bill-run.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict' | ||
|
||
/** | ||
* Process a two-part tariff supplementary bill run for the given billing period | ||
* @module ProcessBillRunService | ||
*/ | ||
|
||
const BillRunModel = require('../../../models/bill-run.model.js') | ||
const { calculateAndLogTimeTaken, currentTimeInNanoseconds } = require('../../../lib/general.lib.js') | ||
const HandleErroredBillRunService = require('../handle-errored-bill-run.service.js') | ||
const MatchAndAllocateService = require('../match/match-and-allocate.service.js') | ||
|
||
/** | ||
* Process a two-part tariff supplementary bill run for the given billing period | ||
* | ||
* Matches and allocates licences to returns for a two-part tariff bill run | ||
* | ||
* The results of the matching process are then persisted to the database ready for the results to be reviewed. The bill | ||
* run status is also updated to 'review'. | ||
* | ||
* In the unlikely event of no licences match to returns it will set the status to 'empty'. It will also handle updating | ||
* the bill run if an error occurs during the process. | ||
* | ||
* @param {module:BillRunModel} billRun - The two-part tariff supplementary bill run being processed | ||
* @param {object[]} billingPeriods - An array of billing periods each containing a `startDate` and `endDate`. For 2PT | ||
* this will only ever contain a single period | ||
*/ | ||
async function go(billRun, billingPeriods) { | ||
const { id: billRunId } = billRun | ||
// NOTE: billingPeriods come from `DetermineBillingPeriodsService` which always returns an array because it is used by | ||
// all billing types. For two-part tariff we know it will only contain one because 2PT supplementary bill runs are | ||
// only for a single financial year | ||
const billingPeriod = billingPeriods[0] | ||
|
||
try { | ||
const startTime = currentTimeInNanoseconds() | ||
|
||
await _updateStatus(billRunId, 'processing') | ||
|
||
// `populated` will be set to true if `MatchAndAllocateService` processes at least one licence | ||
const populated = await MatchAndAllocateService.go(billRun, billingPeriod) | ||
|
||
await _setBillRunStatus(billRunId, populated) | ||
|
||
calculateAndLogTimeTaken(startTime, 'Process bill run complete', { billRunId, type: 'two_part_supplementary' }) | ||
} catch (error) { | ||
await HandleErroredBillRunService.go(billRunId) | ||
global.GlobalNotifier.omfg('Process bill run failed', { billRun }, error) | ||
} | ||
} | ||
|
||
async function _setBillRunStatus(billRunId, populated) { | ||
// It is highly unlikely no licences were matched to returns. So we default status to 'review' | ||
let status = 'review' | ||
|
||
// Just in case no licences were found to be matched to returns we set the status to 'empty' | ||
if (!populated) { | ||
status = 'empty' | ||
} | ||
|
||
// Update the bill run's status | ||
await _updateStatus(billRunId, status) | ||
} | ||
|
||
async function _updateStatus(billRunId, status) { | ||
await BillRunModel.query().findById(billRunId).patch({ status }) | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.