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

Amend billable volume on element review page #878

Merged
merged 38 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8fbe3e2
Edit billable volume on element review page
Beckyrose200 Apr 2, 2024
463a529
WIP
Beckyrose200 Apr 3, 2024
ec2d214
Merge branch 'main' into edit-billable-volume
Beckyrose200 Apr 5, 2024
d2c426b
Views content
Beckyrose200 Apr 5, 2024
8fdad7f
Amend billable returns controller, route, service and presenter
Beckyrose200 Apr 5, 2024
a801ef6
Merge branch 'main' into edit-billable-volume
Beckyrose200 Apr 5, 2024
000ab9d
Hook up button to amend billable returns page
Beckyrose200 Apr 5, 2024
6f4e527
Unit test for amend billable returns presenter
Beckyrose200 Apr 5, 2024
15d39b8
Amend Billable Returns Service Unit tests
Beckyrose200 Apr 5, 2024
bb1a80a
SubmitAmendedBillableReturns WIP
Beckyrose200 Apr 8, 2024
9ee9690
WIP
Beckyrose200 Apr 11, 2024
44e96d9
WIP
Beckyrose200 Apr 11, 2024
909a3d7
Add banner to match page
Beckyrose200 Apr 11, 2024
a68a7db
Merge branch 'main' into edit-billable-volume
Beckyrose200 Apr 17, 2024
dc065ba
Update migration for decimal places
Beckyrose200 Apr 17, 2024
c12952f
Refactor code to persistently show banner
Beckyrose200 Apr 17, 2024
18a3e01
Update banner logic
Beckyrose200 Apr 17, 2024
3c7fec7
Fix presenter unit test
Beckyrose200 Apr 17, 2024
f9bedf2
Add controller test for POST route
Beckyrose200 Apr 17, 2024
eb2efc9
Update bill runs controller to remove URL params
Beckyrose200 Apr 17, 2024
c9fca6e
Line length
Beckyrose200 Apr 17, 2024
32de424
Update comments
Beckyrose200 Apr 18, 2024
f33735f
Merge branch 'main' into edit-billable-volume
Beckyrose200 Apr 18, 2024
60e1f9e
Merge branch 'main' into edit-billable-volume
Beckyrose200 Apr 23, 2024
bb28a10
Add unit tests for the submit amended billable returns service
Beckyrose200 Apr 23, 2024
892b82d
Remove unused code
Beckyrose200 Apr 23, 2024
588dd94
Merge branch 'main' into edit-billable-volume
Beckyrose200 Apr 23, 2024
17a49e8
Add validation in for amend billable volumes page
Beckyrose200 Apr 24, 2024
99ac359
Add unit tests for billable returns validator
Beckyrose200 Apr 24, 2024
eb3a737
Fix unit tests
Beckyrose200 Apr 24, 2024
b14f5bc
Fix bill runs controller test
Beckyrose200 Apr 24, 2024
4a2e748
Change order of exports
Beckyrose200 Apr 24, 2024
1c97dbc
Merge branch 'main' into edit-billable-volume
Beckyrose200 Apr 24, 2024
dc5531c
Change number to a constant
Beckyrose200 Apr 24, 2024
30eb0b0
Change constant
Beckyrose200 Apr 24, 2024
51a51d5
Merge branch 'main' into edit-billable-volume
Beckyrose200 Apr 24, 2024
aca615a
Rename test file
Beckyrose200 Apr 24, 2024
fec58ab
Add test for failing validation
Beckyrose200 Apr 24, 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
28 changes: 28 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 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 CreateBillRunValidator = require('../validators/create-bill-run.validator.js')
const IndexBillRunsService = require('../services/bill-runs/index-bill-runs.service.js')
Expand All @@ -15,10 +16,23 @@ 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 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 SubmitSendBillRunService = require('../services/bill-runs/submit-send-bill-run.service.js')
const ViewBillRunService = require('../services/bill-runs/view-bill-run.service.js')

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

