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

Create chargeVersionNotes table model, helper and view #1240

Merged
merged 27 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3b3fb31
Create `notes` table model and helper
rvsiyad Aug 7, 2024
eecddf3
Add note model
rvsiyad Aug 7, 2024
488fe9f
Amend chargeVersion model to include relationship to notes
rvsiyad Aug 7, 2024
0f7c040
Create the migrations for the notes view
rvsiyad Aug 7, 2024
8ba459e
Create the notes table for the legacy
rvsiyad Aug 7, 2024
94d5b34
Add the note model test
rvsiyad Aug 7, 2024
6a922f3
Add the node helper
rvsiyad Aug 7, 2024
f884ac3
Merge branch 'main' into add-notes-model-and-helper
rvsiyad Aug 12, 2024
862298a
Amend down function for create-note-view migration
rvsiyad Aug 12, 2024
33decc0
WIP
rvsiyad Aug 12, 2024
6674dab
WIP
rvsiyad Aug 12, 2024
eba7fbe
Change 'notes' to 'note' for charge version model relation
rvsiyad Aug 12, 2024
50edee6
Add note relationship test for charge version model
rvsiyad Aug 12, 2024
6345c44
Rename relationship from `chargeVersions` to `chargeVersion` for notes
rvsiyad Aug 12, 2024
6046e39
Add default for type in notes table
rvsiyad Aug 12, 2024
4a64e0a
Remove unused columns from being shown in note view
rvsiyad Aug 12, 2024
70f1ab3
Remove unused columns from note helper
rvsiyad Aug 12, 2024
71e97f6
Amend table and relationship references from 'notes' to 'charge-versi…
rvsiyad Aug 12, 2024
188ff8e
Add user relation to charge version note model
rvsiyad Aug 13, 2024
b634230
Add charge version note relation to user model
rvsiyad Aug 13, 2024
19681c4
Add user relation test to charge version note model test
rvsiyad Aug 13, 2024
16529b4
Add charge version note relation test to user model test
rvsiyad Aug 13, 2024
c6d6b41
Merge branch 'main' into add-notes-model-and-helper
rvsiyad Aug 13, 2024
31f0902
Add more related query for charge version note model test
rvsiyad Aug 14, 2024
f25fd15
Merge branch 'add-notes-model-and-helper' of https://github.com/DEFRA…
rvsiyad Aug 14, 2024
ebb1e0b
Update user test to use select instead of add
rvsiyad Aug 14, 2024
71d3f1c
Remove console.log()
rvsiyad Aug 14, 2024
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
39 changes: 39 additions & 0 deletions app/models/charge-version-note.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

/**
* Model for charge_version_notes (water.notes)
* @module ChargeVersionNote
*/

const { Model } = require('objection')

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

class ChargeVersionNote extends BaseModel {
static get tableName () {
return 'chargeVersionNotes'
}

static get relationMappings () {
return {
chargeVersion: {
relation: Model.BelongsToOneRelation,
modelClass: 'charge-version.model',
join: {
from: 'chargeVersionNotes.id',
to: 'chargeVersions.noteId'
}
},
user: {
relation: Model.BelongsToOneRelation,
modelClass: 'user.model',
join: {
from: 'chargeVersionNotes.userId',
to: 'users.id'
}
}
}
}
}

module.exports = ChargeVersionNote
8 changes: 8 additions & 0 deletions app/models/charge-version.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 8 additions & 0 deletions app/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
31 changes: 31 additions & 0 deletions db/migrations/legacy/20240807142828_water-notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'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()
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())
})
}

exports.down = function (knex) {
return knex
.schema
.withSchema('water')
.dropTableIfExists(tableName)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const viewName = 'charge_version_notes'

exports.up = function (knex) {
return 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 AS note',
// '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)
}
62 changes: 62 additions & 0 deletions test/models/charge-version-note.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it, before, beforeEach } = exports.lab = Lab.script()
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')

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

before(async () => {
testRecord = await ChargeVersionNoteHelper.add()
})

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

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

describe('Relationships', () => {
describe('when linking to charge version', () => {
beforeEach(async () => {
await ChargeVersionHelper.add({ noteId: testRecord.id })
})

it('can successfully run a related query', async () => {
const query = await ChargeVersionNoteModel.query()
.withGraphFetched('chargeVersion')

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()
})
})
rvsiyad marked this conversation as resolved.
Show resolved Hide resolved
})
})
33 changes: 33 additions & 0 deletions test/models/charge-version.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ 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 ReviewChargeVersionHelper = require('../support/helpers/review-charge-version.helper.js')
Expand Down Expand Up @@ -176,6 +178,37 @@ describe('Charge Version model', () => {
})
})

describe('when linking to charge version note', () => {
let testNote

beforeEach(async () => {
testNote = await ChargeVersionNoteHelper.add()

const { id: noteId } = testNote

testRecord = await ChargeVersionHelper.add({ noteId })
})

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

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

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

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

expect(result.chargeVersionNote).to.be.an.instanceOf(ChargeVersionNoteModel)
expect(result.chargeVersionNote).to.equal(testNote)
})
})

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

Expand Down
30 changes: 30 additions & 0 deletions test/models/user.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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')
Expand All @@ -29,6 +31,8 @@
const USER_WIRS_INDEX = 3

describe('User model', () => {
let testChargeVersionNoteOne
let testChargeVersionNoteTwo
let testGroup
let testRecord
let testRole
Expand All @@ -42,6 +46,8 @@
testGroup = GroupHelper.select(GROUP_WIRS_INDEX)
testUserGroup = UserGroupHelper.select(USER_GROUP_WIRS_INDEX)

testChargeVersionNoteOne = await ChargeVersionNoteHelper.add({ userId: testRecord.id, note: '1st test note' })
Dismissed Show dismissed Hide dismissed
testChargeVersionNoteTwo = await ChargeVersionNoteHelper.add({ userId: testRecord.id, note: '2nd test note' })
Dismissed Show dismissed Hide dismissed
testUserRole = await UserRoleHelper.add({ userId: testRecord.id, roleId: testRole.id })
})

Expand All @@ -55,6 +61,30 @@
})

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()
Expand Down
53 changes: 53 additions & 0 deletions test/support/helpers/charge-version-note.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict'

/**
* @module ChargeVersionNoteHelper
*/

const { generateUserId } = require('./user.helper.js')
const ChargeVersionNoteModel = require('../../../app/models/charge-version-note.model.js')

/**
* Add a new charge version note
*
* If no `data` is provided, the default values will be used. These are
*
* - `note` - 'This is a test note'
* - `userId` - [randomly generated - 100001]
*
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database
*
* @returns {Promise<module:NoteModel>} The instance of the newly created record
*/
function add (data = {}) {
const insertData = defaults(data)

return ChargeVersionNoteModel.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 = {
note: 'This is a test note',
userId: generateUserId()
Dismissed Show dismissed Hide dismissed
}

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

module.exports = {
add,
defaults
}
Loading