diff --git a/app/models/crm-v2/address.model.js b/app/models/crm-v2/address.model.js new file mode 100644 index 0000000000..e413c0c174 --- /dev/null +++ b/app/models/crm-v2/address.model.js @@ -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 diff --git a/db/migrations/20230818154709_create-crm-v2-addresses.js b/db/migrations/20230818154709_create-crm-v2-addresses.js new file mode 100644 index 0000000000..f349f5e0ef --- /dev/null +++ b/db/migrations/20230818154709_create-crm-v2-addresses.js @@ -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) +} diff --git a/test/models/crm-v2/address.model.test.js b/test/models/crm-v2/address.model.test.js new file mode 100644 index 0000000000..5cd99e9c97 --- /dev/null +++ b/test/models/crm-v2/address.model.test.js @@ -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) + }) + }) +}) diff --git a/test/support/helpers/crm-v2/address.helper.js b/test/support/helpers/crm-v2/address.helper.js new file mode 100644 index 0000000000..dddf4ca4fe --- /dev/null +++ b/test/support/helpers/crm-v2/address.helper.js @@ -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 +}