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

Fix view licence summary for incomplete licences #1033

Merged
merged 10 commits into from
May 19, 2024
8 changes: 4 additions & 4 deletions app/presenters/licences/view-licence-summary.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @module ViewLicenceSummaryPresenter
*/

const { formatLongDate, formatAbstractionDate } = require('../base.presenter')
const { formatLongDate, formatAbstractionDate } = require('../base.presenter.js')
const { generateAbstractionPointDetail } = require('../../lib/general.lib.js')

/**
Expand Down Expand Up @@ -120,14 +120,14 @@ function _generateAbstractionPeriods (licenceVersions) {
return null
}

const formattedAbstactionPeriods = licenceVersions[0].licenceVersionPurposes.map((purpose) => {
const formattedAbstractionPeriods = 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)]
const uniqueAbstractionPeriods = [...new Set(formattedAbstractionPeriods)]

return {
caption: uniqueAbstractionPeriods.length > 1 ? 'Periods of abstraction' : 'Period of abstraction',
Expand Down Expand Up @@ -175,7 +175,7 @@ function _generatePurposes (licenceVersions) {

function _parseAbstractionsAndSourceOfSupply (permitLicence) {
if (!permitLicence ||
permitLicence?.purposes === undefined ||
!permitLicence.purposes ||
permitLicence.purposes.length === 0 ||
permitLicence.purposes[0]?.purposePoints === undefined ||
permitLicence.purposes[0]?.purposePoints.length === 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,20 @@ const LicenceVersionPurposeModel = require('../../models/licence-version-purpose
* to and the total count of conditions for the licence version.
*/
async function go (licenceVersionId) {
const results = await LicenceVersionPurposeModel.query()
const results = await _fetch(licenceVersionId)

return _processResults(results)
}

function _fetch (licenceVersionId) {
// NOTE: We have found in testing that there are incomplete licences in the DB, for example, with no licence versions.
// If we are dealing with such a licence licenceVersionId will be undefined. As this service knows how to format the
// results for downstream services we handle it here rather than in the calling service.
if (!licenceVersionId) {
return []
}

return LicenceVersionPurposeModel.query()
.distinct([
'licenceVersionPurposes.purposeId',
'licenceVersionPurposeConditionTypes.displayTitle'
Expand All @@ -43,8 +56,6 @@ async function go (licenceVersionId) {
.orderBy([
{ column: 'licenceVersionPurposeConditionTypes.displayTitle', order: 'asc' }
])

return _processResults(results)
}

function _processResults (results) {
Expand Down
11 changes: 6 additions & 5 deletions app/services/licences/view-licence-summary.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
*/

const FetchLicenceAbstractionConditionsService = require('./fetch-licence-abstraction-conditions.service.js')
const FetchLicenceSummaryService = require('./fetch-licence-summary.service')
const ViewLicenceSummaryPresenter = require('../../presenters/licences/view-licence-summary.presenter')
const ViewLicenceService = require('./view-licence.service')
const FetchLicenceSummaryService = require('./fetch-licence-summary.service.js')
const ViewLicenceSummaryPresenter = require('../../presenters/licences/view-licence-summary.presenter.js')
const ViewLicenceService = require('./view-licence.service.js')

/**
* Orchestrates fetching and presenting the data needed for the licence summary page
*
* @param {string} licenceId - The UUID of the licence
* @param {object} auth - The auth object taken from `request.auth` containing user details
*
* @returns {Promise<Object>} an object representing the `pageData` needed by the licence summary template.
*/
Expand All @@ -24,9 +25,9 @@ async function go (licenceId, auth) {

const currentLicenceVersionId = summaryLicenceData?.licenceVersions[0]?.id

const licenceAbstractionConditions = await FetchLicenceAbstractionConditionsService.go(currentLicenceVersionId)
const abstractionConditions = await FetchLicenceAbstractionConditionsService.go(currentLicenceVersionId)

const pageData = ViewLicenceSummaryPresenter.go(summaryLicenceData, licenceAbstractionConditions)
const pageData = ViewLicenceSummaryPresenter.go(summaryLicenceData, abstractionConditions)

return {
...pageData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { describe, it, beforeEach } = exports.lab = Lab.script()
const { expect } = Code

// Thing under test
const ViewLicenceSummaryPresenter = require('../../../app/presenters/licences/view-licence-summary.presenter')
const ViewLicenceSummaryPresenter = require('../../../app/presenters/licences/view-licence-summary.presenter.js')

describe('View Licence Summary presenter', () => {
let licenceAbstractionConditions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,14 @@ describe('Fetch Licence Abstraction Conditions service', () => {
expect(result.numberOfConditions).to.equal(0)
})
})

describe('when an undefined licence version ID is passed in', () => {
it('returns an empty array of conditions, purpose IDs and 0 for the number of conditions', async () => {
const result = await FetchLicenceAbstractionConditionsService.go(undefined)

expect(result.conditions).to.be.empty()
expect(result.purposeIds).to.be.empty()
expect(result.numberOfConditions).to.equal(0)
})
})
})
6 changes: 3 additions & 3 deletions test/services/licences/view-licence-summary.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ const LicenceModel = require('../../../app/models/licence.model.js')

// Things we need to stub
const FetchLicenceAbstractionConditionsService = require('../../../app/services/licences/fetch-licence-abstraction-conditions.service.js')
const FetchLicenceSummaryService = require('../../../app/services/licences/fetch-licence-summary.service')
const ViewLicenceService = require('../../../app/services/licences/view-licence.service')
const FetchLicenceSummaryService = require('../../../app/services/licences/fetch-licence-summary.service.js')
const ViewLicenceService = require('../../../app/services/licences/view-licence.service.js')
// Thing under test
const ViewLicenceSummaryService = require('../../../app/services/licences/view-licence-summary.service')
const ViewLicenceSummaryService = require('../../../app/services/licences/view-licence-summary.service.js')

describe('View Licence service summary', () => {
const testId = '2c80bd22-a005-4cf4-a2a2-73812a9861de'
Expand Down
Loading