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

Removing licence from annual 2pt flags for supp billing #1236

Merged
merged 12 commits into from
Aug 7, 2024
39 changes: 39 additions & 0 deletions app/models/licence-supplementary-year.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

/**
* Model for licence_supplementary_years
* @module LicenceSupplementaryYearModel
*/

const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class LicenceSupplementaryYearModel extends BaseModel {
static get tableName () {
return 'licenceSupplementaryYears'
}

static get relationMappings () {
return {
licence: {
relation: Model.BelongsToOneRelation,
modelClass: 'licence.model',
join: {
from: 'licenceSupplementaryYears.licenceId',
to: 'licences.id'
}
},
billRun: {
relation: Model.BelongsToOneRelation,
modelClass: 'bill-run.model',
join: {
from: 'licenceSupplementaryYears.billRunId',
to: 'billRuns.id'
}
}
}
}
}

module.exports = LicenceSupplementaryYearModel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const BillRunModel = require('../../../models/bill-run.model.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 All @@ -27,7 +28,9 @@ const ReviewLicenceModel = require('../../../models/review-licence.model.js')
async function go (billRunId, licenceId, yar) {
await RemoveReviewDataService.go(billRunId, licenceId)

const licenceRef = await _flagForSupplementaryBilling(licenceId)
await _flagForSupplementaryBilling(licenceId, billRunId)

const licenceRef = await _fetchLicenceRef(licenceId)
Beckyrose200 marked this conversation as resolved.
Show resolved Hide resolved

const allLicencesRemoved = await _allLicencesRemoved(billRunId)

Expand All @@ -53,15 +56,27 @@ async function _allLicencesRemoved (billRunId) {
return false
}

async function _flagForSupplementaryBilling (licenceId) {
async function _fetchLicenceRef (licenceId) {
const licence = await LicenceModel.query()
.findById(licenceId)
.patch({ includeInSrocTptBilling: true })
.returning('licenceRef')

return licence.licenceRef
}

async function _flagForSupplementaryBilling (licenceId, billRunId) {
const financialYearEnd = await BillRunModel.query()
.findById(billRunId)
.returning('toFinancialYearEnding')

await LicenceSupplementaryYearModel.query()
Beckyrose200 marked this conversation as resolved.
Show resolved Hide resolved
.insert({
licenceId,
twoPartTariff: true,
financialYearEnd: financialYearEnd.toFinancialYearEnding
})
}

module.exports = {
go
}
2 changes: 2 additions & 0 deletions app/services/data/load/load.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const LicenceEntityRoleHelper = require('../../../../test/support/helpers/licenc
const LicenceEntityHelper = require('../../../../test/support/helpers/licence-entity.helper.js')
const LicenceGaugingStationHelper = require('../../../../test/support/helpers/licence-gauging-station.helper.js')
const LicenceRoleHelper = require('../../../../test/support/helpers/licence-role.helper.js')
const LicenceSupplementaryYearHelper = require('../../../../test/support/helpers/licence-supplementary-year.helper.js')
const LicenceVersionPurposeConditionTypeHelper = require('../../../../test/support/helpers/licence-version-purpose-condition-type.helper.js')
const LicenceVersionPurposeConditionHelper = require('../../../../test/support/helpers/licence-version-purpose-condition.helper.js')
const LicenceVersionPurposeHelper = require('../../../../test/support/helpers/licence-version-purpose.helper.js')
Expand Down Expand Up @@ -103,6 +104,7 @@ const LOAD_HELPERS = {
licenceEntities: { helper: LicenceEntityHelper, test: false },
licenceGaugingStations: { helper: LicenceGaugingStationHelper, test: true, legacy: { schema: 'water', table: 'licence_gauging_stations', id: 'licence_gauging_station_id' } },
licenceRoles: { helper: LicenceRoleHelper, test: false },
LicenceSupplementaryYears: { helper: LicenceSupplementaryYearHelper, test: false },
licenceVersionPurposeConditionTypes: { helper: LicenceVersionPurposeConditionTypeHelper, test: false },
licenceVersionPurposeConditions: { helper: LicenceVersionPurposeConditionHelper, test: false },
licenceVersionPurposes: { helper: LicenceVersionPurposeHelper, test: true, legacy: { schema: 'water', table: 'licence_version_purposes', id: 'licence_version_purpose_id' } },
Expand Down
10 changes: 10 additions & 0 deletions app/services/data/tear-down/public-schema.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ async function go () {

async function _deleteAllTestData () {
return db.raw(`
DELETE
FROM
"public"."licence_supplementary_years" AS "lsy"
USING "public"."licences" AS "l",
"public"."regions" AS "r"
WHERE
"r"."nald_region_id" = 9
AND "lsy"."licence_id" = "l"."id"
AND "l"."region_id" = "r"."id";

DELETE
FROM
"public"."review_returns" AS "rr"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

const tableName = 'licence_supplementary_years'

exports.up = function (knex) {
return knex
.schema
.createTable(tableName, (table) => {
// Primary Key
table.uuid('id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.uuid('licence_id').notNullable()
table.uuid('bill_run_id')
table.integer('financial_year_end').notNullable()
table.boolean('two_part_tariff')

// Automatic timestamps
table.timestamps(false, true)
})
.then(() => {
knex.raw(`
CREATE TRIGGER update_timestamp
BEFORE UPDATE
ON ${tableName}
FOR EACH ROW
EXECUTE PROCEDURE update_timestamp();
`)
})
}

exports.down = function (knex) {
return knex
.schema
.dropTableIfExists(tableName)
}
95 changes: 95 additions & 0 deletions test/models/licence-supplementary-year.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
'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 BillRunHelper = require('../support/helpers/bill-run.helper.js')
const BillRunModel = require('../../app/models/bill-run.model.js')
const LicenceHelper = require('../support/helpers/licence.helper.js')
const LicenceModel = require('../../app/models/licence.model.js')
const LicenceSupplementaryYearHelper = require('../support/helpers/licence-supplementary-year.helper.js')

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

describe('Licence Supplementary Year model', () => {
let testRecord

describe('Basic query', () => {
beforeEach(async () => {
testRecord = await LicenceSupplementaryYearHelper.add()
})

it('can successfully run a basic query', async () => {
const result = await LicenceSupplementaryYearModel.query().findById(testRecord.id)

expect(result).to.be.an.instanceOf(LicenceSupplementaryYearModel)
expect(result.id).to.equal(testRecord.id)
})
})

describe('Relationships', () => {
describe('when linking to a licence', () => {
let testLicence

beforeEach(async () => {
testLicence = await LicenceHelper.add()

testRecord = await LicenceSupplementaryYearHelper.add({ licenceId: testLicence.id })
})

it('can successfully run a related query', async () => {
const query = await LicenceSupplementaryYearModel.query()
.innerJoinRelated('licence')

expect(query).to.exist()
})

it('can eager load the licence', async () => {
const result = await LicenceSupplementaryYearModel.query()
.findById(testRecord.id)
.withGraphFetched('licence')

expect(result).to.be.instanceOf(LicenceSupplementaryYearModel)
expect(result.id).to.equal(testRecord.id)

expect(result.licence).to.be.instanceOf(LicenceModel)
expect(result.licence).to.equal(testLicence)
})
})

describe('when linking to a bill run', () => {
let testBillRun

beforeEach(async () => {
testBillRun = await BillRunHelper.add()

testRecord = await LicenceSupplementaryYearHelper.add({ billRunId: testBillRun.id })
})

it('can successfully run a related query', async () => {
const query = await LicenceSupplementaryYearModel.query()
.innerJoinRelated('billRun')

expect(query).to.exist()
})

it('can eager load the bill run', async () => {
const result = await LicenceSupplementaryYearModel.query()
.findById(testRecord.id)
.withGraphFetched('billRun')

expect(result).to.be.instanceOf(LicenceSupplementaryYearModel)
expect(result.id).to.equal(testRecord.id)

expect(result.billRun).to.be.instanceOf(BillRunModel)
expect(result.billRun).to.equal(testBillRun)
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ const { expect } = Code
// Test helpers
const BillRunHelper = require('../../../support/helpers/bill-run.helper.js')
const BillRunModel = require('../../../../app/models/bill-run.model.js')
const DatabaseSupport = require('../../../support/database.js')
const LicenceHelper = require('../../../support/helpers/licence.helper.js')
const LicenceModel = require('../../../../app/models/licence.model.js')
const LicenceSupplementaryYearModel = require('../../../../app/models/licence-supplementary-year.model.js')
const ReviewLicenceHelper = require('../../../support/helpers/review-licence.helper.js')

// Things we need to stub
Expand All @@ -26,8 +25,6 @@ describe('Submit Remove Bill Run Licence service', () => {
let yarStub

beforeEach(async () => {
await DatabaseSupport.clean()

Sinon.stub(RemoveReviewDataService, 'go').resolves()
yarStub = { flash: Sinon.stub() }
})
Expand All @@ -37,55 +34,61 @@ describe('Submit Remove Bill Run Licence service', () => {
})

describe('when called with a valid billRunId & licenceId', () => {
let billRunId
let licenceId
let billRun
let licence

beforeEach(async () => {
const billRun = await BillRunHelper.add({ status: 'review' })
billRunId = billRun.id
billRun = await BillRunHelper.add({ status: 'review' })

const licence = await LicenceHelper.add({ licenceRef: '01/123/ABC' })
licenceId = licence.id
licence = await LicenceHelper.add()
})

describe('which has at least one licence remaining in the bill run after the licence is removed', () => {
beforeEach(async () => {
// add an extra record to the `reviewLicence` table so that it isn't empty when the licence is removed
await ReviewLicenceHelper.add({ billRunId })
await ReviewLicenceHelper.add({ billRunId: billRun.id })
})

it('will return false as all licences are not removed and generate a banner message', async () => {
const result = await SubmitRemoveBillRunLicenceService.go(billRunId, licenceId, yarStub)
const result = await SubmitRemoveBillRunLicenceService.go(billRun.id, licence.id, yarStub)
const [flashType, bannerMessage] = yarStub.flash.args[0]

expect(result).to.be.false()

expect(yarStub.flash.called).to.be.true()
expect(flashType).to.equal('banner')
expect(bannerMessage).to.equal('Licence 01/123/ABC removed from the bill run.')
expect(bannerMessage).to.equal(`Licence ${licence.licenceRef} removed from the bill run.`)
})
})

describe('which has NO licences remain in the bill run after the licence is removed', () => {
it('will return true as no licences remain in the bill run and NO banner message is generated', async () => {
const result = await SubmitRemoveBillRunLicenceService.go(billRunId, licenceId, yarStub)
const result = await SubmitRemoveBillRunLicenceService.go(billRun.id, licence.id, yarStub)

expect(result).to.be.true()
expect(yarStub.flash.called).to.be.false()
})

it('will set the bill run status to empty', async () => {
await SubmitRemoveBillRunLicenceService.go(billRunId, licenceId, yarStub)
const { status } = await BillRunModel.query().findById(billRunId).select('status')
await SubmitRemoveBillRunLicenceService.go(billRun.id, licence.id, yarStub)
const { status } = await BillRunModel.query().findById(billRun.id).select('status')

expect(status).to.equal('empty')
})

it('will mark the licence for two-part tariff supplementary billing', async () => {
await SubmitRemoveBillRunLicenceService.go(billRunId, licenceId, yarStub)
const { includeInSrocTptBilling } = await LicenceModel.query().findById(licenceId).select('includeInSrocTptBilling')

expect(includeInSrocTptBilling).to.be.true()
await SubmitRemoveBillRunLicenceService.go(billRun.id, licence.id, yarStub)
const includeInSrocTptBilling = await LicenceSupplementaryYearModel.query()
.select([
'licenceId',
'twoPartTariff',
'financialYearEnd'
])
.where('licenceId', licence.id)

expect(includeInSrocTptBilling[0].licenceId).to.equal(licence.id)
expect(includeInSrocTptBilling[0].twoPartTariff).to.be.true()
expect(includeInSrocTptBilling[0].financialYearEnd).to.equal(billRun.toFinancialYearEnding)
})
})
})
Expand Down
Loading
Loading