-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add change_reason model to water-abstraction-system (#134)
https://eaflood.atlassian.net/browse/WATER-3908 `change_reason` is linked to a charge version record. We have identified it is used to determine if the change triggers a minimum charge. - add the model - update relationships - add unit tests for model - update charge version model unit tests - add test helper
- Loading branch information
Showing
7 changed files
with
264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict' | ||
|
||
/** | ||
* Model for changeReasons | ||
* @module ChangeReasonModel | ||
*/ | ||
|
||
const { Model } = require('objection') | ||
|
||
const WaterBaseModel = require('./water-base.model.js') | ||
|
||
class ChangeReasonModel extends WaterBaseModel { | ||
static get tableName () { | ||
return 'changeReasons' | ||
} | ||
|
||
static get idColumn () { | ||
return 'changeReasonId' | ||
} | ||
|
||
static get translations () { | ||
return [ | ||
{ database: 'dateCreated', model: 'createdAt' }, | ||
{ database: 'dateUpdated', model: 'updatedAt' } | ||
] | ||
} | ||
|
||
static get relationMappings () { | ||
return { | ||
chargeVersions: { | ||
relation: Model.HasManyRelation, | ||
modelClass: 'charge-version.model', | ||
join: { | ||
from: 'changeReasons.changeReasonId', | ||
to: 'chargeVersions.changeReasonId' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = ChangeReasonModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
db/migrations/20230302110453_create-water-change-reasons.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict' | ||
|
||
const tableName = 'change_reasons' | ||
|
||
exports.up = async function (knex) { | ||
await knex | ||
.schema | ||
.withSchema('water') | ||
.createTable(tableName, table => { | ||
// Primary Key | ||
table.uuid('change_reason_id').primary().defaultTo(knex.raw('gen_random_uuid()')) | ||
|
||
// Data | ||
table.string('description').notNullable() | ||
table.boolean('triggers_minimum_charge').notNullable().defaultTo(false) | ||
table.string('type').notNullable() | ||
table.boolean('is_enabled_for_new_charge_versions').notNullable() | ||
|
||
// 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) | ||
} |
23 changes: 23 additions & 0 deletions
23
db/migrations/20230302110745_alter-water-charge-versions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict' | ||
|
||
const tableName = 'charge_versions' | ||
|
||
exports.up = async function (knex) { | ||
await knex | ||
.schema | ||
.withSchema('water') | ||
.alterTable(tableName, table => { | ||
table.uuid('change_reason_id') | ||
}) | ||
} | ||
|
||
exports.down = async function (knex) { | ||
return knex | ||
.schema | ||
.withSchema('water') | ||
.alterTable(tableName, table => { | ||
table.dropColumns( | ||
'change_reason_id' | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'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 ChangeReasonHelper = require('../../support/helpers/water/change-reason.helper.js') | ||
const ChargeVersionHelper = require('../../support/helpers/water/charge-version.helper.js') | ||
const ChargeVersionModel = require('../../../app/models/water/charge-version.model.js') | ||
const DatabaseHelper = require('../../support/helpers/database.helper.js') | ||
|
||
// Thing under test | ||
const ChangeReasonModel = require('../../../app/models/water/change-reason.model.js') | ||
|
||
describe('Change Reason model', () => { | ||
let testRecord | ||
|
||
beforeEach(async () => { | ||
await DatabaseHelper.clean() | ||
|
||
testRecord = await ChangeReasonHelper.add() | ||
}) | ||
|
||
describe('Basic query', () => { | ||
it('can successfully run a basic query', async () => { | ||
const result = await ChangeReasonModel.query().findById(testRecord.changeReasonId) | ||
|
||
expect(result).to.be.an.instanceOf(ChangeReasonModel) | ||
expect(result.changeReasonId).to.equal(testRecord.changeReasonId) | ||
}) | ||
}) | ||
|
||
describe('Relationships', () => { | ||
describe('when linking to charge versions', () => { | ||
let testChargeVersions | ||
|
||
beforeEach(async () => { | ||
const { changeReasonId } = testRecord | ||
|
||
testChargeVersions = [] | ||
for (let i = 0; i < 2; i++) { | ||
const chargeVersion = await ChargeVersionHelper.add({ changeReasonId }) | ||
testChargeVersions.push(chargeVersion) | ||
} | ||
}) | ||
|
||
it('can successfully run a related query', async () => { | ||
const query = await ChangeReasonModel.query() | ||
.innerJoinRelated('chargeVersions') | ||
|
||
expect(query).to.exist() | ||
}) | ||
|
||
it('can eager load the charge versions', async () => { | ||
const result = await ChangeReasonModel.query() | ||
.findById(testRecord.changeReasonId) | ||
.withGraphFetched('chargeVersions') | ||
|
||
expect(result).to.be.instanceOf(ChangeReasonModel) | ||
expect(result.changeReasonId).to.equal(testRecord.changeReasonId) | ||
|
||
expect(result.chargeVersions).to.be.an.array() | ||
expect(result.chargeVersions[0]).to.be.an.instanceOf(ChargeVersionModel) | ||
expect(result.chargeVersions).to.include(testChargeVersions[0]) | ||
expect(result.chargeVersions).to.include(testChargeVersions[1]) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict' | ||
|
||
/** | ||
* @module ChangeReasonHelper | ||
*/ | ||
|
||
const ChangeReasonModel = require('../../../../app/models/water/change-reason.model.js') | ||
|
||
/** | ||
* Add a new change reason | ||
* | ||
* If no `data` is provided, default values will be used. These are | ||
* | ||
* - `description` - Strategic review of charges (SRoC) | ||
* - `type` - new_chargeable_charge_version | ||
* - `isEnabledForNewChargeVersions` - true, | ||
* - `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 ChangeReasonModel.query() | ||
.insert({ ...insertData }) | ||
.returning('*') | ||
} | ||
|
||
/** | ||
* Returns the defaults used when creating a new charge purpose | ||
* | ||
* 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 = { | ||
description: 'Strategic review of charges (SRoC)', | ||
type: 'new_chargeable_charge_version', | ||
isEnabledForNewChargeVersions: true, | ||
createdAt: new Date('2022-02-23') | ||
} | ||
|
||
return { | ||
...defaults, | ||
...data | ||
} | ||
} | ||
|
||
module.exports = { | ||
add, | ||
defaults | ||
} |