Skip to content

Commit

Permalink
Exclude not-required rtn versions from copy exist (#1086)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4283

> Part of the work to replace NALD for handling return requirements

We want to be able to offer users the option to copy data from an existing return requirement when setting up a new one. We [made the option available](#1081) in the `/setup` page.

We then implemented the logic to fetch the existing data and transform it into something that can be stored in the session for later persisting as a new record.

One of the new features we're enabling with our takeover is the ability to record where a return requirement is _not_ needed. Our new journey allows for this.

When this happens, though, we'll need to persist a return version to record it. It will be 'current' but it will have no return requirements linked to it.

Because of this, we need to tweak the query that fetches which return versions can be copied from to exclude those without any return requirements.
  • Loading branch information
Cruikshanks authored Jun 10, 2024
1 parent 41c1456 commit d91da77
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 16 deletions.
7 changes: 7 additions & 0 deletions app/services/return-requirements/initiate-session.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const Boom = require('@hapi/boom')

const LicenceModel = require('../../../app/models/licence.model.js')
const ReturnRequirementModel = require('../../../app/models/return-requirement.model.js')
const SessionModel = require('../../models/session.model.js')

/**
Expand Down Expand Up @@ -93,6 +94,12 @@ async function _fetchLicence (licenceId) {
'reason'
])
.where('status', 'current')
// A return version must include return requirements in order for us to be able to copy from it
.whereExists(
ReturnRequirementModel.query()
.select(1)
.whereColumn('returnVersions.id', 'returnRequirements.returnVersionId')
)
.orderBy('startDate', 'desc')
})
// See licence.model.js `static get modifiers` if you are unsure about what this is doing
Expand Down
91 changes: 75 additions & 16 deletions test/services/return-requirements/initiate-session.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const DatabaseSupport = require('../../support/database.js')
const LicenceHelper = require('../../support/helpers/licence.helper.js')
const LicenceVersionHelper = require('../../support/helpers/licence-version.helper.js')
const LicenceHolderSeeder = require('../../support/seeders/licence-holder.seeder.js')
const ReturnRequirementHelper = require('../../support/helpers/return-requirement.helper.js')
const ReturnVersionHelper = require('../../support/helpers/return-version.helper.js')

// Thing under test
Expand All @@ -20,6 +21,7 @@ const InitiateSessionService = require('../../../app/services/return-requirement
describe('Return Requirements - Initiate Session service', () => {
let journey
let licence
let returnVersionId

beforeEach(async () => {
await DatabaseSupport.clean()
Expand All @@ -28,30 +30,22 @@ describe('Return Requirements - Initiate Session service', () => {
describe('when called', () => {
describe('and the licence exists', () => {
beforeEach(async () => {
journey = 'returns-required'

// Create the licence record with an 'end' date so we can confirm the session gets populated with the licence's
// 'ends' information
licence = await LicenceHelper.add({ expiredDate: new Date('2024-08-10'), licenceRef: '01/ABC' })

// Create 2 licence versions so we can test the service only gets the 'current' version
// Create two licence versions so we can test the service only gets the 'current' version
await LicenceVersionHelper.add({
licenceId: licence.id, startDate: new Date('2021-10-11'), status: 'superseded'
})
await LicenceVersionHelper.add({
licenceId: licence.id, startDate: new Date('2022-05-01')
})

// Create 2 return versions so we can test the service only gets the 'current' version
await ReturnVersionHelper.add({
licenceId: licence.id, startDate: new Date('2021-10-11'), status: 'superseded'
})
await ReturnVersionHelper.add({
id: '7aac9bce-bb11-40c9-81b5-fda735c3d51c', licenceId: licence.id, startDate: new Date('2022-05-01')
})

// Create a licence holder for the licence with the default name 'Licence Holder Ltd'
await LicenceHolderSeeder.seed(licence.licenceRef)

journey = 'returns-required'
})

it('creates a new session record containing details of the licence', async () => {
Expand All @@ -67,17 +61,82 @@ describe('Return Requirements - Initiate Session service', () => {
endDate: new Date('2024-08-10'),
licenceRef: '01/ABC',
licenceHolder: 'Licence Holder Ltd',
returnVersions: [{
id: '7aac9bce-bb11-40c9-81b5-fda735c3d51c',
reason: 'new-licence',
startDate: new Date('2022-05-01')
}],
returnVersions: [],
startDate: new Date('2022-01-01')
},
journey: 'returns-required',
requirements: [{}]
}, { skip: ['id'] })
})

describe('and has return versions with return requirements to copy from', () => {
beforeEach(async () => {
const returnVersion = await ReturnVersionHelper.add({
licenceId: licence.id, startDate: new Date('2022-05-01')
})
returnVersionId = returnVersion.id

await ReturnRequirementHelper.add({ returnVersionId })
})

it('includes details of the return versions in the session record created', async () => {
const result = await InitiateSessionService.go(licence.id, journey)

const { returnVersions } = result.data.licence

expect(returnVersions).to.equal([{
id: returnVersionId,
reason: 'new-licence',
startDate: new Date('2022-05-01')
}])
})
})

describe('and has return versions but they are not "current" (so cannot be copied from)', () => {
beforeEach(async () => {
const returnVersion = await ReturnVersionHelper.add({
licenceId: licence.id, startDate: new Date('2021-10-11'), status: 'superseded'
})

returnVersionId = returnVersion.id

await ReturnRequirementHelper.add({ returnVersionId })
})

it('does not contain any return version details in the session record created', async () => {
const result = await InitiateSessionService.go(licence.id, journey)

const { returnVersions } = result.data.licence

expect(returnVersions).to.be.empty()
})
})

describe('and has return versions but they do not have requirements (so cannot be copied from)', () => {
beforeEach(async () => {
await ReturnVersionHelper.add({
licenceId: licence.id, reason: 'transfer-licence', startDate: new Date('2021-10-11'), status: 'current'
})
})

it('does not contain any return version details in the session record created', async () => {
const result = await InitiateSessionService.go(licence.id, journey)

const { returnVersions } = result.data.licence

expect(returnVersions).to.be.empty()
})
})

describe('and has no return versions (so nothing to copy from)', () => {
it('does not contain any return version details in the session record created', async () => {
const result = await InitiateSessionService.go(licence.id, journey)

const { returnVersions } = result.data.licence

expect(returnVersions).to.be.empty()
})
})
})

describe('but the licence does not exist', () => {
Expand Down

0 comments on commit d91da77

Please sign in to comment.