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 logic to trigger SROC supplementary from PRESROC #2247

Merged
merged 12 commits into from
Dec 8, 2022
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ REDIS_PORT=
REDIS_PASSWORD=

ALLOW_CHARGE_VERSION_UPLOADS=
TRIGGER_SROC_SUPPLEMENTARY=

NODE_ENV=production
ENVIRONMENT=pre
Expand Down
1 change: 1 addition & 0 deletions src/internal/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ module.exports = {
waterAbstractionAlerts: true,
recalculateBills: true,
allowChargeVersionUploads: (get(process.env, 'ALLOW_CHARGE_VERSION_UPLOADS') || '').toLowerCase() === 'true',
triggerSrocSupplementary: (get(process.env, 'TRIGGER_SROC_SUPPLEMENTARY') || '').toLowerCase() === 'true',
acceptanceTestsProxy: !isProduction,
showVerificationCode: process.env.SHOW_VERIFICATION_CODE_FEATURE === 'true' && !isProduction
}
Expand Down
24 changes: 24 additions & 0 deletions src/internal/modules/billing/controllers/create-bill-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ const postBillingBatchFinancialYear = async (request, h) => {
return _batching(h, batch)
}

async function getBillingBatchSroc (request, h) {
// TODO: Correctly populate batch
const dummyBatch = {
id: 'DUMMY_SROC_BATCH',
region: request.params.region,
scheme: 'sroc',
batchType: 'supplementary',
status: 'ready'
}

try {
// TODO: Call the service to create our sroc supplementary billing batch
const path = routing.getBillingBatchRoute(dummyBatch, { isBackEnabled: false })
return h.redirect(path)
} catch (err) {
if (err.statusCode === 409) {
return h.redirect(_creationErrorRedirectUrl(err))
}
throw err
}
}

/**
* If a bill run for the region exists, then display a basic summary page
*
Expand Down Expand Up @@ -283,3 +305,5 @@ exports.getBillingBatchDuplicate = getBillingBatchDuplicate

exports.getBillingBatchFinancialYear = getBillingBatchFinancialYear
exports.postBillingBatchFinancialYear = postBillingBatchFinancialYear

exports.getBillingBatchSroc = getBillingBatchSroc
18 changes: 16 additions & 2 deletions src/internal/modules/billing/lib/routing.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const config = require('../../../config')

/**
* Gets the correct route for the specified batch depending on its
* current status
Expand All @@ -10,12 +12,12 @@
* @return {String} the link
*/
const getBillingBatchRoute = (batch, opts = {}) => {
const { id } = batch
const { id, batchType, scheme, region } = batch

const routeMap = new Map()
.set('processing', `/billing/batch/${id}/processing?back=${opts.isBackEnabled ? 1 : 0}`)
.set('sending', `/billing/batch/${id}/processing?back=${opts.isBackEnabled ? 1 : 0}`)
.set('ready', opts.invoiceId ? `/billing/batch/${id}/invoice/${opts.invoiceId}` : `/billing/batch/${id}/summary`)
.set('ready', _determineReadyUrl(opts, id, batchType, scheme, region))
.set('sent', opts.showSuccessPage ? `/billing/batch/${id}/confirm/success` : `/billing/batch/${id}/summary`)
.set('review', `/billing/batch/${id}/two-part-tariff-review`)

Expand All @@ -28,6 +30,18 @@ const getBillingBatchRoute = (batch, opts = {}) => {
return routeMap.get(batch.status)
}

function _determineReadyUrl (opts, id, batchType, scheme, region) {
if (opts.invoiceId) {
return `/billing/batch/${id}/invoice/${opts.invoiceId}`
}

if (config.featureToggles.triggerSrocSupplementary && batchType === 'supplementary' && scheme === 'presroc') {
return `/billing/batch/sroc/${region}`
}

return `/billing/batch/${id}/summary`
}

const getTwoPartTariffLicenceReviewRoute = (batch, invoiceLicenceId) =>
`/billing/batch/${batch.id}/two-part-tariff/licence/${invoiceLicenceId}`

Expand Down
15 changes: 15 additions & 0 deletions src/internal/modules/billing/routes/create-bill-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ module.exports = {
}
},

getBillingBatchSroc: {
method: 'GET',
path: '/billing/batch/sroc/{region}',
handler: controller.getBillingBatchSroc,
config: {
auth: { scope: allowedScopes },
description: 'get handler for creating an sroc billing batch',
validate: {
params: Joi.object().keys({
region: Joi.string().uuid()
})
}
}
},

getBillingBatchExists: {
method: 'GET',
path: '/billing/batch/{batchId}/exists',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,4 +754,13 @@ experiment('internal/modules/billing/controllers/create-bill-run', () => {
})
})
})

