Skip to content

Commit 5b04b07

Browse files
authored
Add created_by to return versions and link models (#1126)
https://eaflood.atlassian.net/browse/WATER-4522 > Part of the work to replace NALD for handling return requirements When we take control of the setup of return requirements, we'll be persisting them in the same tables that they are currently imported into. One omission is that there is no means to identify who created the return version. Admittedly, there is nothing in NALD to indicate this, so there is nothing to import. But we intend to capture this information when we start persisting the records. We've [added the field to the table](DEFRA/water-abstraction-service#2567). This change makes the field available in the model, and links the return version and user models together using it.
1 parent 8be2bb6 commit 5b04b07

6 files changed

+133
-46
lines changed

app/models/return-version.model.js

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ class ReturnVersionModel extends BaseModel {
3131
from: 'returnVersions.id',
3232
to: 'returnRequirements.returnVersionId'
3333
}
34+
},
35+
user: {
36+
relation: Model.BelongsToOneRelation,
37+
modelClass: 'user.model',
38+
join: {
39+
from: 'returnVersions.createdBy',
40+
to: 'users.id'
41+
}
3442
}
3543
}
3644
}

app/models/user.model.js

+23-15
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,24 @@ class UserModel extends BaseModel {
1717

1818
static get relationMappings () {
1919
return {
20-
userGroups: {
21-
relation: Model.HasManyRelation,
22-
modelClass: 'user-group.model',
20+
groups: {
21+
relation: Model.ManyToManyRelation,
22+
modelClass: 'group.model',
2323
join: {
2424
from: 'users.id',
25-
to: 'userGroups.userId'
25+
through: {
26+
from: 'userGroups.userId',
27+
to: 'userGroups.groupId'
28+
},
29+
to: 'groups.id'
2630
}
2731
},
28-
userRoles: {
32+
returnVersions: {
2933
relation: Model.HasManyRelation,
30-
modelClass: 'user-role.model',
34+
modelClass: 'return-version.model',
3135
join: {
3236
from: 'users.id',
33-
to: 'userRoles.userId'
37+
to: 'returnVersions.createdBy'
3438
}
3539
},
3640
roles: {
@@ -45,16 +49,20 @@ class UserModel extends BaseModel {
4549
to: 'roles.id'
4650
}
4751
},
48-
groups: {
49-
relation: Model.ManyToManyRelation,
50-
modelClass: 'group.model',
52+
userGroups: {
53+
relation: Model.HasManyRelation,
54+
modelClass: 'user-group.model',
5155
join: {
5256
from: 'users.id',
53-
through: {
54-
from: 'userGroups.userId',
55-
to: 'userGroups.groupId'
56-
},
57-
to: 'groups.id'
57+
to: 'userGroups.userId'
58+
}
59+
},
60+
userRoles: {
61+
relation: Model.HasManyRelation,
62+
modelClass: 'user-role.model',
63+
join: {
64+
from: 'users.id',
65+
to: 'userRoles.userId'
5866
}
5967
}
6068
}

db/migrations/legacy/20221108007023_water-return-versions.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ exports.up = function (knex) {
2020
table.text('reason')
2121
table.boolean('multiple_upload').notNullable().defaultTo(false)
2222
table.text('notes')
23+
table.integer('created_by')
2324

2425
// Legacy timestamps
2526
table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now())

db/migrations/public/20240606103641_create-return-versions-view.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ exports.up = function (knex) {
1717
'reason',
1818
'multiple_upload',
1919
'notes',
20+
'created_by',
2021
'date_created AS created_at',
2122
'date_updated AS updated_at'
2223
]))

test/models/return-version.model.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const LicenceModel = require('../../app/models/licence.model.js')
1414
const ReturnRequirementHelper = require('../support/helpers/return-requirement.helper.js')
1515
const ReturnRequirementModel = require('../../app/models/return-requirement.model.js')
1616
const ReturnVersionHelper = require('../support/helpers/return-version.helper.js')
17+
const UserModel = require('../../app/models/user.model.js')
18+
const UserHelper = require('../support/helpers/user.helper.js')
1719

1820
// Thing under test
1921
const ReturnVersionModel = require('../../app/models/return-version.model.js')
@@ -105,5 +107,35 @@ describe('Return Version model', () => {
105107
expect(result.returnRequirements).to.include(testReturnRequirements[1])
106108
})
107109
})
110+
111+
describe('when linking to user', () => {
112+
let testUser
113+
114+
beforeEach(async () => {
115+
testUser = await UserHelper.add()
116+
117+
const { id: createdBy } = testUser
118+
testRecord = await ReturnVersionHelper.add({ createdBy })
119+
})
120+
121+
it('can successfully run a related query', async () => {
122+
const query = await ReturnVersionModel.query()
123+
.innerJoinRelated('user')
124+
125+
expect(query).to.exist()
126+
})
127+
128+
it('can eager load the user', async () => {
129+
const result = await ReturnVersionModel.query()
130+
.findById(testRecord.id)
131+
.withGraphFetched('user')
132+
133+
expect(result).to.be.instanceOf(ReturnVersionModel)
134+
expect(result.id).to.equal(testRecord.id)
135+
136+
expect(result.user).to.be.an.instanceOf(UserModel)
137+
expect(result.user).to.equal(testUser)
138+
})
139+
})
108140
})
109141
})

