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 FetchMatchingBillRunService for PRESROC 2PT #870

Merged
merged 7 commits into from
Mar 28, 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
10 changes: 6 additions & 4 deletions app/services/bill-runs/fetch-matching-bill-run.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

/**
* Fetch bill run(s) that match the options provided
* @module ExistsService
* @module FetchMatchingBillRunService
*/

const BillRunModel = require('../../models/bill-run.model.js')

const LAST_PRESROC_YEAR = 2022

/**
* Fetch bill run(s) that match the options provided
*
Expand Down Expand Up @@ -52,7 +54,7 @@ async function go (regionId, batchType, financialYearEnding, summer = false) {
.where('regionId', regionId)
.where('batchType', batchType)

_applyWhereClauses(baseQuery, batchType, summer, financialYearEnding)
_applyWhereClauses(baseQuery, batchType, financialYearEnding, summer)

return _fetch(baseQuery)
}
Expand All @@ -70,7 +72,7 @@ function _applySupplementaryWhereClauses (query) {
}

function _applyTwoPartTariffWhereClauses (query, financialYearEnding, summer) {
if (['2022', '2021'].includes(financialYearEnding)) {
if (financialYearEnding <= LAST_PRESROC_YEAR) {
query.where('summer', summer)
}

Expand All @@ -80,7 +82,7 @@ function _applyTwoPartTariffWhereClauses (query, financialYearEnding, summer) {
.limit(1)
}

function _applyWhereClauses (baseQuery, batchType, summer, financialYearEnding) {
function _applyWhereClauses (baseQuery, batchType, financialYearEnding, summer) {
if (batchType === 'annual') {
_applyAnnualWhereClauses(baseQuery, financialYearEnding)
} else if (batchType === 'supplementary') {
Expand Down
39 changes: 20 additions & 19 deletions test/services/bill-runs/fetch-matching-bill-run.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,36 +89,37 @@ describe('Fetch Matching Bill Run service', () => {
})

describe('and the financial year is in the PRESROC period', () => {
describe("and 'summer' is set to false", () => {
describe('and a bill run for the same financial year exists', () => {
beforeEach(async () => {
const billRun = await BillRunHelper.add({
regionId, batchType: 'two_part_tariff', status: 'sent', toFinancialYearEnding: 2022, summer: false
})
matchingBillRunId = billRun.id
})
let matchingSummerBillRunId
let matchingWinterBillRunId

it('returns the matching bill run', async () => {
const results = await FetchMatchingBillRunService.go(regionId, 'two_part_tariff', 2022, false)
beforeEach(async () => {
let billRun = await BillRunHelper.add({
regionId, batchType: 'two_part_tariff', status: 'sent', toFinancialYearEnding: 2022, summer: true
})
matchingSummerBillRunId = billRun.id

expect(results[0].id).to.equal(matchingBillRunId)
})
billRun = await BillRunHelper.add({
regionId, batchType: 'two_part_tariff', status: 'sent', toFinancialYearEnding: 2022, summer: false
})
matchingWinterBillRunId = billRun.id
})

describe("and 'summer' is set to true", () => {
describe('and a bill run for the same financial year exists', () => {
beforeEach(async () => {
const billRun = await BillRunHelper.add({
regionId, batchType: 'two_part_tariff', status: 'sent', toFinancialYearEnding: 2022, summer: true
})
matchingBillRunId = billRun.id
it('returns the matching bill run', async () => {
const results = await FetchMatchingBillRunService.go(regionId, 'two_part_tariff', 2022, true)

expect(results[0].id).to.equal(matchingSummerBillRunId)
})
})
})

describe("and 'summer' is set to false", () => {
describe('and a bill run for the same financial year exists', () => {
it('returns the matching bill run', async () => {
const results = await FetchMatchingBillRunService.go(regionId, 'two_part_tariff', 2022, true)
const results = await FetchMatchingBillRunService.go(regionId, 'two_part_tariff', 2022, false)

expect(results[0].id).to.equal(matchingBillRunId)
expect(results[0].id).to.equal(matchingWinterBillRunId)
})
})
})
Expand Down
Loading