experiment('.getBillingBatchSroc', () => {
test('it redirects to the summary page', async () => {
await controller.getBillingBatchSroc(request, h)

const [url] = h.redirect.lastCall.args
expect(url).to.equal('/billing/batch/DUMMY_SROC_BATCH/summary')
})
})
})
112 changes: 98 additions & 14 deletions test/internal/modules/billing/lib/routing.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
'use strict'

const { expect } = require('@hapi/code')
const { experiment, test, beforeEach } = exports.lab = require('@hapi/lab').script()
const { experiment, test, beforeEach, afterEach } = exports.lab = require('@hapi/lab').script()
const { getBillingBatchRoute } = require('internal/modules/billing/lib/routing')

const config = require('internal/config')
const Sinon = require('sinon')

const sandbox = Sinon.createSandbox()

experiment('internal/modules/billing/lib/routing', () => {
afterEach(() => {
sandbox.restore()
})

experiment('.getBillingBatchRoute', () => {
const batch = { id: 'test-batch-id' }
const defaultBatch = {
id: 'test-batch-id',
batchType: 'supplementary',
scheme: 'presroc',
region: 'test-batch-region'
}

experiment('when batch status is "processing"', () => {
let batch

beforeEach(() => {
batch.status = 'processing'
batch = {
...defaultBatch,
status: 'processing'
}
})

test('returns the expected url', () => {
Expand All @@ -26,23 +46,71 @@ experiment('internal/modules/billing/lib/routing', () => {
})

experiment('when batch status is "ready"', () => {
test('returns the batch summary url if no invoice ID is supplied', () => {
batch.status = 'ready'
expect(getBillingBatchRoute(batch)).to.equal('/billing/batch/test-batch-id/summary')
})
let batch

test('returns the invoice page if an invoice ID is supplied', () => {
batch.status = 'ready'
const options = {
invoiceId: 'test-invoice-id'
beforeEach(() => {
batch = {
...defaultBatch,
status: 'ready'
}
expect(getBillingBatchRoute(batch, options)).to.equal('/billing/batch/test-batch-id/invoice/test-invoice-id')
})

experiment('when an invoice ID is supplied', () => {
test('returns the invoice page', () => {
const options = {
invoiceId: 'test-invoice-id'
}
expect(getBillingBatchRoute(batch, options)).to.equal('/billing/batch/test-batch-id/invoice/test-invoice-id')
})
})

experiment('when no invoice ID is supplied', () => {
experiment('when the batch type is `supplementary` and the scheme is `presroc`', () => {
experiment('when the feature toggle is switched on', () => {
beforeEach(() => {
sandbox.stub(config.featureToggles, 'triggerSrocSupplementary').value(true)
})

test('returns the sroc supplementary url', () => {
expect(getBillingBatchRoute(batch)).to.equal('/billing/batch/sroc/test-batch-region')
})
})

experiment('when the feature toggle is switched off', () => {
beforeEach(() => {
sandbox.stub(config.featureToggles, 'triggerSrocSupplementary').value(false)
})

test('returns the batch summary url', () => {
expect(getBillingBatchRoute(batch)).to.equal('/billing/batch/test-batch-id/summary')
})
})
})

experiment('when the batch type is not `supplementary`', () => {
test('returns the batch summary url', () => {
batch.batchType = 'NOT_SUPPLEMENTARY'
expect(getBillingBatchRoute(batch)).to.equal('/billing/batch/test-batch-id/summary')
})
})

experiment('when the scheme is not `presroc`', () => {
test('returns the batch summary url', () => {
batch.batchType = 'NOT_PRESROC'
expect(getBillingBatchRoute(batch)).to.equal('/billing/batch/test-batch-id/summary')
})
})
})
})

experiment('when batch status is "sent"', () => {
let batch

beforeEach(() => {
batch.status = 'sent'
batch = {
...defaultBatch,
status: 'sent'
}
})

test('returns the summary url by default', () => {
Expand All @@ -55,13 +123,29 @@ experiment('internal/modules/billing/lib/routing', () => {
})

experiment('when batch status is "review"', () => {
let batch

beforeEach(() => {
batch = {
...defaultBatch,
status: 'review'
}
})

test('returns the summary url by default', () => {
batch.status = 'review'
expect(getBillingBatchRoute(batch)).to.equal('/billing/batch/test-batch-id/two-part-tariff-review')
})
})

experiment('when isErrorRoutesIncluded flag is true', () => {
let batch

beforeEach(() => {
batch = {
...defaultBatch
}
})

test('and batch status is "error" returns the processing page url', () => {
batch.status = 'error'
expect(getBillingBatchRoute(batch, { isErrorRoutesIncluded: true })).to.equal('/billing/batch/test-batch-id/processing')
Expand Down