Skip to content

Commit

Permalink
fix(app): simplified check licence
Browse files Browse the repository at this point in the history
  • Loading branch information
Demwunz committed Mar 19, 2024
1 parent 6c2e52b commit 33ff4c0
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 139 deletions.
46 changes: 39 additions & 7 deletions app/services/return-requirements/check-licence-ended.service.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
// check-licence-ended.service.js

'use strict'

/**
* Service to check if a licence has ended.
* This service checks the 'ends' date of the licence data and determines if the licence has ended.
* Fetches and checks data needed for the view `/return-requirements/{sessionId}/check-your-answers` page
* @module CheckLicenceService
*/

const LicenceModel = require('../../models/licence.model.js')

/**
* Fetch the matching licence and return data needed for the check your answers page
*
* Was built to provide the data needed for the '/return-requirements/{id}/check-your-answers' page
*
* @param {string} id The UUID for the licence to fetch
*
* @returns {Promise<Object>} the data needed for "check your answers" page when user submits for approval
*/
async function go (id) {
const licence = await _fetchLicence(id)
const licenceEnded = _checkLicenceEnded(licence)
return licenceEnded
}

async function _fetchLicence (id) {
return LicenceModel.query()
.findById(id)
.select([
'expiredDate',
'id',
'lapsedDate',
'licenceRef',
'revokedDate',
'startDate'
])
}

/**
* Check if a licence has ended.
* If it's not within range, we assume it's either lapsed, expired or revoked
* It returns true if the licence has ended, false otherwise.
*
* @param {object} licenceData - The licence data object
* @returns {boolean} - True if the licence has ended, false otherwise
*/
async function checkLicenceEnded (licenceData) {
const { ends } = licenceData
function _checkLicenceEnded (licence) {
const { $ends: ends } = licence

if (!ends) {
return false
Expand All @@ -26,5 +58,5 @@ async function checkLicenceEnded (licenceData) {
}

module.exports = {
checkLicenceEnded
go
}
48 changes: 0 additions & 48 deletions app/services/return-requirements/fetch-licence.service.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
*/
const CheckLicenceEndedService = require('./check-licence-ended.service.js')
const SessionModel = require('../../models/session.model.js')
const FetchLicenceService = require('./fetch-licence.service.js')
const ExpandedError = require('../../errors/expanded.error.js')

async function go (sessionId) {
const session = await SessionModel.query().findById(sessionId)
const licenceData = await FetchLicenceService.go(session.data.licence.id)
const isLicenceEnded = await CheckLicenceEndedService.checkLicenceEnded(licenceData)
const licenceData = await CheckLicenceEndedService.go(session.data.licence.id)
const isLicenceEnded = await licenceData.ended

if (isLicenceEnded) {
throw new ExpandedError('Invalid return requirement', { licenceData })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,30 @@
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

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

// Test helpers
const DatabaseSupport = require('../../support/database.js')
const LicenceHelper = require('../../support/helpers/licence.helper.js')

// Thing under test
const CheckLicenceEndedService = require('../../../app/services/return-requirements/check-licence-ended.service.js')

describe('Check Licence Ended Service', () => {
describe('when the licence has not ended', () => {
it('returns false', async () => {
// tomorrow
const futureDate = new Date()
futureDate.setDate(futureDate.getDate() + 1)

const licenceData = {
ends: {
date: futureDate.toISOString()
}
}
describe('CheckLicenceEndedService', () => {
let licence

const result = await CheckLicenceEndedService.checkLicenceEnded(licenceData)
expect(result).to.be.false()
})
beforeEach(async () => {
await DatabaseSupport.clean()
})

describe('when the licence has ended', () => {
it('returns true', async () => {
// a date in the past
const pastDate = new Date('2024-02-14')

const licenceData = {
ends: {
date: pastDate.toISOString()
}
}

const result = await CheckLicenceEndedService.checkLicenceEnded(licenceData)
expect(result).to.be.true()
describe('go', () => {
beforeEach(async () => {
licence = await LicenceHelper.add()
})
})

describe('when the licence ends data is not available', () => {
it('returns false', async () => {
const licenceData = {}

const result = await CheckLicenceEndedService.checkLicenceEnded(licenceData)
it('fetches licence data correctly', async () => {
const result = await CheckLicenceEndedService.go(licence.id)
expect(result).to.be.false()
})
})
Expand Down
41 changes: 0 additions & 41 deletions test/services/return-requirements/fetch-licence.service.test.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const SessionHelper = require('../../support/helpers/session.helper.js')
const ExpandedError = require('../../../app/errors/expanded.error.js')

// Thing under test
const FetchLicenceService = require('../../../app/services/return-requirements/fetch-licence.service.js')
const CheckLicenceEndedService = require('../../../app/services/return-requirements/check-licence-ended.service.js')
const SubmitCheckYourAnswersService = require('../../../app/services/return-requirements/submit-check-your-answers.service.js')

describe('Submit Check Your Answers service', () => {
Expand All @@ -39,10 +39,7 @@ describe('Submit Check Your Answers service', () => {

describe('POST /return-requirements/{sessionDd}/check-your-answers', () => {
beforeEach(() => {
Sinon.stub(FetchLicenceService, 'go').resolves({
id: licenceId,
ends: null
})
Sinon.stub(CheckLicenceEndedService, 'go').resolves(false)
})

describe('When called with a valid licence', () => {
Expand Down

0 comments on commit 33ff4c0

Please sign in to comment.