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

Two-part tariff review set adjustment factors #987

Merged
merged 33 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
bfeb1ab
Two-part tariff review set adjustment factors
Beckyrose200 May 7, 2024
3866f2f
WIP
Beckyrose200 May 8, 2024
0b379de
Merge branch 'main' into set-adjustment-factors
Beckyrose200 May 8, 2024
f0fb967
Add amendAdjustment route and controller
Beckyrose200 May 8, 2024
2934cbe
Add amendAdjustmentFactor service
Beckyrose200 May 8, 2024
fed9da6
Add amendAdjustmentFactorPresenter
Beckyrose200 May 8, 2024
0ab70cf
Alter chargeReferenceDetails presenter to pass chargeReference Id
Beckyrose200 May 8, 2024
d4d89f7
Amend chargeReferenceDetails view to support new route
Beckyrose200 May 8, 2024
00fe6c6
Build amendAdjustmentFactor view
Beckyrose200 May 8, 2024
8032a3a
Merge branch 'main' into set-adjustment-factors
Beckyrose200 May 8, 2024
9f673e9
Fix the chargeReferenceDetails presenter test
Beckyrose200 May 8, 2024
aa41ff5
Add validation and POST request to the page
Beckyrose200 May 10, 2024
5006f3a
Merge branch 'main' into set-adjustment-factors
Beckyrose200 May 10, 2024
a27e822
Add tests to bill run controller
Beckyrose200 May 10, 2024
c9972e7
Add test service for the amend adjustment factor service
Beckyrose200 May 10, 2024
63770f3
Add yar banner test to chargeReferenceDetailsService tests
Beckyrose200 May 10, 2024
76b3ebb
Add SubmitAmendedAdjustmentFactorService unit tests
Beckyrose200 May 10, 2024
21be206
Add AmendAdjustmentFactorPresenter tests
Beckyrose200 May 10, 2024
105acdb
AdjustmentFactorValidator unit tests
Beckyrose200 May 10, 2024
d721566
Refactor validator
Beckyrose200 May 10, 2024
7da6f18
Refactor validator
Beckyrose200 May 10, 2024
2521d1e
Refactor code
Beckyrose200 May 10, 2024
9a1530d
Add more unit tests to the amend adjustment presenter
Beckyrose200 May 10, 2024
f9ac3b2
DOH! Remove .only
Beckyrose200 May 10, 2024
7afbb22
Fix unit tests
Beckyrose200 May 10, 2024
39852fd
Refactor submit amended adjustment factor service
Beckyrose200 May 10, 2024
e56ce81
Update comment
Beckyrose200 May 10, 2024
f1be39a
Update comments and test blocks
Beckyrose200 May 10, 2024
5292e03
Sort out merge conflicts
Jozzey May 14, 2024
a6fc2e5
Merge branch 'main' into set-adjustment-factors
Jozzey May 14, 2024
d0c0c90
Tidy up validator
Jozzey May 15, 2024
0823dce
Merge branch 'main' into set-adjustment-factors
Jozzey May 15, 2024
a44fafd
Merge branch 'main' into set-adjustment-factors
Jozzey May 15, 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
33 changes: 33 additions & 0 deletions app/controllers/bill-runs.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const Boom = require('@hapi/boom')

const AmendAdjustmentFactorService = require('../services/bill-runs/two-part-tariff/amend-adjustment-factor.service.js')
const AmendBillableReturnsService = require('../services/bill-runs/two-part-tariff/amend-billable-returns.service.js')
const CancelBillRunService = require('../services/bill-runs/cancel-bill-run.service.js')
const ChargeReferenceDetailsService = require('../services/bill-runs/two-part-tariff/charge-reference-details.service.js')
Expand All @@ -18,6 +19,7 @@ const ReviewBillRunService = require('../services/bill-runs/two-part-tariff/revi
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 SubmitAmendedAdjustmentFactorService = require('../services/bill-runs/two-part-tariff/submit-amended-adjustment-factor.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')
Expand All @@ -26,6 +28,18 @@ const SubmitReviewLicenceService = require('../services/bill-runs/two-part-tarif
const SubmitSendBillRunService = require('../services/bill-runs/submit-send-bill-run.service.js')
const ViewBillRunService = require('../services/bill-runs/view-bill-run.service.js')

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

const pageData = await AmendAdjustmentFactorService.go(billRunId, licenceId, reviewChargeReferenceId)

return h.view('bill-runs/amend-adjustment-factor.njk', {
pageTitle: 'Set the adjustment factors',
activeNavBar: 'bill-runs',
...pageData
})
}

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

Expand Down Expand Up @@ -149,6 +163,23 @@ async function send (request, h) {
})
}

async function submitAmendedAdjustmentFactor (request, h) {
const { id: billRunId, licenceId, reviewChargeReferenceId } = request.params
const pageData = await SubmitAmendedAdjustmentFactorService.go(
billRunId,
licenceId,
reviewChargeReferenceId,
request.payload,
request.yar
)

if (pageData.error) {
return h.view('bill-runs/amend-adjustment-factor.njk', pageData)
}

return h.redirect(`/system/bill-runs/${billRunId}/review/${licenceId}/charge-reference-details/${reviewChargeReferenceId}`)
}

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

