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/app/services/licences/fetch-licence-returns.service.js b/app/services/licences/fetch-licence-returns.service.js index a899f81c35..a78334693a 100644 --- a/app/services/licences/fetch-licence-returns.service.js +++ b/app/services/licences/fetch-licence-returns.service.js @@ -5,9 +5,9 @@ * @module FetchLicenceReturnsService */ -const ReturnLogModel = require('../../models/return-log.model') +const ReturnLogModel = require('../../models/return-log.model.js') -const DatabaseConfig = require('../../../config/database.config') +const DatabaseConfig = require('../../../config/database.config.js') /** * Fetches all return logs for a licence which is needed for the view '/licences/{id}/returns` page diff --git a/app/views/licences/tabs/returns.njk b/app/views/licences/tabs/returns.njk index db2861f6a5..fb8873657c 100644 --- a/app/views/licences/tabs/returns.njk +++ b/app/views/licences/tabs/returns.njk @@ -1,23 +1,7 @@ {% from "govuk/components/pagination/macro.njk" import govukPagination %} {% from "govuk/components/table/macro.njk" import govukTable %} -{% from "govuk/components/tag/macro.njk" import govukTag %} -{% macro formatStatus(status) %} - {% if status === 'COMPLETE' %} - {{ govukTag({ - text: status, - classes: "govuk-tag--green" - }) }} - - {% elseif status === 'OVERDUE' %} - {{ govukTag({ - text: status, - classes: "govuk-tag--red" - }) }} - {% else %} - {{ status }} - {% endif %} -{% endmacro %} +{% from "macros/return-status-tag.njk" import statusTag %}

Returns

@@ -33,6 +17,10 @@ {% set returnRows = [] %} {% for returnItem in returns %} + {% set returnStatusTag %} + {{ statusTag(returnItem.status, true) }} + {% endset %} + {% set returnRows = (returnRows.push([ { html: referenceColumn(returnItem) @@ -44,7 +32,7 @@ text: returnItem.dueDate }, { - html: formatStatus(returnItem.status) + html: returnStatusTag } ]), returnRows) %} {% endfor %} @@ -71,4 +59,3 @@ {% if pagination.numberOfPages > 1 %} {{ govukPagination(pagination.component) }} {% endif %} - diff --git a/app/views/macros/return-status-tag.njk b/app/views/macros/return-status-tag.njk new file mode 100644 index 0000000000..ee25ea776c --- /dev/null +++ b/app/views/macros/return-status-tag.njk @@ -0,0 +1,30 @@ +{% from "govuk/components/tag/macro.njk" import govukTag %} + +{% macro statusTag(status, inline=false) %} + {% if status === 'overdue' %} + {% set color = 'govuk-tag--orange' %} + {% elif status === 'void' %} + {% set color = 'govuk-tag--grey' %} + {% elif status === 'due' %} + {% set color = 'govuk-tag--blue' %} + {% elif status === 'received' %} + {% set color = 'govuk-tag--green' %} + {% elif status === 'complete' %} + {% set color = 'govuk-tag--green' %} + {% else %} + {% set color = 'govuk-tag--blue' %} + {% endif %} + + {% if inline %} + {% set fontSize = "" %} + {% else %} + {% set fontSize = "govuk-!-font-size-27" %} + {% endif %} + + {{ + govukTag({ + text: status, + classes: color + ' ' + fontSize + }) + }} +{% endmacro %} diff --git a/test/presenters/licences/view-licence-returns-presenter.test.js b/test/presenters/licences/view-licence-returns.presenter.test.js similarity index 94% rename from test/presenters/licences/view-licence-returns-presenter.test.js rename to test/presenters/licences/view-licence-returns.presenter.test.js index ed13c56ecd..e6ee1ae509 100644 --- a/test/presenters/licences/view-licence-returns-presenter.test.js +++ b/test/presenters/licences/view-licence-returns.presenter.test.js @@ -16,10 +16,10 @@ describe('View Licence returns presenter', () => { 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' }