-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
f381684
commit 4acf8de
Showing
4 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |