Skip to content

Commit

Permalink
refactor(app): WATER-4266 integrated end date amend
Browse files Browse the repository at this point in the history
Reused the endDate from PR #681 which removes calculating the date from this branch. Also refactored adding an error class so that all error types are captured correctly.
  • Loading branch information
Demwunz committed Jan 23, 2024
1 parent dcedc89 commit 2a67b47
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
55 changes: 23 additions & 32 deletions app/presenters/return-requirements/start-date.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function go (session, error = null, payload) {
errorMessage: _error(error),
id: session.id,
licenceId: session.data.licence.id,
licenceEndDateValue: _endDate(session.data.licence),
licenceEndDateValue: session.data.licence.endDate,
licenceRef: session.data.licence.licenceRef,
licenceStartDate: _startDate(session.data.licence.startDate),
licenceStartDateValue: session.data.licence.startDate
Expand All @@ -38,24 +38,33 @@ function _dateFields (error, payload) {
{
classes: _getErrorClass(error, 'day'),
name: 'day',
value: payload['start-date-day'] || ''
value: _getFieldValue(payload, 'start-date-day')
},
{
classes: _getErrorClass(error, 'month'),

name: 'month',
value: payload['start-date-month'] || ''
value: _getFieldValue(payload, 'start-date-month')
},
{
classes: _getErrorClass(error, 'year'),
name: 'year',
value: payload['start-date-year'] || ''
value: _getFieldValue(payload, 'start-date-year')
}
]

return dateFields
}

function _getFieldValue (payload, fieldName) {
let value = null
if (payload) {
value = payload[fieldName]
}

return value
}

function _startDate (date) {
// convert string from JSONB back to JS Date format so we can format it correctly using formatLongDate utility
const dateObj = new Date(date)
Expand All @@ -64,40 +73,22 @@ function _startDate (date) {
return formattedDate
}

/**
* Returns the earliest end date value from the licence expiredDate, lapsedDate, or revokedDate properties.
* Sorts the valid date values and returns the earliest date string in ISO format.
* If no valid end dates exist, returns null.
*/
function _endDate (licence) {
const { expiredDate, lapsedDate, revokedDate } = licence
let endDate = null

const dates = [expiredDate, lapsedDate, revokedDate]
.filter(Boolean)
.map(date => new Date(date))

// If there are no valid dates, return null
if (dates.length === 0) {
return endDate
}

// Sort dates and return the earliest, if the value is null, the array will be empty
endDate = dates.sort((a, b) => a - b)[0].toISOString()

return endDate
}

function _getErrorClass (error, fieldName) {
let classes = 'govuk-input--width-2'
const baseClass = 'govuk-input--width-2'
let errorClass = ''

if (error) {
if (error.details[0].invalidFields.includes(fieldName)) {
classes = 'govuk-input--width-2 govuk-input--error'
const errorDetails = error.details[0]

const isFieldInvalid = errorDetails?.invalidFields?.includes(fieldName)
const isDateInvalid = errorDetails?.type === 'date.greater' || errorDetails?.type === 'date.less'

if (isFieldInvalid || isDateInvalid) {
errorClass = ' govuk-input--error'
}
}

return classes
return baseClass + errorClass
}

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ describe('Start Date Presenter', () => {
}
]
}

// Mock payload
payload = {
'start-date-day': '',
'start-date-month': '',
Expand Down

0 comments on commit 2a67b47

Please sign in to comment.