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

View licence summary abstraction periods #721

Merged
merged 11 commits into from
Feb 9, 2024
8 changes: 8 additions & 0 deletions app/models/licence-version.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class LicenceVersionModel extends BaseModel {
to: 'licences.id'
}
},
licenceVersionPurposes: {
relation: Model.HasManyRelation,
modelClass: 'licence-version-purpose.model',
join: {
from: 'licenceVersions.id',
to: 'licenceVersionPurposes.licenceVersionId'
}
},
purposes: {
relation: Model.ManyToManyRelation,
modelClass: 'purpose.model',
Expand Down
44 changes: 42 additions & 2 deletions app/presenters/licences/view-licence.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @module ViewLicencePresenter
*/

const { formatAbstractionDate } = require('../base.presenter.js')
const { formatLongDate } = require('../base.presenter.js')

/**
Expand All @@ -29,15 +30,27 @@ function go (licence) {
startDate
} = licence

const abstractionPeriods = _generateAbstractionPeriods(licenceVersions)
const purposes = _generatePurposes(licenceVersions)
let abstractionPeriodsAndPurposesLinkText = null

if (abstractionPeriods && purposes) {
const abstractionPeriodsLabel = abstractionPeriods.uniqueAbstractionPeriods.length > 1 ? 'periods' : 'period'
const purposesLabel = purposes.data.length > 1 ? 'purposes' : 'purpose'
abstractionPeriodsAndPurposesLinkText = `View details of your ${purposesLabel}, ${abstractionPeriodsLabel} and amounts`
}

return {
id,
abstractionPeriods,
abstractionPeriodsAndPurposesLinkText,
documentId: licenceDocumentHeader.id,
endDate: _endDate(expiredDate),
licenceHolder: _generateLicenceHolder(licenceHolder),
licenceName,
licenceRef,
pageTitle: `Licence ${licenceRef}`,
purposes: _generatePurposes(licenceVersions),
purposes,
region: region.displayName,
registeredTo,
startDate: formatLongDate(startDate),
Expand All @@ -53,6 +66,29 @@ function _endDate (expiredDate) {
return formatLongDate(expiredDate)
}

function _generateAbstractionPeriods (licenceVersions) {
if (!licenceVersions ||
licenceVersions.length === 0 ||
licenceVersions[0]?.licenceVersionPurposes === undefined ||
licenceVersions[0]?.licenceVersionPurposes?.length === 0
) {
return null
}

const formattedAbstactionPeriods = licenceVersions[0].licenceVersionPurposes.map((purpose) => {
const startDate = formatAbstractionDate(purpose.abstractionPeriodStartDay, purpose.abstractionPeriodStartMonth)
const endDate = formatAbstractionDate(purpose.abstractionPeriodEndDay, purpose.abstractionPeriodEndMonth)
return `${startDate} to ${endDate}`
})

const uniqueAbstractionPeriods = [...new Set(formattedAbstactionPeriods)]

return {
caption: uniqueAbstractionPeriods.length > 1 ? 'Periods of abstraction' : 'Period of abstraction',
uniqueAbstractionPeriods
}
}

function _generateLicenceHolder (licenceHolder) {
if (!licenceHolder) {
return 'Unregistered licence'
Expand All @@ -62,7 +98,11 @@ function _generateLicenceHolder (licenceHolder) {
}

function _generatePurposes (licenceVersions) {
if (!licenceVersions || licenceVersions.length === 0 || licenceVersions[0]?.purposes.length === 0) {
if (!licenceVersions ||
licenceVersions.length === 0 ||
licenceVersions[0]?.purposes === undefined ||
licenceVersions[0]?.purposes?.length === 0
) {
return null
}
const allPurposeDescriptions = licenceVersions[0].purposes.map((item) => {
Expand Down
21 changes: 20 additions & 1 deletion app/services/licences/fetch-licence.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,26 @@ async function _fetchLicence (id) {
'displayName'
])
})
.withGraphFetched('licenceVersions.[purposes]')
.withGraphFetched('licenceDocumentHeader')
.modifyGraph('licenceDocumentHeader', (builder) => {
builder.select([
'licenceDocumentHeaders.id'
])
})
.withGraphFetched('licenceVersions.[licenceVersionPurposes, purposes]')
.modifyGraph('[licenceVersionPurposes]', (builder) => {
builder.select([
'licenceVersionPurposes.abstractionPeriodStartDay',
'licenceVersionPurposes.abstractionPeriodStartMonth',
'licenceVersionPurposes.abstractionPeriodEndDay',
'licenceVersionPurposes.abstractionPeriodEndMonth'
])
})
.modifyGraph('[purposes]', (builder) => {
builder.select([
'purposes.description'
])
})
.modify('licenceHolder')
.modify('registeredToAndLicenceName')

Expand Down
24 changes: 24 additions & 0 deletions app/views/licences/tabs/summary.njk
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@
</dd>
</div>
{% endif %}

{% if abstractionPeriods %}
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">{{ abstractionPeriods.caption }}</dt>
<dd class="govuk-summary-list__value">
{% if abstractionPeriods.uniqueAbstractionPeriods.length > 5 %}
<span class="govuk-form-group govuk-!-margin-bottom-0">
There are {{ abstractionPeriods.uniqueAbstractionPeriods.length }} purposes
</span>
{% elseif abstractionPeriods.uniqueAbstractionPeriods.length > 1 %}
<ul class="govuk-list govuk-!-margin-bottom-0">
{% for abstractionPeriod in abstractionPeriods.uniqueAbstractionPeriods %}
<li>{{ abstractionPeriod }}</li>
{% endfor %}
</ul>
{% else %}
<span class="govuk-form-group govuk-!-margin-bottom-0">
{{ abstractionPeriods.uniqueAbstractionPeriods[0] }}
</span>
{% endif %}
<a href="/licences/{{ documentId }}/purposes">{{ abstractionPeriodsAndPurposesLinkText }}</a>
</dd>
</div>
{% endif %}
</dl>

<div class="govuk-grid-row">
Expand Down
Loading
Loading