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

Trigger 2PT Supplementary Bill Run - Pt2 #1262

Merged
merged 34 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e3cebbe
Trigger 2PT Supplementary Bill Run - Pt2
Jozzey Aug 16, 2024
addfa13
Remove temporary if statement
Jozzey Aug 16, 2024
e692403
Make the financial years dynamic - WIP
Jozzey Aug 16, 2024
3f9998d
Merge branch 'main' into trigger-tpt-supp-bill-run-pt2
Jozzey Aug 27, 2024
8236398
Add new page for when no TPT years exist
Jozzey Aug 28, 2024
46e9ce0
Make the years dynamic for TPT supplementary
Jozzey Aug 28, 2024
a3c1ac6
Add temp code to return to bill runs page if tpt supp
Jozzey Aug 28, 2024
6a51ed8
Merge branch 'main' into trigger-tpt-supp-bill-run-pt2
Jozzey Aug 28, 2024
c33f735
Merge branch 'main' into trigger-tpt-supp-bill-run-pt2
Jozzey Aug 29, 2024
2f15760
Add and fix controller tests
Jozzey Aug 29, 2024
a4562dd
Add and fix year presenter tests
Jozzey Aug 29, 2024
2f5ab96
Tidy up
Jozzey Aug 29, 2024
6984fd3
Create tests for no licences service
Jozzey Aug 29, 2024
e1f5767
Fix submit region service tests
Jozzey Aug 29, 2024
3f28571
Add and fix submit year service tests
Jozzey Aug 29, 2024
adb197d
Fix year service tests
Jozzey Aug 29, 2024
34c7b98
Tidy up
Jozzey Aug 29, 2024
3b1a219
Merge branch 'main' into trigger-tpt-supp-bill-run-pt2
Jozzey Aug 29, 2024
f21d951
Appease SonarCloud
Jozzey Aug 29, 2024
e842cf2
Convert `financialYearEnd` values to strings
Jozzey Aug 30, 2024
f26012a
Make function a bit easier to read
Jozzey Aug 30, 2024
11e3acd
Merge branch 'main' into trigger-tpt-supp-bill-run-pt2
Jozzey Aug 30, 2024
e258d9b
Fix "Commit suggestion" issues
Jozzey Aug 30, 2024
b2c0c60
Merge branch 'main' into trigger-tpt-supp-bill-run-pt2
Jozzey Sep 2, 2024
acfb66b
Create new service to determine supplementary years
Jozzey Sep 2, 2024
22a27ca
Update year presenter tests
Jozzey Sep 2, 2024
d1eb9f2
Tidy up
Jozzey Sep 2, 2024
7ce5ce3
Create unit tests for new service
Jozzey Sep 2, 2024
82d0c45
Fix year service tests
Jozzey Sep 2, 2024
c96568a
Fix year service tests
Jozzey Sep 2, 2024
36c08f6
Merge branch 'main' into trigger-tpt-supp-bill-run-pt2
Jozzey Sep 3, 2024
ab75ea9
Merge branch 'main' into trigger-tpt-supp-bill-run-pt2
Jozzey Sep 4, 2024
3eaaa20
Merge branch 'main' into trigger-tpt-supp-bill-run-pt2
Jozzey Sep 4, 2024
ae87adb
Update as per PR comments
Jozzey Sep 4, 2024
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
24 changes: 24 additions & 0 deletions app/controllers/bill-runs-setup.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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`)
}
Expand All @@ -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',
Expand All @@ -172,6 +195,7 @@ async function year (request, h) {

module.exports = {
create,
noLicences,
region,
season,
setup,
Expand Down
58 changes: 55 additions & 3 deletions app/presenters/bill-runs/setup/year.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,75 @@

/**
* Formats data for the `/bill-runs/setup/{sessionId}/year` page
* @module RegionPresenter
* @module YearPresenter
*/

const LicenceSupplementaryYearModel = require('../../../models/licence-supplementary-year.model.js')

/**
* Formats data for the `/bill-runs/setup/{sessionId}/year` page
*
* @param {module:SessionModel} session - The session instance to format
*
* @returns {object} - The data formatted for the view template
*/
function go (session) {
async function go (session) {
const selectedYear = session.year ? session.year : null

let financialYearsData = []

if (session.type === 'two_part_tariff') {
financialYearsData = _annualFinancialYearsData(selectedYear)
}

if (session.type === 'two_part_supplementary') {
financialYearsData = await _supplementaryFinancialYearsData(session.region, selectedYear)
}

return {
financialYearsData,
sessionId: session.id,
selectedYear: session.year ? session.year : null
selectedYear
}
}

function _annualFinancialYearsData (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' }
]
}

async function _supplementaryFinancialYearsData (regionId, selectedYear) {
const supplementaryFinancialYearsData = []
const tptSupplementaryYears = await _tptSupplementaryYears(regionId)

if (tptSupplementaryYears.length > 0) {
tptSupplementaryYears.forEach((tptSupplementaryYear) => {
Jozzey marked this conversation as resolved.
Show resolved Hide resolved
const { financialYearEnd } = tptSupplementaryYear

supplementaryFinancialYearsData.push({
text: `${financialYearEnd - 1} to ${financialYearEnd}`,
value: financialYearEnd.toString(),
checked: selectedYear === financialYearEnd.toString()
})
})
}

return supplementaryFinancialYearsData
}

async function _tptSupplementaryYears (regionId) {
Jozzey marked this conversation as resolved.
Show resolved Hide resolved
return LicenceSupplementaryYearModel.query()
Jozzey marked this conversation as resolved.
Show resolved Hide resolved
.distinct('financialYearEnd')
.innerJoinRelated('licence')
.where('twoPartTariff', true)
.where('regionId', regionId)
.orderBy('financialYearEnd', 'desc')
}

module.exports = {
go
}
12 changes: 12 additions & 0 deletions app/routes/bill-runs-setup.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
30 changes: 30 additions & 0 deletions app/services/bill-runs/setup/no-licences.service.js
Original file line number Diff line number Diff line change
@@ -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<string>} 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
}
10 changes: 0 additions & 10 deletions app/services/bill-runs/setup/submit-region.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') }
}
Expand Down
8 changes: 7 additions & 1 deletion app/services/bill-runs/setup/submit-year.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ 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 formattedData = await YearPresenter.go(session)

return {
error: validationResult,
Expand Down
2 changes: 1 addition & 1 deletion app/services/bill-runs/setup/year.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const SessionModel = require('../../../models/session.model.js')
*/
async function go (sessionId) {
const session = await SessionModel.query().findById(sessionId)
const formattedData = YearPresenter.go(session)
const formattedData = await YearPresenter.go(session)

return {
...formattedData
Expand Down
29 changes: 29 additions & 0 deletions app/views/bill-runs/setup/no-licences.njk
Original file line number Diff line number Diff line change
@@ -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 #}
<div class="govuk-body">
<h1 class="govuk-heading-l govuk-!-margin-bottom-3">{{ pageTitle }}</h1>
</div>

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

<p class="govuk-body">
<a href="/system/bill-runs" class="govuk-link">Return to bill runs</a>
</p>
{% endblock %}
23 changes: 1 addition & 22 deletions app/views/bill-runs/setup/year.njk
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) }}
Expand Down
56 changes: 51 additions & 5 deletions test/controllers/bill-runs-setup.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -325,21 +345,47 @@ 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)

expect(response.statusCode).to.equal(200)
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', () => {
Expand Down
Loading
Loading