From 3b3fb310e74b951488745c8313b045ba98e557dd Mon Sep 17 00:00:00 2001 From: Roble Date: Wed, 7 Aug 2024 12:03:11 +0100 Subject: [PATCH 01/24] Create `notes` table model and helper While creating the tests for the [Licence History page](https://github.com/DEFRA/water-abstraction-system/pull/1182), we noticed we are missing the model and helper for the notes table. This is being referenced when extracting charge versions for a licence and any relating notes created. As a result, while creating the tests for the `fetch-licence-history.service` database errors occurred as a result of the unknown relation between the notes and charge versions table. This PR will add the model and helper for the `notes` table in the water schema. From eecddf3e5754bd063f0a4e54afe7e9405a83e011 Mon Sep 17 00:00:00 2001 From: Roble Date: Wed, 7 Aug 2024 17:06:48 +0100 Subject: [PATCH 02/24] Add note model --- app/models/note.model.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 app/models/note.model.js diff --git a/app/models/note.model.js b/app/models/note.model.js new file mode 100644 index 0000000000..398b3dc391 --- /dev/null +++ b/app/models/note.model.js @@ -0,0 +1,31 @@ +'use strict' + +/** + * Model for notes (water.notes) + * @module NoteModel + */ + +const { Model } = require('objection') + +const BaseModel = require('./base.model.js') + +class NoteModel extends BaseModel { + static get tableName () { + return 'notes' + } + + static get relationMappings () { + return { + chargeVersions: { + relation: Model.BelongsToOneRelation, + modelClass: 'charge-version.model', + join: { + from: 'notes.id', + to: 'chargeVersions.noteId' + } + } + } + } +} + +module.exports = NoteModel From 488fe9f885521ddd0d35e0abd5b7d46601b3a2d5 Mon Sep 17 00:00:00 2001 From: Roble Date: Wed, 7 Aug 2024 17:07:20 +0100 Subject: [PATCH 03/24] Amend chargeVersion model to include relationship to notes --- app/models/charge-version.model.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/models/charge-version.model.js b/app/models/charge-version.model.js index 94a697f590..09ba1b079c 100644 --- a/app/models/charge-version.model.js +++ b/app/models/charge-version.model.js @@ -56,6 +56,14 @@ class ChargeVersionModel extends BaseModel { to: 'licences.id' } }, + notes: { + relation: Model.HasOneRelation, + modelClass: 'note.model', + join: { + from: 'chargeVersions.noteId', + to: 'notes.id' + } + }, reviewChargeVersions: { relation: Model.HasManyRelation, modelClass: 'review-charge-version.model', From 0f7c040ff27859a4caea06f212facedace1637f8 Mon Sep 17 00:00:00 2001 From: Roble Date: Wed, 7 Aug 2024 17:07:37 +0100 Subject: [PATCH 04/24] Create the migrations for the notes view --- .../public/20240807131002_create-note-view.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 db/migrations/public/20240807131002_create-note-view.js diff --git a/db/migrations/public/20240807131002_create-note-view.js b/db/migrations/public/20240807131002_create-note-view.js new file mode 100644 index 0000000000..8410b1b40b --- /dev/null +++ b/db/migrations/public/20240807131002_create-note-view.js @@ -0,0 +1,26 @@ +'use strict' + +const viewName = 'notes' + +exports.up = function (knex) { + return knex + .schema + .createView(viewName, (view) => { + view.as(knex('notes').withSchema('water').select([ + 'note_id AS id', + 'text', + 'type', + 'type_id', + 'user_id', + 'licence_id', + 'date_created AS created_at', + 'date_updated AS updated_at' + ])) + }) +} + +exports.down = function (knex) { + return knex + .schema + .dropViewIfExists(viewName) +} From 8ba459ef239331ca698e17500ba35c93d6d192b6 Mon Sep 17 00:00:00 2001 From: Roble Date: Wed, 7 Aug 2024 17:08:10 +0100 Subject: [PATCH 05/24] Create the notes table for the legacy --- .../legacy/20240807142828_water-notes.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 db/migrations/legacy/20240807142828_water-notes.js diff --git a/db/migrations/legacy/20240807142828_water-notes.js b/db/migrations/legacy/20240807142828_water-notes.js new file mode 100644 index 0000000000..6054fbbed3 --- /dev/null +++ b/db/migrations/legacy/20240807142828_water-notes.js @@ -0,0 +1,33 @@ +'use strict' + +const tableName = 'notes' + +exports.up = function (knex) { + return knex + .schema + .withSchema('water') + .createTable(tableName, (table) => { + // Primary Key + table.uuid('note_id').primary().defaultTo(knex.raw('gen_random_uuid()')) + + // Data + table.string('text').notNullable() + + // Legacy timestamps + table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now()) + table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now()) + + // Data + table.integer('user_id').notNullable() + table.string('type').notNullable() + table.uuid('type_id').notNullable() + table.uuid('licence_id') + }) +} + +exports.down = function (knex) { + return knex + .schema + .withSchema('water') + .dropTableIfExists(tableName) +} From 94d5b34e7b1a8d2bfb69da9db3ba83eed9787f71 Mon Sep 17 00:00:00 2001 From: Roble Date: Wed, 7 Aug 2024 17:08:21 +0100 Subject: [PATCH 06/24] Add the note model test --- test/models/note.model.test.js | 49 ++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 test/models/note.model.test.js diff --git a/test/models/note.model.test.js b/test/models/note.model.test.js new file mode 100644 index 0000000000..d42b9a5f55 --- /dev/null +++ b/test/models/note.model.test.js @@ -0,0 +1,49 @@ +'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 ChargeVersionHelper = require('../support/helpers/charge-version.helper.js') +const NoteHelper = require('../support/helpers/note.helper.js') + +// Thing under test +const NoteModel = require('../../app/models/note.model.js') + +describe('Note model', () => { + let testRecord + + describe('Basic query', () => { + beforeEach(async () => { + testRecord = await NoteHelper.add() + }) + + it('can successfully run a basic query', async () => { + const result = await NoteModel.query() + .findById(testRecord.id) + + expect(result).to.be.an.instanceOf(NoteModel) + expect(result.id).to.be.equal(testRecord.id) + }) + }) + + describe('Relationships', () => { + describe('when linking to charge versions', () => { + beforeEach(async () => { + testRecord = await NoteHelper.add() + await ChargeVersionHelper.add({ noteId: testRecord.id }) + }) + + it('can successfully run a related query', async () => { + const query = await NoteModel.query() + .withGraphFetched('chargeVersions') + + expect(query).to.exist() + }) + }) + }) +}) From 6a922f30072e1e5c2b86a2d88d285994244d782a Mon Sep 17 00:00:00 2001 From: Roble Date: Wed, 7 Aug 2024 17:08:31 +0100 Subject: [PATCH 07/24] Add the node helper --- test/support/helpers/note.helper.js | 60 +++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 test/support/helpers/note.helper.js diff --git a/test/support/helpers/note.helper.js b/test/support/helpers/note.helper.js new file mode 100644 index 0000000000..a1fc3c982f --- /dev/null +++ b/test/support/helpers/note.helper.js @@ -0,0 +1,60 @@ +'use strict' + +/** + * @module NoteHelper + */ + +const { generateUUID } = require('../../../app/lib/general.lib.js') +const { generateUserId } = require('./user.helper.js') +const NoteModel = require('../../../app/models/note.model.js') + +/** + * Add a new note + * + * If no `data` is provided, the default values will be used. These are + * + * - `text` - 'This is a test note' + * - `userId` - [randomly generated - 100001] + * - `type` - 'Charge version' + * - `typeId` - [random UUID] + * - `licenceId` - [random UUID] + * + * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database + * + * @returns {Promise} The instance of the newly created record + */ +function add (data = {}) { + const insertData = defaults(data) + + return NoteModel.query() + .insert({ ...insertData }) + .returning('*') +} + +/** + * Returns the defaults used + * + * 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 = { + text: 'This is a test note', + userId: generateUserId(), + type: 'Charge version', + typeId: generateUUID(), + licenceId: generateUUID() + } + + return { + ...defaults, + ...data + } +} + +module.exports = { + add, + defaults +} From 862298af97240b4af1702bb12d96ccac8d7580ee Mon Sep 17 00:00:00 2001 From: Roble Date: Mon, 12 Aug 2024 11:56:37 +0100 Subject: [PATCH 08/24] Amend down function for create-note-view migration --- .../public/20240807131002_create-note-view.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/db/migrations/public/20240807131002_create-note-view.js b/db/migrations/public/20240807131002_create-note-view.js index 8410b1b40b..116c38cd83 100644 --- a/db/migrations/public/20240807131002_create-note-view.js +++ b/db/migrations/public/20240807131002_create-note-view.js @@ -5,6 +5,7 @@ const viewName = 'notes' exports.up = function (knex) { return knex .schema + .dropViewIfExists(viewName) .createView(viewName, (view) => { view.as(knex('notes').withSchema('water').select([ 'note_id AS id', @@ -23,4 +24,16 @@ exports.down = function (knex) { return knex .schema .dropViewIfExists(viewName) + .createView(viewName, (view) => { + view.as(knex('notes').withSchema('water').select([ + 'note_id AS id', + 'text', + 'type', + 'type_id', + 'user_id', + 'licence_id', + 'date_created AS created_at', + 'date_updated AS updated_at' + ])) + }) } From 33decc000f1bf32a424d6879c733511ef8b668c1 Mon Sep 17 00:00:00 2001 From: Roble Date: Mon, 12 Aug 2024 12:11:02 +0100 Subject: [PATCH 09/24] WIP --- .../public/20240807131002_create-note-view.js | 78 +++++++++---------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/db/migrations/public/20240807131002_create-note-view.js b/db/migrations/public/20240807131002_create-note-view.js index 116c38cd83..1a64524511 100644 --- a/db/migrations/public/20240807131002_create-note-view.js +++ b/db/migrations/public/20240807131002_create-note-view.js @@ -1,39 +1,39 @@ -'use strict' - -const viewName = 'notes' - -exports.up = function (knex) { - return knex - .schema - .dropViewIfExists(viewName) - .createView(viewName, (view) => { - view.as(knex('notes').withSchema('water').select([ - 'note_id AS id', - 'text', - 'type', - 'type_id', - 'user_id', - 'licence_id', - 'date_created AS created_at', - 'date_updated AS updated_at' - ])) - }) -} - -exports.down = function (knex) { - return knex - .schema - .dropViewIfExists(viewName) - .createView(viewName, (view) => { - view.as(knex('notes').withSchema('water').select([ - 'note_id AS id', - 'text', - 'type', - 'type_id', - 'user_id', - 'licence_id', - 'date_created AS created_at', - 'date_updated AS updated_at' - ])) - }) -} +'use strict' + +const viewName = 'notes' + +exports.up = function (knex) { + return knex + .schema + .dropViewIfExists(viewName) + .createView(viewName, (view) => { + view.as(knex('notes').withSchema('water').select([ + 'note_id AS id', + 'text', + 'type', + 'type_id', + 'user_id', + 'licence_id', + 'date_created AS created_at', + 'date_updated AS updated_at' + ])) + }) +} + +exports.down = function (knex) { + return knex + .schema + .dropViewIfExists(viewName) + .createView(viewName, (view) => { + view.as(knex('notes').withSchema('water').select([ + 'note_id AS id', + 'text', + 'type', + 'type_id', + 'user_id', + 'licence_id', + 'date_created AS created_at', + 'date_updated AS updated_at' + ])) + }) +} From 6674dabf1d7441f2058cc9bc2241649e0d558458 Mon Sep 17 00:00:00 2001 From: Roble Date: Mon, 12 Aug 2024 12:13:10 +0100 Subject: [PATCH 10/24] WIP --- .../legacy/20240807142828_water-notes.js | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/db/migrations/legacy/20240807142828_water-notes.js b/db/migrations/legacy/20240807142828_water-notes.js index 6054fbbed3..84aa0e4252 100644 --- a/db/migrations/legacy/20240807142828_water-notes.js +++ b/db/migrations/legacy/20240807142828_water-notes.js @@ -1,33 +1,33 @@ -'use strict' - -const tableName = 'notes' - -exports.up = function (knex) { - return knex - .schema - .withSchema('water') - .createTable(tableName, (table) => { - // Primary Key - table.uuid('note_id').primary().defaultTo(knex.raw('gen_random_uuid()')) - - // Data - table.string('text').notNullable() - - // Legacy timestamps - table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now()) - table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now()) - - // Data - table.integer('user_id').notNullable() - table.string('type').notNullable() - table.uuid('type_id').notNullable() - table.uuid('licence_id') - }) -} - -exports.down = function (knex) { - return knex - .schema - .withSchema('water') - .dropTableIfExists(tableName) -} +'use strict' + +const tableName = 'notes' + +exports.up = function (knex) { + return knex + .schema + .withSchema('water') + .createTable(tableName, (table) => { + // Primary Key + table.uuid('note_id').primary().defaultTo(knex.raw('gen_random_uuid()')) + + // Data + table.string('text').notNullable() + + // Legacy timestamps + table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now()) + table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now()) + + // Data + table.integer('user_id').notNullable() + table.string('type').notNullable() + table.uuid('type_id').notNullable() + table.uuid('licence_id') + }) +} + +exports.down = function (knex) { + return knex + .schema + .withSchema('water') + .dropTableIfExists(tableName) +} From eba7fbe7a9e6ddd5dc011e3c4cac6db12983ac0c Mon Sep 17 00:00:00 2001 From: Roble Date: Mon, 12 Aug 2024 14:10:24 +0100 Subject: [PATCH 11/24] Change 'notes' to 'note' for charge version model relation --- app/models/charge-version.model.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/charge-version.model.js b/app/models/charge-version.model.js index 09ba1b079c..6bded29322 100644 --- a/app/models/charge-version.model.js +++ b/app/models/charge-version.model.js @@ -56,7 +56,7 @@ class ChargeVersionModel extends BaseModel { to: 'licences.id' } }, - notes: { + note: { relation: Model.HasOneRelation, modelClass: 'note.model', join: { From 50edee6e7d41734b1274a9b77b789e8c29d4165f Mon Sep 17 00:00:00 2001 From: Roble Date: Mon, 12 Aug 2024 14:13:02 +0100 Subject: [PATCH 12/24] Add note relationship test for charge version model --- test/models/charge-version.model.test.js | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/models/charge-version.model.test.js b/test/models/charge-version.model.test.js index df6f1ee081..28093d4d93 100644 --- a/test/models/charge-version.model.test.js +++ b/test/models/charge-version.model.test.js @@ -19,6 +19,8 @@ const ChargeReferenceModel = require('../../app/models/charge-reference.model.js const ChargeVersionHelper = require('../support/helpers/charge-version.helper.js') const LicenceHelper = require('../support/helpers/licence.helper.js') const LicenceModel = require('../../app/models/licence.model.js') +const NoteHelper = require('../support/helpers/note.helper.js') +const NoteModel = require('../../app/models/note.model.js') const ReviewChargeVersionHelper = require('../support/helpers/review-charge-version.helper.js') const ReviewChargeVersionModel = require('../../app/models/review-charge-version.model.js') @@ -207,6 +209,37 @@ describe('Charge Version model', () => { }) }) + describe('when linking to note', () => { + let testNote + + beforeEach(async () => { + testNote = await NoteHelper.add() + + const { id: noteId } = testNote + + testRecord = await ChargeVersionHelper.add({ noteId }) + }) + + it('can successfully run a related query', async () => { + const query = await ChargeVersionModel.query() + .innerJoinRelated('note') + + expect(query).to.exist() + }) + + it('can eager load the note', async () => { + const result = await ChargeVersionModel.query() + .findById(testRecord.id) + .withGraphFetched('note') + + expect(result).to.be.instanceOf(ChargeVersionModel) + expect(result.id).to.equal(testRecord.id) + + expect(result.note).to.be.an.instanceOf(NoteModel) + expect(result.note).to.equal(testNote) + }) + }) + describe('when linking to review charge versions', () => { let testReviewChargeVersions From 6345c44410ca74a634d06cbba32e8364299ce88b Mon Sep 17 00:00:00 2001 From: Roble Date: Mon, 12 Aug 2024 14:16:44 +0100 Subject: [PATCH 13/24] Rename relationship from `chargeVersions` to `chargeVersion` for notes --- app/models/note.model.js | 2 +- test/models/note.model.test.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/note.model.js b/app/models/note.model.js index 398b3dc391..fe75b03d94 100644 --- a/app/models/note.model.js +++ b/app/models/note.model.js @@ -16,7 +16,7 @@ class NoteModel extends BaseModel { static get relationMappings () { return { - chargeVersions: { + chargeVersion: { relation: Model.BelongsToOneRelation, modelClass: 'charge-version.model', join: { diff --git a/test/models/note.model.test.js b/test/models/note.model.test.js index d42b9a5f55..9192e8c219 100644 --- a/test/models/note.model.test.js +++ b/test/models/note.model.test.js @@ -32,7 +32,7 @@ describe('Note model', () => { }) describe('Relationships', () => { - describe('when linking to charge versions', () => { + describe('when linking to charge version', () => { beforeEach(async () => { testRecord = await NoteHelper.add() await ChargeVersionHelper.add({ noteId: testRecord.id }) @@ -40,7 +40,7 @@ describe('Note model', () => { it('can successfully run a related query', async () => { const query = await NoteModel.query() - .withGraphFetched('chargeVersions') + .withGraphFetched('chargeVersion') expect(query).to.exist() }) From 6046e39e9f79a6cfd78548c8108c883f2c87a584 Mon Sep 17 00:00:00 2001 From: Roble Date: Mon, 12 Aug 2024 15:32:00 +0100 Subject: [PATCH 14/24] Add default for type in notes table --- db/migrations/legacy/20240807142828_water-notes.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/db/migrations/legacy/20240807142828_water-notes.js b/db/migrations/legacy/20240807142828_water-notes.js index 84aa0e4252..b18249e137 100644 --- a/db/migrations/legacy/20240807142828_water-notes.js +++ b/db/migrations/legacy/20240807142828_water-notes.js @@ -12,16 +12,14 @@ exports.up = function (knex) { // Data table.string('text').notNullable() + table.integer('user_id').notNullable() + table.string('type').notNullable().defaultTo('charge_version') + table.uuid('type_id') + table.uuid('licence_id') // Legacy timestamps table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now()) table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now()) - - // Data - table.integer('user_id').notNullable() - table.string('type').notNullable() - table.uuid('type_id').notNullable() - table.uuid('licence_id') }) } From 4a64e0a6e6398d997824eae29636cf24bd84c07d Mon Sep 17 00:00:00 2001 From: Roble Date: Mon, 12 Aug 2024 15:32:37 +0100 Subject: [PATCH 15/24] Remove unused columns from being shown in note view --- .../public/20240807131002_create-note-view.js | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/db/migrations/public/20240807131002_create-note-view.js b/db/migrations/public/20240807131002_create-note-view.js index 1a64524511..53f6e9910e 100644 --- a/db/migrations/public/20240807131002_create-note-view.js +++ b/db/migrations/public/20240807131002_create-note-view.js @@ -7,13 +7,14 @@ exports.up = function (knex) { .schema .dropViewIfExists(viewName) .createView(viewName, (view) => { + // NOTE: We have commented out unused columns from the source table view.as(knex('notes').withSchema('water').select([ 'note_id AS id', - 'text', - 'type', - 'type_id', + 'text AS note', + // 'type', + // 'type_id', 'user_id', - 'licence_id', + // 'licence_id', 'date_created AS created_at', 'date_updated AS updated_at' ])) @@ -24,16 +25,4 @@ exports.down = function (knex) { return knex .schema .dropViewIfExists(viewName) - .createView(viewName, (view) => { - view.as(knex('notes').withSchema('water').select([ - 'note_id AS id', - 'text', - 'type', - 'type_id', - 'user_id', - 'licence_id', - 'date_created AS created_at', - 'date_updated AS updated_at' - ])) - }) } From 70f1ab3124650efcc7cee18486288a3d52e1c7b8 Mon Sep 17 00:00:00 2001 From: Roble Date: Mon, 12 Aug 2024 15:33:02 +0100 Subject: [PATCH 16/24] Remove unused columns from note helper --- test/support/helpers/note.helper.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/test/support/helpers/note.helper.js b/test/support/helpers/note.helper.js index a1fc3c982f..e04d9ac030 100644 --- a/test/support/helpers/note.helper.js +++ b/test/support/helpers/note.helper.js @@ -4,7 +4,6 @@ * @module NoteHelper */ -const { generateUUID } = require('../../../app/lib/general.lib.js') const { generateUserId } = require('./user.helper.js') const NoteModel = require('../../../app/models/note.model.js') @@ -13,11 +12,8 @@ const NoteModel = require('../../../app/models/note.model.js') * * If no `data` is provided, the default values will be used. These are * - * - `text` - 'This is a test note' + * - `note` - 'This is a test note' * - `userId` - [randomly generated - 100001] - * - `type` - 'Charge version' - * - `typeId` - [random UUID] - * - `licenceId` - [random UUID] * * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database * @@ -41,11 +37,8 @@ function add (data = {}) { */ function defaults (data = {}) { const defaults = { - text: 'This is a test note', - userId: generateUserId(), - type: 'Charge version', - typeId: generateUUID(), - licenceId: generateUUID() + note: 'This is a test note', + userId: generateUserId() } return { From 71e97f655e30ff18e457ba71685efa67b1f20ae4 Mon Sep 17 00:00:00 2001 From: Roble Date: Mon, 12 Aug 2024 16:29:31 +0100 Subject: [PATCH 17/24] Amend table and relationship references from 'notes' to 'charge-version-notes' --- ....model.js => charge-version-note.model.js} | 12 ++--- app/models/charge-version.model.js | 16 +++---- ...131002_create-charge-version-note-view.js} | 2 +- ...t.js => charge-version-note.model.test.js} | 23 +++++----- test/models/charge-version.model.test.js | 44 +++++++++---------- ...elper.js => charge-version-note.helper.js} | 8 ++-- 6 files changed, 52 insertions(+), 53 deletions(-) rename app/models/{note.model.js => charge-version-note.model.js} (62%) rename db/migrations/public/{20240807131002_create-note-view.js => 20240807131002_create-charge-version-note-view.js} (93%) rename test/models/{note.model.test.js => charge-version-note.model.test.js} (59%) rename test/support/helpers/{note.helper.js => charge-version-note.helper.js} (84%) diff --git a/app/models/note.model.js b/app/models/charge-version-note.model.js similarity index 62% rename from app/models/note.model.js rename to app/models/charge-version-note.model.js index fe75b03d94..d7f0e037f4 100644 --- a/app/models/note.model.js +++ b/app/models/charge-version-note.model.js @@ -1,17 +1,17 @@ 'use strict' /** - * Model for notes (water.notes) - * @module NoteModel + * Model for charge_version_notes (water.notes) + * @module ChargeVersionNote */ const { Model } = require('objection') const BaseModel = require('./base.model.js') -class NoteModel extends BaseModel { +class ChargeVersionNote extends BaseModel { static get tableName () { - return 'notes' + return 'chargeVersionNotes' } static get relationMappings () { @@ -20,7 +20,7 @@ class NoteModel extends BaseModel { relation: Model.BelongsToOneRelation, modelClass: 'charge-version.model', join: { - from: 'notes.id', + from: 'chargeVersionNotes.id', to: 'chargeVersions.noteId' } } @@ -28,4 +28,4 @@ class NoteModel extends BaseModel { } } -module.exports = NoteModel +module.exports = ChargeVersionNote diff --git a/app/models/charge-version.model.js b/app/models/charge-version.model.js index 6bded29322..38812b1df0 100644 --- a/app/models/charge-version.model.js +++ b/app/models/charge-version.model.js @@ -48,6 +48,14 @@ class ChargeVersionModel extends BaseModel { to: 'chargeReferences.chargeVersionId' } }, + chargeVersionNote: { + relation: Model.HasOneRelation, + modelClass: 'charge-version-note.model', + join: { + from: 'chargeVersions.noteId', + to: 'chargeVersionNotes.id' + } + }, licence: { relation: Model.BelongsToOneRelation, modelClass: 'licence.model', @@ -56,14 +64,6 @@ class ChargeVersionModel extends BaseModel { to: 'licences.id' } }, - note: { - relation: Model.HasOneRelation, - modelClass: 'note.model', - join: { - from: 'chargeVersions.noteId', - to: 'notes.id' - } - }, reviewChargeVersions: { relation: Model.HasManyRelation, modelClass: 'review-charge-version.model', diff --git a/db/migrations/public/20240807131002_create-note-view.js b/db/migrations/public/20240807131002_create-charge-version-note-view.js similarity index 93% rename from db/migrations/public/20240807131002_create-note-view.js rename to db/migrations/public/20240807131002_create-charge-version-note-view.js index 53f6e9910e..6358bcc7e5 100644 --- a/db/migrations/public/20240807131002_create-note-view.js +++ b/db/migrations/public/20240807131002_create-charge-version-note-view.js @@ -1,6 +1,6 @@ 'use strict' -const viewName = 'notes' +const viewName = 'charge_version_notes' exports.up = function (knex) { return knex diff --git a/test/models/note.model.test.js b/test/models/charge-version-note.model.test.js similarity index 59% rename from test/models/note.model.test.js rename to test/models/charge-version-note.model.test.js index 9192e8c219..7e4f8eb49b 100644 --- a/test/models/note.model.test.js +++ b/test/models/charge-version-note.model.test.js @@ -4,29 +4,29 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it, beforeEach } = exports.lab = Lab.script() +const { describe, it, before, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers const ChargeVersionHelper = require('../support/helpers/charge-version.helper.js') -const NoteHelper = require('../support/helpers/note.helper.js') +const ChargeVersionNoteHelper = require('../support/helpers/charge-version-note.helper.js') // Thing under test -const NoteModel = require('../../app/models/note.model.js') +const ChargeVersionNoteModel = require('../../app/models/charge-version-note.model.js') -describe('Note model', () => { +describe('Charge Version Note model', () => { let testRecord - describe('Basic query', () => { - beforeEach(async () => { - testRecord = await NoteHelper.add() - }) + before(async () => { + testRecord = await ChargeVersionNoteHelper.add() + }) + describe('Basic query', () => { it('can successfully run a basic query', async () => { - const result = await NoteModel.query() + const result = await ChargeVersionNoteModel.query() .findById(testRecord.id) - expect(result).to.be.an.instanceOf(NoteModel) + expect(result).to.be.an.instanceOf(ChargeVersionNoteModel) expect(result.id).to.be.equal(testRecord.id) }) }) @@ -34,12 +34,11 @@ describe('Note model', () => { describe('Relationships', () => { describe('when linking to charge version', () => { beforeEach(async () => { - testRecord = await NoteHelper.add() await ChargeVersionHelper.add({ noteId: testRecord.id }) }) it('can successfully run a related query', async () => { - const query = await NoteModel.query() + const query = await ChargeVersionNoteModel.query() .withGraphFetched('chargeVersion') expect(query).to.exist() diff --git a/test/models/charge-version.model.test.js b/test/models/charge-version.model.test.js index 28093d4d93..ed889f2b27 100644 --- a/test/models/charge-version.model.test.js +++ b/test/models/charge-version.model.test.js @@ -17,10 +17,10 @@ const ChangeReasonModel = require('../../app/models/change-reason.model.js') const ChargeReferenceHelper = require('../support/helpers/charge-reference.helper.js') const ChargeReferenceModel = require('../../app/models/charge-reference.model.js') const ChargeVersionHelper = require('../support/helpers/charge-version.helper.js') +const ChargeVersionNoteHelper = require('../support/helpers/charge-version-note.helper.js') +const ChargeVersionNoteModel = require('../../app/models/charge-version-note.model.js') const LicenceHelper = require('../support/helpers/licence.helper.js') const LicenceModel = require('../../app/models/licence.model.js') -const NoteHelper = require('../support/helpers/note.helper.js') -const NoteModel = require('../../app/models/note.model.js') const ReviewChargeVersionHelper = require('../support/helpers/review-charge-version.helper.js') const ReviewChargeVersionModel = require('../../app/models/review-charge-version.model.js') @@ -178,65 +178,65 @@ describe('Charge Version model', () => { }) }) - describe('when linking to licence', () => { - let testLicence + describe('when linking to charge version note', () => { + let testNote beforeEach(async () => { - testLicence = await LicenceHelper.add() + testNote = await ChargeVersionNoteHelper.add() - const { id: licenceId } = testLicence + const { id: noteId } = testNote - testRecord = await ChargeVersionHelper.add({ licenceId }) + testRecord = await ChargeVersionHelper.add({ noteId }) }) it('can successfully run a related query', async () => { const query = await ChargeVersionModel.query() - .innerJoinRelated('licence') + .innerJoinRelated('chargeVersionNote') expect(query).to.exist() }) - it('can eager load the licence', async () => { + it('can eager load the note', async () => { const result = await ChargeVersionModel.query() .findById(testRecord.id) - .withGraphFetched('licence') + .withGraphFetched('chargeVersionNote') expect(result).to.be.instanceOf(ChargeVersionModel) expect(result.id).to.equal(testRecord.id) - expect(result.licence).to.be.an.instanceOf(LicenceModel) - expect(result.licence).to.equal(testLicence) + expect(result.chargeVersionNote).to.be.an.instanceOf(ChargeVersionNoteModel) + expect(result.chargeVersionNote).to.equal(testNote) }) }) - describe('when linking to note', () => { - let testNote + describe('when linking to licence', () => { + let testLicence beforeEach(async () => { - testNote = await NoteHelper.add() + testLicence = await LicenceHelper.add() - const { id: noteId } = testNote + const { id: licenceId } = testLicence - testRecord = await ChargeVersionHelper.add({ noteId }) + testRecord = await ChargeVersionHelper.add({ licenceId }) }) it('can successfully run a related query', async () => { const query = await ChargeVersionModel.query() - .innerJoinRelated('note') + .innerJoinRelated('licence') expect(query).to.exist() }) - it('can eager load the note', async () => { + it('can eager load the licence', async () => { const result = await ChargeVersionModel.query() .findById(testRecord.id) - .withGraphFetched('note') + .withGraphFetched('licence') expect(result).to.be.instanceOf(ChargeVersionModel) expect(result.id).to.equal(testRecord.id) - expect(result.note).to.be.an.instanceOf(NoteModel) - expect(result.note).to.equal(testNote) + expect(result.licence).to.be.an.instanceOf(LicenceModel) + expect(result.licence).to.equal(testLicence) }) }) diff --git a/test/support/helpers/note.helper.js b/test/support/helpers/charge-version-note.helper.js similarity index 84% rename from test/support/helpers/note.helper.js rename to test/support/helpers/charge-version-note.helper.js index e04d9ac030..3f5f0d7b84 100644 --- a/test/support/helpers/note.helper.js +++ b/test/support/helpers/charge-version-note.helper.js @@ -1,14 +1,14 @@ 'use strict' /** - * @module NoteHelper + * @module ChargeVersionNoteHelper */ const { generateUserId } = require('./user.helper.js') -const NoteModel = require('../../../app/models/note.model.js') +const ChargeVersionNoteModel = require('../../../app/models/charge-version-note.model.js') /** - * Add a new note + * Add a new charge version note * * If no `data` is provided, the default values will be used. These are * @@ -22,7 +22,7 @@ const NoteModel = require('../../../app/models/note.model.js') function add (data = {}) { const insertData = defaults(data) - return NoteModel.query() + return ChargeVersionNoteModel.query() .insert({ ...insertData }) .returning('*') } From 188ff8ebd8667c837ecd971242251bccd54fcb47 Mon Sep 17 00:00:00 2001 From: Roble Date: Tue, 13 Aug 2024 13:43:12 +0100 Subject: [PATCH 18/24] Add user relation to charge version note model --- app/models/charge-version-note.model.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/models/charge-version-note.model.js b/app/models/charge-version-note.model.js index d7f0e037f4..4747a89bd6 100644 --- a/app/models/charge-version-note.model.js +++ b/app/models/charge-version-note.model.js @@ -23,6 +23,14 @@ class ChargeVersionNote extends BaseModel { from: 'chargeVersionNotes.id', to: 'chargeVersions.noteId' } + }, + user: { + relation: Model.BelongsToOneRelation, + modelClass: 'user.model', + join: { + from: 'chargeVersionNotes.userId', + to: 'users.id' + } } } } From b6342306162042f2c4d7553557b9d4586a6ab2b3 Mon Sep 17 00:00:00 2001 From: Roble Date: Tue, 13 Aug 2024 13:43:33 +0100 Subject: [PATCH 19/24] Add charge version note relation to user model --- app/models/user.model.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/models/user.model.js b/app/models/user.model.js index f3284742ca..c96fa173fd 100644 --- a/app/models/user.model.js +++ b/app/models/user.model.js @@ -17,6 +17,14 @@ class UserModel extends BaseModel { static get relationMappings () { return { + chargeVersionNotes: { + relation: Model.HasManyRelation, + modelClass: 'charge-version-note.model', + join: { + from: 'users.id', + to: 'chargeVersionNotes.userId' + } + }, groups: { relation: Model.ManyToManyRelation, modelClass: 'group.model', From 19681c4756baabc4a8e566ac75553e93a774e601 Mon Sep 17 00:00:00 2001 From: Roble Date: Tue, 13 Aug 2024 13:43:59 +0100 Subject: [PATCH 20/24] Add user relation test to charge version note model test --- test/models/charge-version-note.model.test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/models/charge-version-note.model.test.js b/test/models/charge-version-note.model.test.js index 7e4f8eb49b..00500937a8 100644 --- a/test/models/charge-version-note.model.test.js +++ b/test/models/charge-version-note.model.test.js @@ -10,6 +10,7 @@ const { expect } = Code // Test helpers const ChargeVersionHelper = require('../support/helpers/charge-version.helper.js') const ChargeVersionNoteHelper = require('../support/helpers/charge-version-note.helper.js') +const UserHelper = require('../support/helpers/user.helper.js') // Thing under test const ChargeVersionNoteModel = require('../../app/models/charge-version-note.model.js') @@ -44,5 +45,18 @@ describe('Charge Version Note model', () => { expect(query).to.exist() }) }) + + describe('when linking to user', () => { + beforeEach(async () => { + await UserHelper.add({ id: testRecord.userId }) + }) + + it('can successfully run a related query', async () => { + const query = await ChargeVersionNoteModel.query() + .withGraphFetched('user') + + expect(query).to.exist() + }) + }) }) }) From 16529b40200b863debfeb03e0d34cba427229a19 Mon Sep 17 00:00:00 2001 From: Roble Date: Tue, 13 Aug 2024 13:44:27 +0100 Subject: [PATCH 21/24] Add charge version note relation test to user model test --- test/models/user.model.test.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/models/user.model.test.js b/test/models/user.model.test.js index 7296d315f2..50ebdf5764 100644 --- a/test/models/user.model.test.js +++ b/test/models/user.model.test.js @@ -8,6 +8,8 @@ const { describe, it, before, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers +const ChargeVersionNoteHelper = require('../support/helpers/charge-version-note.helper.js') +const ChargeVersionNoteModel = require('../../app/models/charge-version-note.model.js') const GroupHelper = require('../support/helpers/group.helper.js') const GroupModel = require('../../app/models/group.model.js') const ReturnVersionHelper = require('../support/helpers/return-version.helper.js') @@ -29,6 +31,8 @@ const USER_GROUP_WIRS_INDEX = 3 const USER_WIRS_INDEX = 3 describe('User model', () => { + let testChargeVersionNoteOne + let testChargeVersionNoteTwo let testGroup let testRecord let testRole @@ -42,6 +46,8 @@ describe('User model', () => { testGroup = GroupHelper.select(GROUP_WIRS_INDEX) testUserGroup = UserGroupHelper.select(USER_GROUP_WIRS_INDEX) + testChargeVersionNoteOne = await ChargeVersionNoteHelper.add({ userId: testRecord.id, note: '1st test note' }) + testChargeVersionNoteTwo = await ChargeVersionNoteHelper.add({ userId: testRecord.id, note: '2nd test note' }) testUserRole = await UserRoleHelper.add({ userId: testRecord.id, roleId: testRole.id }) }) @@ -55,6 +61,30 @@ describe('User model', () => { }) describe('Relationships', () => { + describe('when linking to charge version notes', () => { + it('can successfully run a related query', async () => { + const query = await UserModel.query() + .innerJoinRelated('chargeVersionNotes') + + expect(query).to.exist() + }) + + it('can eager load the charge version notes', async () => { + const result = await UserModel.query() + .findById(testRecord.id) + .withGraphFetched('chargeVersionNotes') + + expect(result).to.be.instanceOf(UserModel) + expect(result.id).to.equal(testRecord.id) + + expect(result.chargeVersionNotes).to.be.an.array() + expect(result.chargeVersionNotes).to.have.length(2) + expect(result.chargeVersionNotes[0]).to.be.an.instanceOf(ChargeVersionNoteModel) + expect(result.chargeVersionNotes[0]).to.equal(testChargeVersionNoteOne) + expect(result.chargeVersionNotes[1]).to.equal(testChargeVersionNoteTwo) + }) + }) + describe('when linking through user groups to groups', () => { it('can successfully run a related query', async () => { const query = await UserModel.query() From 31f0902c51c09bae7a3530a4c311ecf6fd7b0e0a Mon Sep 17 00:00:00 2001 From: Roble Date: Wed, 14 Aug 2024 10:59:35 +0100 Subject: [PATCH 22/24] Add more related query for charge version note model test --- test/models/charge-version-note.model.test.js | 42 ++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/test/models/charge-version-note.model.test.js b/test/models/charge-version-note.model.test.js index 00500937a8..0d31f6d386 100644 --- a/test/models/charge-version-note.model.test.js +++ b/test/models/charge-version-note.model.test.js @@ -4,13 +4,15 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it, before, beforeEach } = exports.lab = Lab.script() +const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers const ChargeVersionHelper = require('../support/helpers/charge-version.helper.js') +const ChargeVersionModel = require('../../app/models/charge-version.model.js') const ChargeVersionNoteHelper = require('../support/helpers/charge-version-note.helper.js') const UserHelper = require('../support/helpers/user.helper.js') +const UserModel = require('../../app/models/user.model.js') // Thing under test const ChargeVersionNoteModel = require('../../app/models/charge-version-note.model.js') @@ -18,7 +20,7 @@ const ChargeVersionNoteModel = require('../../app/models/charge-version-note.mod describe('Charge Version Note model', () => { let testRecord - before(async () => { + beforeEach(async () => { testRecord = await ChargeVersionNoteHelper.add() }) @@ -34,29 +36,57 @@ describe('Charge Version Note model', () => { describe('Relationships', () => { describe('when linking to charge version', () => { + let testChargeVersion + beforeEach(async () => { - await ChargeVersionHelper.add({ noteId: testRecord.id }) + testChargeVersion = await ChargeVersionHelper.add({ noteId: testRecord.id }) }) it('can successfully run a related query', async () => { const query = await ChargeVersionNoteModel.query() - .withGraphFetched('chargeVersion') + .innerJoinRelated('chargeVersion') expect(query).to.exist() }) + + it('can eager load the charge version', async () => { + const result = await ChargeVersionNoteModel.query() + .findById(testRecord.id) + .withGraphFetched('chargeVersion') + + expect(result).to.be.instanceOf(ChargeVersionNoteModel) + expect(result.id).to.equal(testRecord.id) + + expect(result.chargeVersion).to.be.instanceOf(ChargeVersionModel) + expect(result.chargeVersion).to.equal(testChargeVersion) + }) }) describe('when linking to user', () => { + let testUser + beforeEach(async () => { - await UserHelper.add({ id: testRecord.userId }) + testUser = await UserHelper.add({ id: testRecord.userId }) }) it('can successfully run a related query', async () => { const query = await ChargeVersionNoteModel.query() - .withGraphFetched('user') + .innerJoinRelated('user') expect(query).to.exist() }) + + it('can eager load the user', async () => { + const result = await ChargeVersionNoteModel.query() + .findById(testRecord.id) + .withGraphFetched('user') + + expect(result).to.be.instanceOf(ChargeVersionNoteModel) + expect(result.id).to.equal(testRecord.id) + + expect(result.user).to.be.instanceOf(UserModel) + expect(result.user).to.equal(testUser) + }) }) }) }) From ebb1e0b2b5cbf1ca8b1fd80d22ebf73c0369abc0 Mon Sep 17 00:00:00 2001 From: Roble Date: Wed, 14 Aug 2024 12:45:38 +0100 Subject: [PATCH 23/24] Update user test to use select instead of add --- test/models/charge-version-note.model.test.js | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/test/models/charge-version-note.model.test.js b/test/models/charge-version-note.model.test.js index 0d31f6d386..17f6b8d202 100644 --- a/test/models/charge-version-note.model.test.js +++ b/test/models/charge-version-note.model.test.js @@ -4,7 +4,7 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it, beforeEach } = exports.lab = Lab.script() +const { describe, it, before } = exports.lab = Lab.script() const { expect } = Code // Test helpers @@ -18,10 +18,15 @@ const UserModel = require('../../app/models/user.model.js') const ChargeVersionNoteModel = require('../../app/models/charge-version-note.model.js') describe('Charge Version Note model', () => { + let testChargeVersion let testRecord + let testUser - beforeEach(async () => { - testRecord = await ChargeVersionNoteHelper.add() + before(async () => { + testUser = UserHelper.select() + console.log('🚀🚀🚀 ~ testUser:', testUser) + testRecord = await ChargeVersionNoteHelper.add({ userId: testUser.id }) + testChargeVersion = await ChargeVersionHelper.add({ noteId: testRecord.id }) }) describe('Basic query', () => { @@ -36,12 +41,6 @@ describe('Charge Version Note model', () => { describe('Relationships', () => { describe('when linking to charge version', () => { - let testChargeVersion - - beforeEach(async () => { - testChargeVersion = await ChargeVersionHelper.add({ noteId: testRecord.id }) - }) - it('can successfully run a related query', async () => { const query = await ChargeVersionNoteModel.query() .innerJoinRelated('chargeVersion') @@ -63,12 +62,6 @@ describe('Charge Version Note model', () => { }) describe('when linking to user', () => { - let testUser - - beforeEach(async () => { - testUser = await UserHelper.add({ id: testRecord.userId }) - }) - it('can successfully run a related query', async () => { const query = await ChargeVersionNoteModel.query() .innerJoinRelated('user') @@ -85,7 +78,7 @@ describe('Charge Version Note model', () => { expect(result.id).to.equal(testRecord.id) expect(result.user).to.be.instanceOf(UserModel) - expect(result.user).to.equal(testUser) + expect(result.user).to.equal(testUser, { skip: ['createdAt', 'password', 'updatedAt'] }) }) }) }) From 71d3f1c705932fa5b201df2be553c01c0bb7e480 Mon Sep 17 00:00:00 2001 From: Roble Date: Wed, 14 Aug 2024 12:46:52 +0100 Subject: [PATCH 24/24] Remove console.log() --- test/models/charge-version-note.model.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/models/charge-version-note.model.test.js b/test/models/charge-version-note.model.test.js index 17f6b8d202..6b1df9c2a3 100644 --- a/test/models/charge-version-note.model.test.js +++ b/test/models/charge-version-note.model.test.js @@ -24,7 +24,6 @@ describe('Charge Version Note model', () => { before(async () => { testUser = UserHelper.select() - console.log('🚀🚀🚀 ~ testUser:', testUser) testRecord = await ChargeVersionNoteHelper.add({ userId: testUser.id }) testChargeVersion = await ChargeVersionHelper.add({ noteId: testRecord.id }) })