diff --git a/app/controllers/return-logs-edit.controller.js b/app/controllers/return-logs-edit.controller.js index d9b7985d47..6a9020d82c 100644 --- a/app/controllers/return-logs-edit.controller.js +++ b/app/controllers/return-logs-edit.controller.js @@ -5,14 +5,14 @@ * @module ReturnLogsEditController */ -const EditReturnLogService = require('../services/return-logs/edit-return-log.service.js') +const EditReturnLogService = require('../services/return-logs-edit/edit-return-log.service.js') const InitiateSessionService = require('../services/return-logs-edit/initiate-session.service.js') -const basePath = 'return-log-edit' +const basePath = '/system/return-log-edit' async function edit(request, h) { - const { returnLogId } = request.query - const pageData = await EditReturnLogService.go(returnLogId) + const { sessionId } = request.params + const pageData = await EditReturnLogService.go(sessionId) return h.view('return-logs-edit/how-to-edit.njk', { activeNavBar: 'search', ...pageData }) } @@ -21,14 +21,14 @@ async function setup(request, h) { const { returnLogId } = request.query const session = await InitiateSessionService.go(returnLogId) - return h.redirect(`/system/${basePath}/${session.id}/how-to-edit`) + return h.redirect(`${basePath}/${session.id}/how-to-edit`) } async function submitEdit(request, h) { - const { returnLogId } = request.query + const { sessionId } = request.params const { howToEdit } = request.payload - const pageData = await EditReturnLogService.go(returnLogId) + const pageData = await EditReturnLogService.go(sessionId) if (!howToEdit) { return h.view('return-logs-edit/how-to-edit.njk', { @@ -38,11 +38,7 @@ async function submitEdit(request, h) { }) } - if (howToEdit === 'query') { - // TODO: Set/unset the query flag - } - - return h.redirect(`/system/return-logs/edit/${howToEdit}?returnLogId=${returnLogId}`) + return h.redirect(`${basePath}/${sessionId}/${howToEdit}`) } module.exports = { diff --git a/app/presenters/return-logs/edit-return-log.presenter.js b/app/presenters/return-logs-edit/edit-return-log.presenter.js similarity index 52% rename from app/presenters/return-logs/edit-return-log.presenter.js rename to app/presenters/return-logs-edit/edit-return-log.presenter.js index eb392774f7..2962e3d689 100644 --- a/app/presenters/return-logs/edit-return-log.presenter.js +++ b/app/presenters/return-logs-edit/edit-return-log.presenter.js @@ -10,12 +10,11 @@ const { formatAbstractionPeriod, formatLongDate } = require('../base.presenter.j /** * Formats the data ready for presenting in the abstraction return page * - * @param {module:ReturnLogModel} editReturnLog - instance of the `ReturnLogModel` returned from - * `FetchEditReturnLogService` + * @param {module:SessionModel} session - The session instance to format * * @returns {object} page date needed for the abstraction return page */ -function go(editReturnLog) { +function go(session) { const { endDate, licenceId, @@ -31,7 +30,7 @@ function go(editReturnLog) { startDate, twoPartTariff, underQuery - } = editReturnLog + } = session return { abstractionPeriod: `From ${formatAbstractionPeriod( @@ -43,34 +42,16 @@ function go(editReturnLog) { licenceId, licenceRef, pageTitle: 'Abstraction return', - purposes: _formatPurposes(purposes), + purposes, queryText: underQuery ? 'Resolve query' : 'Record under query', returnLogId, - returnsPeriod: `From ${formatLongDate(startDate)} to ${formatLongDate(endDate)}`, + returnsPeriod: `From ${formatLongDate(new Date(startDate))} to ${formatLongDate(new Date(endDate))}`, returnReference, siteDescription, tariffType: twoPartTariff ? 'Two part tariff' : 'Standard tariff' } } -/** - * Format the purposes for a return log - * - * It is possible that a return log has more than one purpose associated it. If this is the case we need the description - * of each purpose from the array of purposes to be concatenated into a single comma separated string ready to present. - * - * @param {Array} purposes - the purposes taken from the return logs metadata - * - * @returns {string} the purpose descriptions as a string, separated by commas if more than one description exists - */ -function _formatPurposes(purposes) { - const purposeDescriptionArray = purposes.map((purpose) => { - return purpose.tertiary.description - }) - - return purposeDescriptionArray.join(', ') -} - module.exports = { go } diff --git a/app/services/return-logs/edit-return-log.service.js b/app/services/return-logs-edit/edit-return-log.service.js similarity index 54% rename from app/services/return-logs/edit-return-log.service.js rename to app/services/return-logs-edit/edit-return-log.service.js index 206dd2b2f2..4301e74813 100644 --- a/app/services/return-logs/edit-return-log.service.js +++ b/app/services/return-logs-edit/edit-return-log.service.js @@ -5,21 +5,20 @@ * @module EditReturnLogService */ -const FetchEditReturnLogService = require('./fetch-edit-return-log.service.js') -const EditReturnLogPresenter = require('../../presenters/return-logs/edit-return-log.presenter.js') +const SessionModel = require('../../models/session.model.js') +const EditReturnLogPresenter = require('../../presenters/return-logs-edit/edit-return-log.presenter.js') /** * Orchestrates fetching and presenting the data needed for the how to edit an abstraction return page * - * @param {string} returnLogId - The ID of the return log to edit + * @param {string} sessionId - The UUID for setup bill run session record * * @returns {Promise} page data needed by the view template */ -async function go(returnLogId) { - const editReturnLog = await FetchEditReturnLogService.go(returnLogId) - const pageData = EditReturnLogPresenter.go(editReturnLog) +async function go(sessionId) { + const session = await SessionModel.query().findById(sessionId) - return pageData + return EditReturnLogPresenter.go(session) } module.exports = { diff --git a/app/services/return-logs/fetch-edit-return-log.service.js b/app/services/return-logs/fetch-edit-return-log.service.js deleted file mode 100644 index 8af08aef66..0000000000 --- a/app/services/return-logs/fetch-edit-return-log.service.js +++ /dev/null @@ -1,49 +0,0 @@ -'use strict' - -/** - * Fetches the selected return log and related data for the abstraction return page - * @module FetchEditReturnLogService - */ - -const { ref } = require('objection') - -const ReturnLogModel = require('../../models/return-log.model.js') - -/** - * Fetches the selected return log and related data for the abstraction return page - * - * @param {string} returnLogId - the UUID of the selected return log - * - * @returns {module:ReturnLogModel} the matching `ReturnLogModel` instance and related data needed for the - * abstraction return page - */ -async function go(returnLogId) { - return _fetch(returnLogId) -} - -async function _fetch(returnLogId) { - return ReturnLogModel.query() - .findById(returnLogId) - .select( - 'licence.id as licenceId', - 'licence.licenceRef', - 'returnLogs.id as returnLogId', - 'returnLogs.startDate', - 'returnLogs.endDate', - 'returnLogs.returnReference', - 'returnLogs.underQuery', - ref('returnLogs.metadata:nald.periodStartDay').castInt().as('periodStartDay'), - ref('returnLogs.metadata:nald.periodStartMonth').castInt().as('periodStartMonth'), - ref('returnLogs.metadata:nald.periodEndDay').castInt().as('periodEndDay'), - ref('returnLogs.metadata:nald.periodEndMonth').castInt().as('periodEndMonth'), - ref('returnLogs.metadata:description').as('siteDescription'), - ref('returnLogs.metadata:purposes').as('purposes'), - ref('returnLogs.metadata:isTwoPartTariff').as('twoPartTariff') - ) - .innerJoinRelated('licence') - .where('returnLogs.id', returnLogId) -} - -module.exports = { - go -}