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

Add create licence supplementary years service #1256

Merged
merged 9 commits into from
Aug 15, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

const BillRunModel = require('../../../models/bill-run.model.js')
const CreateLicenceSupplementaryYearService = require('../../licences/supplementary/create-licence-supplementary-year.service.js')
const LicenceModel = require('../../../models/licence.model.js')
const LicenceSupplementaryYearModel = require('../../../models/licence-supplementary-year.model.js')
const RemoveReviewDataService = require('./remove-review-data.service.js')
const ReviewLicenceModel = require('../../../models/review-licence.model.js')

Expand Down Expand Up @@ -57,15 +57,11 @@ async function _allLicencesRemoved (billRunId) {
}

async function _flagForSupplementaryBilling (licenceId, billRunId) {
const twoPartTariff = true
const { toFinancialYearEnding } = await BillRunModel.query()
.findById(billRunId)

return LicenceSupplementaryYearModel.query()
.insert({
licenceId,
twoPartTariff: true,
financialYearEnd: toFinancialYearEnding
})
return CreateLicenceSupplementaryYearService.go(licenceId, [toFinancialYearEnding], twoPartTariff)
}

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'

/**
* Creates a licenceSupplementaryYears record based on the provided licence data
* @module CreateLicenceSupplementaryYearService
*/

const LicenceSupplementaryYearModel = require('../../../models/licence-supplementary-year.model.js')

/**
* Creates a licenceSupplementaryYears record based on the provided licence data
*
* @param {module:LicenceModel} licenceId - The UUID of the licence to be persisted
* @param {Object[]} years - An array of the years to be persisted as individual records
* @param {Boolean} twoPartTariff - If there are any two-part tariff indicators on the licence
*/
async function go (licenceId, years, twoPartTariff) {
for (const year of years) {
const existingLicenceSupplementaryYears = await _fetchExistingLicenceSupplementaryYears(
licenceId, year, twoPartTariff
)

// Create a new record only if no existing record matches all the provided properties, and where 'billRunId' is null
if (existingLicenceSupplementaryYears.length === 0) {
await _persistSupplementaryBillingYearsData(licenceId, year, twoPartTariff)
}
}
}

async function _fetchExistingLicenceSupplementaryYears (licenceId, year, twoPartTariff) {
return LicenceSupplementaryYearModel.query()
.select('id')
.where('licenceId', licenceId)
.where('financialYearEnd', year)
.where('twoPartTariff', twoPartTariff)
.where('billRunId', null)
Beckyrose200 marked this conversation as resolved.
Show resolved Hide resolved
}

async function _persistSupplementaryBillingYearsData (licenceId, year, twoPartTariff) {
Beckyrose200 marked this conversation as resolved.
Show resolved Hide resolved
return LicenceSupplementaryYearModel.query()
.insert({
licenceId,
financialYearEnd: year,
twoPartTariff
})
}

module.exports = {
go
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

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

// Test helpers
const { generateUUID } = require('../../../../app/lib/general.lib.js')
const LicenceSupplementaryYearHelper = require('../../../support/helpers/licence-supplementary-year.helper.js')
const LicenceSupplementaryYearModel = require('../../../../app/models/licence-supplementary-year.model.js')

// Thing under test
const CreateLicenceSupplementaryYearService = require('../../../../app/services/licences/supplementary/create-licence-supplementary-year.service.js')

describe('Create Licence Supplementary Years Service', () => {
let licenceId
let twoPartTariff
let years

describe('when provided a licenceId, years and twoPartTariff data', () => {
beforeEach(async () => {
licenceId = generateUUID()
twoPartTariff = true
years = [2023]
})

describe('that does not already exist', () => {
it('persists the data', async () => {
await CreateLicenceSupplementaryYearService.go(licenceId, years, twoPartTariff)

const result = await _fetchLicenceSupplementaryYears(licenceId)

expect(result).to.have.length(1)
expect(result[0].licenceId).to.equal(licenceId)
Beckyrose200 marked this conversation as resolved.
Show resolved Hide resolved
})
})

describe('that already exist', () => {
beforeEach(async () => {
await LicenceSupplementaryYearHelper.add({ licenceId, financialYearEnd: 2023, twoPartTariff: true })
})

describe('without the billRunId', () => {
it('does not persist the data', async () => {
await CreateLicenceSupplementaryYearService.go(licenceId, years, twoPartTariff)

const result = await _fetchLicenceSupplementaryYears(licenceId)

expect(result).to.have.length(1)
expect(result[0].licenceId).to.equal(licenceId)
})
})

describe('with the billRunId', () => {
let billRunId

beforeEach(async () => {
billRunId = generateUUID()

await LicenceSupplementaryYearModel.query()
.update({ billRunId })
.where('licenceId', licenceId)
.orderBy('billRunId')
})

it('persist the data', async () => {
await CreateLicenceSupplementaryYearService.go(licenceId, years, twoPartTariff)

const result = await _fetchLicenceSupplementaryYears(licenceId)

expect(result).to.have.length(2)
expect(result[0].billRunId).to.equal(billRunId)
expect(result[1].billRunId).to.equal(null)
})
})
})
})
})

function _fetchLicenceSupplementaryYears (licenceId) {
return LicenceSupplementaryYearModel.query()
.select([
'id',
'licenceId',
'billRunId',
'financialYearEnd',
'twoPartTariff'
])
.where('licenceId', licenceId)
}
Loading