Skip to content

Commit 31fff20

Browse files
authored
Add new queued batch status (#1965)
https://eaflood.atlassian.net/browse/WATER-3877 When a supplementary bill run is started, we'll be creating both a PRESROC and SROC one at the same time. The PRESROC one will then begin `processing` (labelled as BUILDING in the UI) whilst the SROC one is `queued` waiting to be processed. At the moment though, all we can do is initialise the SROC bill run with a status of processing as that is all that is available to us. In the UI this will give the impression both are building when their not. So, we need to add a new `queued` status to the code and DB. We'll make it so all new bill runs are given this status initially. Then, once processing starts the status will be updated. This is what happens at the moment. The bill run is created and then a job is added to a 'queue' for it to be processed.
1 parent 27a8472 commit 31fff20

11 files changed

+107
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict'
2+
3+
const fs = require('fs')
4+
const path = require('path')
5+
let Promise
6+
7+
/**
8+
* We receive the dbmigrate dependency from dbmigrate initially.
9+
* This enables us to not have to rely on NODE_PATH.
10+
*/
11+
exports.setup = function (options, _seedLink) {
12+
Promise = options.Promise
13+
}
14+
15+
exports.up = function (db) {
16+
const filePath = path.join(__dirname, 'sqls', '20230120140933-queued-batch-status-up.sql')
17+
return new Promise(function (resolve, reject) {
18+
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) {
19+
if (err) return reject(err)
20+
console.log('received data: ' + data)
21+
22+
resolve(data)
23+
})
24+
})
25+
.then(function (data) {
26+
return db.runSql(data)
27+
})
28+
}
29+
30+
exports.down = function (db) {
31+
const filePath = path.join(__dirname, 'sqls', '20230120140933-queued-batch-status-down.sql')
32+
return new Promise(function (resolve, reject) {
33+
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) {
34+
if (err) return reject(err)
35+
console.log('received data: ' + data)
36+
37+
resolve(data)
38+
})
39+
})
40+
.then(function (data) {
41+
return db.runSql(data)
42+
})
43+
}
44+
45+
exports._meta = {
46+
version: 1
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
ALTER TYPE water.batch_status
2+
RENAME to batch_status_old;
3+
4+
CREATE TYPE water.batch_status AS ENUM (
5+
'processing',
6+
'review',
7+
'ready',
8+
'error',
9+
'sent',
10+
'empty',
11+
'cancel',
12+
'sending'
13+
);
14+
15+
ALTER TABLE water.billing_batches
16+
ALTER COLUMN status TYPE water.batch_status USING status::text::water.batch_status;
17+
18+
ALTER TABLE water.billing_batch_charge_version_years
19+
ALTER COLUMN status TYPE water.batch_status USING status::text::water.batch_status;
20+
21+
DROP TYPE water.batch_status_old;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
ALTER TYPE water.batch_status
2+
RENAME to batch_status_old;
3+
4+
CREATE TYPE water.batch_status AS ENUM (
5+
'processing',
6+
'review',
7+
'ready',
8+
'error',
9+
'sent',
10+
'empty',
11+
'cancel',
12+
'sending',
13+
'queued'
14+
);
15+
16+
ALTER TABLE water.billing_batches
17+
ALTER COLUMN status TYPE water.batch_status USING status::text::water.batch_status;
18+
19+
ALTER TABLE water.billing_batch_charge_version_years
20+
ALTER COLUMN status TYPE water.batch_status USING status::text::water.batch_status;
21+
22+
DROP TYPE water.batch_status_old;

src/lib/models/batch.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ const BATCH_STATUS = {
2525
cancel: 'cancel',
2626
// if there are no charge versions, or all billing has already happened
2727
// in earlier run, or all customers have been removed from the batch
28-
empty: 'empty'
28+
empty: 'empty',
29+
// Initial state of all new bill runs. Once picked up for processing the status is then updated to `processing`
30+
queued: 'queued'
2931
}
3032

3133
const BATCH_ERROR_CODE = {

src/modules/billing/jobs/create-bill-run.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { get, partial } = require('lodash')
55
const JOB_NAME = 'billing.create-bill-run'
66

77
const batchService = require('../services/batch-service')
8-
const { BATCH_ERROR_CODE, BATCH_TYPE } = require('../../../lib/models/batch')
8+
const { BATCH_ERROR_CODE, BATCH_STATUS, BATCH_TYPE } = require('../../../lib/models/batch')
99
const batchJob = require('./lib/batch-job')
1010
const helpers = require('./lib/helpers')
1111

@@ -23,6 +23,7 @@ const handler = async job => {
2323
const batchId = get(job, 'data.batchId')
2424

2525
try {
26+
await batchService.setStatus(batchId, BATCH_STATUS.processing)
2627
const batch = await batchService.createChargeModuleBillRun(batchId)
2728
return { type: batch.type }
2829
} catch (err) {

src/modules/billing/jobs/lib/batch-status.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ const assertCmBatchIsGeneratedOrBilled = cmBatch => {
2121
}
2222
}
2323

24-
const assertBatchIsProcessing = partialRight(assertBatchIsStatus, [Batch.BATCH_STATUS.processing, Batch.BATCH_STATUS.sending])
24+
const assertBatchIsProcessing = partialRight(
25+
assertBatchIsStatus,
26+
[Batch.BATCH_STATUS.queued, Batch.BATCH_STATUS.processing, Batch.BATCH_STATUS.sending]
27+
)
2528
const assertBatchIsInReview = partialRight(assertBatchIsStatus, [Batch.BATCH_STATUS.review])
2629

2730
exports.assertCmBatchIsGeneratedOrBilled = assertCmBatchIsGeneratedOrBilled

src/modules/billing/services/batch-service.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ const create = async (regionId, batchType, toFinancialYearEnding, isSummer) => {
295295
}
296296

297297
const { billingBatchId } = await newRepos.billingBatches.create({
298-
status: Batch.BATCH_STATUS.processing,
298+
status: Batch.BATCH_STATUS.queued,
299299
regionId,
300300
batchType,
301301
fromFinancialYearEnding,

test/modules/billing/jobs/create-bill-run.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ experiment('modules/billing/jobs/create-bill-run', () => {
4545

4646
sandbox.stub(batchService, 'createChargeModuleBillRun').resolves(batch)
4747
sandbox.stub(batchService, 'setErrorStatus').resolves(batch)
48+
sandbox.stub(batchService, 'setStatus').resolves({ ...batch, status: 'processing' })
4849

4950
queueManager = {
5051
add: sandbox.stub()

test/modules/billing/jobs/process-charge-versions.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ experiment('modules/billing/jobs/process-charge-versions', () => {
121121
test('an error is logged and rethrown', async () => {
122122
const func = () => processChargeVersionsJob.handler(message)
123123
const err = await expect(func()).to.reject()
124-
expect(err.message).to.equal(`Expected processing,sending batch status, but got ${Batch.BATCH_STATUS.ready}`)
124+
expect(err.message).to.equal(`Expected queued,processing,sending batch status, but got ${Batch.BATCH_STATUS.ready}`)
125125
expect(batchJob.logHandlingErrorAndSetBatchStatus.calledWith(message, err, Batch.BATCH_ERROR_CODE.failedToProcessChargeVersions)).to.be.true()
126126
})
127127
})

test/modules/billing/jobs/two-part-tariff-matching.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ experiment('modules/billing/jobs/two-part-tariff-matching', () => {
103103
err,
104104
Batch.BATCH_ERROR_CODE.failedToProcessTwoPartTariff
105105
)).to.be.true()
106-
expect(err.message).to.equal(`Expected processing,sending batch status, but got ${Batch.BATCH_STATUS.ready}`)
106+
expect(err.message).to.equal(`Expected queued,processing,sending batch status, but got ${Batch.BATCH_STATUS.ready}`)
107107
})
108108
})
109109

test/modules/billing/services/batch-service.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1051,10 +1051,10 @@ experiment('modules/billing/services/batch-service', () => {
10511051
result = await batchService.create(regionId, 'annual', 2023, 'all-year')
10521052
})
10531053

1054-
test('a batch is created processing 1 financial year', async () => {
1054+
test('a batch is created queued 1 financial year', async () => {
10551055
const args = newRepos.billingBatches.create.lastCall.args[0]
10561056
expect(args).to.equal({
1057-
status: 'processing',
1057+
status: 'queued',
10581058
regionId,
10591059
batchType: 'annual',
10601060
fromFinancialYearEnding: 2023,
@@ -1074,10 +1074,10 @@ experiment('modules/billing/services/batch-service', () => {
10741074
result = await batchService.create(regionId, 'supplementary', 2022, 'all-year')
10751075
})
10761076

1077-
test('a batch is created processing the number of years specified in config.billing.supplementaryYears', async () => {
1077+
test('a batch is created queued the number of years specified in config.billing.supplementaryYears', async () => {
10781078
const args = newRepos.billingBatches.create.lastCall.args[0]
10791079
expect(args).to.equal({
1080-
status: 'processing',
1080+
status: 'queued',
10811081
regionId,
10821082
batchType: 'supplementary',
10831083
fromFinancialYearEnding: 2017,

0 commit comments

Comments
 (0)