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

Build models for returns based on new Views #533

Merged
merged 18 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 38 additions & 0 deletions app/models/return-log.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

/**
* Model for return_logs
* @module ReturnLogModel
*/

const { Model } = require('objection')

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

class ReturnLogModel extends BaseModel {
static get tableName () {
return 'returnLogs'
}

// Defining which fields contain json allows us to insert an object without needing to stringify it first
static get jsonAttributes () {
return [
'metadata'
]
}

static get relationMappings () {
return {
returnSubmissions: {
relation: Model.HasManyRelation,
modelClass: 'return-submission.model',
join: {
from: 'returnLogs.id',
to: 'returnSubmissions.returnLogId'
}
}
}
}
}

module.exports = ReturnLogModel
31 changes: 31 additions & 0 deletions app/models/return-submission-line.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

/**
* Model for return_submission_lines
* @module ReturnSubmissionLineModel
*/

const { Model } = require('objection')

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

class ReturnSubmissionLineModel extends BaseModel {
static get tableName () {
return 'returnSubmissionLines'
}

static get relationMappings () {
return {
returnSubmission: {
relation: Model.BelongsToOneRelation,
modelClass: 'return-submission.model',
join: {
from: 'returnSubmissionLines.returnSubmissionId',
to: 'returnSubmissions.id'
}
}
}
}
}

module.exports = ReturnSubmissionLineModel
46 changes: 46 additions & 0 deletions app/models/return-submission.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict'

/**
* Model for return_submissions
* @module ReturnSubmissionModel
*/

const { Model } = require('objection')

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

class ReturnSubmissionModel extends BaseModel {
static get tableName () {
return 'returnSubmissions'
}

// Defining which fields contain json allows us to insert an object without needing to stringify it first
static get jsonAttributes () {
return [
'metadata'
]
}

static get relationMappings () {
return {
returnLog: {
relation: Model.BelongsToOneRelation,
modelClass: 'return-log.model',
join: {
from: 'returnSubmissions.returnLogId',
to: 'returnLogs.id'
}
},
returnSubmissionLines: {
relation: Model.HasManyRelation,
modelClass: 'return-submission-line.model',
join: {
from: 'returnSubmissions.id',
to: 'returnSubmissionLines.returnSubmissionId'
}
}
}
}
}

module.exports = ReturnSubmissionModel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const tableName = 'returns'

exports.up = function (knex) {
return knex
.schema
.withSchema('returns')
.alterTable(tableName, (table) => {
table.string('regime').notNullable().defaultTo('water').alter()
table.string('licence_type').notNullable().defaultTo('abstraction').alter()
})
}

exports.down = async function (knex) {
return knex
.schema
.withSchema('returns')
.alterTable(tableName, (table) => {
table.string('regime').notNullable().alter()
table.string('licence_type').notNullable().alter()
})
}
23 changes: 23 additions & 0 deletions db/migrations/legacy/20231122183613_add-defaults-to-lines-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const tableName = 'lines'

exports.up = function (knex) {
return knex
.schema
.withSchema('returns')
.alterTable(tableName, (table) => {
table.string('substance').notNullable().defaultTo('water').alter()
table.string('unit').notNullable().defaultTo('m³').alter()
})
}

exports.down = async function (knex) {
return knex
.schema
.withSchema('returns')
.alterTable(tableName, (table) => {
table.string('substance').notNullable().alter()
table.string('unit').notNullable().alter()
})
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const viewName = 'form_logs'
const viewName = 'return_logs'

exports.up = function (knex) {
return knex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exports.up = function (knex) {
.createView(viewName, (view) => {
view.as(knex('versions').withSchema('returns').select([
'versions.version_id AS id',
'versions.return_id as form_log_id',
'versions.return_id as return_log_id',
'versions.user_id',
'versions.user_type',
'versions.version_number as version',
Expand Down
74 changes: 74 additions & 0 deletions test/models/return-log.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'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 DatabaseHelper = require('../support/helpers/database.helper.js')
const ReturnLogHelper = require('../support/helpers/return-log.helper.js')
const ReturnSubmissionHelper = require('../support/helpers/return-submission.helper.js')
const ReturnSubmissionModel = require('../../app/models/return-submission.model.js')

// Thing under test
const ReturnLogModel = require('../../app/models/return-log.model.js')

describe('Return Log model', () => {
let testRecord

beforeEach(async () => {
await DatabaseHelper.clean()

testRecord = await ReturnLogHelper.add()
})

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

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

describe('Relationships', () => {
describe('when linking to return submissions', () => {
let returnSubmissions

beforeEach(async () => {
const { id: returnLogId } = testRecord

returnSubmissions = []
for (let i = 0; i < 2; i++) {
const version = i
const returnSubmission = await ReturnSubmissionHelper.add({ returnLogId, version })
returnSubmissions.push(returnSubmission)
}
})

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

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

it('can eager load the return submissions', async () => {
const result = await ReturnLogModel.query()
.findById(testRecord.id)
.withGraphFetched('returnSubmissions')

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

expect(result.returnSubmissions).to.be.an.array()
expect(result.returnSubmissions[0]).to.be.an.instanceOf(ReturnSubmissionModel)
expect(result.returnSubmissions).to.include(returnSubmissions[0])
expect(result.returnSubmissions).to.include(returnSubmissions[1])
})
})
})
})
68 changes: 68 additions & 0 deletions test/models/return-submission-line.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'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 DatabaseHelper = require('../support/helpers/database.helper.js')
const ReturnSubmissionLineHelper = require('../support/helpers/return-submission-line.helper.js')
const ReturnSubmissionHelper = require('../support/helpers/return-submission.helper.js')
const ReturnSubmissionModel = require('../../app/models/return-submission.model.js')

// Thing under test
const ReturnSubmissionLineModel = require('../../app/models/return-submission-line.model.js')

describe('Return Submission Line model', () => {
let testRecord

beforeEach(async () => {
await DatabaseHelper.clean()
})

describe('Basic query', () => {
beforeEach(async () => {
testRecord = await ReturnSubmissionLineHelper.add()
})

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

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

describe('Relationships', () => {
describe('when linking to return submission', () => {
let testReturnSubmission

beforeEach(async () => {
testReturnSubmission = await ReturnSubmissionHelper.add()
testRecord = await ReturnSubmissionLineHelper.add({ returnSubmissionId: testReturnSubmission.id })
})

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

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

it('can eager load the return submission', async () => {
const result = await ReturnSubmissionLineModel.query()
.findById(testRecord.id)
.withGraphFetched('returnSubmission')

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

expect(result.returnSubmission).to.be.an.instanceOf(ReturnSubmissionModel)
expect(result.returnSubmission).to.equal(testReturnSubmission)
})
})
})
})
Loading
Loading