const pageData = await AmendBillableReturnsService.go(billRunId, licenceId, reviewChargeElementId)

return h.view('bill-runs/amend-billable-returns.njk', {
pageTitle: 'Set the billable returns quantity for this bill run',
activeNavBar: 'bill-runs',
...pageData
})
}

async function cancel (request, h) {
const { id } = request.params

Expand Down Expand Up @@ -120,6 +134,18 @@ async function submitCancel (request, h) {
}
}

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

const pageData = await SubmitAmendedBillableReturnsService.go(billRunId, licenceId, reviewChargeElementId, request.payload)

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

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

async function submitSend (request, h) {
const { id } = request.params

Expand Down Expand Up @@ -147,13 +173,15 @@ async function view (request, h) {
}

module.exports = {
amendBillableReturns,
cancel,
create,
index,
matchDetails,
review,
reviewLicence,
send,
submitAmendedBillableReturns,
submitCancel,
submitSend,
view
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use strict'

/**
* Formats the two part tariff review data ready for presenting in the edit billable returns page
* @module AmendBillableReturnsPresenter
*/

const DetermineAbstractionPeriodService = require('../../../services/bill-runs/determine-abstraction-periods.service.js')
const { formatLongDate } = require('../../base.presenter.js')

/**
* Prepares and processes bill run and review charge element data for presenting
*
* @param {module:BillRunModel} billRun - the data from the bill run
* @param {module:ReviewChargeElement} reviewChargeElement - the data from the review charge element
* @param {String} licenceId - the UUID of the licence being reviewed
*
* @returns {Object} the prepared bill run and charge element data to be passed to the edit billable returns page
*/
function go (billRun, reviewChargeElement, licenceId) {
return {
chargeElement: {
description: reviewChargeElement.chargeElement.description,
dates: _prepareChargeElementDates(
reviewChargeElement.chargeElement,
reviewChargeElement.reviewChargeReference.reviewChargeVersion
),
authorisedQuantity: reviewChargeElement.chargeElement.authorisedAnnualQuantity,
reviewChargeElementId: reviewChargeElement.id
},
billRun: {
id: billRun.id,
financialYear: _financialYear(billRun.toFinancialYearEnding)
},
chargeVersion: {
chargePeriod: _prepareDate(
reviewChargeElement.reviewChargeReference.reviewChargeVersion.chargePeriodStartDate,
reviewChargeElement.reviewChargeReference.reviewChargeVersion.chargePeriodEndDate
)
},
licenceId
}
}

function _financialYear (financialYearEnding) {
const startYear = financialYearEnding - 1
const endYear = financialYearEnding

return `${startYear} to ${endYear}`
}

function _prepareChargeElementDates (chargeElement, chargeVersion) {
const chargePeriod = {
startDate: chargeVersion.chargePeriodStartDate,
endDate: chargeVersion.chargePeriodEndDate
}

const {
abstractionPeriodStartDay,
abstractionPeriodStartMonth,
abstractionPeriodEndDay,
abstractionPeriodEndMonth
} = chargeElement

const abstractionPeriods = DetermineAbstractionPeriodService.go(
chargePeriod,
abstractionPeriodStartDay,
abstractionPeriodStartMonth,
abstractionPeriodEndDay,
abstractionPeriodEndMonth
)

const dates = []

// NOTE: There can be more than 1 abstraction period for an element, hence why we loop through them
abstractionPeriods.forEach((abstractionPeriod) => {
dates.push(_prepareDate(abstractionPeriod.startDate, abstractionPeriod.endDate))
})

return dates
}

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 @@ -38,7 +38,8 @@ function go (billRun, reviewChargeElement, licenceId) {
authorisedVolume: reviewChargeElement.chargeElement.authorisedAnnualQuantity,
issues: reviewChargeElement.issues?.length > 0 ? reviewChargeElement.issues.split(', ') : []
},
matchedReturns: _matchedReturns(reviewChargeElement.reviewReturns)
matchedReturns: _matchedReturns(reviewChargeElement.reviewReturns),
showBanner: reviewChargeElement.allocated !== reviewChargeElement.calculated
}
}

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 @@ -136,6 +136,32 @@ const routes = [
description: 'View match details of a charge element'
}
},
{
method: 'GET',
path: '/bill-runs/{id}/review/{licenceId}/match-details/{reviewChargeElementId}/amend-billable-returns',
handler: BillRunsController.amendBillableReturns,
options: {
auth: {
access: {
scope: ['billing']
}
},
description: 'Amend the billable return volumes on a charge element'
}
},
{
method: 'POST',
path: '/bill-runs/{id}/review/{licenceId}/match-details/{reviewChargeElementId}/amend-billable-returns',
handler: BillRunsController.submitAmendedBillableReturns,
options: {
auth: {
access: {
scope: ['billing']
}
},
description: 'Submit the amended billable return volumes on a charge element'
}
},
{
method: 'GET',
path: '/bill-runs/{id}/send',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

/**
* Orchestrates fetching and presenting the data needed for the amend billable returns page
* @module AmendBillableReturnsService
*/

const AmendBillableReturnsPresenter = require('../../../presenters/bill-runs/two-part-tariff/amend-billable-returns.presenter.js')
const FetchMatchDetailsService = require('./fetch-match-details.service.js')

/**
* Orchestrates fetching and presenting the data needed for the amend billable returns page
*
* @param {String} billRunId - The UUID for the bill run
* @param {String} licenceId - The UUID of the licence that is being reviewed
* @param {String} reviewChargeElementId - The UUID of the review charge element being viewed
*
* @returns {Promise<Object>} the 'pageData' needed to view the edit billable return volumes page
*/
async function go (billRunId, licenceId, reviewChargeElementId) {
const { billRun, reviewChargeElement } = await FetchMatchDetailsService.go(billRunId, reviewChargeElementId)

const pageData = AmendBillableReturnsPresenter.go(billRun, reviewChargeElement, licenceId)

return pageData
}

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

/**
* Orchestrates validating and patching the data for the amend billable returns page
* @module SubmitAmendedBillableReturnsService
*/

const AmendBillableReturnsService = require('../../../services/bill-runs/two-part-tariff/amend-billable-returns.service.js')
const BillableReturnsValidator = require('../../../validators/bill-runs/two-part-tariff/billable-returns.validator.js')
const ReviewChargeElementModel = require('../../../models/review-charge-element.model.js')

/**
* Orchestrates validating the data for the amend billable returns 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} reviewChargeElementId - The UUID of the review charge element being updated
* @param {Object} payload - The submitted form data
*
* @returns {Promise<Object>} The updated value for the billable returns
*/
async function go (billRunId, licenceId, reviewChargeElementId, payload) {
const validationResult = await _validate(payload)

if (!validationResult) {
await _persistAmendedBillableReturns(reviewChargeElementId, payload)

return { error: null }
}

const pageData = await AmendBillableReturnsService.go(billRunId, licenceId, reviewChargeElementId)

return {
activeNavBar: 'search',
pageTitle: 'Set the billable returns quantity for this bill run',
error: validationResult,
...pageData
}
}

function _persistAmendedBillableReturns (reviewChargeElementId, payload) {
const volume = payload['quantity-options'] === 'customQuantity' ? payload.customQuantity : payload['quantity-options']

return ReviewChargeElementModel.query()
.findById(reviewChargeElementId)
.patch({ allocated: volume })
}

function _validate (payload) {
const validation = BillableReturnsValidator.go(payload)

if (!validation.error) {
return null
}

const { message } = validation.error.details[0]

if (payload['quantity-options'] === 'customQuantity') {
return {
message,
radioFormElement: null,
customQuantityInputFormElement: { text: message }
}
}

return {
message,
radioFormElement: { text: message },
customQuantityInputFormElement: null
}
}

module.exports = {
go
}
Loading
Loading