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 CRM_V2 address model #378

Merged
merged 2 commits into from
Aug 25, 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
27 changes: 27 additions & 0 deletions app/models/crm-v2/address.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

/**
* Model for addresses
* @module AddressModel
*/

const CrmV2BaseModel = require('./crm-v2-base.model.js')

class AddressModel extends CrmV2BaseModel {
static get tableName () {
return 'addresses'
}

static get idColumn () {
return 'addressId'
}

static get translations () {
return [
{ database: 'dateCreated', model: 'createdAt' },
{ database: 'dateUpdated', model: 'updatedAt' }
]
}
}

module.exports = AddressModel
40 changes: 40 additions & 0 deletions db/migrations/20230818154709_create-crm-v2-addresses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

const tableName = 'addresses'

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

// Data
table.string('address_1')
table.string('address_2')
table.string('address_3')
table.string('address_4')
table.string('town')
table.string('county')
table.string('postcode')
table.string('country')
table.string('external_id')
table.boolean('is_test').notNullable().defaultTo(false)
table.string('data_source')
table.integer('uprn')
table.string('last_hash')
table.string('current_hash')

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

exports.down = function (knex) {
return knex
.schema
.withSchema('crm_v2')
.dropTableIfExists(tableName)
}
34 changes: 34 additions & 0 deletions test/models/crm-v2/address.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 AddressHelper = require('../../support/helpers/crm-v2/address.helper.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')

// Thing under test
const AddressModel = require('../../../app/models/crm-v2/address.model.js')

describe('Address model', () => {
let testRecord

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

testRecord = await AddressHelper.add()
})

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

expect(result).to.be.an.instanceOf(AddressModel)
expect(result.addressId).to.equal(testRecord.addressId)
})
})
})
64 changes: 64 additions & 0 deletions test/support/helpers/crm-v2/address.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict'

/**
* @module AddressHelper
*/

const AddressModel = require('../../../../app/models/crm-v2/address.model.js')

/**
* Add a new address
*
* If no `data` is provided, default values will be used. These are
*
* - `address1` - ENVIRONMENT AGENCY
* - `address2` - HORIZON HOUSE
* - `address3` - DEANERY ROAD
* - `town` - BRISTOL
* - `postcode` - BS1 5AH
* - `country` - United Kingdom
* - `dataSource` - wrls
* - `uprn` - 340116
*
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database
*
* @returns {module:AddressModel} The instance of the newly created record
*/
function add (data = {}) {
const insertData = defaults(data)

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

/**
* Returns the defaults used when creating a new address
*
* 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 = {
address1: 'ENVIRONMENT AGENCY',
address2: 'HORIZON HOUSE',
address3: 'DEANERY ROAD',
town: 'BRISTOL',
postcode: 'BS1 5AH',
country: 'United Kingdom',
dataSource: 'wrls',
uprn: 340116
}

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

module.exports = {
add,
defaults
}