diff --git a/app/controllers/bill-runs-setup.controller.js b/app/controllers/bill-runs-setup.controller.js index c256b1c6f7..16df181180 100644 --- a/app/controllers/bill-runs-setup.controller.js +++ b/app/controllers/bill-runs-setup.controller.js @@ -10,6 +10,7 @@ const Boom = require('@hapi/boom') const CreateService = require('../services/bill-runs/setup/create.service.js') const ExistsService = require('../services/bill-runs/setup/exists.service.js') const InitiateSessionService = require('../services/bill-runs/setup/initiate-session.service.js') +const NoLicencesService = require('../services/bill-runs/setup/no-licences.service.js') const RegionService = require('../services/bill-runs/setup/region.service.js') const SeasonService = require('../services/bill-runs/setup/season.service.js') const SubmitRegionService = require('../services/bill-runs/setup/submit-region.service.js') @@ -44,6 +45,18 @@ async function create (request, h) { } } +async function noLicences (request, h) { + const { sessionId } = request.params + + const regionName = await NoLicencesService.go(sessionId) + + return h.view('bill-runs/setup/no-licences.njk', { + activeNavBar: 'bill-runs', + pageTitle: `There are no licences marked for two-part tariff supplementary billing in the ${regionName} region`, + sessionId + }) +} + async function region (request, h) { const { sessionId } = request.params @@ -139,6 +152,12 @@ async function submitYear (request, h) { }) } + // Temporary code to end the journey if the bill run type is two-part supplementary as processing this bill run type + // is not currently possible + if (pageData.goBackToBillRuns) { + return h.redirect('/system/bill-runs') + } + if (pageData.setupComplete) { return h.redirect(`/system/bill-runs/setup/${sessionId}/create`) } @@ -163,6 +182,10 @@ async function year (request, h) { const pageData = await YearService.go(sessionId) + if (pageData.financialYearsData.length === 0) { + return h.redirect(`/system/bill-runs/setup/${sessionId}/no-licences`) + } + return h.view('bill-runs/setup/year.njk', { activeNavBar: 'bill-runs', pageTitle: 'Select the financial year', @@ -172,6 +195,7 @@ async function year (request, h) { module.exports = { create, + noLicences, region, season, setup, diff --git a/app/presenters/bill-runs/setup/year.presenter.js b/app/presenters/bill-runs/setup/year.presenter.js index 616842e245..19eb1cb7c3 100644 --- a/app/presenters/bill-runs/setup/year.presenter.js +++ b/app/presenters/bill-runs/setup/year.presenter.js @@ -2,21 +2,64 @@ /** * Formats data for the `/bill-runs/setup/{sessionId}/year` page - * @module RegionPresenter + * @module YearPresenter */ /** * Formats data for the `/bill-runs/setup/{sessionId}/year` page * + * @param {module:LicenceSupplementaryYearModel} licenceSupplementaryYears - An array of distinct `financialYearEnd` + * years flagged for supplementary billing for the selected region and bill run type * @param {module:SessionModel} session - The session instance to format * * @returns {object} - The data formatted for the view template */ -function go (session) { +function go (licenceSupplementaryYears, session) { + const selectedYear = session.year ? session.year : null + + let financialYearsData = [] + + // Currently for Two-part tariff Annual the financial years are hardcoded. This is because the Annual billing process + // has not been available to be run for several years. Once caught up the annual two-part tariff will only be run for + // a single year and this temporary code can be removed. + if (session.type === 'two_part_tariff') { + financialYearsData = _tptAnnualFinancialYearsData(selectedYear) + } else { + financialYearsData = _financialYearsData(licenceSupplementaryYears, selectedYear) + } + return { + financialYearsData, sessionId: session.id, - selectedYear: session.year ? session.year : null + selectedYear + } +} + +function _financialYearsData (licenceSupplementaryYears, selectedYear) { + const financialYearsData = [] + + if (licenceSupplementaryYears.length > 0) { + licenceSupplementaryYears.forEach((licenceSupplementaryYear) => { + const { financialYearEnd } = licenceSupplementaryYear + + financialYearsData.push({ + text: `${financialYearEnd - 1} to ${financialYearEnd}`, + value: financialYearEnd, + checked: parseInt(selectedYear) === financialYearEnd + }) + }) } + + return financialYearsData +} + +function _tptAnnualFinancialYearsData (selectedYear) { + return [ + { text: '2023 to 2024', value: 2024, checked: selectedYear === '2024' }, + { text: '2022 to 2023', value: 2023, checked: selectedYear === '2023' }, + { text: '2021 to 2022', value: 2022, checked: selectedYear === '2022' }, + { text: '2020 to 2021', value: 2021, checked: selectedYear === '2021' } + ] } module.exports = { diff --git a/app/routes/bill-runs-setup.routes.js b/app/routes/bill-runs-setup.routes.js index f266f32a41..05ab9cf4c9 100644 --- a/app/routes/bill-runs-setup.routes.js +++ b/app/routes/bill-runs-setup.routes.js @@ -15,6 +15,18 @@ const routes = [ } } }, + { + method: 'GET', + path: '/bill-runs/setup/{sessionId}/no-licences', + options: { + handler: BillRunsSetupController.noLicences, + auth: { + access: { + scope: ['billing'] + } + } + } + }, { method: 'GET', path: '/bill-runs/setup/{sessionId}/region', diff --git a/app/services/bill-runs/setup/fetch-licence-supplementary-years.service.js b/app/services/bill-runs/setup/fetch-licence-supplementary-years.service.js new file mode 100644 index 0000000000..32dd2e8e4c --- /dev/null +++ b/app/services/bill-runs/setup/fetch-licence-supplementary-years.service.js @@ -0,0 +1,29 @@ +'use strict' + +/** + * Fetches the years that have licences flagged for supplementary billing for the given region + * @module FetchLicenceSupplementaryYearsService + */ + +const LicenceSupplementaryYearModel = require('../../../models/licence-supplementary-year.model.js') + +/** + * Fetches the years that have licences flagged for supplementary billing for the given region + * + * @param {string} regionId - The UUID for the region + * @param {boolean} twoPartTariff - Whether the supplementary billing is for two-part tariff + * + * @returns {Promise} An array of distinct years flagged for supplementary billing in descending order + */ +async function go (regionId, twoPartTariff) { + return LicenceSupplementaryYearModel.query() + .distinct('financialYearEnd') + .innerJoinRelated('licence') + .where('twoPartTariff', twoPartTariff) + .where('regionId', regionId) + .orderBy('financialYearEnd', 'desc') +} + +module.exports = { + go +} diff --git a/app/services/bill-runs/setup/no-licences.service.js b/app/services/bill-runs/setup/no-licences.service.js new file mode 100644 index 0000000000..8ac9e5f63e --- /dev/null +++ b/app/services/bill-runs/setup/no-licences.service.js @@ -0,0 +1,30 @@ +'use strict' + +/** + * Handles fetching the region name for `/bill-runs/setup/{sessionId}/no-licences` page + * @module NoLicencesService + */ + +const RegionModel = require('../../../models/region.model.js') +const SessionModel = require('../../../models/session.model.js') + +/** + * Handles fetching the region name for `/bill-runs/setup/{sessionId}/no-licences` page + * + * Supports generating the data needed for the no-licences page in the setup bill run journey. It fetches the regionId + * from the session record and uses this to look up the display name for the region. + * + * @param {string} sessionId - The UUID for setup bill run session record + * + * @returns {Promise} The display name of the region + */ +async function go (sessionId) { + const { region: regionId } = await SessionModel.query().findById(sessionId) + const { displayName: regionName } = await RegionModel.query().findById(regionId).select('displayName') + + return regionName +} + +module.exports = { + go +} diff --git a/app/services/bill-runs/setup/submit-region.service.js b/app/services/bill-runs/setup/submit-region.service.js index afcd55640c..8e7183d32a 100644 --- a/app/services/bill-runs/setup/submit-region.service.js +++ b/app/services/bill-runs/setup/submit-region.service.js @@ -39,16 +39,6 @@ async function go (sessionId, payload) { if (!validationResult) { await _save(session, payload) - // Temporary if statement to end the journey if the bill run is for two-part tariff supplementary - if (session.type === 'two_part_supplementary') { - const temporaryFormattedData = RegionPresenter.go(session, regions) - - return { - error: { text: 'Currently you can progress no further for a two-part tariff supplementary bill run' }, - ...temporaryFormattedData - } - } - // The journey is complete (we don't need any details) if the bill run type is not 2PT return { setupComplete: !session.type.startsWith('two_part') } } diff --git a/app/services/bill-runs/setup/submit-year.service.js b/app/services/bill-runs/setup/submit-year.service.js index ef4cd1e66a..cd05fa24b3 100644 --- a/app/services/bill-runs/setup/submit-year.service.js +++ b/app/services/bill-runs/setup/submit-year.service.js @@ -5,6 +5,7 @@ * @module SubmitYearService */ +const FetchLicenceSupplementaryYearsService = require('./fetch-licence-supplementary-years.service.js') const SessionModel = require('../../../models/session.model.js') const YearPresenter = require('../../../presenters/bill-runs/setup/year.presenter.js') const YearValidator = require('../../../validators/bill-runs/setup/year.validator.js') @@ -37,10 +38,20 @@ async function go (sessionId, payload) { if (!validationResult) { await _save(session, payload) + // Temporary code to end the journey if the bill run type is two-part supplementary as processing this bill run type + // is not currently possible + if (session.type === 'two_part_supplementary') { + return { goBackToBillRuns: true } + } + return { setupComplete: ['2024', '2023'].includes(session.year) } } - const formattedData = YearPresenter.go(session) + const regionId = session.region + const twoPartTariffSupplementary = session.type === 'two_part_supplementary' + const licenceSupplementaryYears = await FetchLicenceSupplementaryYearsService.go(regionId, twoPartTariffSupplementary) + + const formattedData = YearPresenter.go(licenceSupplementaryYears, session) return { error: validationResult, diff --git a/app/services/bill-runs/setup/year.service.js b/app/services/bill-runs/setup/year.service.js index 0577dca3cc..2250246213 100644 --- a/app/services/bill-runs/setup/year.service.js +++ b/app/services/bill-runs/setup/year.service.js @@ -5,8 +5,9 @@ * @module YearService */ -const YearPresenter = require('../../../presenters/bill-runs/setup/year.presenter.js') +const FetchLicenceSupplementaryYearsService = require('./fetch-licence-supplementary-years.service.js') const SessionModel = require('../../../models/session.model.js') +const YearPresenter = require('../../../presenters/bill-runs/setup/year.presenter.js') /** * Orchestrates fetching and presenting the data for `/bill-runs/setup/{sessionId}/year` page @@ -20,7 +21,12 @@ const SessionModel = require('../../../models/session.model.js') */ async function go (sessionId) { const session = await SessionModel.query().findById(sessionId) - const formattedData = YearPresenter.go(session) + + const regionId = session.region + const twoPartTariffSupplementary = session.type === 'two_part_supplementary' + const licenceSupplementaryYears = await FetchLicenceSupplementaryYearsService.go(regionId, twoPartTariffSupplementary) + + const formattedData = YearPresenter.go(licenceSupplementaryYears, session) return { ...formattedData diff --git a/app/views/bill-runs/setup/no-licences.njk b/app/views/bill-runs/setup/no-licences.njk new file mode 100644 index 0000000000..2fe0b6a0a8 --- /dev/null +++ b/app/views/bill-runs/setup/no-licences.njk @@ -0,0 +1,29 @@ +{% extends 'layout.njk' %} +{% from "govuk/components/back-link/macro.njk" import govukBackLink %} +{% from "govuk/components/warning-text/macro.njk" import govukWarningText %} + +{% block breadcrumbs %} + {# Back link #} + {{ + govukBackLink({ + text: 'Back', + href: '/system/bill-runs/setup/' + sessionId + '/region' + }) + }} +{% endblock %} + +{% block content %} + {# Main heading #} +
+

