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 all 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
64 changes: 57 additions & 7 deletions app/presenters/licences/view-licence.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,30 @@ 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
*
* @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
}

function _endDate (expiredDate) {
if (!expiredDate || expiredDate < Date.now()) {
return null
Expand All @@ -39,6 +47,48 @@ function _endDate (expiredDate) {
return formatLongDate(expiredDate)
}

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
}

const earliestPriorityEndDate = endDates.reduce((result, endDate) => {
return _compareEndDates(result, endDate)
})

return earliestPriorityEndDate.message
}

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
249 changes: 232 additions & 17 deletions test/services/licences/view-licence.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,257 @@ describe('View Licence service', () => {
})

describe('when a licence with a matching ID exists', () => {
describe('and it has an expired date', () => {
describe('and it does not have an expired, lapsed, or revoke date', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.expiredDate = new Date('2033-03-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result).to.equal({
id: testId,
licenceRef: '01/130/R01',
region: 'South West',
startDate: '7 March 2013',
endDate: '7 March 2033'
})
expect(result.warning).to.equal(null)
})
})

describe('and it does not have an expired date', () => {
describe('and it has an expired date, revoked date and lapsed date all in the past on the same day', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.expiredDate = new Date('2023-03-07')
fetchLicenceResult.lapsedDate = new Date('2023-03-07')
fetchLicenceResult.revokedDate = new Date('2023-03-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result).to.equal({
id: testId,
licenceRef: '01/130/R01',
region: 'South West',
startDate: '7 March 2013',
endDate: null
})
expect(result.warning).to.equal('This licence was revoked on 7 March 2023')
})
})

describe('and it has no expired date but revoked and lapsed dates are in the past on the same day', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.lapsedDate = new Date('2023-03-07')
fetchLicenceResult.revokedDate = new Date('2023-03-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.warning).to.equal('This licence was revoked on 7 March 2023')
})
})

describe('and it has no lapsed date but expired and revoked dates are in the past on the same day', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.expiredDate = new Date('2023-03-07')
fetchLicenceResult.revokedDate = new Date('2023-03-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.warning).to.equal('This licence was revoked on 7 March 2023')
})
})

describe('and it has no revoked date but expired and lapsed dates are in the past on the same day', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.expiredDate = new Date('2023-03-07')
fetchLicenceResult.lapsedDate = new Date('2023-03-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.warning).to.equal('This licence lapsed on 7 March 2023')
})
})

describe('and it only has an expired date', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.expiredDate = new Date('2023-03-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.warning).to.equal('This licence expired on 7 March 2023')
})
})

describe('and it only has a lapsed date', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.lapsedDate = new Date('2023-03-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.warning).to.equal('This licence lapsed on 7 March 2023')
})
})

describe('and it only has a revoked date', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.revokedDate = new Date('2023-03-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.warning).to.equal('This licence was revoked on 7 March 2023')
})
})

describe('and it has an expired and revoked date with expired being earlier', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.revokedDate = new Date('2023-03-07')
fetchLicenceResult.expiredDate = new Date('2023-02-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.warning).to.equal('This licence expired on 7 February 2023')
})
})

describe('and it has an expired and lapsed date with expired being earlier', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.expiredDate = new Date('2023-02-07')
fetchLicenceResult.lapsedDate = new Date('2023-03-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.warning).to.equal('This licence expired on 7 February 2023')
})
})

describe('and it has an expired and lapsed date with lapsed being earlier', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.expiredDate = new Date('2023-03-07')
fetchLicenceResult.lapsedDate = new Date('2023-02-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.warning).to.equal('This licence lapsed on 7 February 2023')
})
})

describe('and it has an expired and revoked date with revoked being earlier', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.expiredDate = new Date('2023-03-07')
fetchLicenceResult.revokedDate = new Date('2023-02-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.warning).to.equal('This licence was revoked on 7 February 2023')
})
})

describe('and it has a revoked and lapsed date with lapsed being earlier', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.lapsedDate = new Date('2023-02-07')
fetchLicenceResult.revokedDate = new Date('2023-03-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.warning).to.equal('This licence lapsed on 7 February 2023')
})
})

describe('and it has a revoked and lapsed date with revoked being earlier', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.revokedDate = new Date('2023-02-07')
fetchLicenceResult.lapsedDate = new Date('2023-03-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.warning).to.equal('This licence was revoked on 7 February 2023')
})
})

describe('and it has a revoked, expired and lapsed date with revoked being earlier', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.revokedDate = new Date('2023-02-07')
fetchLicenceResult.lapsedDate = new Date('2023-03-07')
fetchLicenceResult.expiredDate = new Date('2023-03-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.warning).to.equal('This licence was revoked on 7 February 2023')
})
})

describe('and it has a revoked, expired and lapsed date with expired being earlier', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.revokedDate = new Date('2023-03-07')
fetchLicenceResult.lapsedDate = new Date('2023-03-07')
fetchLicenceResult.expiredDate = new Date('2023-02-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.warning).to.equal('This licence expired on 7 February 2023')
})
})

describe('and it has a revoked, expired and lapsed date with lapsed being earlier', () => {
beforeEach(() => {
fetchLicenceResult = _licenceData()
fetchLicenceResult.revokedDate = new Date('2023-03-07')
fetchLicenceResult.lapsedDate = new Date('2023-02-07')
fetchLicenceResult.expiredDate = new Date('2023-03-07')
Sinon.stub(FetchLicenceService, 'go').resolves(fetchLicenceResult)
})

it('will return the data and format it for use in the licence summary page', async () => {
const result = await ViewLicenceService.go(testId)

expect(result.warning).to.equal('This licence lapsed on 7 February 2023')
})
})
})
Expand Down
Loading