Skip to content

Commit

Permalink
Merge branch 'main' into return-log-received-confirmation-page
Browse files Browse the repository at this point in the history
  • Loading branch information
rvsiyad authored Feb 6, 2025
2 parents ab70908 + 26c1fd5 commit 6e40627
Show file tree
Hide file tree
Showing 21 changed files with 1,438 additions and 48 deletions.
25 changes: 24 additions & 1 deletion app/controllers/return-logs-setup.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ const InitiateSessionService = require('../services/return-logs/setup/initiate-s
const MeterDetailsService = require('../services/return-logs/setup/meter-details.service.js')
const MeterProvidedService = require('../services/return-logs/setup/meter-provided.service.js')
const NoteService = require('../services/return-logs/setup/note.service.js')
const PeriodUsedService = require('../services/return-logs/setup/period-used.service.js')
const ReceivedService = require('../services/return-logs/setup/received.service.js')
const ReportedService = require('../services/return-logs/setup/reported.service.js')
const SingleVolumeService = require('../services/return-logs/setup/single-volume.service.js')
const SubmissionService = require('../services/return-logs/setup/submission.service.js')
const SubmitMeterDetailsService = require('../services/return-logs/setup/submit-meter-details.service.js')
const SubmitMeterProvidedService = require('../services/return-logs/setup/submit-meter-provided.service.js')
const SubmitNoteService = require('../services/return-logs/setup/submit-note.service.js')
const SubmitPeriodUsedService = require('../services/return-logs/setup/submit-period-used.service.js')
const SubmitReceivedService = require('../services/return-logs/setup/submit-received.service.js')
const SubmitReportedService = require('../services/return-logs/setup/submit-reported.service.js')
const SubmitSingleVolumeService = require('../services/return-logs/setup/submit-single-volume.service.js')
Expand Down Expand Up @@ -74,6 +76,13 @@ async function note(request, h) {
return h.view('return-logs/setup/note.njk', pageData)
}

async function periodUsed(request, h) {
const { sessionId } = request.params
const pageData = await PeriodUsedService.go(sessionId)

return h.view('return-logs/setup/period-used.njk', pageData)
}

async function received(request, h) {
const { sessionId } = request.params
const pageData = await ReceivedService.go(sessionId)
Expand Down Expand Up @@ -156,6 +165,18 @@ async function submitNote(request, h) {
return h.redirect(`/system/return-logs/setup/${sessionId}/check`)
}

async function submitPeriodUsed(request, h) {
const { sessionId } = request.params

const pageData = await SubmitPeriodUsedService.go(sessionId, request.payload)

if (pageData.error) {
return h.view('return-logs/setup/period-used.njk', pageData)
}

return h.redirect(`/system/return-logs/setup/${sessionId}/check`)
}

async function submitReceived(request, h) {
const {
params: { sessionId },
Expand Down Expand Up @@ -199,7 +220,7 @@ async function submitSingleVolume(request, h) {
}

if (pageData.singleVolume === 'no') {
return h.redirect(`/system/return-logs/setup/${sessionId}/check-answers`)
return h.redirect(`/system/return-logs/setup/${sessionId}/check`)
}

return h.redirect(`/system/return-logs/setup/${sessionId}/period-used`)
Expand Down Expand Up @@ -247,6 +268,7 @@ module.exports = {
meterDetails,
meterProvided,
note,
periodUsed,
received,
reported,
setup,
Expand All @@ -255,6 +277,7 @@ module.exports = {
submitMeterDetails,
submitMeterProvided,
submitNote,
submitPeriodUsed,
submitReceived,
submitReported,
submitSingleVolume,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
'use strict'

/**
* Formats data for the `/notifications/setup/download` link
* @module DownloadRecipientsPresenter
*/

const { contactName } = require('../../crm.presenter.js')
const { formatDateObjectToISO } = require('../../../lib/dates.lib.js')
const { transformArrayToCSVRow } = require('../../../lib/transform-to-csv.lib.js')

const HEADERS = [
'Licences',
'Return references',
'Returns period start date',
'Returns period end date',
'Returns due date',
'Message type',
'Message reference',
'Email',
'Recipient name',
'Address line 1',
'Address line 2',
'Address line 3',
'Address line 4',
'Address line 5',
'Address line 6',
'Postcode'
]

/**
* Formats data for the `/notifications/setup/download` link.
*
* This function takes an array of recipient objects and transforms it into a CSV
* string suitable for download.
*
* The headers are fixed and in the correct order. If a value for a row does not match the header then it will default
* to an empty string.
*
* @param {object[]} recipients - An array of recipients
*
* @returns {string} - A CSV-formatted string that includes the recipients' data, with the first
* row as column headers and subsequent rows corresponding to the recipient details.
*/
function go(recipients) {
const rows = _transformToCsv(recipients)

return [HEADERS + '\n', ...rows].join('')
}

function _address(contact) {
if (!contact) {
return ['', '', '', '', '', '', '']
}

return [
contact.addressLine1,
contact.addressLine2,
contact.addressLine3,
contact.addressLine4,
contact.town || contact.county,
contact.country,
contact.postcode
]
}
/**
* Transforms the recipients' data into a CSV-compatible format.
*
* The order of the object dictates the CSV header order.
*
* @private
*/
function _transformToCsv(recipients) {
return recipients.map((recipient) => {
const { contact } = recipient

const row = [
recipient.licence_ref,
recipient.return_reference,
formatDateObjectToISO(recipient.start_date),
formatDateObjectToISO(recipient.end_date),
formatDateObjectToISO(recipient.due_date),
contact ? 'letter' : 'email',
'invitations',
recipient.email || '',
contact ? contactName(recipient.contact) : '',
..._address(contact)
]

return transformArrayToCSVRow(row)
})
}

module.exports = {
go
}
52 changes: 52 additions & 0 deletions app/presenters/return-logs/setup/period-used.presenter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict'

/**
* Format data for the `/return-log/setup/{sessionId}/period-used` page
* @module PeriodUsedPresenter
*/

const { formatAbstractionPeriod } = require('../../base.presenter.js')

/**
* Format data for the `/return-log/setup/{sessionId}/period-used` page
*
* @param {module:SessionModel} session - The return log setup session instance
*
* @returns {object} page data needed by the view template
*/
function go(session) {
const {
id: sessionId,
periodStartDay,
periodStartMonth,
periodEndDay,
periodEndMonth,
periodDateUsedOptions,
returnReference,
periodUsedFromDay,
periodUsedFromMonth,
periodUsedFromYear,
periodUsedToDay,
periodUsedToMonth,
periodUsedToYear
} = session

return {
abstractionPeriod: formatAbstractionPeriod(periodStartDay, periodStartMonth, periodEndDay, periodEndMonth),
backLink: `/system/return-logs/setup/${sessionId}/single-volume`,
pageTitle: 'What period was used for this volume?',
periodDateUsedOptions: periodDateUsedOptions ?? null,
periodUsedFromDay: periodUsedFromDay ?? null,
periodUsedFromMonth: periodUsedFromMonth ?? null,
periodUsedFromYear: periodUsedFromYear ?? null,
periodUsedToDay: periodUsedToDay ?? null,
periodUsedToMonth: periodUsedToMonth ?? null,
periodUsedToYear: periodUsedToYear ?? null,
returnReference,
sessionId
}
}

module.exports = {
go
}
24 changes: 24 additions & 0 deletions app/routes/return-logs-setup.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,30 @@ const routes = [
}
}
}
},
{
method: 'GET',
path: '/return-logs/setup/{sessionId}/period-used',
options: {
handler: ReturnLogsSetupController.periodUsed,
auth: {
access: {
scope: ['billing']
}
}
}
},
{
method: 'POST',
path: '/return-logs/setup/{sessionId}/period-used',
options: {
handler: ReturnLogsSetupController.submitPeriodUsed,
auth: {
access: {
scope: ['billing']
}
}
}
}
]

Expand Down
16 changes: 13 additions & 3 deletions app/services/notifications/setup/download-recipients.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* @module DownloadRecipientsService
*/

const DetermineReturnsPeriodService = require('./determine-returns-period.service.js')
const DownloadRecipientsPresenter = require('../../../presenters/notifications/setup/download-recipients.presenter.js')
const FetchDownloadRecipientsService = require('./fetch-download-recipients.service.js')
const SessionModel = require('../../../models/session.model.js')

/**
Expand All @@ -19,12 +22,19 @@ const SessionModel = require('../../../models/session.model.js')
*/
async function go(sessionId) {
const session = await SessionModel.query().findById(sessionId)
const { notificationType, referenceCode } = session
const { notificationType, referenceCode, returnsPeriod } = session

const csv = 'Licences\n12234\n'
const determinedReturnsPeriod = DetermineReturnsPeriodService.go(returnsPeriod)

const recipients = await FetchDownloadRecipientsService.go(
determinedReturnsPeriod.returnsPeriod.dueDate,
determinedReturnsPeriod.summer
)

const formattedData = DownloadRecipientsPresenter.go(recipients)

return {
data: csv,
data: formattedData,
type: 'text/csv',
filename: `${notificationType} - ${referenceCode}.csv`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const { db } = require('../../../../db/db.js')
* different contacts types (but still preferring the registered over unregistered licence).
*
* @param {Date} dueDate
* @param {boolean} summer
* @param {string} summer
*
* @returns {Promise<object[]>} - matching recipients
*/
Expand Down
34 changes: 34 additions & 0 deletions app/services/return-logs/setup/period-used.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

/**
* Orchestrates fetching and presenting the data for `/return-logs/setup/{sessionId}/period-used` page
* @module PeriodUsedService
*/

const PeriodUsedPresenter = require('../../../presenters/return-logs/setup/period-used.presenter.js')
const SessionModel = require('../../../models/session.model.js')

/**
* Orchestrates fetching and presenting the data for `/return-logs/setup/{sessionId}/period-used` page
*
* Supports generating the data needed for the period used page in the return log setup journey. It fetches the
* current session record and formats the data needed for the page.
*
* @param {string} sessionId - The UUID of the current session
*
* @returns {Promise<object>} The view data for the period used page
*/
async function go(sessionId) {
const session = await SessionModel.query().findById(sessionId)

const formattedData = PeriodUsedPresenter.go(session)

return {
activeNavBar: 'search',
...formattedData
}
}

module.exports = {
go
}
Loading

0 comments on commit 6e40627

Please sign in to comment.