Skip to content

Commit

Permalink
Remove Licence from bill 2PT bill run during review (#927)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4216

On the licence review page add a button for “remove from bill run”. When clicked you should be presented with a confirmation page with a confirm button, when this is clicked the licence should be removed from the current bill run.

Once the licence is removed the user should be returned to the bill run review page with a banner confirming the licence has been removed. When removed the licence will be flagged for two-part tariff supplementary billing.
  • Loading branch information
Jozzey authored May 3, 2024
1 parent 75fc994 commit 45912bc
Show file tree
Hide file tree
Showing 20 changed files with 848 additions and 15 deletions.
29 changes: 28 additions & 1 deletion app/controllers/bill-runs.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ const CancelBillRunService = require('../services/bill-runs/cancel-bill-run.serv
const CreateBillRunValidator = require('../validators/create-bill-run.validator.js')
const IndexBillRunsService = require('../services/bill-runs/index-bill-runs.service.js')
const MatchDetailsService = require('../services/bill-runs/two-part-tariff/match-details.service.js')
const RemoveBillRunLicenceService = require('../services/bill-runs/two-part-tariff/remove-bill-run-licence.service.js')
const ReviewBillRunService = require('../services/bill-runs/two-part-tariff/review-bill-run.service.js')
const ReviewLicenceService = require('../services/bill-runs/two-part-tariff/review-licence.service.js')
const SendBillRunService = require('../services/bill-runs/send-bill-run.service.js')
const StartBillRunProcessService = require('../services/bill-runs/start-bill-run-process.service.js')
const SubmitAmendedBillableReturnsService = require('..//services/bill-runs/two-part-tariff/submit-amended-billable-returns.service.js')
const SubmitCancelBillRunService = require('../services/bill-runs/submit-cancel-bill-run.service.js')
const SubmitRemoveBillRunLicenceService = require('../services/bill-runs/two-part-tariff/submit-remove-bill-run-licence.service.js')
const SubmitReviewLicenceService = require('../services/bill-runs/two-part-tariff/submit-review-licence.service.js')
const SubmitSendBillRunService = require('../services/bill-runs/submit-send-bill-run.service.js')
const ViewBillRunService = require('../services/bill-runs/view-bill-run.service.js')
Expand Down Expand Up @@ -86,9 +88,20 @@ async function matchDetails (request, h) {
})
}

async function removeLicence (request, h) {
const { id: billRunId, licenceId } = request.params

const pageData = await RemoveBillRunLicenceService.go(billRunId, licenceId)

return h.view('bill-runs/remove-licence.njk', {
activeNavBar: 'bill-runs',
...pageData
})
}

