@@ -13,7 +13,7 @@ const { defaultPageSize } = require('../../../../config/database.config.js')
13
13
*
14
14
* @param {object[] } recipients - List of recipient objects, each containing recipient details like email or name.
15
15
* @param {number|string } page - The currently selected page
16
- * @param {object } pagination -
16
+ * @param {object } pagination - The result from `PaginatorPresenter`
17
17
*
18
18
* @returns {object } - The data formatted for the view template
19
19
*/
@@ -29,13 +29,10 @@ function go(recipients, page, pagination) {
29
29
/**
30
30
* Contact can be an email or an address (letter)
31
31
*
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.
33
34
*
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
39
36
*/
40
37
function _contact ( recipient ) {
41
38
if ( recipient . email ) {
@@ -44,17 +41,18 @@ function _contact(recipient) {
44
41
45
42
const name = contactName ( recipient . contact )
46
43
const address = contactAddress ( recipient . contact )
44
+
47
45
return [ name , ...address ]
48
46
}
49
47
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
+ } )
58
56
}
59
57
60
58
function _pageTitle ( page , pagination ) {
@@ -66,24 +64,55 @@ function _pageTitle(page, pagination) {
66
64
}
67
65
68
66
/**
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.
70
69
*
71
70
* @private
72
71
*/
73
- function _pagination ( recipients , page ) {
72
+ function _paginateRecipients ( recipients , page ) {
74
73
const pageNumber = Number ( page ) * defaultPageSize
74
+
75
75
return recipients . slice ( pageNumber - defaultPageSize , pageNumber )
76
76
}
77
77
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
+ */
78
90
function _recipients ( recipients , page ) {
79
- const paginatedRecipients = _pagination ( recipients , page )
91
+ const formattedRecipients = _formatRecipients ( recipients )
92
+ const sortedRecipients = _sortRecipients ( formattedRecipients )
80
93
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
86
113
}
114
+
115
+ return 0
87
116
} )
88
117
}
89
118
0 commit comments