Expand Down Expand Up @@ -236,6 +267,7 @@ async function view (request, h) {
}

module.exports = {
amendAdjustmentFactor,
amendBillableReturns,
cancel,
chargeReferenceDetails,
Expand All @@ -246,6 +278,7 @@ module.exports = {
review,
reviewLicence,
send,
submitAmendedAdjustmentFactor,
submitAmendedBillableReturns,
submitCancel,
submitReview,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict'

/**
* Formats the two part tariff review data ready for presenting in the amend adjustment factor page
* @module AmendAdjustmentFactorPresenter
*/

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

/**
* Prepares and processes bill run and review charge reference data for presenting
*
* @param {module:BillRunModel} billRun - the data from the bill run
* @param {module:ReviewChargeReference} reviewChargeReference - the data from the review charge reference
* @param {String} licenceId - the UUID of the licence being reviewed
*
* @returns {Object} the prepared bill run and charge reference data to be passed to the amend adjustment factor page
*/
function go (billRun, reviewChargeReference, licenceId) {
return {
billRunId: billRun.id,
licenceId,
financialYear: formatFinancialYear(billRun.toFinancialYearEnding),
chargePeriod: _prepareDate(
reviewChargeReference.reviewChargeVersion.chargePeriodStartDate,
reviewChargeReference.reviewChargeVersion.chargePeriodEndDate
),
chargeReference: {
id: reviewChargeReference.id,
description: reviewChargeReference.chargeReference.chargeCategory.shortDescription,
aggregateFactor: reviewChargeReference.amendedAggregate,
chargeAdjustment: reviewChargeReference.amendedChargeAdjustment,
otherAdjustments: _otherAdjustments(reviewChargeReference)
}
}
}

function _otherAdjustments (reviewChargeReference) {
const { supportedSourceName, waterCompanyCharge } = reviewChargeReference.chargeReference

const adjustments = []

if (supportedSourceName) {
adjustments.push(`Supported source ${supportedSourceName}`)
}

if (waterCompanyCharge) {
adjustments.push('Public Water Supply')
}

if (reviewChargeReference.abatementAgreement !== 1) {
adjustments.push(`Abatement agreement (${reviewChargeReference.abatementAgreement})`)
}

if (reviewChargeReference.winterDiscount) {
adjustments.push('Winter discount')
}

if (reviewChargeReference.twoPartTariffAgreement) {
adjustments.push('Two part tariff agreement')
}

if (reviewChargeReference.canalAndRiverTrustAgreement) {
adjustments.push('Canal and River trust agreement')
}

return adjustments
}

function _prepareDate (startDate, endDate) {
const preparedStartDate = formatLongDate(startDate)
const preparedEndDate = formatLongDate(endDate)

return `${preparedStartDate} to ${preparedEndDate}`
}

module.exports = {
go
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function go (billRun, reviewChargeReference, licenceId) {
reviewChargeReference.reviewChargeVersion.chargePeriodEndDate
),
chargeReference: {
id: reviewChargeReference.id,
reference: reviewChargeReference.chargeReference.chargeCategory.reference,
description: reviewChargeReference.chargeReference.chargeCategory.shortDescription,
totalBillableReturns: _totalBillableReturns(reviewChargeReference.reviewChargeElements),
Expand Down
26 changes: 26 additions & 0 deletions app/routes/bill-runs.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,32 @@ const routes = [
description: 'Review a charge reference from a two-part tariff licence'
}
},
{
method: 'GET',
path: '/bill-runs/{id}/review/{licenceId}/charge-reference-details/{reviewChargeReferenceId}/amend-adjustment-factor',
handler: BillRunsController.amendAdjustmentFactor,
options: {
auth: {
access: {
scope: ['billing']
}
},
description: 'Amend a charge references adjustment factors from a two-part tariff licence'
}
},
{
method: 'POST',
path: '/bill-runs/{id}/review/{licenceId}/charge-reference-details/{reviewChargeReferenceId}/amend-adjustment-factor',
handler: BillRunsController.submitAmendedAdjustmentFactor,
options: {
auth: {
access: {
scope: ['billing']
}
},
description: 'Submit the amended charge references adjustment factors from a two-part tariff licence'
}
},
{
method: 'GET',
path: '/bill-runs/{id}/review/{licenceId}/match-details/{reviewChargeElementId}',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

/**
* Orchestrates fetching and presenting the data needed for the amend adjustment factor page
* @module AmendAdjustmentFactorService
*/

const AmendAdjustmentFactorPresenter = require('../../../presenters/bill-runs/two-part-tariff/amend-adjustment-factor.presenter.js')
const FetchReviewChargeReferenceService = require('./fetch-review-charge-reference.service.js')

/**
* Orchestrates fetching and presenting the data needed for the amend adjustment factor page
*
* @param {String} billRunId - The UUID for the bill run
* @param {String} licenceId - The UUID of the licence that is being reviewed
* @param {String} reviewChargeReferenceId - The UUID of the review charge reference being viewed
*
* @returns {Promise<Object>} the 'pageData' needed to view the amend adjustment factor page
*/
async function go (billRunId, licenceId, reviewChargeReferenceId) {
const {
billRun,
reviewChargeReference
} = await FetchReviewChargeReferenceService.go(billRunId, reviewChargeReferenceId)

const pageData = AmendAdjustmentFactorPresenter.go(billRun, reviewChargeReference, licenceId)

return pageData
}

module.exports = {
go
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ const FetchReviewChargeReferenceService = require('./fetch-review-charge-referen
* @param {String} billRunId - The UUID for the bill run
* @param {String} licenceId - The UUID of the licence that is being reviewed
* @param {String} reviewChargeReferenceId - The UUID of the charge reference being reviewed
* @param {Object} yar - The Hapi `request.yar` session manager passed on by the controller
*
* @returns {Promise<Object>} the 'pageData' needed for the review charge reference page. It contains details of the
* bill run, charge reference and the charge adjustments
*/
async function go (billRunId, licenceId, reviewChargeReferenceId) {
async function go (billRunId, licenceId, reviewChargeReferenceId, yar) {
const {
billRun,
reviewChargeReference
} = await FetchReviewChargeReferenceService.go(billRunId, reviewChargeReferenceId)

const [bannerMessage] = yar.flash('banner')
const pageData = ChargeReferenceDetailsPresenter.go(billRun, reviewChargeReference, licenceId)

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

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict'

/**
* Orchestrates validating and patching the data for the amend adjustment factors page
* @module SubmitAmendedAdjustmentFactorService
*/

const AdjustmentFactorValidator = require('../../../validators/bill-runs/two-part-tariff/adjustment-factor.validator.js')
const AmendAdjustmentFactorPresenter = require('../../../presenters/bill-runs/two-part-tariff/amend-adjustment-factor.presenter.js')
const FetchReviewChargeReferenceService = require('./fetch-review-charge-reference.service.js')
const ReviewChargeReferenceModel = require('../../../models/review-charge-reference.model.js')

/**
* Orchestrates validating the data for the amend adjustment factor page and patching the db value

* @param {String} billRunId - The UUID for the bill run
* @param {String} licenceId - The UUID of the licence that is being reviewed
* @param {String} reviewChargeReferenceId - The UUID of the review charge reference being updated
* @param {Object} payload - The submitted form data
* @param {Object} yar - The Hapi `request.yar` session manager passed on by the controller
*
* @returns {Promise<Object>} The updated value for the adjustment factor
*/
async function go (billRunId, licenceId, reviewChargeReferenceId, payload, yar) {
const validationResult = _validate(payload)

if (!validationResult) {
await _persistAmendedAdjustmentFactor(reviewChargeReferenceId, payload)
yar.flash('banner', 'The adjustment factors for this licence have been updated')

return { error: null }
}

const pageData = await _getPageData(billRunId, reviewChargeReferenceId, licenceId)

return {
activeNavBar: 'search',
pageTitle: 'Set the adjustment factors',
error: validationResult,
inputtedAggregateValue: payload.amendedAggregateFactor,
inputtedChargeValue: payload.amendedChargeAdjustment,
...pageData
}
}

async function _getPageData (billRunId, reviewChargeReferenceId, licenceId) {
const {
billRun,
reviewChargeReference
} = await FetchReviewChargeReferenceService.go(billRunId, reviewChargeReferenceId)

return AmendAdjustmentFactorPresenter.go(billRun, reviewChargeReference, licenceId)
}

async function _persistAmendedAdjustmentFactor (reviewChargeReferenceId, payload) {
await ReviewChargeReferenceModel.query()
.findById(reviewChargeReferenceId)
.patch({ amendedAggregate: payload.amendedAggregateFactor })

await ReviewChargeReferenceModel.query()
.findById(reviewChargeReferenceId)
.patch({ amendedChargeAdjustment: payload.amendedChargeAdjustment })
}

function _validate (payload) {
const maxDecimalsForAggregateValue = 2
const maxDecimalsForChargeAdjustmentValue = 15

const aggregateValidation = AdjustmentFactorValidator.go(
payload.amendedAggregateFactor,
maxDecimalsForAggregateValue,
'aggregate')

const chargeValidation = AdjustmentFactorValidator.go(
payload.amendedChargeAdjustment,
maxDecimalsForChargeAdjustmentValue,
'charge')

if (!aggregateValidation.error && !chargeValidation.error) {
return null
}

return {
aggregateFactorElement: aggregateValidation.error ? { text: aggregateValidation.error.details[0].message } : null,
chargeAdjustmentElement: chargeValidation.error ? { text: chargeValidation.error.details[0].message } : null
}
}

module.exports = {
go
}
Loading
Loading