From c2ce9f4ff83ea0da587511b97256278b4513d3bc Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Sun, 16 Jun 2024 12:17:23 +0100 Subject: [PATCH] Update how presenter determines status --- .../view-licence-returns.presenter.js | 33 +++++++++++-------- .../view-licence-returns.presenter.test.js | 12 +++---- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/app/presenters/licences/view-licence-returns.presenter.js b/app/presenters/licences/view-licence-returns.presenter.js index 93b0284157..81fd73c7ee 100644 --- a/app/presenters/licences/view-licence-returns.presenter.js +++ b/app/presenters/licences/view-licence-returns.presenter.js @@ -34,29 +34,36 @@ function _formatPurpose (purpose) { } function _formatReturnToTableRow (returns) { - return returns.map((r) => { + return returns.map((returnLog) => { return { - dates: `${formatLongDate(new Date(r.startDate))} to ${formatLongDate(new Date(r.endDate))}`, - description: r.metadata.description, - dueDate: formatLongDate(new Date(r.dueDate)), - id: r.id, - purpose: _formatPurpose(r.metadata.purposes), - reference: r.returnReference, - status: _formatStatus(r.status) + dates: `${formatLongDate(new Date(returnLog.startDate))} to ${formatLongDate(new Date(returnLog.endDate))}`, + description: returnLog.metadata.description, + dueDate: formatLongDate(new Date(returnLog.dueDate)), + id: returnLog.id, + purpose: _formatPurpose(returnLog.metadata.purposes), + reference: returnLog.returnReference, + status: _formatStatus(returnLog) } }) } -function _formatStatus (status) { +function _formatStatus (returnLog) { + const { status, dueDate } = returnLog + + // If the return is completed we are required to display it as 'complete'. This also takes priority over the other + // statues if (status === 'completed') { - return 'COMPLETE' + return 'complete' } - if (status === 'due') { - return 'OVERDUE' + // Work out if the return is overdue (status is still 'due' and it is past the due date) + const today = new Date() + if (status === 'due' && dueDate < today) { + return 'overdue' } - return 'NO STATUS' + // For all other cases we can just return the status and the return-status-tag macro will know how to display it + return status } function _noReturnsMessage (hasReturns, hasRequirements) { diff --git a/test/presenters/licences/view-licence-returns.presenter.test.js b/test/presenters/licences/view-licence-returns.presenter.test.js index ed13c56ecd..66818f7020 100644 --- a/test/presenters/licences/view-licence-returns.presenter.test.js +++ b/test/presenters/licences/view-licence-returns.presenter.test.js @@ -10,16 +10,16 @@ const { expect } = Code // Thing under test const ViewLicenceReturnsPresenter = require('../../../app/presenters/licences/view-licence-returns.presenter.js') -describe('View Licence returns presenter', () => { +describe.only('View Licence returns presenter', () => { let returnsData let hasRequirements const returnItem = { id: 'mock-id-1', - dueDate: '2012-11-28T00:00:00.000Z', + dueDate: new Date('2012-11-28'), status: 'completed', - startDate: '2020/01/02', - endDate: '2020/02/01', + startDate: new Date('2020/01/02'), + endDate: new Date('2020/02/01'), metadata: { purposes: [ { @@ -69,7 +69,7 @@ describe('View Licence returns presenter', () => { reference: '1068', purpose: 'SPRAY IRRIGATION', dueDate: '28 November 2012', - status: 'COMPLETE', + status: 'complete', dates: '2 January 2020 to 1 February 2020', description: 'empty description' }, @@ -78,7 +78,7 @@ describe('View Licence returns presenter', () => { reference: '1069', purpose: 'SPRAY IRRIGATION', dueDate: '28 November 2012', - status: 'OVERDUE', + status: 'overdue', dates: '2 January 2020 to 1 February 2020', description: 'empty description' }