From 6fb44814f4fda702a324455438572323c42f8767 Mon Sep 17 00:00:00 2001 From: Jason Claxton Date: Thu, 3 Aug 2023 16:08:28 +0100 Subject: [PATCH 01/12] Create model for `purpose_uses` table https://eaflood.atlassian.net/browse/WATER-4046 Whilst working on ticket 4046 we have realised that we need a model for the `purpose_uses` table to be able to match the Returns to a charge purpose. The Return has a value in the `metadata` that matches to the `legacy_id` in the `purpose_uses` table. From 7fdb0fde8d205984c1bdad2deea049df61d71125 Mon Sep 17 00:00:00 2001 From: Jason Claxton Date: Thu, 3 Aug 2023 16:42:50 +0100 Subject: [PATCH 02/12] Create model for `purposes_uses` table --- app/models/water/purposes-use.model.js | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 app/models/water/purposes-use.model.js diff --git a/app/models/water/purposes-use.model.js b/app/models/water/purposes-use.model.js new file mode 100644 index 0000000000..05186b07d6 --- /dev/null +++ b/app/models/water/purposes-use.model.js @@ -0,0 +1,42 @@ +'use strict' + +/** + * Model for purposesUses + * @module PurposesUseModel + */ + +const { Model } = require('objection') + +const WaterBaseModel = require('./water-base.model.js') + +class PurposesUseModel extends WaterBaseModel { + static get tableName () { + return 'purposesUses' + } + + static get idColumn () { + return 'purposeUseId' + } + + static get translations () { + return [ + { database: 'dateCreated', model: 'createdAt' }, + { database: 'dateUpdated', model: 'updatedAt' } + ] + } + + static get relationMappings () { + return { + chargePurpose: { + relation: Model.BelongsToOneRelation, + modelClass: 'charge-purpose.model', + join: { + from: 'purposeUses.purposeUseId', + to: 'chargePurposes.purposeUseId' + } + } + } + } +} + +module.exports = PurposesUseModel From b26d070f65560f15940a4ea1e8e4f09686996dbb Mon Sep 17 00:00:00 2001 From: Jason Claxton Date: Thu, 3 Aug 2023 16:48:44 +0100 Subject: [PATCH 03/12] Add relation mapping to `charge-purpose` model --- app/models/water/charge-purpose.model.js | 8 ++++++++ app/models/water/purposes-use.model.js | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/water/charge-purpose.model.js b/app/models/water/charge-purpose.model.js index fdc6de58fb..9675266e42 100644 --- a/app/models/water/charge-purpose.model.js +++ b/app/models/water/charge-purpose.model.js @@ -34,6 +34,14 @@ class ChargePurposeModel extends WaterBaseModel { from: 'chargePurposes.chargeElementId', to: 'chargeElements.chargeElementId' } + }, + purposesUse: { + relation: Model.BelongsToOneRelation, + modelClass: 'purposes-use.model', + join: { + from: 'chargePurposes.purposeUseId', + to: 'purposesUses.purposeUseId' + } } } } diff --git a/app/models/water/purposes-use.model.js b/app/models/water/purposes-use.model.js index 05186b07d6..b327150f69 100644 --- a/app/models/water/purposes-use.model.js +++ b/app/models/water/purposes-use.model.js @@ -31,7 +31,7 @@ class PurposesUseModel extends WaterBaseModel { relation: Model.BelongsToOneRelation, modelClass: 'charge-purpose.model', join: { - from: 'purposeUses.purposeUseId', + from: 'purposesUses.purposeUseId', to: 'chargePurposes.purposeUseId' } } From 1539f79c24ffedccb26884b3086b4d141b9795bf Mon Sep 17 00:00:00 2001 From: Jason Claxton Date: Thu, 3 Aug 2023 17:26:45 +0100 Subject: [PATCH 04/12] Create migration for `purposes_uses` table --- ...230803160405_create-water-purposes-uses.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 db/migrations/20230803160405_create-water-purposes-uses.js diff --git a/db/migrations/20230803160405_create-water-purposes-uses.js b/db/migrations/20230803160405_create-water-purposes-uses.js new file mode 100644 index 0000000000..b7c4d93357 --- /dev/null +++ b/db/migrations/20230803160405_create-water-purposes-uses.js @@ -0,0 +1,31 @@ +'use strict' + +const tableName = 'purposes_uses' + +exports.up = async function (knex) { + await knex + .schema + .withSchema('water') + .createTable(tableName, table => { + // Primary Key + table.uuid('purpose_use_id').primary().defaultTo(knex.raw('gen_random_uuid()')) + + // Data + table.string('legacy_id').notNullable().unique() + table.string('description').notNullable() + table.string('loss_factor').notNullable() + table.boolean('is_two_part_tariff').notNullable().defaultTo(false) + table.boolean('is_test').notNullable().defaultTo(false) + + // Legacy timestamps + table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now()) + table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now()) + }) +} + +exports.down = function (knex) { + return knex + .schema + .withSchema('water') + .dropTableIfExists(tableName) +} From a6cef7b6faae2c73cfc9c97f9c1671636ffbc20a Mon Sep 17 00:00:00 2001 From: Jason Claxton Date: Thu, 3 Aug 2023 17:30:06 +0100 Subject: [PATCH 05/12] Rename returns migrations --- ..._returns_schema.js => 20230802134043_create-returns-schema.js} | 0 ...eturns_returns.js => 20230802134153_create-returns-returns.js} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename db/migrations/{20230802134043_create_returns_schema.js => 20230802134043_create-returns-schema.js} (100%) rename db/migrations/{20230802134153_create_returns_returns.js => 20230802134153_create-returns-returns.js} (100%) diff --git a/db/migrations/20230802134043_create_returns_schema.js b/db/migrations/20230802134043_create-returns-schema.js similarity index 100% rename from db/migrations/20230802134043_create_returns_schema.js rename to db/migrations/20230802134043_create-returns-schema.js diff --git a/db/migrations/20230802134153_create_returns_returns.js b/db/migrations/20230802134153_create-returns-returns.js similarity index 100% rename from db/migrations/20230802134153_create_returns_returns.js rename to db/migrations/20230802134153_create-returns-returns.js From bf54f27cd7cd0a9f30f0522224c451ca1f098058 Mon Sep 17 00:00:00 2001 From: Jason Claxton Date: Thu, 3 Aug 2023 17:34:58 +0100 Subject: [PATCH 06/12] Tidy up returns-returns migration --- .../20230802134153_create-returns-returns.js | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/db/migrations/20230802134153_create-returns-returns.js b/db/migrations/20230802134153_create-returns-returns.js index 38ef567d73..316aaa3ae9 100644 --- a/db/migrations/20230802134153_create-returns-returns.js +++ b/db/migrations/20230802134153_create-returns-returns.js @@ -11,21 +11,21 @@ exports.up = async function (knex) { table.string('return_id').primary() // Data - table.string('regime') - table.string('licence_type') - table.string('licence_ref') - table.date('start_date') - table.date('end_date') - table.string('returns_frequency') - table.string('status') + table.string('regime').notNullable() + table.string('licence_type').notNullable() + table.string('licence_ref').notNullable() + table.date('start_date').notNullable() + table.date('end_date').notNullable() + table.string('returns_frequency').notNullable() + table.string('status').notNullable() table.string('source') table.jsonb('metadata') table.date('received_date') - table.string('return_requirement') - table.date('due_date') - table.boolean('under_query') + table.string('return_requirement').notNullable() + table.date('due_date').notNullable() + table.boolean('under_query').notNullable() table.string('under_query_comment') - table.boolean('is_test') + table.boolean('is_test').notNullable().defaultTo(false) table.uuid('return_cycle_id') // Legacy timestamps From dcacc5c52031ff92fd353a4f120195d9570d3e58 Mon Sep 17 00:00:00 2001 From: Jason Claxton Date: Thu, 3 Aug 2023 17:51:02 +0100 Subject: [PATCH 07/12] Create purposes-use helper --- .../helpers/water/purposes-use.helper.js | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 test/support/helpers/water/purposes-use.helper.js diff --git a/test/support/helpers/water/purposes-use.helper.js b/test/support/helpers/water/purposes-use.helper.js new file mode 100644 index 0000000000..859f06bc2e --- /dev/null +++ b/test/support/helpers/water/purposes-use.helper.js @@ -0,0 +1,56 @@ +'use strict' + +/** + * @module PurposesUseHelper + */ + +const PurposesUseModel = require('../../../../app/models/water/purposes-use.model.js') + +/** + * Add a new purposes use + * + * If no `data` is provided, default values will be used. These are + * + * - `legacyId` - 420 + * - `description` - Spray Irrigation - Storage + * - `lossFactor` - high + * - `isTwoPartTarrif` - true + * + * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database + * + * @returns {module:PurposesUseModel} The instance of the newly created record + */ +function add (data = {}) { + const insertData = defaults(data) + + return PurposesUseModel.query() + .insert({ ...insertData }) + .returning('*') +} + +/** + * Returns the defaults used when creating a new purposes use + * + * 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 = { + legacyId: '420', + description: 'Spray Irrigation - Storage', + lossFactor: 'high', + isTwoPartTarrif: true + } + + return { + ...defaults, + ...data + } +} + +module.exports = { + add, + defaults +} From ea27d36ccecccd3b3d5204ec7e97ad88e95c2c90 Mon Sep 17 00:00:00 2001 From: Jason Claxton Date: Thu, 3 Aug 2023 18:00:18 +0100 Subject: [PATCH 08/12] Fix returns model test --- db/migrations/20230802134153_create-returns-returns.js | 2 +- test/support/helpers/returns/return.helper.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/db/migrations/20230802134153_create-returns-returns.js b/db/migrations/20230802134153_create-returns-returns.js index 316aaa3ae9..813fe6a607 100644 --- a/db/migrations/20230802134153_create-returns-returns.js +++ b/db/migrations/20230802134153_create-returns-returns.js @@ -23,7 +23,7 @@ exports.up = async function (knex) { table.date('received_date') table.string('return_requirement').notNullable() table.date('due_date').notNullable() - table.boolean('under_query').notNullable() + table.boolean('under_query').notNullable().defaultTo(false) table.string('under_query_comment') table.boolean('is_test').notNullable().defaultTo(false) table.uuid('return_cycle_id') diff --git a/test/support/helpers/returns/return.helper.js b/test/support/helpers/returns/return.helper.js index 245199f3e8..807eed9b67 100644 --- a/test/support/helpers/returns/return.helper.js +++ b/test/support/helpers/returns/return.helper.js @@ -50,6 +50,7 @@ function defaults (data = {}) { returnId: 'v1:1:9/99/99/99/9999:10021668:2022-04-01:2023-03-31', regime: 'water', licenceType: 'abstraction', + licenceRef: '9/99/99/99/9999', startDate: '2022-04-01', endDate: '2023-03-31', returnsFrequency: 'month', From 4bbcfa5e6e9cbd82fe2853dc26ecf3c8eeebb4c3 Mon Sep 17 00:00:00 2001 From: Jason Claxton Date: Thu, 3 Aug 2023 18:16:56 +0100 Subject: [PATCH 09/12] Add purposes-use relation to related model test --- .../models/water/charge-purpose.model.test.js | 32 +++++++++++++++++++ .../helpers/water/purposes-use.helper.js | 4 +-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/test/models/water/charge-purpose.model.test.js b/test/models/water/charge-purpose.model.test.js index 168e609831..d86e6a3246 100644 --- a/test/models/water/charge-purpose.model.test.js +++ b/test/models/water/charge-purpose.model.test.js @@ -12,6 +12,8 @@ const ChargeElementHelper = require('../../support/helpers/water/charge-element. const ChargeElementModel = require('../../../app/models/water/charge-element.model.js') const ChargePurposeHelper = require('../../support/helpers/water/charge-purpose.helper.js') const DatabaseHelper = require('../../support/helpers/database.helper.js') +const PurposesUseModel = require('../../../app/models/water/purposes-use.model.js') +const PurposesUseHelper = require('../../support/helpers/water/purposes-use.helper.js') // Thing under test const ChargePurposeModel = require('../../../app/models/water/charge-purpose.model.js') @@ -64,5 +66,35 @@ describe('Charge Purpose model', () => { expect(result.chargeElement).to.equal(testChargeElement) }) }) + + describe('when linking to purposes use', () => { + let testPurposesUse + + beforeEach(async () => { + testPurposesUse = await PurposesUseHelper.add() + + const { purposeUseId } = testPurposesUse + testRecord = await ChargePurposeHelper.add({ purposeUseId }) + }) + + it('can successfully run a related query', async () => { + const query = await ChargePurposeModel.query() + .innerJoinRelated('purposesUse') + + expect(query).to.exist() + }) + + it('can eager load the purposes use', async () => { + const result = await ChargePurposeModel.query() + .findById(testRecord.chargePurposeId) + .withGraphFetched('purposesUse') + + expect(result).to.be.instanceOf(ChargePurposeModel) + expect(result.chargePurposeId).to.equal(testRecord.chargePurposeId) + + expect(result.purposesUse).to.be.an.instanceOf(PurposesUseModel) + expect(result.purposesUse).to.equal(testPurposesUse) + }) + }) }) }) diff --git a/test/support/helpers/water/purposes-use.helper.js b/test/support/helpers/water/purposes-use.helper.js index 859f06bc2e..1ce20200c5 100644 --- a/test/support/helpers/water/purposes-use.helper.js +++ b/test/support/helpers/water/purposes-use.helper.js @@ -14,7 +14,7 @@ const PurposesUseModel = require('../../../../app/models/water/purposes-use.mode * - `legacyId` - 420 * - `description` - Spray Irrigation - Storage * - `lossFactor` - high - * - `isTwoPartTarrif` - true + * - `isTwoPartTariff` - true * * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database * @@ -41,7 +41,7 @@ function defaults (data = {}) { legacyId: '420', description: 'Spray Irrigation - Storage', lossFactor: 'high', - isTwoPartTarrif: true + isTwoPartTariff: true } return { From e49578af9d38e326f92f47910d1804bbd1a377bf Mon Sep 17 00:00:00 2001 From: Jason Claxton Date: Thu, 3 Aug 2023 18:51:30 +0100 Subject: [PATCH 10/12] Create purposes-use model test --- test/models/water/purposes-use.model.test.js | 67 ++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 test/models/water/purposes-use.model.test.js diff --git a/test/models/water/purposes-use.model.test.js b/test/models/water/purposes-use.model.test.js new file mode 100644 index 0000000000..438e257e9a --- /dev/null +++ b/test/models/water/purposes-use.model.test.js @@ -0,0 +1,67 @@ +'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 ChargePurposeHelper = require('../../support/helpers/water/charge-purpose.helper.js') +const ChargePurposeModel = require('../../../app/models/water/charge-purpose.model.js') +const DatabaseHelper = require('../../support/helpers/database.helper.js') +const PurposesUseHelper = require('../../support/helpers/water/purposes-use.helper.js') + +// Thing under test +const PurposesUseModel = require('../../../app/models/water/purposes-use.model.js') + +describe.only('Purposes Use model', () => { + let testRecord + + beforeEach(async () => { + await DatabaseHelper.clean() + + testRecord = await PurposesUseHelper.add() + }) + + describe('Basic query', () => { + it('can successfully run a basic query', async () => { + const result = await PurposesUseModel.query().findById(testRecord.purposeUseId) + + expect(result).to.be.an.instanceOf(PurposesUseModel) + expect(result.purposeUseId).to.equal(testRecord.purposeUseId) + }) + }) + + describe('Relationships', () => { + describe('when linking to charge purpose', () => { + let testChargePurpose + + beforeEach(async () => { + const { purposeUseId } = testRecord + + testChargePurpose = await ChargePurposeHelper.add({ purposeUseId }) + }) + + it('can successfully run a related query', async () => { + const query = await PurposesUseModel.query() + .innerJoinRelated('chargePurpose') + + expect(query).to.exist() + }) + + it('can eager load the charge purpose', async () => { + const result = await PurposesUseModel.query() + .findById(testRecord.purposeUseId) + .withGraphFetched('chargePurpose') + + expect(result).to.be.instanceOf(PurposesUseModel) + expect(result.purposeUseId).to.equal(testRecord.purposeUseId) + + expect(result.chargePurpose).to.be.an.instanceOf(ChargePurposeModel) + expect(result.chargePurpose).to.equal(testChargePurpose) + }) + }) + }) +}) From 16bca80f9ac472dd8298ecb9ce490a652a3a5c1c Mon Sep 17 00:00:00 2001 From: Jason Claxton Date: Thu, 3 Aug 2023 18:57:42 +0100 Subject: [PATCH 11/12] Update migrations to follow team convention --- db/migrations/20230802134153_create-returns-returns.js | 6 +++--- db/migrations/20230803160405_create-water-purposes-uses.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/db/migrations/20230802134153_create-returns-returns.js b/db/migrations/20230802134153_create-returns-returns.js index 813fe6a607..76e62c506e 100644 --- a/db/migrations/20230802134153_create-returns-returns.js +++ b/db/migrations/20230802134153_create-returns-returns.js @@ -2,11 +2,11 @@ const tableName = 'returns' -exports.up = async function (knex) { - await knex +exports.up = function (knex) { + return knex .schema .withSchema('returns') - .createTable(tableName, table => { + .createTable(tableName, (table) => { // Primary Key table.string('return_id').primary() diff --git a/db/migrations/20230803160405_create-water-purposes-uses.js b/db/migrations/20230803160405_create-water-purposes-uses.js index b7c4d93357..b43ed851ac 100644 --- a/db/migrations/20230803160405_create-water-purposes-uses.js +++ b/db/migrations/20230803160405_create-water-purposes-uses.js @@ -2,11 +2,11 @@ const tableName = 'purposes_uses' -exports.up = async function (knex) { - await knex +exports.up = function (knex) { + return knex .schema .withSchema('water') - .createTable(tableName, table => { + .createTable(tableName, (table) => { // Primary Key table.uuid('purpose_use_id').primary().defaultTo(knex.raw('gen_random_uuid()')) From c5a45955b1a5684efa221764a7635366ee84930b Mon Sep 17 00:00:00 2001 From: Jason Claxton Date: Thu, 3 Aug 2023 19:04:23 +0100 Subject: [PATCH 12/12] Remove .only --- test/models/water/purposes-use.model.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/models/water/purposes-use.model.test.js b/test/models/water/purposes-use.model.test.js index 438e257e9a..d345d79645 100644 --- a/test/models/water/purposes-use.model.test.js +++ b/test/models/water/purposes-use.model.test.js @@ -16,7 +16,7 @@ const PurposesUseHelper = require('../../support/helpers/water/purposes-use.help // Thing under test const PurposesUseModel = require('../../../app/models/water/purposes-use.model.js') -describe.only('Purposes Use model', () => { +describe('Purposes Use model', () => { let testRecord beforeEach(async () => {