{{ pageTitle }}

+
+ + {{ govukWarningText({ + text: 'Check there are licences ready to be billed and try again.', + iconFallbackText: 'Warning' + }) }} + +

+ Return to bill runs +

+{% endblock %} diff --git a/app/views/bill-runs/setup/year.njk b/app/views/bill-runs/setup/year.njk index 0e016f77a3..685edc40b3 100644 --- a/app/views/bill-runs/setup/year.njk +++ b/app/views/bill-runs/setup/year.njk @@ -44,28 +44,7 @@ classes: 'govuk-fieldset__legend--l govuk-!-margin-bottom-6' } }, - items: [ - { - text: '2023 to 2024', - value: '2024', - checked: '2024' == selectedYear - }, - { - text: '2022 to 2023', - value: '2023', - checked: '2023' == selectedYear - }, - { - text: '2021 to 2022', - value: '2022', - checked: '2022' == selectedYear - }, - { - text: '2020 to 2021', - value: '2021', - checked: '2021' == selectedYear - } - ] + items: financialYearsData }) }} {{ govukButton({ text: 'Continue', preventDoubleClick: true }) }} diff --git a/test/controllers/bill-runs-setup.controller.test.js b/test/controllers/bill-runs-setup.controller.test.js index a64d70e640..f7561aa132 100644 --- a/test/controllers/bill-runs-setup.controller.test.js +++ b/test/controllers/bill-runs-setup.controller.test.js @@ -15,6 +15,7 @@ const { postRequestOptions } = require('../support/general.js') const CreateService = require('../../app/services/bill-runs/setup/create.service.js') const ExistsService = require('../../app/services/bill-runs/setup/exists.service.js') const InitiateSessionService = require('../../app/services/bill-runs/setup/initiate-session.service.js') +const NoLicencesService = require('../../app/services/bill-runs/setup/no-licences.service.js') const RegionService = require('../../app/services/bill-runs/setup/region.service.js') const SeasonService = require('../../app/services/bill-runs/setup/season.service.js') const SubmitRegionService = require('../../app/services/bill-runs/setup/submit-region.service.js') @@ -122,6 +123,25 @@ describe('Bill Runs Setup controller', () => { }) }) + describe('/bill-runs/setup/{sessionId}/no-licences', () => { + describe('GET', () => { + beforeEach(async () => { + options = _getOptions('no-licences') + + Sinon.stub(NoLicencesService, 'go').resolves('Test') + }) + + describe('when the request succeeds', () => { + it('returns the page successfully', async () => { + const response = await server.inject(options) + + expect(response.statusCode).to.equal(200) + expect(response.payload).to.contain('There are no licences marked for two-part tariff supplementary billing in the Test region') + }) + }) + }) + }) + describe('/bill-runs/setup/{sessionId}/region', () => { describe('GET', () => { beforeEach(async () => { @@ -325,14 +345,23 @@ describe('Bill Runs Setup controller', () => { describe('GET', () => { beforeEach(async () => { options = _getOptions('year') + }) - Sinon.stub(YearService, 'go').resolves({ - sessionId: 'e009b394-8405-4358-86af-1a9eb31298a5', - selectedYear: null + describe('when the request succeeds with at least 1 year to display', () => { + beforeEach(async () => { + Sinon.stub(YearService, 'go').resolves({ + financialYearsData: [ + { + text: '2023 to 2024', + value: 2024, + checked: false + } + ], + sessionId: 'e009b394-8405-4358-86af-1a9eb31298a5', + selectedYear: null + }) }) - }) - describe('when the request succeeds', () => { it('returns the page successfully', async () => { const response = await server.inject(options) @@ -340,6 +369,23 @@ describe('Bill Runs Setup controller', () => { expect(response.payload).to.contain('Select the financial year') }) }) + + describe('when the request succeeds with no years to display', () => { + beforeEach(async () => { + Sinon.stub(YearService, 'go').resolves({ + financialYearsData: [], + sessionId: 'e009b394-8405-4358-86af-1a9eb31298a5', + selectedYear: null + }) + }) + + it('redirects to the no licences endpoint', async () => { + const response = await server.inject(options) + + expect(response.statusCode).to.equal(302) + expect(response.headers.location).to.equal('/system/bill-runs/setup/e009b394-8405-4358-86af-1a9eb31298a5/no-licences') + }) + }) }) describe('POST', () => { diff --git a/test/presenters/bill-runs/setup/year.presenter.test.js b/test/presenters/bill-runs/setup/year.presenter.test.js index 53ba68575d..381b4776ae 100644 --- a/test/presenters/bill-runs/setup/year.presenter.test.js +++ b/test/presenters/bill-runs/setup/year.presenter.test.js @@ -11,21 +11,36 @@ const { expect } = Code const YearPresenter = require('../../../../app/presenters/bill-runs/setup/year.presenter.js') describe('Bill Runs Setup Year presenter', () => { + let licenceSupplementaryYears let session - describe('when provided with a bill run setup session record', () => { + describe('when provided with a bill run setup session record for two-part tariff supplementary', () => { beforeEach(() => { + licenceSupplementaryYears = [{ financialYearEnd: 2024 }, { financialYearEnd: 2022 }] session = { id: '98ad3a1f-8e4f-490a-be05-0aece6755466', - data: {} + data: {}, + type: 'two_part_supplementary' } }) describe('where the user has not previously selected a financial year', () => { it('correctly presents the data', () => { - const result = YearPresenter.go(session) + const result = YearPresenter.go(licenceSupplementaryYears, session) expect(result).to.equal({ + financialYearsData: [ + { + text: '2023 to 2024', + value: 2024, + checked: false + }, + { + text: '2021 to 2022', + value: 2022, + checked: false + } + ], sessionId: '98ad3a1f-8e4f-490a-be05-0aece6755466', selectedYear: null }) @@ -34,17 +49,92 @@ describe('Bill Runs Setup Year presenter', () => { describe('where the user has previously selected a financial year', () => { beforeEach(() => { - session.year = 2022 + session.year = '2022' }) it('correctly presents the data', () => { - const result = YearPresenter.go(session) + const result = YearPresenter.go(licenceSupplementaryYears, session) expect(result).to.equal({ + financialYearsData: [ + { + text: '2023 to 2024', + value: 2024, + checked: false + }, + { + text: '2021 to 2022', + value: 2022, + checked: true + } + ], sessionId: '98ad3a1f-8e4f-490a-be05-0aece6755466', - selectedYear: 2022 + selectedYear: '2022' + }) + }) + }) + }) + + describe('when provided with a bill run setup session record for two-part tariff annual', () => { + beforeEach(() => { + session = { + id: '98ad3a1f-8e4f-490a-be05-0aece6755466', + data: {}, + type: 'two_part_tariff' + } + }) + + describe('where the user has not previously selected a financial year', () => { + it('correctly presents the data', () => { + const result = YearPresenter.go(licenceSupplementaryYears, session) + + expect(result).to.equal({ + financialYearsData: _financialYearsData(null), + sessionId: '98ad3a1f-8e4f-490a-be05-0aece6755466', + selectedYear: null + }) + }) + }) + + describe('where the user has previously selected a financial year', () => { + beforeEach(() => { + session.year = '2022' + }) + + it('correctly presents the data', () => { + const result = YearPresenter.go(licenceSupplementaryYears, session) + + expect(result).to.equal({ + financialYearsData: _financialYearsData('2022'), + sessionId: '98ad3a1f-8e4f-490a-be05-0aece6755466', + selectedYear: '2022' }) }) }) }) }) + +function _financialYearsData (selectedYear) { + return [ + { + text: '2023 to 2024', + value: 2024, + checked: selectedYear === '2024' + }, + { + text: '2022 to 2023', + value: 2023, + checked: selectedYear === '2023' + }, + { + text: '2021 to 2022', + value: 2022, + checked: selectedYear === '2022' + }, + { + text: '2020 to 2021', + value: 2021, + checked: selectedYear === '2021' + } + ] +} diff --git a/test/services/bill-runs/setup/fetch-licence-supplementary-years.service.test.js b/test/services/bill-runs/setup/fetch-licence-supplementary-years.service.test.js new file mode 100644 index 0000000000..0e0371eea3 --- /dev/null +++ b/test/services/bill-runs/setup/fetch-licence-supplementary-years.service.test.js @@ -0,0 +1,56 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') + +const { describe, it, beforeEach } = exports.lab = Lab.script() +const { expect } = Code + +// Test helpers +const LicenceHelper = require('../../../support/helpers/licence.helper.js') +const LicenceSupplementaryYearHelper = require('../../../support/helpers/licence-supplementary-year.helper.js') + +// Thing under test +const LicenceSupplementaryYearModel = require('../../../../app/services/bill-runs/setup/fetch-licence-supplementary-years.service.js') + +describe('Fetch Licence Supplementary Years service', () => { + let regionId + let twoPartTariff + + describe('when provided with data that will return years selected for supplementary billing', () => { + beforeEach(async () => { + const licence = await LicenceHelper.add() + + regionId = licence.regionId + twoPartTariff = true + + await LicenceSupplementaryYearHelper.add({ licenceId: licence.id, financialYearEnd: 2023, twoPartTariff: true }) + await LicenceSupplementaryYearHelper.add({ licenceId: licence.id, financialYearEnd: 2024, twoPartTariff: true }) + await LicenceSupplementaryYearHelper.add({ licenceId: licence.id, financialYearEnd: 2022, twoPartTariff: true }) + }) + + it('returns an array of the years selected for supplementary billing', async () => { + const result = await LicenceSupplementaryYearModel.go(regionId, twoPartTariff) + + expect(result).to.equal([{ financialYearEnd: 2024 }, { financialYearEnd: 2023 }, { financialYearEnd: 2022 }]) + }) + }) + + describe('when provided with data that will not return any years for supplementary billing', () => { + beforeEach(async () => { + const licence = await LicenceHelper.add() + + regionId = licence.regionId + twoPartTariff = false + + await LicenceSupplementaryYearHelper.add({ licenceId: licence.id, financialYearEnd: 2022, twoPartTariff: true }) + }) + + it('returns an empty array', async () => { + const result = await LicenceSupplementaryYearModel.go(regionId, twoPartTariff) + + expect(result).to.equal([]) + }) + }) +}) diff --git a/test/services/bill-runs/setup/no-licences.service.test.js b/test/services/bill-runs/setup/no-licences.service.test.js new file mode 100644 index 0000000000..22e389d856 --- /dev/null +++ b/test/services/bill-runs/setup/no-licences.service.test.js @@ -0,0 +1,34 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') + +const { describe, it, beforeEach } = exports.lab = Lab.script() +const { expect } = Code + +// Test helpers +const RegionHelper = require('../../../support/helpers/region.helper.js') +const SessionHelper = require('../../../support/helpers/session.helper.js') + +// Thing under test +const NoLicencesService = require('../../../../app/services/bill-runs/setup/no-licences.service.js') + +describe('Bill Runs Setup No Licences service', () => { + let sessionId + + describe('when called with a valid session id', () => { + beforeEach(async () => { + const region = RegionHelper.select(RegionHelper.TEST_REGION_INDEX) + const session = await SessionHelper.add({ data: { region: region.id } }) + + sessionId = session.id + }) + + it('returns the regions display name', async () => { + const result = await NoLicencesService.go(sessionId) + + expect(result).to.equal('Test Region') + }) + }) +}) diff --git a/test/services/bill-runs/setup/submit-region.service.test.js b/test/services/bill-runs/setup/submit-region.service.test.js index 9c14d197ae..1d877e9ba7 100644 --- a/test/services/bill-runs/setup/submit-region.service.test.js +++ b/test/services/bill-runs/setup/submit-region.service.test.js @@ -80,17 +80,13 @@ describe('Bill Runs Setup Submit Region service', () => { session = await SessionHelper.add({ data: { type: 'two_part_supplementary' } }) }) - it('returns page data needed to re-render the view including the error', async () => { + it('saves the submitted value and returns an object confirming setup is not complete', async () => { const result = await SubmitRegionService.go(session.id, payload) - expect(result).to.equal({ - sessionId: session.id, - regions, - selectedRegion: payload.region, - error: { - text: 'Currently you can progress no further for a two-part tariff supplementary bill run' - } - }) + const refreshedSession = await session.$query() + + expect(refreshedSession.region).to.equal(region.id) + expect(result.setupComplete).to.be.false() }) }) }) diff --git a/test/services/bill-runs/setup/submit-year.service.test.js b/test/services/bill-runs/setup/submit-year.service.test.js index 8d16cf37fd..cb32dcb07b 100644 --- a/test/services/bill-runs/setup/submit-year.service.test.js +++ b/test/services/bill-runs/setup/submit-year.service.test.js @@ -3,6 +3,7 @@ // Test framework dependencies const Lab = require('@hapi/lab') const Code = require('@hapi/code') +const Sinon = require('sinon') const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code @@ -10,6 +11,9 @@ const { expect } = Code // Test helpers const SessionHelper = require('../../../support/helpers/session.helper.js') +// Things we need to stub +const FetchLicenceSupplementaryYearsService = require('../../../../app/services/bill-runs/setup/fetch-licence-supplementary-years.service.js') + // Thing under test const SubmitYearService = require('../../../../app/services/bill-runs/setup/submit-year.service.js') @@ -56,18 +60,45 @@ describe('Bill Runs Setup Submit Year service', () => { expect(result.setupComplete).to.be.false() }) }) + + describe('and the type is two-part tariff supplementary', () => { + beforeEach(async () => { + payload = { + year: '2023' + } + session = await SessionHelper.add({ data: { type: 'two_part_supplementary' } }) + }) + + it('saves the submitted value and returns an object to redirect the user to the Bill Runs page', async () => { + const result = await SubmitYearService.go(session.id, payload) + + const refreshedSession = await session.$query() + + expect(refreshedSession.year).to.equal('2023') + expect(result.goBackToBillRuns).to.be.true() + }) + }) }) describe('with an invalid payload', () => { describe('because the user has not selected anything', () => { + const regionId = 'cff057a0-f3a7-4ae6-bc2b-01183e40fd05' + + let yearsStub + beforeEach(async () => { + session = await SessionHelper.add({ data: { region: regionId, type: 'two_part_supplementary' } }) payload = {} + yearsStub = Sinon.stub(FetchLicenceSupplementaryYearsService, 'go').resolves([{ financialYearEnd: 2024 }]) }) it('returns page data needed to re-render the view including the validation error', async () => { const result = await SubmitYearService.go(session.id, payload) + expect(yearsStub.calledWith(regionId, true)).to.be.true() + expect(result).to.equal({ + financialYearsData: [{ text: '2023 to 2024', value: 2024, checked: false }], sessionId: session.id, selectedYear: null, error: { diff --git a/test/services/bill-runs/setup/year.service.test.js b/test/services/bill-runs/setup/year.service.test.js index 67c1792412..a92e9bd92d 100644 --- a/test/services/bill-runs/setup/year.service.test.js +++ b/test/services/bill-runs/setup/year.service.test.js @@ -3,6 +3,7 @@ // Test framework dependencies const Lab = require('@hapi/lab') const Code = require('@hapi/code') +const Sinon = require('sinon') const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code @@ -10,21 +11,32 @@ const { expect } = Code // Test helpers const SessionHelper = require('../../../support/helpers/session.helper.js') +// Things we need to stub +const FetchLicenceSupplementaryYearsService = require('../../../../app/services/bill-runs/setup/fetch-licence-supplementary-years.service.js') + // Thing under test const YearService = require('../../../../app/services/bill-runs/setup/year.service.js') describe('Bill Runs Setup Year service', () => { + const regionId = 'cff057a0-f3a7-4ae6-bc2b-01183e40fd05' + let session + let yearsStub beforeEach(async () => { - session = await SessionHelper.add({ data: { year: 2024 } }) + session = await SessionHelper.add({ data: { region: regionId, type: 'two_part_supplementary', year: 2024 } }) + + yearsStub = Sinon.stub(FetchLicenceSupplementaryYearsService, 'go').resolves([{ financialYearEnd: 2024 }]) }) describe('when called', () => { it('returns page data for the view', async () => { const result = await YearService.go(session.id) + expect(yearsStub.calledWith(regionId, true)).to.be.true() + expect(result).to.equal({ + financialYearsData: [{ text: '2023 to 2024', value: 2024, checked: true }], sessionId: session.id, selectedYear: 2024 })