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

Add LicenceDocumentModel from crm_v2.documents #618

Merged
merged 7 commits into from
Dec 22, 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
16 changes: 16 additions & 0 deletions app/models/licence-document.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

/**
* Model for licence_documents (crm_v2.documents)
* @module LicenceDocumentModel
*/

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

class LicenceDocumentModel extends BaseModel {
static get tableName () {
return 'licenceDocuments'
}
}

module.exports = LicenceDocumentModel
36 changes: 36 additions & 0 deletions db/migrations/legacy/20231221145235_create-crm-v2-documents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

const tableName = 'documents'

exports.up = function (knex) {
return knex
.schema
.withSchema('crm_v2')
.createTable(tableName, (table) => {
// Primary Key
table.uuid('document_id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.string('regime').notNullable().defaultTo('water')
table.string('document_type').notNullable().defaultTo('abstraction_licence')
table.string('document_ref').notNullable()
table.date('start_date').notNullable()
table.date('end_date')
table.string('external_id')
table.boolean('is_test').notNullable().defaultTo(false)
table.timestamp('date_deleted')

// Legacy timestamps
table.timestamp('date_created').notNullable().defaultTo(knex.fn.now())
table.timestamp('date_updated').notNullable().defaultTo(knex.fn.now())

table.unique(['regime', 'document_type', 'document_ref'], { useConstraint: true })
})
}

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

const viewName = 'licence_documents'

exports.up = function (knex) {
return knex
.schema
.createView(viewName, (view) => {
// NOTE: We have commented out unused columns from the source table
view.as(knex('documents').withSchema('crm_v2').select([
'document_id AS id',
// 'regime',
// 'document_type',
'document_ref AS licence_ref',
'start_date',
'end_date',
// 'is_test',
'date_deleted AS deleted_at',
'date_created AS created_at',
'date_updated AS updated_at'
]))
})
}

exports.down = function (knex) {
return knex
.schema
.dropViewIfExists(viewName)
}
34 changes: 34 additions & 0 deletions test/models/licence-document.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'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 LicenceDocumentHelper = require('../support/helpers/licence-document.helper.js')

// Thing under test
const LicenceDocumentModel = require('../../app/models/licence-document.model.js')

describe('Licence Document model', () => {
let testRecord

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

testRecord = await LicenceDocumentHelper.add()
})

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

expect(result).to.be.an.instanceOf(LicenceDocumentModel)
expect(result.id).to.equal(testRecord.id)
})
})
})
53 changes: 53 additions & 0 deletions test/support/helpers/licence-document.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict'

/**
* @module LicenceDocumentHelper
*/

const { generateLicenceRef } = require('./licence.helper.js')
const LicenceDocumentModel = require('../../../app/models/licence-document.model.js')

/**
* Add a new licence document
*
* If no `data` is provided, default values will be used. These are
*
* - `licenceRef` - [randomly generated - 01/123]
* - `startDate` - new Date('2022-01-01')
*
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database
*
* @returns {module:LicenceDocumentModel} The instance of the newly created record
*/
async function add (data = {}) {
const insertData = defaults(data)

return LicenceDocumentModel.query()
.insert({ ...insertData })
.returning('*')
}

/**
* Returns the defaults used when creating a new licence
*
* 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 = {
licenceRef: generateLicenceRef(),
startDate: new Date('2022-01-01')
}

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

module.exports = {
add,
defaults
}
Loading