Skip to content

Commit

Permalink
Merge branch 'main' into new-view-completed-monthly-return-logs-page
Browse files Browse the repository at this point in the history
  • Loading branch information
StuAA78 authored Jan 30, 2025
2 parents bb4aef5 + c40da27 commit 82400e2
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 128 deletions.
75 changes: 52 additions & 23 deletions app/presenters/notifications/setup/review.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { defaultPageSize } = require('../../../../config/database.config.js')
*
* @param {object[]} recipients - List of recipient objects, each containing recipient details like email or name.
* @param {number|string} page - The currently selected page
* @param {object} pagination -
* @param {object} pagination - The result from `PaginatorPresenter`
*
* @returns {object} - The data formatted for the view template
*/
Expand All @@ -29,13 +29,10 @@ function go(recipients, page, pagination) {
/**
* Contact can be an email or an address (letter)
*
* If it is an address then we convert the contact CSV string to an array
* If it is an address then we convert the contact CSV string to an array. If it is an email we return the email in
* array for the UI to have consistent formatting.
*
* If it is an email we return the email in array for the UI to have consistent formatting
*
* @param {object} recipient
*
* @returns {string[]}
* @private
*/
function _contact(recipient) {
if (recipient.email) {
Expand All @@ -44,17 +41,18 @@ function _contact(recipient) {

const name = contactName(recipient.contact)
const address = contactAddress(recipient.contact)

return [name, ...address]
}

/**
* Convert the licence CSV string to an array
*
* @param {string} licences
* @returns {string[]}
*/
function _licences(licences) {
return licences.split(',')
function _formatRecipients(recipients) {
return recipients.map((recipient) => {
return {
contact: _contact(recipient),
licences: recipient.licence_refs.split(','),
method: `${recipient.message_type} - ${recipient.contact_type}`
}
})
}

function _pageTitle(page, pagination) {
Expand All @@ -66,24 +64,55 @@ function _pageTitle(page, pagination) {
}

/**
* Due to the complexity of the query to get the recipients data, we handle pagination in the presenter.
* Due to the complexity of the query we had to use a raw query to get the recipients data. This means we need to handle
* pagination (which recipients to display based on selected page and page size) in here.
*
* @private
*/
function _pagination(recipients, page) {
function _paginateRecipients(recipients, page) {
const pageNumber = Number(page) * defaultPageSize

return recipients.slice(pageNumber - defaultPageSize, pageNumber)
}

/**
* Sorts, maps, and paginates the recipients list.
*
* This function first maps over the recipients to transform each recipient object into a new format, then sorts the
* resulting array of transformed recipients alphabetically by their contact's name. After sorting, it uses pagination
* to return only the relevant subset of recipients for the given page.
*
* The map and sort are performed before pagination, as it is necessary to have the recipients in a defined order before
* determining which recipients should appear on the page.
*
* @private
*/
function _recipients(recipients, page) {
const paginatedRecipients = _pagination(recipients, page)
const formattedRecipients = _formatRecipients(recipients)
const sortedRecipients = _sortRecipients(formattedRecipients)

return paginatedRecipients.map((recipient) => {
return {
contact: _contact(recipient),
licences: _licences(recipient.licence_refs),
method: `${recipient.message_type} - ${recipient.contact_type}`
return _paginateRecipients(sortedRecipients, page)
}

/**
* Sorts the recipients alphabetically by their 'contact name'.
*
* The contact name is the first element in the recipient's `contact` array. For letter-based recipients this will
* be either the person or organisation name, and for email recipients this will be the email address.
*
* @private
*/
function _sortRecipients(recipients) {
return recipients.sort((a, b) => {
if (a.contact[0] < b.contact[0]) {
return -1
}

if (a.contact[0] > b.contact[0]) {
return 1
}

return 0
})
}

Expand Down
Loading

0 comments on commit 82400e2

Please sign in to comment.