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

Refactor review licence POST pattern #928

Merged
merged 5 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 23 additions & 9 deletions app/controllers/bill-runs.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const SendBillRunService = require('../services/bill-runs/send-bill-run.service.
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 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 @@ -99,7 +100,7 @@ async function review (request, h) {
async function reviewLicence (request, h) {
const { id: billRunId, licenceId } = request.params

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

return h.view('bill-runs/review-licence.njk', {
pageTitle: `Licence ${pageData.licence.licenceRef}`,
Expand All @@ -120,6 +121,18 @@ async function send (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 submitCancel (request, h) {
const { id } = request.params

Expand All @@ -134,16 +147,16 @@ 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)
async function submitReviewLicence (request, h) {
const { id: billRunId, licenceId } = request.params

if (pageData.error) {
return h.view('bill-runs/amend-billable-returns.njk', pageData)
}
const pageData = await SubmitReviewLicenceService.go(billRunId, licenceId, request.payload)

return h.redirect(`/system/bill-runs/${billRunId}/review/${licenceId}/match-details/${reviewChargeElementId}`)
return h.view('bill-runs/review-licence.njk', {
pageTitle: `Licence ${pageData.licence.licenceRef}`,
activeNavBar: 'bill-runs',
...pageData
})
}

async function submitSend (request, h) {
Expand Down Expand Up @@ -183,6 +196,7 @@ module.exports = {
send,
submitAmendedBillableReturns,
submitCancel,
submitReviewLicence,
submitSend,
view
}
2 changes: 1 addition & 1 deletion app/routes/bill-runs.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const routes = [
{
method: 'POST',
path: '/bill-runs/{id}/review/{licenceId}',
handler: BillRunsController.reviewLicence,
handler: BillRunsController.submitReviewLicence,
options: {
auth: {
access: {
Expand Down
44 changes: 4 additions & 40 deletions app/services/bill-runs/two-part-tariff/review-licence.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,25 @@
*/

const FetchReviewLicenceResultsService = require('./fetch-review-licence-results.service.js')
const ReviewLicenceModel = require('../../../models/review-licence.model.js')
const ReviewLicencePresenter = require('../../../presenters/bill-runs/two-part-tariff/review-licence.presenter.js')

/**
* Orchestrated fetching and presenting the data needed for the licence review page
*
* @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} payload The `request.payload` containing the `marKProgress` data. This is only passed to the service
* when there is a POST request, which only occurs when the 'Mark progress' button is clicked.
*
* @returns {Object} an object representing the 'pageData' needed to review the individual licence. It contains the
* licence, bill run, matched and unmatched returns and the licence charge data
* @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
*/
async function go (billRunId, licenceId, payload) {
const licenceStatus = payload?.licenceStatus
const markProgress = payload?.marKProgress

if (payload) {
await _processPayload(billRunId, licenceId, licenceStatus, markProgress)
}

async function go (billRunId, licenceId) {
const { billRun, licence } = await FetchReviewLicenceResultsService.go(billRunId, licenceId)

const pageData = ReviewLicencePresenter.go(billRun, licence, licenceStatus, markProgress)
const pageData = ReviewLicencePresenter.go(billRun, licence)

return pageData
}

async function _processPayload (billRunId, licenceId, licenceStatus, markProgress) {
if (licenceStatus === 'ready' || licenceStatus === 'review') {
await _updateStatus(billRunId, licenceId, licenceStatus)
}

if (markProgress === 'mark' || markProgress === 'unmark') {
await _updateProgress(billRunId, licenceId, markProgress)
}
}

async function _updateProgress (billRunId, licenceId, marKProgress) {
const progress = marKProgress === 'mark'

await ReviewLicenceModel.query()
.patch({ progress })
.where('billRunId', billRunId)
.andWhere('licenceId', licenceId)
}

async function _updateStatus (billRunId, licenceId, licenceStatus) {
await ReviewLicenceModel.query()
.patch({ status: licenceStatus })
.where('billRunId', billRunId)
.andWhere('licenceId', licenceId)
}

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

/**
* Orchestrates fetching and presenting the data needed for the licence review page in a two-part tariff bill run
* @module ReviewLicenceService
*/

const FetchReviewLicenceResultsService = require('./fetch-review-licence-results.service.js')
const ReviewLicenceModel = require('../../../models/review-licence.model.js')
const ReviewLicencePresenter = require('../../../presenters/bill-runs/two-part-tariff/review-licence.presenter.js')

/**
* Orchestrated fetching and presenting the data needed for the licence review page
*
* @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} payload The `request.payload` containing the `marKProgress` data. This is only passed to the service
* when there is a POST request, which only occurs when the 'Mark progress' button is clicked.
*
* @returns {Object} an object representing the 'pageData' needed to review the individual licence. It contains the
* licence, bill run, matched and unmatched returns and the licence charge data
*/
async function go (billRunId, licenceId, payload) {
const licenceStatus = payload?.licenceStatus
const markProgress = payload?.marKProgress

if (payload) {
await _processPayload(billRunId, licenceId, licenceStatus, markProgress)
}

const { billRun, licence } = await FetchReviewLicenceResultsService.go(billRunId, licenceId)

const pageData = ReviewLicencePresenter.go(billRun, licence, licenceStatus, markProgress)

return pageData
}

async function _processPayload (billRunId, licenceId, licenceStatus, markProgress) {
if (licenceStatus === 'ready' || licenceStatus === 'review') {
await _updateStatus(billRunId, licenceId, licenceStatus)
}

if (markProgress === 'mark' || markProgress === 'unmark') {
await _updateProgress(billRunId, licenceId, markProgress)
}
}

async function _updateProgress (billRunId, licenceId, marKProgress) {
const progress = marKProgress === 'mark'

await ReviewLicenceModel.query()
.patch({ progress })
.where('billRunId', billRunId)
.andWhere('licenceId', licenceId)
}

async function _updateStatus (billRunId, licenceId, licenceStatus) {
await ReviewLicenceModel.query()
.patch({ status: licenceStatus })
.where('billRunId', billRunId)
.andWhere('licenceId', licenceId)
}

module.exports = {
go
}
5 changes: 3 additions & 2 deletions test/controllers/bill-runs.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ const Boom = require('@hapi/boom')
const CancelBillRunService = require('../../app/services/bill-runs/cancel-bill-run.service.js')
const IndexBillRunsService = require('../../app/services/bill-runs/index-bill-runs.service.js')
const MatchDetailsService = require('../../app/services/bill-runs/two-part-tariff/match-details.service.js')
const ReviewLicenceService = require('../../app/services/bill-runs/two-part-tariff/review-licence.service.js')
const ReviewBillRunService = require('../../app/services/bill-runs/two-part-tariff/review-bill-run.service.js')
const ReviewLicenceService = require('../../app/services/bill-runs/two-part-tariff/review-licence.service.js')
const SendBillRunService = require('../../app/services/bill-runs/send-bill-run.service.js')
const StartBillRunProcessService = require('../../app/services/bill-runs/start-bill-run-process.service.js')
const SubmitAmendedBillableReturnsService = require('../../app/services/bill-runs/two-part-tariff/submit-amended-billable-returns.service.js')
const SubmitCancelBillRunService = require('../../app/services/bill-runs/submit-cancel-bill-run.service.js')
const SubmitReviewLicenceService = require('../../app/services/bill-runs/two-part-tariff/submit-review-licence.service.js')
const SubmitSendBillRunService = require('../../app/services/bill-runs/submit-send-bill-run.service.js')
const ViewBillRunService = require('../../app/services/bill-runs/view-bill-run.service.js')

Expand Down Expand Up @@ -373,7 +374,7 @@ describe('Bill Runs controller', () => {

describe('when a request is valid', () => {
beforeEach(() => {
Sinon.stub(ReviewLicenceService, 'go').resolves(_licenceReviewData())
Sinon.stub(SubmitReviewLicenceService, 'go').resolves(_licenceReviewData())
})

it('returns a 200 response', async () => {
Expand Down
Loading
Loading