Skip to content

Commit

Permalink
Update submit service to save existing to session
Browse files Browse the repository at this point in the history
  • Loading branch information
Cruikshanks committed Jun 10, 2024
1 parent c953e43 commit 7380c3d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
14 changes: 13 additions & 1 deletion app/services/return-requirements/submit-existing.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const ExistingPresenter = require('../../presenters/return-requirements/existing.presenter.js')
const ExistingValidator = require('../../validators/return-requirements/existing.validator.js')
const FetchExistingRequirementsService = require('./fetch-existing-requirements.service.js')
const SessionModel = require('../../models/session.model.js')

/**
Expand All @@ -16,7 +17,8 @@ const SessionModel = require('../../models/session.model.js')
*
* The validation result is then combined with the output of the presenter to generate the page data needed by the view.
* If there was a validation error the controller will re-render the page so needs this information. If all is well the
* controller will redirect to the next page in the journey.
* controller will redirect to the next page in the journey after the existing return requirements have been saved to
* the session.
*
* @param {string} sessionId - The UUID of the current session
* @param {Object} payload - The submitted form data
Expand All @@ -30,6 +32,10 @@ async function go (sessionId, payload) {
const validationResult = _validate(payload, session)

if (!validationResult) {
const existingReturnRequirements = await FetchExistingRequirementsService.go(payload.existing)

await _save(session, existingReturnRequirements)

return {}
}

Expand All @@ -43,6 +49,12 @@ async function go (sessionId, payload) {
}
}

async function _save (session, existingReturnRequirements) {
session.requirements = existingReturnRequirements

return session.$update()
}

function _validate (payload, session) {
const { licence: { returnVersions } } = session

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, beforeEach } = exports.lab = Lab.script()
const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
const { expect } = Code

// Test helpers
const DatabaseSupport = require('../../support/database.js')
const SessionHelper = require('../../support/helpers/session.helper.js')

// Things we need to stub
const FetchExistingRequirementsService = require('../../../app/services/return-requirements/fetch-existing-requirements.service.js')

// Thing under test
const SubmitExistingService = require('../../../app/services/return-requirements/submit-existing.service.js')

Expand Down Expand Up @@ -47,19 +51,33 @@ describe('Return Requirements - Submit Existing service', () => {
session = await SessionHelper.add(sessionData)
})

afterEach(() => {
Sinon.restore()
})

describe('when called', () => {
describe('with a valid payload', () => {
beforeEach(() => {
payload = {
existing: '60b5d10d-1372-4fb2-b222-bfac81da69ab'
}

Sinon.stub(FetchExistingRequirementsService, 'go').resolves([_transformedReturnRequirement()])
})

it('returns the correct details the controller needs to redirect the journey', async () => {
const result = await SubmitExistingService.go(session.id, payload)

expect(result).to.equal({})
})

it('saves the selected existing return requirements', async () => {
await SubmitExistingService.go(session.id, payload)

const refreshedSession = await session.$query()

expect(refreshedSession.requirements).to.equal([_transformedReturnRequirement()])
})
})

describe('with an invalid payload', () => {
Expand Down Expand Up @@ -88,3 +106,21 @@ describe('Return Requirements - Submit Existing service', () => {
})
})
})

function _transformedReturnRequirement () {
return {
points: ['1234'],
purposes: ['1a1a68cc-b1f5-43db-8d1a-3452425bcc68'],
returnsCycle: 'winter-and-all-year',
siteDescription: 'FIRST BOREHOLE AT AVALON',
abstractionPeriod: {
'end-abstraction-period-day': 31,
'end-abstraction-period-month': 3,
'start-abstraction-period-day': 1,
'start-abstraction-period-month': 4
},
frequencyReported: 'weekly',
frequencyCollected: 'weekly',
agreementsExceptions: ['none']
}
}

0 comments on commit 7380c3d

Please sign in to comment.