test/models/user.model.test.js

+68-31
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const { expect } = Code
1111
const DatabaseSupport = require('../support/database.js')
1212
const GroupHelper = require('../support/helpers/group.helper.js')
1313
const GroupModel = require('../../app/models/group.model.js')
14+
const ReturnVersionHelper = require('../support/helpers/return-version.helper.js')
15+
const ReturnVersionModel = require('../../app/models/return-version.model.js')
1416
const RoleHelper = require('../support/helpers/role.helper.js')
1517
const RoleModel = require('../../app/models/role.model.js')
1618
const UserGroupHelper = require('../support/helpers/user-group.helper.js')
@@ -43,63 +45,69 @@ describe('User model', () => {
4345
})
4446

4547
describe('Relationships', () => {
46-
describe('when linking to user groups', () => {
47-
let testUserGroup
48+
describe('when linking through user groups to groups', () => {
49+
let testGroup
4850

4951
beforeEach(async () => {
5052
testRecord = await UserHelper.add()
51-
testUserGroup = await UserGroupHelper.add({ userId: testRecord.id })
53+
testGroup = await GroupHelper.add()
54+
await UserGroupHelper.add({ userId: testRecord.id, groupId: testGroup.id })
5255
})
5356

5457
it('can successfully run a related query', async () => {
5558
const query = await UserModel.query()
56-
.innerJoinRelated('userGroups')
59+
.innerJoinRelated('groups')
5760

5861
expect(query).to.exist()
5962
})
6063

61-
it('can eager load the user groups', async () => {
64+
it('can eager load the groups', async () => {
6265
const result = await UserModel.query()
6366
.findById(testRecord.id)
64-
.withGraphFetched('userGroups')
67+
.withGraphFetched('groups')
6568

6669
expect(result).to.be.instanceOf(UserModel)
6770
expect(result.id).to.equal(testRecord.id)
6871

69-
expect(result.userGroups).to.be.an.array()
70-
expect(result.userGroups).to.have.length(1)
71-
expect(result.userGroups[0]).to.be.an.instanceOf(UserGroupModel)
72-
expect(result.userGroups[0]).to.equal(testUserGroup)
72+
expect(result.groups).to.be.an.array()
73+
expect(result.groups).to.have.length(1)
74+
expect(result.groups[0]).to.be.an.instanceOf(GroupModel)
75+
expect(result.groups[0]).to.equal(testGroup)
7376
})
7477
})
7578

76-
describe('when linking to user roles', () => {
77-
let testUserRole
79+
describe('when linking to return versions', () => {
80+
let testReturnVersions
7881

7982
beforeEach(async () => {
8083
testRecord = await UserHelper.add()
81-
testUserRole = await UserRoleHelper.add({ userId: testRecord.id })
84+
85+
testReturnVersions = []
86+
for (let i = 0; i < 2; i++) {
87+
const returnVersion = await ReturnVersionHelper.add({ createdBy: testRecord.id })
88+
testReturnVersions.push(returnVersion)
89+
}
8290
})
8391

8492
it('can successfully run a related query', async () => {
8593
const query = await UserModel.query()
86-
.innerJoinRelated('userRoles')
94+
.innerJoinRelated('returnVersions')
8795

8896
expect(query).to.exist()
8997
})
9098

91-
it('can eager load the user roles', async () => {
99+
it('can eager load the return versions', async () => {
92100
const result = await UserModel.query()
93101
.findById(testRecord.id)
94-
.withGraphFetched('userRoles')
102+
.withGraphFetched('returnVersions')
95103

96104
expect(result).to.be.instanceOf(UserModel)
97105
expect(result.id).to.equal(testRecord.id)
98106

99-
expect(result.userRoles).to.be.an.array()
100-
expect(result.userRoles).to.have.length(1)
101-
expect(result.userRoles[0]).to.be.an.instanceOf(UserRoleModel)
102-
expect(result.userRoles[0]).to.equal(testUserRole)
107+
expect(result.returnVersions).to.be.an.array()
108+
expect(result.returnVersions[0]).to.be.an.instanceOf(ReturnVersionModel)
109+
expect(result.returnVersions).to.include(testReturnVersions[0])
110+
expect(result.returnVersions).to.include(testReturnVersions[1])
103111
})
104112
})
105113

@@ -134,34 +142,63 @@ describe('User model', () => {
134142
})
135143
})
136144

137-
describe('when linking through user groups to groups', () => {
138-
let testGroup
145+
describe('when linking to user groups', () => {
146+
let testUserGroup
139147

140148
beforeEach(async () => {
141149
testRecord = await UserHelper.add()
142-
testGroup = await GroupHelper.add()
143-
await UserGroupHelper.add({ userId: testRecord.id, groupId: testGroup.id })
150+
testUserGroup = await UserGroupHelper.add({ userId: testRecord.id })
144151
})
145152

146153
it('can successfully run a related query', async () => {
147154
const query = await UserModel.query()
148-
.innerJoinRelated('groups')
155+
.innerJoinRelated('userGroups')
149156

150157
expect(query).to.exist()
151158
})
152159

153-
it('can eager load the groups', async () => {
160+
it('can eager load the user groups', async () => {
154161
const result = await UserModel.query()
155162
.findById(testRecord.id)
156-
.withGraphFetched('groups')
163+
.withGraphFetched('userGroups')
157164

158165
expect(result).to.be.instanceOf(UserModel)
159166
expect(result.id).to.equal(testRecord.id)
160167

161-
expect(result.groups).to.be.an.array()
162-
expect(result.groups).to.have.length(1)
163-
expect(result.groups[0]).to.be.an.instanceOf(GroupModel)
164-
expect(result.groups[0]).to.equal(testGroup)
168+
expect(result.userGroups).to.be.an.array()
169+
expect(result.userGroups).to.have.length(1)
170+
expect(result.userGroups[0]).to.be.an.instanceOf(UserGroupModel)
171+
expect(result.userGroups[0]).to.equal(testUserGroup)
172+
})
173+
})
174+
175+
describe('when linking to user roles', () => {
176+
let testUserRole
177+
178+
beforeEach(async () => {
179+
testRecord = await UserHelper.add()
180+
testUserRole = await UserRoleHelper.add({ userId: testRecord.id })
181+
})
182+
183+
it('can successfully run a related query', async () => {
184+
const query = await UserModel.query()
185+
.innerJoinRelated('userRoles')
186+
187+
expect(query).to.exist()
188+
})
189+
190+
it('can eager load the user roles', async () => {
191+
const result = await UserModel.query()
192+
.findById(testRecord.id)
193+
.withGraphFetched('userRoles')
194+
195+
expect(result).to.be.instanceOf(UserModel)
196+
expect(result.id).to.equal(testRecord.id)
197+
198+
expect(result.userRoles).to.be.an.array()
199+
expect(result.userRoles).to.have.length(1)
200+
expect(result.userRoles[0]).to.be.an.instanceOf(UserRoleModel)
201+
expect(result.userRoles[0]).to.equal(testUserRole)
165202
})
166203
})
167204
})

0 commit comments

Comments
 (0)