Skip to content

Commit 82400e2

Browse files
authored
Merge branch 'main' into new-view-completed-monthly-return-logs-page
2 parents bb4aef5 + c40da27 commit 82400e2

File tree

5 files changed

+211
-128
lines changed

5 files changed

+211
-128
lines changed

app/presenters/notifications/setup/review.presenter.js

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { defaultPageSize } = require('../../../../config/database.config.js')
1313
*
1414
* @param {object[]} recipients - List of recipient objects, each containing recipient details like email or name.
1515
* @param {number|string} page - The currently selected page
16-
* @param {object} pagination -
16+
* @param {object} pagination - The result from `PaginatorPresenter`
1717
*
1818
* @returns {object} - The data formatted for the view template
1919
*/
@@ -29,13 +29,10 @@ function go(recipients, page, pagination) {
2929
/**
3030
* Contact can be an email or an address (letter)
3131
*
32-
* If it is an address then we convert the contact CSV string to an array
32+
* 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
33+
* array for the UI to have consistent formatting.
3334
*
34-
* If it is an email we return the email in array for the UI to have consistent formatting
35-
*
36-
* @param {object} recipient
37-
*
38-
* @returns {string[]}
35+
* @private
3936
*/
4037
function _contact(recipient) {
4138
if (recipient.email) {
@@ -44,17 +41,18 @@ function _contact(recipient) {
4441

4542
const name = contactName(recipient.contact)
4643
const address = contactAddress(recipient.contact)
44+
4745
return [name, ...address]
4846
}
4947

50-
/**
51-
* Convert the licence CSV string to an array
52-
*
53-
* @param {string} licences
54-
* @returns {string[]}
55-
*/
56-
function _licences(licences) {
57-
return licences.split(',')
48+
function _formatRecipients(recipients) {
49+
return recipients.map((recipient) => {
50+
return {
51+
contact: _contact(recipient),
52+
licences: recipient.licence_refs.split(','),
53+
method: `${recipient.message_type} - ${recipient.contact_type}`
54+
}
55+
})
5856
}
5957

6058
function _pageTitle(page, pagination) {
@@ -66,24 +64,55 @@ function _pageTitle(page, pagination) {
6664
}
6765

6866
/**
69-
* Due to the complexity of the query to get the recipients data, we handle pagination in the presenter.
67+
* 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
68+
* pagination (which recipients to display based on selected page and page size) in here.
7069
*
7170
* @private
7271
*/
73-
function _pagination(recipients, page) {
72+
function _paginateRecipients(recipients, page) {
7473
const pageNumber = Number(page) * defaultPageSize
74+
7575
return recipients.slice(pageNumber - defaultPageSize, pageNumber)
7676
}
7777

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

81-
return paginatedRecipients.map((recipient) => {
82-
return {
83-
contact: _contact(recipient),
84-
licences: _licences(recipient.licence_refs),
85-
method: `${recipient.message_type} - ${recipient.contact_type}`
94+
return _paginateRecipients(sortedRecipients, page)
95+
}
96+
97+
/**
98+
* Sorts the recipients alphabetically by their 'contact name'.
99+
*
100+
* The contact name is the first element in the recipient's `contact` array. For letter-based recipients this will
101+
* be either the person or organisation name, and for email recipients this will be the email address.
102+
*
103+
* @private
104+
*/
105+
function _sortRecipients(recipients) {
106+
return recipients.sort((a, b) => {
107+
if (a.contact[0] < b.contact[0]) {
108+
return -1
109+
}
110+
111+
if (a.contact[0] > b.contact[0]) {
112+
return 1
86113
}
114+
115+
return 0
87116
})
88117
}
89118

0 commit comments

Comments
 (0)