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

Fix FetchLiveBillRunsService #845

Merged
merged 4 commits into from
Mar 21, 2024
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
13 changes: 6 additions & 7 deletions app/services/bill-runs/fetch-live-bill-runs.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,15 @@ function _matchLiveBillRuns (liveBillRuns, financialYearEnding, supplementary) {
const matches = []

for (const liveBillRun of liveBillRuns) {
// If it's not supplementary we are looking on behalf of annual or 2PT. This means as soon as we have a match we
// can break the loop and return the result
if (!supplementary && liveBillRun.toFinancialYearEnding === financialYearEnding) {
// Regardless of bill run type if the years match we have a matching live bill run
if (liveBillRun.toFinancialYearEnding === financialYearEnding) {
matches.push(liveBillRun)
break
}

// Because of the check above this will only be applied to supplementary bill runs. This is where we check the
// live bill run against both the specified financial year ending and the last year of PRESROC 2022
if (liveBillRun.toFinancialYearEnding === financialYearEnding || liveBillRun.toFinancialYearEnding === LAST_PRESROC_YEAR) {
// If the bill run we are checking for is supplementary we also need to check if there is a match for the last year
// of PRESROC. This won't necessarily block the bill run from being created. But it will help the calling service
// determine whether to ping the legacy app or not to create a PRESROC bill run
if (supplementary && liveBillRun.toFinancialYearEnding === LAST_PRESROC_YEAR) {
matches.push(liveBillRun)
}
}
Expand Down
177 changes: 110 additions & 67 deletions test/services/bill-runs/fetch-live-bill-runs.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ const { expect } = Code
const BillRunHelper = require('../../support/helpers/bill-run.helper.js')
const DatabaseSupport = require('../../support/database.js')
const RegionHelper = require('../../support/helpers/region.helper.js')
const { determineCurrentFinancialYear } = require('../../../app/lib/general.lib.js')

// Thing under test
const FetchLiveBillRunsService = require('../../../app/services/bill-runs/fetch-live-bill-runs.service.js')

describe('Fetch Live Bill Runs service', () => {
const currentFinancialYear = determineCurrentFinancialYear()

let financialYearEnding
let regionId

Expand All @@ -27,23 +30,23 @@ describe('Fetch Live Bill Runs service', () => {
})

describe('when there is a live bill run', () => {
beforeEach(() => {
financialYearEnding = 2024
})

describe('and it matches the financial year end requested', () => {
describe('and it is for the current year (SROC)', () => {
beforeEach(async () => {
await BillRunHelper.add({
id: '1021c5bc-673c-48fa-98dd-733b46c84f90',
regionId,
batchType: 'two_part_tariff',
status: 'review',
toFinancialYearEnding: financialYearEnding,
batchType: 'supplementary',
status: 'ready',
toFinancialYearEnding: currentFinancialYear.endDate.getFullYear(),
scheme: 'sroc'
})
})

describe('and we are not setting up a supplementary bill run (supplementary is false)', () => {
describe('and we are checking for an annual or two-part tariff bill run for the current year', () => {
beforeEach(async () => {
financialYearEnding = currentFinancialYear.endDate.getFullYear()
})

it('returns the live bill run as the match', async () => {
const results = await FetchLiveBillRunsService.go(regionId, financialYearEnding, false)

Expand All @@ -52,88 +55,128 @@ describe('Fetch Live Bill Runs service', () => {
})
})

describe('and we are setting up a supplementary bill run (supplementary is true)', () => {
describe('and there is also a live bill run in 2022 (last year for PRESROC)', () => {
beforeEach(async () => {
await BillRunHelper.add({
id: 'b65bb671-8961-4d0c-93f4-d19e1998e778',
regionId,
batchType: 'supplementary',
status: 'ready',
toFinancialYearEnding: 2022,
scheme: 'alcs'
})
})
describe('and we are checking for a supplementary bill run for the current year', () => {
beforeEach(async () => {
financialYearEnding = currentFinancialYear.endDate.getFullYear()
})

it('returns multiple live bill run matches', async () => {
const results = await FetchLiveBillRunsService.go(regionId, financialYearEnding, true)
it('returns the live bill run as the match', async () => {
const results = await FetchLiveBillRunsService.go(regionId, financialYearEnding, true)

expect(results).to.have.length(2)
expect(results[0].id).to.equal('1021c5bc-673c-48fa-98dd-733b46c84f90')
expect(results[1].id).to.equal('b65bb671-8961-4d0c-93f4-d19e1998e778')
})
expect(results).to.have.length(1)
expect(results[0].id).to.equal('1021c5bc-673c-48fa-98dd-733b46c84f90')
})
})

describe('and we are checking for a backlog two-part tariff bill run for the previous year', () => {
beforeEach(async () => {
financialYearEnding = currentFinancialYear.endDate.getFullYear() - 1
})

describe('and there are no PRESROC live bill runs', () => {
it('returns just the live bill run as the match', async () => {
const results = await FetchLiveBillRunsService.go(regionId, financialYearEnding, false)
it('returns no matches', async () => {
const results = await FetchLiveBillRunsService.go(regionId, financialYearEnding, false)

expect(results).to.have.length(1)
expect(results[0].id).to.equal('1021c5bc-673c-48fa-98dd-733b46c84f90')
})
expect(results).to.be.empty()
})
})
})

describe('but it does not match the financial year end requested', () => {
describe('and it is for the previous year (SROC)', () => {
beforeEach(async () => {
financialYearEnding = 2024

await BillRunHelper.add({
id: '1021c5bc-673c-48fa-98dd-733b46c84f90',
regionId,
batchType: 'two_part_tariff',
status: 'review',
toFinancialYearEnding: 2023,
batchType: 'supplementary',
status: 'ready',
toFinancialYearEnding: currentFinancialYear.endDate.getFullYear() - 1,
scheme: 'sroc'
})
})

describe('and we are not setting up a supplementary bill run (supplementary is false)', () => {
describe('and we are checking for an annual or two-part tariff bill run for the current year', () => {
beforeEach(async () => {
financialYearEnding = currentFinancialYear.endDate.getFullYear()
})

it('returns no matches', async () => {
const results = await FetchLiveBillRunsService.go(regionId, financialYearEnding, false)

expect(results).to.be.empty()
})
})

describe('and we are checking for a supplementary bill run for the current year', () => {
beforeEach(async () => {
financialYearEnding = currentFinancialYear.endDate.getFullYear()
})

it('returns no matches', async () => {
const results = await FetchLiveBillRunsService.go(regionId, financialYearEnding, true)

expect(results).to.be.empty()
})
})

describe('and we are checking for a backlog two-part tariff bill run for the previous year', () => {
beforeEach(async () => {
financialYearEnding = currentFinancialYear.endDate.getFullYear() - 1
})

it('returns the live bill run as the match', async () => {
const results = await FetchLiveBillRunsService.go(regionId, financialYearEnding, false)

expect(results).to.have.length(1)
expect(results[0].id).to.equal('1021c5bc-673c-48fa-98dd-733b46c84f90')
})
})
})

describe('and it is for the last PRESROC year (PRESROC)', () => {
beforeEach(async () => {
await BillRunHelper.add({
id: '1021c5bc-673c-48fa-98dd-733b46c84f90',
regionId,
batchType: 'supplementary',
status: 'ready',
toFinancialYearEnding: 2022,
scheme: 'alcs'
})
})

describe('and we are checking for an annual or two-part tariff bill run for the current year', () => {
beforeEach(async () => {
financialYearEnding = currentFinancialYear.endDate.getFullYear()
})

it('returns no matches', async () => {
const results = await FetchLiveBillRunsService.go(regionId, financialYearEnding, false)

expect(results).to.be.empty()
})
})

describe('and we are setting up a supplementary bill run (supplementary is true)', () => {
describe('and there is also a live bill run in 2022 (last year for PRESROC)', () => {
beforeEach(async () => {
await BillRunHelper.add({
id: 'b65bb671-8961-4d0c-93f4-d19e1998e778',
regionId,
batchType: 'supplementary',
status: 'ready',
toFinancialYearEnding: 2022,
scheme: 'alcs'
})
})

it('returns just the PRESROC live bill as the match', async () => {
const results = await FetchLiveBillRunsService.go(regionId, financialYearEnding, true)

expect(results).to.have.length(1)
expect(results[0].id).to.equal('b65bb671-8961-4d0c-93f4-d19e1998e778')
})
})

describe('and there are no PRESROC live bill runs', () => {
it('returns no matches', async () => {
const results = await FetchLiveBillRunsService.go(regionId, financialYearEnding, false)

expect(results).to.be.empty()
})
describe('and we are checking for a supplementary bill run for the current year', () => {
beforeEach(async () => {
financialYearEnding = currentFinancialYear.endDate.getFullYear()
})

it('returns the live bill run as the match', async () => {
const results = await FetchLiveBillRunsService.go(regionId, financialYearEnding, true)

expect(results).to.have.length(1)
expect(results[0].id).to.equal('1021c5bc-673c-48fa-98dd-733b46c84f90')
})
})

describe('and we are checking for a backlog two-part tariff bill run for the previous year', () => {
beforeEach(async () => {
financialYearEnding = currentFinancialYear.endDate.getFullYear() - 1
})

it('returns no matches', async () => {
const results = await FetchLiveBillRunsService.go(regionId, financialYearEnding, false)

expect(results).to.be.empty()
})
})
})
Expand Down
Loading