Skip to content

Commit

Permalink
Add CRM_V2 address model (#378)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4092

We are building a new endpoint to handle changing the address for a billing account.

We'll get into why we need to build a new endpoint when we start adding the code for it. But there are a series of changes we need to make in preparation.

The first of these was [Add automatic payload cleaning](#375). The next set of changes is adding the models we'll need to interact with the `crm_v2` schema.
  • Loading branch information
Cruikshanks authored Aug 25, 2023
1 parent f381684 commit 4acf8de
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
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
}

0 comments on commit 4acf8de

Please sign in to comment.