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 32 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
49 changes: 46 additions & 3 deletions app/presenters/bill-runs/setup/year.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
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
Original file line number Diff line number Diff line change
@@ -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<object[]>} 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
}
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
13 changes: 12 additions & 1 deletion app/services/bill-runs/setup/submit-year.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 twoPartTariff = session.type.startsWith('two_part')
Jozzey marked this conversation as resolved.
Show resolved Hide resolved
const licenceSupplementaryYears = await FetchLicenceSupplementaryYearsService.go(regionId, twoPartTariff)

const formattedData = YearPresenter.go(licenceSupplementaryYears, session)

return {
error: validationResult,
Expand Down
8 changes: 7 additions & 1 deletion app/services/bill-runs/setup/year.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @module YearService
*/

const FetchLicenceSupplementaryYearsService = require('./fetch-licence-supplementary-years.service.js')
const YearPresenter = require('../../../presenters/bill-runs/setup/year.presenter.js')
const SessionModel = require('../../../models/session.model.js')
Jozzey marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -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 twoPartTariff = session.type.startsWith('two_part')
Jozzey marked this conversation as resolved.
Show resolved Hide resolved
const licenceSupplementaryYears = await FetchLicenceSupplementaryYearsService.go(regionId, twoPartTariff)
Jozzey marked this conversation as resolved.
Show resolved Hide resolved

const formattedData = YearPresenter.go(licenceSupplementaryYears, 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
Loading
Loading