Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning text to view licence page #670

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/licences.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async function view (request, h) {
const { id } = request.params

const data = await ViewLicenceService.go(id)
console.log(data)
robertparkinson marked this conversation as resolved.
Show resolved Hide resolved

return h.view('licences/view.njk', {
activeNavBar: 'search',
Expand Down
88 changes: 85 additions & 3 deletions app/presenters/licences/view-licence.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,46 @@ const { formatLongDate } = require('../base.presenter.js')
* @returns {Object} The data formatted for the view template
*/
function go (licence) {
const { expiredDate, id, licenceRef, region, startDate } = licence
const { expiredDate, id, lapsedDate, licenceRef, region, revokedDate, startDate } = licence
const warning = _generateWarningMessage(expiredDate, lapsedDate, revokedDate)

return {
id,
endDate: _endDate(expiredDate),
licenceRef,
region: region.displayName,
startDate: formatLongDate(startDate)
startDate: formatLongDate(startDate),
warning
}
}

/**
* Formats the expired date of the licence as the end date for the view
* Compares two end dates and chooses the correct one to return
*
* @param {date} firstEndDate - The first date to compare
*
robertparkinson marked this conversation as resolved.
Show resolved Hide resolved
* @param {date} secondEndDate - The second date to compare
*
* @module ViewLicencePresenter
*/
function _compareEndDates (firstEndDate, secondEndDate) {
if (firstEndDate.date.getTime() === secondEndDate.date.getTime()) {
if (firstEndDate.name === 'revoked') return firstEndDate
if (secondEndDate.name === 'revoked') return secondEndDate
if (firstEndDate.name === 'lapsed') return firstEndDate
} else if (firstEndDate.date < secondEndDate.date) {
return firstEndDate
}
return secondEndDate
}

/**
* Formats the expired date of the licence as the end date for the view
*
* @param {date} expiredDate - The expired date to be formatted if it is in the past
*
* @module ViewLicencePresenter
robertparkinson marked this conversation as resolved.
Show resolved Hide resolved
*/
function _endDate (expiredDate) {
if (!expiredDate || expiredDate < Date.now()) {
return null
Expand All @@ -39,6 +63,64 @@ function _endDate (expiredDate) {
return formatLongDate(expiredDate)
}

/**
* When given up to three possible end dates for a licence this function will determine the correct one to show
* and return it formatted in the standard way
*
* @module ViewLicencePresenter
*
* @param {date} expiredDate - The expired date or null if not present
*
* @param {date} lapsedDate - The lapsed date or null if not present
*
* @param {date} revokedDate - The revoked date or null if not present
*
* @returns {string} The warning message formatted for the view template
*/
robertparkinson marked this conversation as resolved.
Show resolved Hide resolved
function _generateWarningMessage (expiredDate, lapsedDate, revokedDate) {
const endDates = []

if (lapsedDate) {
endDates.push({
name: 'lapsed',
message: `This licence lapsed on ${formatLongDate(lapsedDate)}`,
date: lapsedDate
})
}

if (expiredDate) {
endDates.push({
name: 'expired',
message: `This licence expired on ${formatLongDate(expiredDate)}`,
date: expiredDate
})
}

if (revokedDate) {
endDates.push({
name: 'revoked',
message: `This licence was revoked on ${formatLongDate(revokedDate)}`,
date: revokedDate
})
}

if (endDates.length === 0) {
return null
}

if (endDates.length === 1) {
return endDates[0].message
}

if (endDates.length > 1) {
return endDates.reduce((result, endDate) => {
return _compareEndDates(result, endDate)
}).message
}

return null
robertparkinson marked this conversation as resolved.
Show resolved Hide resolved
}

module.exports = {
go
}
9 changes: 8 additions & 1 deletion app/views/licences/view.njk
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% extends 'layout.njk' %}
{% from "govuk/components/back-link/macro.njk" import govukBackLink %}
{% from "govuk/components/tabs/macro.njk" import govukTabs %}
{% from "govuk/components/warning-text/macro.njk" import govukWarningText %}

{% block breadcrumbs %}
{{
Expand Down Expand Up @@ -31,7 +32,13 @@
{% endset %}

{% block content %}
<h1 class="govuk-heading-l">Licence Number {{ licenceRef }}</h1>
{% if warning %}
{{ govukWarningText({
text: warning,
iconFallbackText: "Warning"
}) }}
{% endif %}
<h1 class="govuk-heading-l">Licence number {{ licenceRef }}</h1>

{{ govukTabs({
items: [
Expand Down
Loading
Loading