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

Licences in Workflow should not be considered #176

Merged
merged 6 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions app/models/water/charge-version-workflow.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

/**
* Model for chargeVersionWorkflows
* @module ChargeVersionWorkflowModel
*/

const { Model } = require('objection')

const WaterBaseModel = require('./water-base.model.js')

class ChargeVersionWorkflowModel extends WaterBaseModel {
static get tableName () {
return 'chargeVersionWorkflows'
}

static get idColumn () {
return 'chargeVersionWorkflowId'
}

static get translations () {
return [
{ database: 'dateCreated', model: 'createdAt' },
{ database: 'dateUpdated', model: 'updatedAt' }
]
}

static get relationMappings () {
return {
licence: {
relation: Model.BelongsToOneRelation,
modelClass: 'licence.model',
join: {
from: 'chargeVersionWorkflows.licenceId',
to: 'licences.licenceId'
}
}
}
}
}

module.exports = ChargeVersionWorkflowModel
8 changes: 8 additions & 0 deletions app/models/water/licence.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class LicenceModel extends WaterBaseModel {
from: 'licences.licenceId',
to: 'billingInvoiceLicences.licenceId'
}
},
chargeVersionWorkflows: {
relation: Model.HasManyRelation,
modelClass: 'charge-version-workflow.model',
join: {
from: 'licences.licenceId',
to: 'chargeVersionWorkflows.licenceId'
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { ref } = require('objection')
*/

const ChargeVersion = require('../../models/water/charge-version.model.js')
const ChargeVersionWorkflow = require('../../models/water/charge-version-workflow.model.js')

/**
* Fetch all SROC charge versions linked to licences flagged for supplementary billing that are in the period being
Expand Down Expand Up @@ -42,6 +43,11 @@ async function _fetch (regionId, billingPeriod) {
.where('chargeVersions.status', 'current')
.where('chargeVersions.startDate', '>=', billingPeriod.startDate)
.where('chargeVersions.startDate', '<=', billingPeriod.endDate)
.whereNotExists(
ChargeVersionWorkflow.query()
.select(1)
.whereColumn('chargeVersions.licenceId', 'chargeVersionWorkflows.licenceId')
)
.orderBy([
{ column: 'chargeVersions.invoiceAccountId' },
{ column: 'chargeVersions.licenceId' }
Expand Down
33 changes: 33 additions & 0 deletions db/migrations/20230327095020_create-charge-version-workflows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

const tableName = 'charge_version_workflows'

exports.up = async function (knex) {
await knex
.schema
.withSchema('water')
.createTable(tableName, table => {
// Primary Key
table.uuid('charge_version_workflow_id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.uuid('licence_id').notNullable()
table.jsonb('created_by')
table.string('approver_comments')
table.string('status')
table.jsonb('data')
table.uuid('licence_version_id')
table.timestamp('date_deleted', { useTz: false })

// Legacy timestamps
table.timestamp('date_created', { useTz: false }).notNullable()
table.timestamp('date_updated', { useTz: false })
})
}

exports.down = function (knex) {
return knex
.schema
.withSchema('water')
.dropTableIfExists(tableName)
}
68 changes: 68 additions & 0 deletions test/models/water/charge-version-workflow.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'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 ChargeVersionWorkflowHelper = require('../../support/helpers/water/charge-version-workflow.helper.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')
const LicenceHelper = require('../../support/helpers/water/licence.helper.js')
const LicenceModel = require('../../../app/models/water/licence.model.js')

// Thing under test
const ChargeVersionWorkflowModel = require('../../../app/models/water/charge-version-workflow.model.js')

describe('Charge Version Workflow model', () => {
let testRecord

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

testRecord = await ChargeVersionWorkflowHelper.add()
})

describe('Basic query', () => {
it('can successfully run a basic query', async () => {
const result = await ChargeVersionWorkflowModel.query().findById(testRecord.chargeVersionWorkflowId)

expect(result).to.be.an.instanceOf(ChargeVersionWorkflowModel)
expect(result.chargeVersionWorkflowId).to.equal(testRecord.chargeVersionWorkflowId)
})
})

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

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

const { licenceId } = testLicence
testRecord = await ChargeVersionWorkflowHelper.add({ licenceId })
})

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

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

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

expect(result).to.be.instanceOf(ChargeVersionWorkflowModel)
expect(result.chargeVersionWorkflowId).to.equal(testRecord.chargeVersionWorkflowId)

expect(result.licence).to.be.an.instanceOf(LicenceModel)
expect(result.licence).to.equal(testLicence)
})
})
})
})
37 changes: 37 additions & 0 deletions test/models/water/licence.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const BillingInvoiceLicenceHelper = require('../../support/helpers/water/billing
const BillingInvoiceLicenceModel = require('../../../app/models/water/billing-invoice-licence.model.js')
const ChargeVersionHelper = require('../../support/helpers/water/charge-version.helper.js')
const ChargeVersionModel = require('../../../app/models/water/charge-version.model.js')
const ChargeVersionWorkflowHelper = require('../../support/helpers/water/charge-version-workflow.helper.js')
const ChargeVersionWorkflowModel = require('../../../app/models/water/charge-version-workflow.model.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')
const LicenceHelper = require('../../support/helpers/water/licence.helper.js')
const RegionHelper = require('../../support/helpers/water/region.helper.js')
Expand Down Expand Up @@ -138,5 +140,40 @@ describe('Licence model', () => {
expect(result.billingInvoiceLicences).to.include(testBillingInvoiceLicences[1])
})
})

describe('when linking to charge version workflows', () => {
let testChargeVersionWorkflows

beforeEach(async () => {
const { licenceId } = testRecord

testChargeVersionWorkflows = []
for (let i = 0; i < 2; i++) {
const chargeVersionWorkflow = await ChargeVersionWorkflowHelper.add({ licenceId })
testChargeVersionWorkflows.push(chargeVersionWorkflow)
}
})

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

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

it('can eager load the charge version workflows', async () => {
const result = await LicenceModel.query()
.findById(testRecord.licenceId)
.withGraphFetched('chargeVersionWorkflows')

expect(result).to.be.instanceOf(LicenceModel)
expect(result.licenceId).to.equal(testRecord.licenceId)

expect(result.chargeVersionWorkflows).to.be.an.array()
expect(result.chargeVersionWorkflows[0]).to.be.an.instanceOf(ChargeVersionWorkflowModel)
expect(result.chargeVersionWorkflows).to.include(testChargeVersionWorkflows[0])
expect(result.chargeVersionWorkflows).to.include(testChargeVersionWorkflows[1])
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const ChargeElementHelper = require('../../support/helpers/water/charge-element.
const ChargePurposeHelper = require('../../support/helpers/water/charge-purpose.helper.js')
const ChangeReasonHelper = require('../../support/helpers/water/change-reason.helper.js')
const ChargeVersionHelper = require('../../support/helpers/water/charge-version.helper.js')
const ChargeVersionWorkflowHelper = require('../../support/helpers/water/charge-version-workflow.helper.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')
const LicenceHelper = require('../../support/helpers/water/licence.helper.js')
const RegionHelper = require('../../support/helpers/water/region.helper.js')
Expand Down Expand Up @@ -276,5 +277,31 @@ describe('Fetch Charge Versions service', () => {
expect(result.length).to.equal(0)
})
})

describe('because the licence is in workflow', () => {
beforeEach(async () => {
billingPeriod = {
startDate: new Date('2022-04-01'),
endDate: new Date('2023-03-31')
}

const chargeVersion = await ChargeVersionHelper.add(
{},
{
includeInSupplementaryBilling: 'yes',
regionId
}
)
await ChargeVersionWorkflowHelper.add({ licenceId: chargeVersion.licenceId })

testRecords = [chargeVersion]
})

it('returns no applicable charge versions', async () => {
const result = await FetchChargeVersionsService.go(regionId, billingPeriod)

expect(result.length).to.equal(0)
})
})
})
})
56 changes: 56 additions & 0 deletions test/support/helpers/water/charge-version-workflow.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict'

/**
* @module ChargeVersionWorkflowHelper
*/

const ChargeVersionWorkflowModel = require('../../../../app/models/water/charge-version-workflow.model.js')

/**
* Add a new charge version workflow
*
* If no `data` is provided, default values will be used. These are
*
* - `licenceId` - 1acfbded-9cd4-4933-8e98-04cd9e92d884
* - `status` - to_setup - Other possible values are: changes_requested & review
* - `data` - { chargeVersion: null },
* - `createdAt` - 2022-02-23
*
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database
*
* @returns {module:ChangeReasonModel} The instance of the newly created record
*/
function add (data = {}) {
const insertData = defaults(data)

return ChargeVersionWorkflowModel.query()
.insert({ ...insertData })
.returning('*')
}

/**
* Returns the defaults used when creating a new charge version workflow
*
* It will override or append to them any data provided. Mainly used by the `add()` method, we make it available
* for use in tests to avoid having to duplicate values.
*
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database
*/
function defaults (data = {}) {
const defaults = {
licenceId: '1acfbded-9cd4-4933-8e98-04cd9e92d884',
status: 'to_setup',
data: { chargeVersion: null },
createdAt: new Date()
}

return {
...defaults,
...data
}
}

module.exports = {
add,
defaults
}