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

Add new queued batch status #1965

Merged
merged 5 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
47 changes: 47 additions & 0 deletions migrations/20230120140933-queued-batch-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

const fs = require('fs')
const path = require('path')
let Promise

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function (options, _seedLink) {
Promise = options.Promise
}

exports.up = function (db) {
const filePath = path.join(__dirname, 'sqls', '20230120140933-queued-batch-status-up.sql')
return new Promise(function (resolve, reject) {
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) {
if (err) return reject(err)
console.log('received data: ' + data)

resolve(data)
})
})
.then(function (data) {
return db.runSql(data)
})
}

exports.down = function (db) {
const filePath = path.join(__dirname, 'sqls', '20230120140933-queued-batch-status-down.sql')
return new Promise(function (resolve, reject) {
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) {
if (err) return reject(err)
console.log('received data: ' + data)

resolve(data)
})
})
.then(function (data) {
return db.runSql(data)
})
}

exports._meta = {
version: 1
}
21 changes: 21 additions & 0 deletions migrations/sqls/20230120140933-queued-batch-status-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ALTER TYPE water.batch_status
RENAME to batch_status_old;

CREATE TYPE water.batch_status AS ENUM (
'processing',
'review',
'ready',
'error',
'sent',
'empty',
'cancel',
'sending'
);

ALTER TABLE water.billing_batches
ALTER COLUMN status TYPE water.batch_status USING status::text::water.batch_status;

ALTER TABLE water.billing_batch_charge_version_years
ALTER COLUMN status TYPE water.batch_status USING status::text::water.batch_status;

DROP TYPE water.batch_status_old;
22 changes: 22 additions & 0 deletions migrations/sqls/20230120140933-queued-batch-status-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
ALTER TYPE water.batch_status
RENAME to batch_status_old;

CREATE TYPE water.batch_status AS ENUM (
'processing',
'review',
'ready',
'error',
'sent',
'empty',
'cancel',
'sending',
'queued'
);

ALTER TABLE water.billing_batches
ALTER COLUMN status TYPE water.batch_status USING status::text::water.batch_status;

ALTER TABLE water.billing_batch_charge_version_years
ALTER COLUMN status TYPE water.batch_status USING status::text::water.batch_status;

DROP TYPE water.batch_status_old;
4 changes: 3 additions & 1 deletion src/lib/models/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const BATCH_STATUS = {
cancel: 'cancel',
// if there are no charge versions, or all billing has already happened
// in earlier run, or all customers have been removed from the batch
empty: 'empty'
empty: 'empty',
// Initial state of all new bill runs. Once picked up for processing the status is then updated to `processing`
queued: 'queued'
}

const BATCH_ERROR_CODE = {
Expand Down
3 changes: 2 additions & 1 deletion src/modules/billing/jobs/create-bill-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { get, partial } = require('lodash')
const JOB_NAME = 'billing.create-bill-run'

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

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

try {
await batchService.setStatus(batchId, BATCH_STATUS.processing)
const batch = await batchService.createChargeModuleBillRun(batchId)
return { type: batch.type }
} catch (err) {
Expand Down
5 changes: 4 additions & 1 deletion src/modules/billing/jobs/lib/batch-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ const assertCmBatchIsGeneratedOrBilled = cmBatch => {
}
}

const assertBatchIsProcessing = partialRight(assertBatchIsStatus, [Batch.BATCH_STATUS.processing, Batch.BATCH_STATUS.sending])
const assertBatchIsProcessing = partialRight(
assertBatchIsStatus,
[Batch.BATCH_STATUS.queued, Batch.BATCH_STATUS.processing, Batch.BATCH_STATUS.sending]
)
const assertBatchIsInReview = partialRight(assertBatchIsStatus, [Batch.BATCH_STATUS.review])

exports.assertCmBatchIsGeneratedOrBilled = assertCmBatchIsGeneratedOrBilled
Expand Down
2 changes: 1 addition & 1 deletion src/modules/billing/services/batch-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ const create = async (regionId, batchType, toFinancialYearEnding, isSummer) => {
}

const { billingBatchId } = await newRepos.billingBatches.create({
status: Batch.BATCH_STATUS.processing,
status: Batch.BATCH_STATUS.queued,
regionId,
batchType,
fromFinancialYearEnding,
Expand Down
1 change: 1 addition & 0 deletions test/modules/billing/jobs/create-bill-run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ experiment('modules/billing/jobs/create-bill-run', () => {

sandbox.stub(batchService, 'createChargeModuleBillRun').resolves(batch)
sandbox.stub(batchService, 'setErrorStatus').resolves(batch)
sandbox.stub(batchService, 'setStatus').resolves({ ...batch, status: 'processing' })

queueManager = {
add: sandbox.stub()
Expand Down
2 changes: 1 addition & 1 deletion test/modules/billing/jobs/process-charge-versions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ experiment('modules/billing/jobs/process-charge-versions', () => {
test('an error is logged and rethrown', async () => {
const func = () => processChargeVersionsJob.handler(message)
const err = await expect(func()).to.reject()
expect(err.message).to.equal(`Expected processing,sending batch status, but got ${Batch.BATCH_STATUS.ready}`)
expect(err.message).to.equal(`Expected queued,processing,sending batch status, but got ${Batch.BATCH_STATUS.ready}`)
expect(batchJob.logHandlingErrorAndSetBatchStatus.calledWith(message, err, Batch.BATCH_ERROR_CODE.failedToProcessChargeVersions)).to.be.true()
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/modules/billing/jobs/two-part-tariff-matching.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ experiment('modules/billing/jobs/two-part-tariff-matching', () => {
err,
Batch.BATCH_ERROR_CODE.failedToProcessTwoPartTariff
)).to.be.true()
expect(err.message).to.equal(`Expected processing,sending batch status, but got ${Batch.BATCH_STATUS.ready}`)
expect(err.message).to.equal(`Expected queued,processing,sending batch status, but got ${Batch.BATCH_STATUS.ready}`)
})
})

Expand Down
8 changes: 4 additions & 4 deletions test/modules/billing/services/batch-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1051,10 +1051,10 @@ experiment('modules/billing/services/batch-service', () => {
result = await batchService.create(regionId, 'annual', 2023, 'all-year')
})

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

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