async function review (request, h) {
const { id } = request.params
const pageData = await ReviewBillRunService.go(id, request.payload)
const pageData = await ReviewBillRunService.go(id, request.payload, request.yar)

return h.view('bill-runs/review.njk', {
pageTitle: 'Review licences',
Expand Down Expand Up @@ -147,6 +160,18 @@ async function submitCancel (request, h) {
}
}

async function submitRemoveLicence (request, h) {
const { id: billRunId, licenceId } = request.params

const allLicencesRemoved = await SubmitRemoveBillRunLicenceService.go(billRunId, licenceId, request.yar)

if (allLicencesRemoved) {
return h.redirect('/system/bill-runs')
}

return h.redirect(`/system/bill-runs/${billRunId}/review`)
}

async function submitReviewLicence (request, h) {
const { id: billRunId, licenceId } = request.params

Expand Down Expand Up @@ -187,11 +212,13 @@ module.exports = {
create,
index,
matchDetails,
removeLicence,
review,
reviewLicence,
send,
submitAmendedBillableReturns,
submitCancel,
submitRemoveLicence,
submitReviewLicence,
submitSend,
view
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

/**
* Formats the data ready for presenting in the remove bill run licence confirmation page
* @module RemoveBillRunLicencePresenter
*/

const { formatFinancialYear, formatLongDate } = require('../../base.presenter.js')

/**
* Formats the data ready for presenting in the remove bill run licence confirmation page
*
* @param {module:BillRunModel} billRun - an instance of `BillRunModel`
* @param {string} licenceId - UUID of the licence to remove from the bill run
* @param {string} licenceRef - the licence reference of the licence to remove from the bill run
*
* @returns {Object} - the prepared data to be passed to the remove licence template
*/
function go (billRun, licenceId, licenceRef) {
const { billRunNumber, createdAt, region, status, toFinancialYearEnding } = billRun

return {
pageTitle: `You're about to remove ${licenceRef} from the bill run`,
backLink: `../review/${licenceId}`,
billRunNumber,
billRunStatus: status,
dateCreated: formatLongDate(createdAt),
financialYear: formatFinancialYear(toFinancialYearEnding),
region
}
}

module.exports = {
go
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function _financialYear (financialYearEnding) {
}

function _getIssueOnLicence (issues) {
// if there is more than one issue the issues will be seperated by a comma
// if there is more than one issue the issues will be separated by a comma
if (issues.includes(',')) {
return 'Multiple Issues'
}
Expand Down
28 changes: 27 additions & 1 deletion app/routes/bill-runs.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ const routes = [
description: 'Submit bill run cancellation'
}
},
{
method: 'GET',
path: '/bill-runs/{id}/remove/{licenceId}',
handler: BillRunsController.removeLicence,
options: {
auth: {
access: {
scope: ['billing']
}
},
description: 'Confirm removing a licence from a bill run'
}
},
{
method: 'POST',
path: '/bill-runs/{id}/remove/{licenceId}',
handler: BillRunsController.submitRemoveLicence,
options: {
auth: {
access: {
scope: ['billing']
}
},
description: 'Submit licence removal from a bill run'
}
},
{
method: 'GET',
path: '/bill-runs/{id}/review',
Expand All @@ -94,7 +120,7 @@ const routes = [
scope: ['billing']
}
},
description: 'POST request recieved when filtering applied to review two-part tariff match and allocation results'
description: 'POST request received when filtering applied to review two-part tariff match and allocation results'
}
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict'

/**
* Orchestrates fetching and presenting the data needed for the remove bill run licence confirmation page
* @module RemoveBillRunLicenceService
*/

const BillRunModel = require('../../../models/bill-run.model.js')
const LicenceModel = require('../../../models/licence.model.js')
const RemoveBillRunLicencePresenter = require('../../../presenters/bill-runs/two-part-tariff/remove-bill-run-licence.presenter.js')

/**
* Orchestrates fetching and presenting the data needed for the remove bill run licence confirmation page
*
* @param {string} billRunId - The UUID of the bill run that the licence is in
* @param {string} licenceId - UUID of the licence to remove from the bill run
*
* @returns {Promise<Object}> an object representing the `pageData` needed by the remove licence template. It contains
* details of the bill run & the licence reference.
*/
async function go (billRunId, licenceId) {
const billRun = await _fetchBillRun(billRunId)
const licenceRef = await _fetchLicenceRef(licenceId)

const pageData = RemoveBillRunLicencePresenter.go(billRun, licenceId, licenceRef)

return pageData
}

async function _fetchBillRun (billRunId) {
return BillRunModel.query()
.findById(billRunId)
.select(
'billRuns.billRunNumber',
'billRuns.createdAt',
'billRuns.status',
'billRuns.toFinancialYearEnding',
'region.displayName as region'
)
.innerJoinRelated('region')
}

async function _fetchLicenceRef (licenceId) {
const licence = await LicenceModel.query()
.findById(licenceId)
.select('licenceRef')

return licence.licenceRef
}

module.exports = {
go
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict'

/**
* Deletes all of the persisted data relating to the bill and licence from the review tables.
* @module RemoveReviewDataService
*/

const { db } = require('../../../../db/db.js')

/**
* Deletes all of the persisted data relating to the bill and licence from the review tables
*
* @param {string} billRunId - The UUID of the bill run that the licence is in
* @param {string} licenceId - UUID of the licence to remove from the review tables
*/
async function go (billRunId, licenceId) {
await _removeChargeElementReturns(billRunId, licenceId)
await _removeReturns(billRunId, licenceId)
await _removeChargeElements(billRunId, licenceId)
await _removeChargeReferences(billRunId, licenceId)
await _removeChargeVersions(billRunId, licenceId)
await _removeLicence(billRunId, licenceId)
}

async function _removeChargeElements (billRunId, licenceId) {
return db
.del()
.from('reviewChargeElements AS rce')
.innerJoin('reviewChargeReferences AS rcr', 'rce.reviewChargeReferenceId', 'rcr.id')
.innerJoin('reviewChargeVersions AS rcv', 'rcr.reviewChargeVersionId', 'rcv.id')
.innerJoin('reviewLicences AS rl', 'rcv.reviewLicenceId', 'rl.id')
.where('rl.billRunId', billRunId)
.andWhere('rl.licenceId', licenceId)
}

async function _removeChargeElementReturns (billRunId, licenceId) {
return db
.del()
.from('reviewChargeElementsReturns AS rcer')
.innerJoin('reviewChargeElements AS rce', 'rcer.reviewChargeElementId', 'rce.id')
.innerJoin('reviewChargeReferences AS rcr', 'rce.reviewChargeReferenceId', 'rcr.id')
.innerJoin('reviewChargeVersions AS rcv', 'rcr.reviewChargeVersionId', 'rcv.id')
.innerJoin('reviewLicences AS rl', 'rcv.reviewLicenceId', 'rl.id')
.where('rl.billRunId', billRunId)
.andWhere('rl.licenceId', licenceId)
}

async function _removeChargeReferences (billRunId, licenceId) {
return db
.del()
.from('reviewChargeReferences AS rcr')
.innerJoin('reviewChargeVersions AS rcv', 'rcr.reviewChargeVersionId', 'rcv.id')
.innerJoin('reviewLicences AS rl', 'rcv.reviewLicenceId', 'rl.id')
.where('rl.billRunId', billRunId)
.andWhere('rl.licenceId', licenceId)
}

async function _removeChargeVersions (billRunId, licenceId) {
return db
.del()
.from('reviewChargeVersions AS rcv')
.innerJoin('reviewLicences AS rl', 'rcv.reviewLicenceId', 'rl.id')
.where('rl.billRunId', billRunId)
.andWhere('rl.licenceId', licenceId)
}

async function _removeLicence (billRunId, licenceId) {
return db
.del()
.from('reviewLicences')
.where('billRunId', billRunId)
.andWhere('licenceId', licenceId)
}

async function _removeReturns (billRunId, licenceId) {
return db
.del()
.from('reviewReturns AS rr')
.innerJoin('reviewLicences AS rl', 'rr.reviewLicenceId', 'rl.id')
.where('rl.billRunId', billRunId)
.andWhere('rl.licenceId', licenceId)
}

module.exports = {
go
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ const ReviewBillRunPresenter = require('../../../presenters/bill-runs/two-part-t
* @param {String} id The UUID for the bill run to review
* @param {Object} payload The `request.payload` containing the filter data. This is only passed to the service when
* there is a POST request, which only occurs when a filter is applied to the results.
* @param {Object} yar - The Hapi `request.yar` session manager passed on by the controller
*
* @returns {Promise<Object>} An object representing the `pageData` needed by the review bill run template. It contains
* details of the bill run and the licences linked to it as well as any data that has been used to filter the results.
*/
async function go (id, payload) {
async function go (id, payload, yar) {
const filterIssues = payload?.filterIssues
const filterLicenceHolder = payload?.filterLicenceHolder
const filterLicenceStatus = payload?.filterLicenceStatus
Expand All @@ -30,9 +31,13 @@ async function go (id, payload) {
filterLicenceStatus
)

const [bannerMessage] = yar.flash('banner')
const pageData = ReviewBillRunPresenter.go(billRun, filterIssues, filterLicenceHolder, filterLicenceStatus, licences)

return pageData
return {
bannerMessage,
...pageData
}
}

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const ReviewLicencePresenter = require('../../../presenters/bill-runs/two-part-t
*
* @param {module:BillRunModel} billRunId - The UUID for the bill run
* @param {module:LicenceModel} licenceId - The UUID of the licence that is being reviewed
* @param {Object} sessionManager - The Hapi `request.yar` session manager passed on by the controller
* @param {Object} yar - The Hapi `request.yar` session manager passed on by the controller
*
* @returns {Promise<Object>} the 'pageData' needed for the review licence page. It contains the licence, bill run,
* matched and unmatched returns and the licence charge data
Expand Down
Loading

0 comments on commit 45912bc

Please sign in to comment.