-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix broken send bill run functionality (#2446)
https://eaflood.atlassian.net/browse/WATER-4345 We have been working on replacing the legacy SROC annual billing engine with one in [water-abstraction-system](https://github.com/DEFRA/water-abstraction-system). That engine is based on the one we built for supplementary billing, also in **water-abstraction-system**. In a previous change, we updated this project to [Stop marking bill runs as ready when they are not](#2442). The refresh job was marking the bill run as **ready** and _then_ kicking off the process to refresh WRLS with data from the Charging Module API. That process entails making a request for every bill in the bill run to the CHA and then updating our local invoice and transactions with the result. You can't **SEND** a bill run until this step is complete. When you view the bill run all the values will be £0.00 and the download CSV will be working. Yet the legacy code still marks the bill run as **READY**. Users familiar with the service may know they need to leave it a while for the numbers to refresh. What they probably don't know is the CHA is getting hammered and the service is busy with this process. Ideally, they should wait for this process to complete before starting a new bill run. But there is no way to know that. So, the next bill run will take longer to complete and you increase the risk of an error. Anyway, that's why we made the change. But when we did we had no way of knowing when you send a bill run this legacy service _calls the refresh invoices job again_!! When you confirm and send a bill run it tells the [charging-module-api (CHA)](https://github.com/DEFRA/sroc-charging-module-api) to send the bill run to SOP. The CHA does a range of things behind the scenes one of which is to generate a transaction reference for each invoice. That's the only new piece of information we need to obtain and you can get it from a single request to `GET`` the bill run summary from the CHA. So, the fact we are reusing the refresh invoices job again is quite simply 🦇💩! Anyway, we're going to stop this insanity once and for all in [Migrate legacy send bill run functionality](DEFRA/water-abstraction-system#771). But till then we need to get it working again in this project. We broke it by having the job set the bill run status to `ready` at the end of the process. Before our change when the refresh job was kicked off, it would immediately set the status to `ready` or `sent` then get on with the process. Our change means the bill run is getting marked as `sent`, then reset back to `ready` after the update is complete. This ~hack~ change ensures a sent bill run stays sent!
- Loading branch information
1 parent
1f32b51
commit d987b4d
Showing
9 changed files
with
73 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict' | ||
|
||
const { v4: uuid } = require('uuid') | ||
|
||
// Utils | ||
const batchService = require('../services/batch-service.js') | ||
const { logger } = require('../../../logger.js') | ||
const UpdateInvoicesWorker = require('./lib/update-invoices-worker.js') | ||
|
||
// Constants | ||
const { BATCH_ERROR_CODE } = require('../../../lib/models/batch.js') | ||
const JOB_NAME = 'billing.update-invoice-references' | ||
|
||
const createMessage = data => ([ | ||
JOB_NAME, | ||
data, | ||
{ | ||
jobId: `${JOB_NAME}.${data.batch.id}.${uuid()}` | ||
} | ||
]) | ||
|
||
const handler = async job => { | ||
const { id: batchId } = job.data.batch | ||
|
||
try { | ||
await UpdateInvoicesWorker.updateInvoices(job) | ||
await batchService.setStatus(batchId, 'sent') | ||
} catch (error) { | ||
await batchService.setErrorStatus(batchId, BATCH_ERROR_CODE.failedToGetChargeModuleBillRunSummary) | ||
} | ||
} | ||
|
||
const onComplete = async (job) => { | ||
logger.info(`onComplete: ${job.id}`) | ||
} | ||
|
||
const onFailed = async (job, err) => { | ||
logger.error(`onFailed: ${job.id} - ${err.message}`, err.stack) | ||
} | ||
|
||
module.exports = { | ||
jobName: JOB_NAME, | ||
createMessage, | ||
handler, | ||
onFailed, | ||
onComplete, | ||
workerOptions: { | ||
lockDuration: 3600000, | ||
lockRenewTime: 3600000 / 2 | ||
} | ||
} |
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