-
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.
- Loading branch information
1 parent
e57036d
commit 8956c01
Showing
8 changed files
with
120 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,23 @@ | |
* @module RootController | ||
*/ | ||
|
||
function index(_request, _h) { | ||
return { status: 'alive' } | ||
const FetchChargeVersionsService = require('../services/bill-runs/match/fetch-charge-versions.service.js') | ||
|
||
const StartBillRunProcessService = require('../services/bill-runs/start-bill-run-process.service.js') | ||
|
||
async function index(_request, h) { | ||
const regionId = '4ccf3c5b-ab4e-48e1-afa8-3b18b5d07fab' | ||
const batchType = 'two_part_supplementary' | ||
const userEmail = '[email protected]' | ||
const financialYearEnding = 2024 | ||
// const billingPeriod = { startDate: new Date('2023-04-01'), endDate: new Date('2024-03-31') } | ||
// const chargeVersions = await FetchChargeVersionsService.go(, billingPeriod, false) | ||
|
||
await StartBillRunProcessService.go(regionId, batchType, userEmail, financialYearEnding) | ||
|
||
return h.response().code(204) | ||
|
||
// return { status: 'alive' } | ||
} | ||
|
||
module.exports = { | ||
|
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 | ||
} |