-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add notifications download presenter (#1684)
* Add notifications download presenter https://eaflood.atlassian.net/browse/WATER-4776 As part of the work to implement notifications in the system repo we have a requirement to download the recipients data as a CSV. This change updates the notification download service to use the fetch download service and download recipients presenter. The download recipients presenter will map the data into a csv format. The headers are defined and set in the order expected for the csv and the rows mapped based on the recipient data. The notion of a Registered licence and unregistered licence is used in the CSV to signify the notification method (currently email / letter). --------- Co-authored-by: Alan Cruikshanks <[email protected]>
- Loading branch information
1 parent
21ad4e3
commit c43fd92
Showing
5 changed files
with
444 additions
and
6 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
app/presenters/notifications/setup/download-recipients.presenter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.