diff --git a/app/models/crm-v2/address.model.js b/app/models/crm-v2/address.model.js deleted file mode 100644 index b304bd8cc0..0000000000 --- a/app/models/crm-v2/address.model.js +++ /dev/null @@ -1,42 +0,0 @@ -'use strict' - -/** - * Model for addresses - * @module AddressModel - */ - -const { Model } = require('objection') - -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' } - ] - } - - static get relationMappings () { - return { - billingAccountAddresses: { - relation: Model.HasManyRelation, - modelClass: 'billing-account-address.model', - join: { - from: 'addresses.addressId', - to: 'invoiceAccountAddresses.addressId' - } - } - } - } -} - -module.exports = AddressModel diff --git a/app/models/crm-v2/billing-account-address.model.js b/app/models/crm-v2/billing-account-address.model.js deleted file mode 100644 index e68edfeab1..0000000000 --- a/app/models/crm-v2/billing-account-address.model.js +++ /dev/null @@ -1,66 +0,0 @@ -'use strict' - -/** - * Model for invoice_account_addresses - * @module BillingAccountAddressModel - */ - -const { Model } = require('objection') - -const CrmV2BaseModel = require('./crm-v2-base.model.js') - -class BillingAccountAddressModel extends CrmV2BaseModel { - static get tableName () { - return 'invoiceAccountAddresses' - } - - static get idColumn () { - return 'invoiceAccountAddressId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - address: { - relation: Model.BelongsToOneRelation, - modelClass: 'address.model', - join: { - from: 'invoiceAccountAddresses.addressId', - to: 'addresses.addressId' - } - }, - agentCompany: { - relation: Model.BelongsToOneRelation, - modelClass: 'company.model', - join: { - from: 'invoiceAccountAddresses.agentCompanyId', - to: 'companies.companyId' - } - }, - billingAccount: { - relation: Model.BelongsToOneRelation, - modelClass: 'billing-account.model', - join: { - from: 'invoiceAccountAddresses.invoiceAccountId', - to: 'invoiceAccounts.invoiceAccountId' - } - }, - contact: { - relation: Model.BelongsToOneRelation, - modelClass: 'contact.model', - join: { - from: 'invoiceAccountAddresses.contactId', - to: 'contacts.contactId' - } - } - } - } -} - -module.exports = BillingAccountAddressModel diff --git a/app/models/crm-v2/billing-account.model.js b/app/models/crm-v2/billing-account.model.js deleted file mode 100644 index 717f4aada6..0000000000 --- a/app/models/crm-v2/billing-account.model.js +++ /dev/null @@ -1,50 +0,0 @@ -'use strict' - -/** - * Model for invoice_accounts - * @module BillingAccountModel - */ - -const { Model } = require('objection') - -const CrmV2BaseModel = require('./crm-v2-base.model.js') - -class BillingAccountModel extends CrmV2BaseModel { - static get tableName () { - return 'invoiceAccounts' - } - - static get idColumn () { - return 'invoiceAccountId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - billingAccountAddresses: { - relation: Model.HasManyRelation, - modelClass: 'billing-account-address.model', - join: { - from: 'invoiceAccounts.invoiceAccountId', - to: 'invoiceAccountAddresses.invoiceAccountId' - } - }, - company: { - relation: Model.BelongsToOneRelation, - modelClass: 'company.model', - join: { - from: 'invoiceAccounts.companyId', - to: 'companies.companyId' - } - } - } - } -} - -module.exports = BillingAccountModel diff --git a/app/models/crm-v2/company.model.js b/app/models/crm-v2/company.model.js deleted file mode 100644 index dcc8133860..0000000000 --- a/app/models/crm-v2/company.model.js +++ /dev/null @@ -1,82 +0,0 @@ -'use strict' - -/** - * Model for company - * @module CompanyModel - */ - -const { Model } = require('objection') - -const CrmV2BaseModel = require('./crm-v2-base.model.js') - -/** - * Objection model that represents a `company` in the `crm_v2.companies` table - * - * ### Notes - * - * There is no `dataSource` field in the table. But anything that has `externalId` populated can be assumed to have been - * imported from NALD and everything else directly entered into WRLS. - * - * For all records `name` and `type` are set - * - * When the source is 'nald' - * - * - `companyNumber` is always null - * - `organisationType` is always null - * - `lastHash` is always set - * - `currentHash` is always set - * - `externalId` is always set - * - * When the source is 'wrls' - * - * - `lastHash` is always null - * - `currentHash` is always null - * - `externalId` is always null - * - if `type` is 'organisation' then `companyNumber` is always set - * - if `type` is 'organisation' then `organisationType` can be null or set - * - if `type` is 'person' then `companyNumber` is always null - * - if `type` is 'person' then `organisationType` is always null - * - as of 2023-08-01 there were 28 companies with `type` set to 'organisation' - * - * We currently do not know how `organisationType` gets populated! - * - */ -class CompanyModel extends CrmV2BaseModel { - static get tableName () { - return 'companies' - } - - static get idColumn () { - return 'companyId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - billingAccountAddresses: { - relation: Model.HasManyRelation, - modelClass: 'billing-account-address.model', - join: { - from: 'companies.companyId', - to: 'invoiceAccountAddresses.agentCompanyId' - } - }, - billingAccounts: { - relation: Model.HasManyRelation, - modelClass: 'billing-account.model', - join: { - from: 'companies.companyId', - to: 'invoiceAccounts.companyId' - } - } - } - } -} - -module.exports = CompanyModel diff --git a/app/models/crm-v2/contact.model.js b/app/models/crm-v2/contact.model.js deleted file mode 100644 index 53953091c3..0000000000 --- a/app/models/crm-v2/contact.model.js +++ /dev/null @@ -1,113 +0,0 @@ -'use strict' - -/** - * Model for contacts - * @module ContactModel - */ - -const { Model } = require('objection') - -const CrmV2BaseModel = require('./crm-v2-base.model.js') - -/** - * Objection model that represents a `contact` in the `crm_v2.contacts` table - * - * ### Notes - * - * For all records `lastName` is always set - * - * When the `dataSource` is 'nald' - * - * - `contactType` is always null - * - `department` is always null - * - `middleInitials` is always null - * - `suffix` is always null - * - `externalId` is always set - * - * When the `dataSource` is 'wrls' - * - * - `contactType` is always set - * - `initials` is always null - * - `externalId` is always null - * - if `middleInitials` is set then `firstName` is always set - * - a 'person' always has `firstName` and `lastName` set - * - a 'person' can have `department` populated - * - a 'department' will only have `department` populated - * - as of 2023-08-01 there were 6 contacts with `suffix` populated out of 42,827 (1,621 WRLS) - * - */ -class ContactModel extends CrmV2BaseModel { - static get tableName () { - return 'contacts' - } - - static get idColumn () { - return 'contactId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - billingAccountAddresses: { - relation: Model.HasManyRelation, - modelClass: 'billing-account-address.model', - join: { - from: 'contacts.contactId', - to: 'invoiceAccountAddresses.contactId' - } - } - } - } - - /** - * Returns the name for the contact derived from its various parts - * - * We have 2 sources for contact data; the import from NALD and those directly entered into the service. For reasons - * only the previous team will know we have not been consistent across them. What fields are populated depends on - * the data source. Added to that 'contacts' entered via the service are used to hold both departments and people. - * - * We have to send a derived name when sending customer changes to the Charging Module API as it accepts only a - * single `customerName` value. What we have implemented here replicates what the legacy code was doing to derive - * what that name should be. - * - * @returns {String} The name for the contact derived from its various parts - */ - $name () { - if (this.contactType === 'department') { - return this.department - } - - const initials = this._determineInitials() - - const allNameParts = [ - this.salutation, - initials || this.firstName, // if we have initials use them else use firstName - this.lastName, - this.suffix - ] - - const onlyPopulatedNameParts = allNameParts.filter((item) => item) - - return onlyPopulatedNameParts.join(' ') - } - - _determineInitials () { - if (this.initials) { - return this.initials - } - - if (this.middleInitials) { - return `${this.firstName.slice(0, 1)} ${this.middleInitials}` - } - - return null - } -} - -module.exports = ContactModel diff --git a/app/models/crm-v2/crm-v2-base.model.js b/app/models/crm-v2/crm-v2-base.model.js deleted file mode 100644 index 7749edf7ff..0000000000 --- a/app/models/crm-v2/crm-v2-base.model.js +++ /dev/null @@ -1,25 +0,0 @@ -'use strict' - -/** - * Base class for all models based on the legacy 'crm_v2' schema - * @module CrmV2BaseModel - */ - -const LegacyBaseModel = require('../legacy-base.model.js') - -/** - * An issue with the way the standard implementation of the SnakeCase conversion was incorrectly handling the schema - * name of `crmV2` by converting it to `crm_v_2` has meant that a custom SnakeCase mapper has been created so that - * formatting is not applied to the `crm_v2` schema name. - * - * See `app/lib/legacy-db-snake-case-mappers.lib.js` or https://github.com/DEFRA/water-abstraction-system/pull/131 for - * more details. - */ - -class CrmV2BaseModel extends LegacyBaseModel { - static get schema () { - return 'crm_v2' - } -} - -module.exports = CrmV2BaseModel diff --git a/app/presenters/bills/view-bill.presenter.js b/app/presenters/bills/view-bill.presenter.js index 1bc0abce3d..5780a4f2bd 100644 --- a/app/presenters/bills/view-bill.presenter.js +++ b/app/presenters/bills/view-bill.presenter.js @@ -16,10 +16,10 @@ function go (bill, billingAccount) { const formattedBill = { accountName: _accountName(billingAccount), - accountNumber: billingAccount.invoiceAccountNumber, + accountNumber: billingAccount.accountNumber, addressLines: _addressLines(billingAccount), billId: bill.billingInvoiceId, - billingAccountId: billingAccount.invoiceAccountId, + billingAccountId: billingAccount.id, billNumber: bill.invoiceNumber, billRunId: billRun.billingBatchId, billRunNumber: billRun.billRunNumber, @@ -46,8 +46,8 @@ function go (bill, billingAccount) { function _accountName (billingAccount) { const accountAddress = billingAccount.billingAccountAddresses[0] - if (accountAddress.agentCompany) { - return accountAddress.agentCompany.name + if (accountAddress.company) { + return accountAddress.company.name } return billingAccount.company.name @@ -61,8 +61,8 @@ function _addressLines (billingAccount) { address.address2, address.address3, address.address4, - address.town, - address.county, + address.address5, + address.address6, address.postcode, address.country ] diff --git a/app/presenters/charging-module/create-customer-change.presenter.js b/app/presenters/charging-module/create-customer-change.presenter.js index 4b1c52d88f..519e33b41f 100644 --- a/app/presenters/charging-module/create-customer-change.presenter.js +++ b/app/presenters/charging-module/create-customer-change.presenter.js @@ -55,7 +55,7 @@ * @returns {Object} the request data needed in the format required by the Charging Module */ function go (billingAccount, address, company, contact) { - const { invoiceAccountNumber: customerReference } = billingAccount + const { accountNumber: customerReference } = billingAccount const region = customerReference.charAt(0) const customerName = _customerName(billingAccount, company) @@ -69,17 +69,17 @@ function go (billingAccount, address, company, contact) { } } -function _addressLine6 (county, country) { - if (!county && !country) { +function _addressLine6 (address6, country) { + if (!address6 && !country) { return '' } - if (county && country) { - return `${county}, ${country}` + if (address6 && country) { + return `${address6}, ${country}` } - if (county) { - return county + if (address6) { + return address6 } return country @@ -96,7 +96,7 @@ function _customerName (billingAccount, company) { function _formattedAddress (address, contact) { const addressLines = [] - const { address1, address2, address3, address4, town, county, country, postcode } = address + const { address1, address2, address3, address4, address5, address6, country, postcode } = address const contactName = contact.$name() if (address1) { @@ -133,8 +133,8 @@ function _formattedAddress (address, contact) { addressLine3: addressLines[2] ? addressLines[2] : null, addressLine4: addressLines[3] ? addressLines[3] : null, // We have encountered instances of ton being null so we confirm we have something to truncate to avoid an error - addressLine5: town ? _truncate(town, 60) : null, - addressLine6: _truncate(_addressLine6(county, country), 60), + addressLine5: address5 ? _truncate(address5, 60) : null, + addressLine6: _truncate(_addressLine6(address6, country), 60), postcode: _truncate(postcode, 60) } } diff --git a/app/services/bill-runs/supplementary/fetch-billing-accounts.service.js b/app/services/bill-runs/supplementary/fetch-billing-accounts.service.js index 6d2095560e..002b850a16 100644 --- a/app/services/bill-runs/supplementary/fetch-billing-accounts.service.js +++ b/app/services/bill-runs/supplementary/fetch-billing-accounts.service.js @@ -5,7 +5,7 @@ * @module FetchBillingAccountsService */ -const BillingAccountModel = require('../../../models/crm-v2/billing-account.model.js') +const BillingAccountModel = require('../../../models/billing-account.model.js') /** * Fetch all billing accounts for the supplied charge versions @@ -15,8 +15,8 @@ const BillingAccountModel = require('../../../models/crm-v2/billing-account.mode * @returns {Object[]} Array of objects in the format { invoiceAccountId: '...', invoiceAccountNumber: '...' } */ async function go (chargeVersions) { - const uniqueInvoiceAccountIds = _extractUniqueInvoiceAccountIds(chargeVersions) - const billingAccountModels = await _fetch(uniqueInvoiceAccountIds) + const uniqueBillingAccountIds = _extractUniqueBillingAccountIds(chargeVersions) + const billingAccountModels = await _fetch(uniqueBillingAccountIds) // The results come back from Objection as BillingAccountModels. Since we want to be clear that these are not // full-blown models, we turn them into plain objects using Objection's .toJSON() method @@ -25,20 +25,20 @@ async function go (chargeVersions) { return billingAccountObjects } -function _extractUniqueInvoiceAccountIds (chargeVersions) { - const allInvoiceAccountIds = chargeVersions.map((chargeVersion) => { +function _extractUniqueBillingAccountIds (chargeVersions) { + const allBillingAccountIds = chargeVersions.map((chargeVersion) => { return chargeVersion.invoiceAccountId }) - // Creating a new set from allInvoiceAccountIds gives us just the unique ids. Objection does not accept sets in + // Creating a new set from allBillingAccountIds gives us just the unique ids. Objection does not accept sets in // .findByIds() so we spread it into an array - return [...new Set(allInvoiceAccountIds)] + return [...new Set(allBillingAccountIds)] } -function _fetch (uniqueInvoiceAccountIds) { +function _fetch (uniqueBillingAccountIds) { return BillingAccountModel.query() - .select('invoiceAccountId', 'invoiceAccountNumber') - .findByIds([...uniqueInvoiceAccountIds]) + .select('id', 'accountNumber') + .findByIds([...uniqueBillingAccountIds]) } function _makeObjects (models) { diff --git a/app/services/bill-runs/supplementary/generate-bill.service.js b/app/services/bill-runs/supplementary/generate-bill.service.js index 5061ea4ee3..ff86efda4b 100644 --- a/app/services/bill-runs/supplementary/generate-bill.service.js +++ b/app/services/bill-runs/supplementary/generate-bill.service.js @@ -20,10 +20,10 @@ function go (billingAccount, billRunId, financialYearEnding) { const bill = { billingBatchId: billRunId, financialYearEnding, - invoiceAccountId: billingAccount.invoiceAccountId, + invoiceAccountId: billingAccount.id, billingInvoiceId: generateUUID(), address: {}, // Address is set to an empty object for SROC billing invoices - invoiceAccountNumber: billingAccount.invoiceAccountNumber, + invoiceAccountNumber: billingAccount.accountNumber, isCredit: false } diff --git a/app/services/bill-runs/supplementary/pre-generate-billing-data.service.js b/app/services/bill-runs/supplementary/pre-generate-billing-data.service.js index fde29b20f0..25e108a659 100644 --- a/app/services/bill-runs/supplementary/pre-generate-billing-data.service.js +++ b/app/services/bill-runs/supplementary/pre-generate-billing-data.service.js @@ -70,11 +70,11 @@ function _billLicenceKey (billId, licenceId) { /** * We pre-generate bills for every billing account so that we don't need to fetch any data from the db during the main * charge version processing loop. This function generates the required bill licences and returns an object where - * each key is the invoice account id, and each value is the bill, ie: + * each key is the billing account id, and each value is the bill, ie: * * { - * 'uuid-1': { invoiceAccountId: 'uuid-1', ... }, - * 'uuid-2': { invoiceAccountId: 'uuid-2', ... } + * 'uuid-1': { id: 'uuid-1', ... }, + * 'uuid-2': { id: 'uuid-2', ... } * } */ function _preGenerateBills (billingAccounts, billRunId, billingPeriod) { @@ -83,7 +83,7 @@ function _preGenerateBills (billingAccounts, billRunId, billingPeriod) { // bill licence already exists in the object before generating one return { ...acc, - [billingAccount.invoiceAccountId]: GenerateBillService.go( + [billingAccount.id]: GenerateBillService.go( billingAccount, billRunId, billingPeriod.endDate.getFullYear() diff --git a/app/services/billing-accounts/change-address.service.js b/app/services/billing-accounts/change-address.service.js index 6dde757765..05ba1b97e1 100644 --- a/app/services/billing-accounts/change-address.service.js +++ b/app/services/billing-accounts/change-address.service.js @@ -5,11 +5,11 @@ * @module ChangeAddressService */ -const AddressModel = require('../../models/crm-v2/address.model.js') -const BillingAccountAddressModel = require('../../models/crm-v2/billing-account-address.model.js') -const BillingAccountModel = require('../../models/crm-v2/billing-account.model.js') -const CompanyModel = require('../../models/crm-v2/company.model.js') -const ContactModel = require('../../models/crm-v2/contact.model.js') +const AddressModel = require('../../models/address.model.js') +const BillingAccountAddressModel = require('../../models/billing-account-address.model.js') +const BillingAccountModel = require('../../models/billing-account.model.js') +const CompanyModel = require('../../models/company.model.js') +const ContactModel = require('../../models/contact.model.js') const SendCustomerChangeService = require('./send-customer-change.service.js') /** @@ -18,11 +18,11 @@ const SendCustomerChangeService = require('./send-customer-change.service.js') * Within the service an internal user can 'Change the address' of a billing account. That is what the button says but * they can also change or set the agent company and the contact along with the address. * - * Behind the scenes a new `crm_v2.invoice_account_address` record is created that links to the `crm_v2.address`, - * `crm_v2.company` and `crm_v2.contact` records. It will also be linked to the `crm_v2.invoice_account` which + * Behind the scenes a new `billing_account_address` record is created that links to the `addresses`, + * `companies` and `contacts` records. It will also be linked to the `billing_account` which * represents the billing account being amended. * - * It won't have an end date, which marks it as the 'current' address. The previous `invoice_account_address` will get + * It won't have an end date, which marks it as the 'current' address. The previous `billing_account_address` will get * its `end_date` updated. The legacy service then knows that address is no longer current. * * When a change like this is made SOP, the system that sends invoices to customers, needs to know about it. That @@ -34,7 +34,7 @@ const SendCustomerChangeService = require('./send-customer-change.service.js') * - the source for addresses, companies and contacts is NALD and WRLS. But rather than transform them at ingress to a * single format the tables manage both. For example, NALD contacts use `initials` whereas WRLS contacts use * `middle_initials`. A contact can also be a 'department' under WRLS, which means the format changes again - * - where possible duplication of records has tried to be avoided. For example, you cannot have 2 addresses with the + * - where possible, duplication of records has tried to be avoided. For example, you cannot have 2 addresses with the * same UPRN, or 2 companies with the same company number. The problem is the source data _does_ change. We have seen * the OS Places result for Horizon House change from Environment Agency, to Natural England, Defra and back to the * Environment Agency over the years. This means when a user changes the address, for example, they may actually be @@ -81,9 +81,9 @@ async function go (billingAccountId, address, agentCompany = {}, contact = {}) { return _response(persistedData) } -async function _fetchBillingAccount (invoiceAccountId) { +async function _fetchBillingAccount (billingAccountId) { return BillingAccountModel.query() - .findById(invoiceAccountId) + .findById(billingAccountId) .withGraphFetched('company') .modifyGraph('company', (builder) => { builder.select([ @@ -105,7 +105,7 @@ async function _fetchBillingAccount (invoiceAccountId) { * instances. * * We attempt to persist the address, company and contact model instances first because we need their IDs in order to - * create the new `crm_v2.invoice_account_address` record. When we create that record we also need to apply an end date + * create the new `billing_account_addresses` record. When we create that record we also need to apply an end date * to any existing billing account addresses with a null end date. This is how the service determines which address * details are current (end date is null). * @@ -130,24 +130,24 @@ async function _persist (timestamp, billingAccount, address, company, contact) { persistedData.contact = await _persistContact(trx, contact) const billingAccountAddress = BillingAccountAddressModel.fromJson({ - invoiceAccountId: billingAccount.invoiceAccountId, - addressId: persistedData.address.addressId, - agentCompanyId: persistedData.company.companyId, - contactId: persistedData.contact.contactId, + billingAccountId: billingAccount.id, + addressId: persistedData.address.id, + companyId: persistedData.company.id, + contactId: persistedData.contact.id, startDate: timestamp, endDate: null, createdAt: timestamp, updatedAt: timestamp }) - await _patchExistingBillingAccountAddressEndDate(trx, billingAccount.invoiceAccountId, timestamp) + await _patchExistingBillingAccountAddressEndDate(trx, billingAccount.id, timestamp) persistedData.billingAccountAddress = await _persistBillingAccountAddress(trx, billingAccountAddress) }) return persistedData } -async function _patchExistingBillingAccountAddressEndDate (trx, invoiceAccountId, timestamp) { +async function _patchExistingBillingAccountAddressEndDate (trx, billingAccountId, timestamp) { // The timestamp represents the current date and time we're making this change, i.e. today. So, the new billing // account address will start from today. To show that the old record is no longer current, we need to set its // `endDate` to be today - 1 (yesterday). The following works it all out even if we're over a month or year boundary @@ -160,14 +160,14 @@ async function _patchExistingBillingAccountAddressEndDate (trx, invoiceAccountId endDate, updatedAt: timestamp }) - .where('invoiceAccountId', invoiceAccountId) + .where('billingAccountId', billingAccountId) .whereNull('endDate') } /** * Persist the new billing account address * - * The legacy code included logic to handle a situation where the start date and invoice account ID are the same. This + * The legacy code included logic to handle a situation where the start date and billing account ID are the same. This * could happen if you make a change to a billing account's address more than once on the same day. It would first * SELECT any records where that was the case and then DELETE them. * @@ -177,21 +177,21 @@ async function _patchExistingBillingAccountAddressEndDate (trx, invoiceAccountId async function _persistBillingAccountAddress (trx, billingAccountAddress) { return billingAccountAddress.$query(trx) .insert() - .onConflict(['invoiceAccountId', 'startDate']) + .onConflict(['billingAccountId', 'startDate']) // If a conflict is found this specifies what fields should get updated .merge([ 'addressId', - 'agentCompanyId', + 'companyId', 'contactId', 'endDate', - 'dateUpdated' + 'updatedAt' ]) } /** * Persist the address entered during the change address process * - * If the address has an `addressId:` we assume it was an existing address selected during the journey. So, the address + * If the address has an `id:` we assume it was an existing address selected during the journey. So, the address * is already persisted hence we just return `address`. * * Else we attempt to insert a new address record. If the address has a `uprn:` it will be one selected from the @@ -199,7 +199,7 @@ async function _persistBillingAccountAddress (trx, billingAccountAddress) { * records with matching UPRNs. But we use this to our advantage. Using `onConflict()` and `merge()` we can have * Objection JS update the existing address record if a matching UPRN exists. * - * Because either INSERT or UPDATE gets fired `returning()` will kick in and return the all important `addressId` which + * Because either INSERT or UPDATE gets fired `returning()` will kick in and return the all important `id` which * we'll need later for the billing account address. It will also return the fields specified in the INSERT/UPDATE hence * we get a 'complete' address back that we can return to the calling function. * @@ -211,7 +211,7 @@ async function _persistBillingAccountAddress (trx, billingAccountAddress) { * > returned when first added. */ async function _persistAddress (trx, address) { - if (address.addressId) { + if (address.id) { return address } @@ -224,21 +224,21 @@ async function _persistAddress (trx, address) { 'address2', 'address3', 'address4', - 'town', - 'county', + 'address5', + 'address6', 'country', 'postcode', - 'dateUpdated' + 'updatedAt' ]) .returning([ - 'addressId' + 'id' ]) } /** * Persist the company (Agent) entered during the change address process * - * If the company has a `companyId:` we assume it was an existing company selected during the journey. So, the company + * If the company has an `id:` we assume it was an existing company selected during the journey. So, the company * is already persisted hence we just return `company`. If the company name is not set then the user has opted not to * change the agent in the journey. So, we just return the empty `CompanyModel` instance. * @@ -246,9 +246,9 @@ async function _persistAddress (trx, address) { * company record selected by the user, or they will have been required to enter the company number. The previous team * added a unique constraint on `company_number` in the table so we cannot insert 2 records with matching numbers. But * we use this to our advantage. Using `onConflict()` and `merge()` we can have Objection JS update the existing company - * record if a matching company number. + * record if a matching company number exists. * - * Because either INSERT or UPDATE gets fired `returning()` will kick in and return the all important `companyId` which + * Because either INSERT or UPDATE gets fired `returning()` will kick in and return the all important `id` which * we'll need later for the billing account address. It will also return the fields specified in the INSERT/UPDATE hence * we get a 'complete' company back that we can return to the calling function. * @@ -260,7 +260,7 @@ async function _persistAddress (trx, address) { * > what it returned when first added. */ async function _persistCompany (trx, company) { - if (company.companyId || !company.name) { + if (company.id || !company.name) { return company } @@ -270,10 +270,10 @@ async function _persistCompany (trx, company) { // If a conflict is found this specifies what fields should get updated .merge([ 'name', - 'dateUpdated' + 'updatedAt' ]) .returning([ - 'companyId' + 'id' ]) } @@ -293,7 +293,7 @@ async function _persistContact (trx, contact) { return contact.$query(trx) .insert() .returning([ - 'contactId' + 'id' ]) } @@ -314,22 +314,22 @@ function _response (persistedData) { const { address, company, contact, billingAccountAddress } = persistedData return { - invoiceAccountAddress: { ...billingAccountAddress }, + billingAccountAddress: { ...billingAccountAddress }, address: { ...address }, - agentCompany: company.companyId ? { ...company } : null, - contact: contact.contactId ? { ...contact } : null + agentCompany: company.id ? { ...company } : null, + contact: contact.id ? { ...contact } : null } } function _transformAddress (timestamp, address) { return AddressModel.fromJson({ - addressId: address.addressId, + id: address.addressId, address1: address.addressLine1, address2: address.addressLine2, address3: address.addressLine3, address4: address.addressLine4, - town: address.town, - county: address.county, + address5: address.town, + address6: address.county, country: address.country, postcode: address.postcode, uprn: address.uprn, @@ -341,7 +341,7 @@ function _transformAddress (timestamp, address) { function _transformCompany (timestamp, company) { return CompanyModel.fromJson({ - companyId: company.companyId, + id: company.companyId, type: company.type, name: company.name, companyNumber: company.companyNumber, diff --git a/app/services/bills/fetch-billing-account.service.js b/app/services/bills/fetch-billing-account.service.js index 3a5cdde34a..568853a950 100644 --- a/app/services/bills/fetch-billing-account.service.js +++ b/app/services/bills/fetch-billing-account.service.js @@ -5,7 +5,7 @@ * @module FetchBillingAccountService */ -const BillingAccount = require('../../models/crm-v2/billing-account.model.js') +const BillingAccount = require('../../models/billing-account.model.js') /** * Fetch the matching Billing account plus the current billing account address record linked to it @@ -22,13 +22,13 @@ async function _fetch (id) { const result = BillingAccount.query() .findById(id) .select([ - 'invoiceAccountId', - 'invoiceAccountNumber' + 'id', + 'accountNumber' ]) .withGraphFetched('company') .modifyGraph('company', (builder) => { builder.select([ - 'company_id', + 'id', 'name', 'type' ]) @@ -41,21 +41,21 @@ async function _fetch (id) { .withGraphFetched('billingAccountAddresses.address') .modifyGraph('billingAccountAddresses.address', (builder) => { builder.select([ - 'addressId', + 'id', 'address1', 'address2', 'address3', 'address4', - 'town', - 'county', + 'address5', + 'address6', 'postcode', 'country' ]) }) - .withGraphFetched('billingAccountAddresses.agentCompany') - .modifyGraph('billingAccountAddresses.agentCompany', (builder) => { + .withGraphFetched('billingAccountAddresses.company') + .modifyGraph('billingAccountAddresses.company', (builder) => { builder.select([ - 'companyId', + 'id', 'name', 'type' ]) @@ -63,7 +63,7 @@ async function _fetch (id) { .withGraphFetched('billingAccountAddresses.contact') .modifyGraph('billingAccountAddresses.contact', (builder) => { builder.select([ - 'contactId', + 'id', 'contactType', 'dataSource', 'department', diff --git a/test/models/crm-v2/address.model.test.js b/test/models/crm-v2/address.model.test.js deleted file mode 100644 index a498f1c5cb..0000000000 --- a/test/models/crm-v2/address.model.test.js +++ /dev/null @@ -1,79 +0,0 @@ -'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 BillingAccountAddressHelper = require('../../support/helpers/crm-v2/billing-account-address.helper.js') -const BillingAccountAddressModel = require('../../../app/models/crm-v2/billing-account-address.model.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() - }) - - describe('Basic query', () => { - beforeEach(async () => { - testRecord = await AddressHelper.add() - }) - - 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) - }) - }) - - describe('Relationships', () => { - describe('when linking to billing account addresses', () => { - let testBillingAccountAddresses - - beforeEach(async () => { - testRecord = await AddressHelper.add() - const { addressId } = testRecord - - testBillingAccountAddresses = [] - for (let i = 0; i < 2; i++) { - // NOTE: A constraint in the invoice_account_addresses table means you cannot have 2 records with the same - // invoiceAccountId and start date - const startDate = i === 0 ? new Date(2023, 8, 4) : new Date(2023, 8, 3) - const billingAccountAddress = await BillingAccountAddressHelper.add({ startDate, addressId }) - testBillingAccountAddresses.push(billingAccountAddress) - } - }) - - it('can successfully run a related query', async () => { - const query = await AddressModel.query() - .innerJoinRelated('billingAccountAddresses') - - expect(query).to.exist() - }) - - it('can eager load the billing account addresses', async () => { - const result = await AddressModel.query() - .findById(testRecord.addressId) - .withGraphFetched('billingAccountAddresses') - - expect(result).to.be.instanceOf(AddressModel) - expect(result.addressId).to.equal(testRecord.addressId) - - expect(result.billingAccountAddresses).to.be.an.array() - expect(result.billingAccountAddresses[0]).to.be.an.instanceOf(BillingAccountAddressModel) - expect(result.billingAccountAddresses).to.include(testBillingAccountAddresses[0]) - expect(result.billingAccountAddresses).to.include(testBillingAccountAddresses[1]) - }) - }) - }) -}) diff --git a/test/models/crm-v2/billing-account-address.model.test.js b/test/models/crm-v2/billing-account-address.model.test.js deleted file mode 100644 index 8b714a0351..0000000000 --- a/test/models/crm-v2/billing-account-address.model.test.js +++ /dev/null @@ -1,158 +0,0 @@ -'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 AddressModel = require('../../../app/models/crm-v2/address.model.js') -const BillingAccountAddressHelper = require('../../support/helpers/crm-v2/billing-account-address.helper.js') -const BillingAccountHelper = require('../../support/helpers/crm-v2/billing-account.helper.js') -const BillingAccountModel = require('../../../app/models/crm-v2/billing-account.model.js') -const CompanyHelper = require('../../support/helpers/crm-v2/company.helper.js') -const CompanyModel = require('../../../app/models/crm-v2/company.model.js') -const ContactHelper = require('../../support/helpers/crm-v2/contact.helper.js') -const ContactModel = require('../../../app/models/crm-v2/contact.model.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') - -// Thing under test -const BillingAccountAddressModel = require('../../../app/models/crm-v2/billing-account-address.model.js') - -describe('Billing Account Address model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - }) - - describe('Basic query', () => { - beforeEach(async () => { - testRecord = await BillingAccountAddressHelper.add() - }) - - it('can successfully run a basic query', async () => { - const result = await BillingAccountAddressModel.query().findById(testRecord.invoiceAccountAddressId) - - expect(result).to.be.an.instanceOf(BillingAccountAddressModel) - expect(result.invoiceAccountAddressId).to.equal(testRecord.invoiceAccountAddressId) - }) - }) - - describe('Relationships', () => { - describe('when linking to address', () => { - let testAddress - - beforeEach(async () => { - testAddress = await AddressHelper.add() - testRecord = await BillingAccountAddressHelper.add({ addressId: testAddress.addressId }) - }) - - it('can successfully run a related query', async () => { - const query = await BillingAccountAddressModel.query() - .innerJoinRelated('address') - - expect(query).to.exist() - }) - - it('can eager load the address', async () => { - const result = await BillingAccountAddressModel.query() - .findById(testRecord.invoiceAccountAddressId) - .withGraphFetched('address') - - expect(result).to.be.instanceOf(BillingAccountAddressModel) - expect(result.invoiceAccountAddressId).to.equal(testRecord.invoiceAccountAddressId) - - expect(result.address).to.be.an.instanceOf(AddressModel) - expect(result.address).to.equal(testAddress) - }) - }) - - describe('when linking to company (agent)', () => { - let testCompany - - beforeEach(async () => { - testCompany = await CompanyHelper.add() - testRecord = await BillingAccountAddressHelper.add({ agentCompanyId: testCompany.companyId }) - }) - - it('can successfully run a related query', async () => { - const query = await BillingAccountAddressModel.query() - .innerJoinRelated('agentCompany') - - expect(query).to.exist() - }) - - it('can eager load the agent company', async () => { - const result = await BillingAccountAddressModel.query() - .findById(testRecord.invoiceAccountAddressId) - .withGraphFetched('agentCompany') - - expect(result).to.be.instanceOf(BillingAccountAddressModel) - expect(result.invoiceAccountAddressId).to.equal(testRecord.invoiceAccountAddressId) - - expect(result.agentCompany).to.be.an.instanceOf(CompanyModel) - expect(result.agentCompany).to.equal(testCompany) - }) - }) - - describe('when linking to billing account', () => { - let testBillingAccount - - beforeEach(async () => { - testBillingAccount = await BillingAccountHelper.add() - testRecord = await BillingAccountAddressHelper.add({ invoiceAccountId: testBillingAccount.invoiceAccountId }) - }) - - it('can successfully run a related query', async () => { - const query = await BillingAccountAddressModel.query() - .innerJoinRelated('billingAccount') - - expect(query).to.exist() - }) - - it('can eager load the billing account', async () => { - const result = await BillingAccountAddressModel.query() - .findById(testRecord.invoiceAccountAddressId) - .withGraphFetched('billingAccount') - - expect(result).to.be.instanceOf(BillingAccountAddressModel) - expect(result.invoiceAccountAddressId).to.equal(testRecord.invoiceAccountAddressId) - - expect(result.billingAccount).to.be.an.instanceOf(BillingAccountModel) - expect(result.billingAccount).to.equal(testBillingAccount) - }) - }) - - describe('when linking to contact', () => { - let testContact - - beforeEach(async () => { - testContact = await ContactHelper.add() - testRecord = await BillingAccountAddressHelper.add({ contactId: testContact.contactId }) - }) - - it('can successfully run a related query', async () => { - const query = await BillingAccountAddressModel.query() - .innerJoinRelated('contact') - - expect(query).to.exist() - }) - - it('can eager load the contact', async () => { - const result = await BillingAccountAddressModel.query() - .findById(testRecord.invoiceAccountAddressId) - .withGraphFetched('contact') - - expect(result).to.be.instanceOf(BillingAccountAddressModel) - expect(result.invoiceAccountAddressId).to.equal(testRecord.invoiceAccountAddressId) - - expect(result.contact).to.be.an.instanceOf(ContactModel) - expect(result.contact).to.equal(testContact) - }) - }) - }) -}) diff --git a/test/models/crm-v2/billing-account.model.test.js b/test/models/crm-v2/billing-account.model.test.js deleted file mode 100644 index 7acd48fbc8..0000000000 --- a/test/models/crm-v2/billing-account.model.test.js +++ /dev/null @@ -1,109 +0,0 @@ -'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 BillingAccountAddressHelper = require('../../support/helpers/crm-v2/billing-account-address.helper.js') -const BillingAccountAddressModel = require('../../../app/models/crm-v2/billing-account-address.model.js') -const BillingAccountHelper = require('../../support/helpers/crm-v2/billing-account.helper.js') -const CompanyHelper = require('../../support/helpers/crm-v2/company.helper.js') -const CompanyModel = require('../../../app/models/crm-v2/company.model.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') - -// Thing under test -const BillingAccountModel = require('../../../app/models/crm-v2/billing-account.model.js') - -describe('Billing Account model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - }) - - describe('Basic query', () => { - beforeEach(async () => { - testRecord = await BillingAccountHelper.add() - }) - - it('can successfully run a basic query', async () => { - const result = await BillingAccountModel.query().findById(testRecord.invoiceAccountId) - - expect(result).to.be.an.instanceOf(BillingAccountModel) - expect(result.invoiceAccountId).to.equal(testRecord.invoiceAccountId) - }) - }) - - describe('Relationships', () => { - describe('when linking to company', () => { - let testCompany - - beforeEach(async () => { - testCompany = await CompanyHelper.add() - testRecord = await BillingAccountHelper.add({ companyId: testCompany.companyId }) - }) - - it('can successfully run a related query', async () => { - const query = await BillingAccountModel.query() - .innerJoinRelated('company') - - expect(query).to.exist() - }) - - it('can eager load the company', async () => { - const result = await BillingAccountModel.query() - .findById(testRecord.invoiceAccountId) - .withGraphFetched('company') - - expect(result).to.be.instanceOf(BillingAccountModel) - expect(result.invoiceAccountId).to.equal(testRecord.invoiceAccountId) - - expect(result.company).to.be.an.instanceOf(CompanyModel) - expect(result.company).to.equal(testCompany) - }) - }) - - describe('when linking to billing account addresses', () => { - let testBillingAccountAddresses - - beforeEach(async () => { - testRecord = await BillingAccountHelper.add() - const { invoiceAccountId } = testRecord - - testBillingAccountAddresses = [] - for (let i = 0; i < 2; i++) { - // NOTE: A constraint in the invoice_account_addresses table means you cannot have 2 records with the same - // invoiceAccountId and start date - const startDate = i === 0 ? new Date(2023, 8, 4) : new Date(2023, 8, 3) - const billingAccountAddress = await BillingAccountAddressHelper.add({ startDate, invoiceAccountId }) - testBillingAccountAddresses.push(billingAccountAddress) - } - }) - - it('can successfully run a related query', async () => { - const query = await BillingAccountModel.query() - .innerJoinRelated('billingAccountAddresses') - - expect(query).to.exist() - }) - - it('can eager load the billing account addresses', async () => { - const result = await BillingAccountModel.query() - .findById(testRecord.invoiceAccountId) - .withGraphFetched('billingAccountAddresses') - - expect(result).to.be.instanceOf(BillingAccountModel) - expect(result.invoiceAccountId).to.equal(testRecord.invoiceAccountId) - - expect(result.billingAccountAddresses).to.be.an.array() - expect(result.billingAccountAddresses[0]).to.be.an.instanceOf(BillingAccountAddressModel) - expect(result.billingAccountAddresses).to.include(testBillingAccountAddresses[0]) - expect(result.billingAccountAddresses).to.include(testBillingAccountAddresses[1]) - }) - }) - }) -}) diff --git a/test/models/crm-v2/company.model.test.js b/test/models/crm-v2/company.model.test.js deleted file mode 100644 index b6a9e09ed8..0000000000 --- a/test/models/crm-v2/company.model.test.js +++ /dev/null @@ -1,117 +0,0 @@ -'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 BillingAccountAddressHelper = require('../../support/helpers/crm-v2/billing-account-address.helper.js') -const BillingAccountAddressModel = require('../../../app/models/crm-v2/billing-account-address.model.js') -const BillingAccountHelper = require('../../support/helpers/crm-v2/billing-account.helper.js') -const BillingAccountModel = require('../../../app/models/crm-v2/billing-account.model.js') -const CompanyHelper = require('../../support/helpers/crm-v2/company.helper.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') - -// Thing under test -const CompanyModel = require('../../../app/models/crm-v2/company.model.js') - -describe('Company model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - }) - - describe('Basic query', () => { - beforeEach(async () => { - testRecord = await CompanyHelper.add() - }) - - it('can successfully run a basic query', async () => { - const result = await CompanyModel.query().findById(testRecord.companyId) - - expect(result).to.be.an.instanceOf(CompanyModel) - expect(result.companyId).to.equal(testRecord.companyId) - }) - }) - - describe('Relationships', () => { - describe('when linking to billing account addresses', () => { - let testBillingAccountAddresses - - beforeEach(async () => { - testRecord = await CompanyHelper.add() - const { companyId: agentCompanyId } = testRecord - - testBillingAccountAddresses = [] - for (let i = 0; i < 2; i++) { - // NOTE: A constraint in the invoice_account_addresses table means you cannot have 2 records with the same - // invoiceAccountId and start date - const startDate = i === 0 ? new Date(2023, 8, 4) : new Date(2023, 8, 3) - const billingAccountAddress = await BillingAccountAddressHelper.add({ startDate, agentCompanyId }) - testBillingAccountAddresses.push(billingAccountAddress) - } - }) - - it('can successfully run a related query', async () => { - const query = await CompanyModel.query() - .innerJoinRelated('billingAccountAddresses') - - expect(query).to.exist() - }) - - it('can eager load the billing account addresses', async () => { - const result = await CompanyModel.query() - .findById(testRecord.companyId) - .withGraphFetched('billingAccountAddresses') - - expect(result).to.be.instanceOf(CompanyModel) - expect(result.companyId).to.equal(testRecord.companyId) - - expect(result.billingAccountAddresses).to.be.an.array() - expect(result.billingAccountAddresses[0]).to.be.an.instanceOf(BillingAccountAddressModel) - expect(result.billingAccountAddresses).to.include(testBillingAccountAddresses[0]) - expect(result.billingAccountAddresses).to.include(testBillingAccountAddresses[1]) - }) - }) - - describe('when linking to billing accounts', () => { - let testBillingAccounts - - beforeEach(async () => { - testRecord = await CompanyHelper.add() - const { companyId } = testRecord - - testBillingAccounts = [] - for (let i = 0; i < 2; i++) { - const billingAccount = await BillingAccountHelper.add({ companyId }) - testBillingAccounts.push(billingAccount) - } - }) - - it('can successfully run a related query', async () => { - const query = await CompanyModel.query() - .innerJoinRelated('billingAccounts') - - expect(query).to.exist() - }) - - it('can eager load the billing accounts', async () => { - const result = await CompanyModel.query() - .findById(testRecord.companyId) - .withGraphFetched('billingAccounts') - - expect(result).to.be.instanceOf(CompanyModel) - expect(result.companyId).to.equal(testRecord.companyId) - - expect(result.billingAccounts).to.be.an.array() - expect(result.billingAccounts[0]).to.be.an.instanceOf(BillingAccountModel) - expect(result.billingAccounts).to.include(testBillingAccounts[0]) - expect(result.billingAccounts).to.include(testBillingAccounts[1]) - }) - }) - }) -}) diff --git a/test/models/crm-v2/contact.model.test.js b/test/models/crm-v2/contact.model.test.js deleted file mode 100644 index 60f1029dd0..0000000000 --- a/test/models/crm-v2/contact.model.test.js +++ /dev/null @@ -1,274 +0,0 @@ -'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 BillingAccountAddressHelper = require('../../support/helpers/crm-v2/billing-account-address.helper.js') -const BillingAccountAddressModel = require('../../../app/models/crm-v2/billing-account-address.model.js') -const ContactHelper = require('../../support/helpers/crm-v2/contact.helper.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') - -// Thing under test -const ContactModel = require('../../../app/models/crm-v2/contact.model.js') - -describe('Contact model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - }) - - describe('Basic query', () => { - beforeEach(async () => { - testRecord = await ContactHelper.add() - }) - - it('can successfully run a basic query', async () => { - const result = await ContactModel.query().findById(testRecord.contactId) - - expect(result).to.be.an.instanceOf(ContactModel) - expect(result.contactId).to.equal(testRecord.contactId) - }) - }) - - describe('Relationships', () => { - describe('when linking to billing account addresses', () => { - let testBillingAccountAddresses - - beforeEach(async () => { - testRecord = await ContactHelper.add() - const { contactId } = testRecord - - testBillingAccountAddresses = [] - for (let i = 0; i < 2; i++) { - // NOTE: A constraint in the invoice_account_addresses table means you cannot have 2 records with the same - // invoiceAccountId and start date - const startDate = i === 0 ? new Date(2023, 8, 4) : new Date(2023, 8, 3) - const billingAccountAddress = await BillingAccountAddressHelper.add({ startDate, contactId }) - testBillingAccountAddresses.push(billingAccountAddress) - } - }) - - it('can successfully run a related query', async () => { - const query = await ContactModel.query() - .innerJoinRelated('billingAccountAddresses') - - expect(query).to.exist() - }) - - it('can eager load the billing account addresses', async () => { - const result = await ContactModel.query() - .findById(testRecord.contactId) - .withGraphFetched('billingAccountAddresses') - - expect(result).to.be.instanceOf(ContactModel) - expect(result.contactId).to.equal(testRecord.contactId) - - expect(result.billingAccountAddresses).to.be.an.array() - expect(result.billingAccountAddresses[0]).to.be.an.instanceOf(BillingAccountAddressModel) - expect(result.billingAccountAddresses).to.include(testBillingAccountAddresses[0]) - expect(result.billingAccountAddresses).to.include(testBillingAccountAddresses[1]) - }) - }) - }) - - // NOTE: The test data we generate in these tests is in accordance with how a contact record could be populated. For - // example, if the data source is 'wrls' and the contact type is 'person', then first name and last name is always - // populated. We don't test what would happen if last name wasn't because analysis of the data confirms this would - // never happen. See the method's documentation for more details. - describe('$name', () => { - let contactRecord - - beforeEach(() => { - contactRecord = { - contactType: null, - dataSource: null, - department: null, - firstName: null, - initials: null, - lastName: null, - middleInitials: null, - salutation: null, - suffix: null - } - }) - - describe('when the contact was imported in NALD', () => { - beforeEach(() => { - contactRecord.dataSource = 'nald' - }) - - describe("and has a last name of 'Villar'", () => { - beforeEach(() => { - contactRecord.lastName = 'Villar' - }) - - it("returns 'Villar'", () => { - testRecord = ContactModel.fromJson(contactRecord) - - expect(testRecord.$name()).to.equal('Villar') - }) - - describe("and the salutation 'Mrs'", () => { - beforeEach(() => { - contactRecord.salutation = 'Mrs' - }) - - it("returns 'Mrs Villar'", () => { - testRecord = ContactModel.fromJson(contactRecord) - - expect(testRecord.$name()).to.equal('Mrs Villar') - }) - - describe("and the initial 'J'", () => { - beforeEach(() => { - contactRecord.initials = 'J' - }) - - it("returns 'Mrs J Villar'", () => { - testRecord = ContactModel.fromJson(contactRecord) - - expect(testRecord.$name()).to.equal('Mrs J Villar') - }) - }) - }) - - describe("and a first name of 'Margherita'", () => { - beforeEach(() => { - contactRecord.firstName = 'Margherita' - }) - - it("returns 'Margherita Villar'", () => { - testRecord = ContactModel.fromJson(contactRecord) - - expect(testRecord.$name()).to.equal('Margherita Villar') - }) - - describe("and the salutation 'Mrs'", () => { - beforeEach(() => { - contactRecord.salutation = 'Mrs' - }) - - it("returns 'Mrs Margherita Villar'", () => { - testRecord = ContactModel.fromJson(contactRecord) - - expect(testRecord.$name()).to.equal('Mrs Margherita Villar') - }) - - describe("and the initial 'J'", () => { - beforeEach(() => { - contactRecord.initials = 'J' - }) - - it("returns 'Mrs J Villar'", () => { - testRecord = ContactModel.fromJson(contactRecord) - - expect(testRecord.$name()).to.equal('Mrs J Villar') - }) - }) - }) - }) - }) - }) - - describe('when the contact was entered into WRLS', () => { - beforeEach(() => { - contactRecord.dataSource = 'wrls' - }) - - describe("and it is a department named 'Humanoid Risk Assessment'", () => { - beforeEach(() => { - contactRecord.contactType = 'department' - contactRecord.department = 'Humanoid Risk Assessment' - }) - - it("returns 'Humanoid Risk Assessment'", () => { - testRecord = ContactModel.fromJson(contactRecord) - - expect(testRecord.$name()).to.equal('Humanoid Risk Assessment') - }) - }) - - describe("and it is a 'person'", () => { - beforeEach(() => { - contactRecord.contactType = 'person' - contactRecord.firstName = 'Vincent' - contactRecord.lastName = 'Anderson' - }) - - describe("with the name 'Vincent Anderson'", () => { - it("returns 'Vincent Anderson'", () => { - testRecord = ContactModel.fromJson(contactRecord) - - expect(testRecord.$name()).to.equal('Vincent Anderson') - }) - }) - - describe("and the salutation 'Mr'", () => { - beforeEach(() => { - contactRecord.salutation = 'Mr' - }) - - it("returns 'Mr Vincent Anderson'", () => { - testRecord = ContactModel.fromJson(contactRecord) - - expect(testRecord.$name()).to.equal('Mr Vincent Anderson') - }) - - describe("and the middle initial 'P'", () => { - beforeEach(() => { - contactRecord.middleInitials = 'P' - }) - - it("returns 'Mr V P Anderson'", () => { - testRecord = ContactModel.fromJson(contactRecord) - - expect(testRecord.$name()).to.equal('Mr V P Anderson') - }) - - describe("and the suffix 'MBE'", () => { - beforeEach(() => { - contactRecord.suffix = 'MBE' - }) - - it("returns 'Mr V P Anderson MBE'", () => { - testRecord = ContactModel.fromJson(contactRecord) - - expect(testRecord.$name()).to.equal('Mr V P Anderson MBE') - }) - }) - }) - }) - - describe("and the middle initial 'P'", () => { - beforeEach(() => { - contactRecord.middleInitials = 'P' - }) - - it("returns 'V P Anderson'", () => { - testRecord = ContactModel.fromJson(contactRecord) - - expect(testRecord.$name()).to.equal('V P Anderson') - }) - - describe("and the suffix 'MBE'", () => { - beforeEach(() => { - contactRecord.suffix = 'MBE' - }) - - it("returns 'V P Anderson MBE'", () => { - testRecord = ContactModel.fromJson(contactRecord) - - expect(testRecord.$name()).to.equal('V P Anderson MBE') - }) - }) - }) - }) - }) - }) -}) diff --git a/test/presenters/bills/view-bill.presenter.test.js b/test/presenters/bills/view-bill.presenter.test.js index d8dcb0a1a0..bcb0de7e93 100644 --- a/test/presenters/bills/view-bill.presenter.test.js +++ b/test/presenters/bills/view-bill.presenter.test.js @@ -8,7 +8,7 @@ const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const ContactModel = require('../../../app/models/crm-v2/contact.model.js') +const ContactModel = require('../../../app/models/contact.model.js') // Thing under test const ViewBillPresenter = require('../../../app/presenters/bills/view-bill.presenter.js') @@ -64,7 +64,7 @@ describe('View Bill presenter', () => { describe('when the billing account is linked to an agent', () => { beforeEach(() => { - billingAccount.billingAccountAddresses[0].agentCompany = { + billingAccount.billingAccountAddresses[0].company = { companyId: 'b0d35412-f76c-44ca-9d63-c6350337e03d', type: 'person', name: 'Alan Broke' @@ -400,34 +400,33 @@ function _testBill () { function _testBillingAccount () { return { - invoiceAccountId: 'ee3f5562-26ad-4d58-9b59-5c388a13d7d0', - invoiceAccountNumber: 'E88888888A', + id: 'ee3f5562-26ad-4d58-9b59-5c388a13d7d0', + accountNumber: 'E88888888A', company: { type: 'organisation', name: 'Wessex Water Services Ltd' }, billingAccountAddresses: [ { - invoiceAccountAddressId: 'b0e53215-b73a-4570-992b-2a724944ea19', - invoiceAccountId: 'ee3f5562-26ad-4d58-9b59-5c388a13d7d0', + id: 'b0e53215-b73a-4570-992b-2a724944ea19', + billingAccountId: 'ee3f5562-26ad-4d58-9b59-5c388a13d7d0', addressId: 'f3360183-8002-4802-a6d4-80d7e7160a50', startDate: new Date('1999-10-01'), endDate: null, - isTest: false, - agentCompanyId: null, + companyId: null, contactId: null, createdAt: new Date('2022-06-15'), updatedAt: new Date('2023-10-17'), - agentCompany: null, + company: null, contact: null, address: { - addressId: 'f3360183-8002-4802-a6d4-80d7e7160a50', + id: 'f3360183-8002-4802-a6d4-80d7e7160a50', address1: '86 Oxford Road', address2: 'WOOTTON', address3: null, address4: null, - town: 'COURTENAY', - county: null, + address5: 'COURTENAY', + address6: null, postcode: 'TA24 8NX', country: null } diff --git a/test/presenters/charging-module/create-customer-change.presenter.test.js b/test/presenters/charging-module/create-customer-change.presenter.test.js index 74adc5a859..62a6f3ab13 100644 --- a/test/presenters/charging-module/create-customer-change.presenter.test.js +++ b/test/presenters/charging-module/create-customer-change.presenter.test.js @@ -8,10 +8,10 @@ const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const AddressModel = require('../../../app/models/crm-v2/address.model.js') -const BillingAccountModel = require('../../../app/models/crm-v2/billing-account.model.js') -const CompanyModel = require('../../../app/models/crm-v2/company.model.js') -const ContactModel = require('../../../app/models/crm-v2/contact.model.js') +const AddressModel = require('../../../app/models/address.model.js') +const BillingAccountModel = require('../../../app/models/billing-account.model.js') +const CompanyModel = require('../../../app/models/company.model.js') +const ContactModel = require('../../../app/models/contact.model.js') // Thing under test const CreateCustomerChangePresenter = require('../../../app/presenters/charging-module/create-customer-change.presenter.js') @@ -23,7 +23,7 @@ const CreateCustomerChangePresenter = require('../../../app/presenters/charging- // what scenarios we face when converting the WRLS data. describe('Charging Module Create Transaction presenter', () => { const billingAccount = BillingAccountModel.fromJson({ - invoiceAccountNumber: 'B19120000A', + accountNumber: 'B19120000A', company: { name: 'BILLING ACCOUNT COMPANY' } @@ -33,8 +33,8 @@ describe('Charging Module Create Transaction presenter', () => { address2: 'HORIZON HOUSE', address3: 'DEANERY ROAD', address4: null, - town: 'BRISTOL', - county: null, + address5: 'BRISTOL', + address6: null, country: 'United Kingdom', postcode: 'BS1 5AH' } @@ -300,21 +300,21 @@ describe('Charging Module Create Transaction presenter', () => { contact = ContactModel.fromJson({}) }) - describe('when town is set in the address', () => { + describe('when address5 is set in the address', () => { beforeEach(() => { address = AddressModel.fromJson({ ...standardAddress }) }) - it('returns the town for addressLine5', () => { + it('returns address for addressLine5', () => { const result = CreateCustomerChangePresenter.go(billingAccount, address, company, contact) expect(result.addressLine5).to.equal('BRISTOL') }) }) - describe('when town is not set in the address', () => { + describe('when address5 is not set in the address', () => { beforeEach(() => { - address = AddressModel.fromJson({ ...standardAddress, town: null }) + address = AddressModel.fromJson({ ...standardAddress, address5: null }) }) it('returns null for addressLine5', () => { @@ -331,22 +331,22 @@ describe('Charging Module Create Transaction presenter', () => { contact = ContactModel.fromJson({}) }) - describe('when both county and country are set', () => { + describe('when both address6 and country are set', () => { beforeEach(() => { address = AddressModel.fromJson({ ...standardAddress, - county: 'AVON' + address6: 'AVON' }) }) - it('returns both county and country for addressLine6', () => { + it('returns both address6 and country for addressLine6', () => { const result = CreateCustomerChangePresenter.go(billingAccount, address, company, contact) expect(result.addressLine6).to.equal('AVON, United Kingdom') }) }) - describe('when neither county and country are set', () => { + describe('when neither address6 and country are set', () => { beforeEach(() => { address = AddressModel.fromJson({ ...standardAddress, @@ -361,16 +361,16 @@ describe('Charging Module Create Transaction presenter', () => { }) }) - describe('when only county is set', () => { + describe('when only address6 is set', () => { beforeEach(() => { address = AddressModel.fromJson({ ...standardAddress, - county: 'AVON', + address6: 'AVON', country: null }) }) - it('returns just the county for addressLine6', () => { + it('returns just address for addressLine6', () => { const result = CreateCustomerChangePresenter.go(billingAccount, address, company, contact) expect(result.addressLine6).to.equal('AVON') @@ -408,7 +408,7 @@ describe('Charging Module Create Transaction presenter', () => { beforeEach(() => { address = AddressModel.fromJson({ ...standardAddress, - town: 'Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch Town' + address5: 'Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch Town' }) company = CompanyModel.fromJson({}) contact = ContactModel.fromJson({}) diff --git a/test/services/bill-runs/fetch-bill-run.service.test.js b/test/services/bill-runs/fetch-bill-run.service.test.js index 4fa179d512..be26e0a72d 100644 --- a/test/services/bill-runs/fetch-bill-run.service.test.js +++ b/test/services/bill-runs/fetch-bill-run.service.test.js @@ -8,14 +8,14 @@ const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const AddressHelper = require('../../support/helpers/crm-v2/address.helper.js') +const AddressHelper = require('../../support/helpers/address.helper.js') const BillHelper = require('../../support/helpers/water/bill.helper.js') -const BillingAccountHelper = require('../../support/helpers/crm-v2/billing-account.helper.js') -const BillingAccountAddressHelper = require('../../support/helpers/crm-v2/billing-account-address.helper.js') +const BillingAccountHelper = require('../../support/helpers/billing-account.helper.js') +const BillingAccountAddressHelper = require('../../support/helpers/billing-account-address.helper.js') const BillRunHelper = require('../../support/helpers/water/bill-run.helper.js') const BillRunModel = require('../../../app/models/water/bill-run.model.js') const BillLicenceHelper = require('../../support/helpers/water/bill-licence.helper.js') -const CompanyHelper = require('../../support/helpers/crm-v2/company.helper.js') +const CompanyHelper = require('../../support/helpers/company.helper.js') const DatabaseHelper = require('../../support/helpers/database.helper.js') const LicenceHelper = require('../../support/helpers/water/licence.helper.js') const RegionHelper = require('../../support/helpers/water/region.helper.js') @@ -62,8 +62,8 @@ describe('Fetch Bill Run service', () => { // We intend to create 2 Bills linked to this bill run and test we get 2 summaries back. Each bill _must_ be linked // to a different billing account. So, we need two. linkedBillingAccounts = await Promise.all([ - BillingAccountHelper.add({ companyId: linkedCompanies[0].companyId }), - BillingAccountHelper.add({ companyId: linkedCompanies[1].companyId }) + BillingAccountHelper.add({ companyId: linkedCompanies[0].id }), + BillingAccountHelper.add({ companyId: linkedCompanies[1].id }) ]) // We don't care about the address in our service. But it's a required field when creating a BillingAccountAddress @@ -73,14 +73,14 @@ describe('Fetch Bill Run service', () => { // company we prepared earlier await Promise.all([ BillingAccountAddressHelper.add({ - invoiceAccountId: linkedBillingAccounts[0].invoiceAccountId, - addressId: address.addressId + billingAccountId: linkedBillingAccounts[0].id, + addressId: address.id }), BillingAccountAddressHelper.add({ - invoiceAccountId: linkedBillingAccounts[1].invoiceAccountId, - addressId: address.addressId, - agentCompanyId: linkedCompanies[2].companyId + billingAccountId: linkedBillingAccounts[1].id, + addressId: address.id, + companyId: linkedCompanies[2].id }) ]) @@ -89,14 +89,14 @@ describe('Fetch Bill Run service', () => { linkedBills = await Promise.all([ BillHelper.add({ billingBatchId: testBillRun.billingBatchId, - invoiceAccountId: linkedBillingAccounts[0].invoiceAccountId, - invoiceAccountNumber: linkedBillingAccounts[0].invoiceAccountNumber, + invoiceAccountId: linkedBillingAccounts[0].id, + invoiceAccountNumber: linkedBillingAccounts[0].accountNumber, netAmount: 1550 }), BillHelper.add({ billingBatchId: testBillRun.billingBatchId, - invoiceAccountId: linkedBillingAccounts[1].invoiceAccountId, - invoiceAccountNumber: linkedBillingAccounts[1].invoiceAccountNumber, + invoiceAccountId: linkedBillingAccounts[1].id, + invoiceAccountNumber: linkedBillingAccounts[1].accountNumber, netAmount: 2345 }) ]) diff --git a/test/services/bill-runs/supplementary/fetch-billing-accounts.service.test.js b/test/services/bill-runs/supplementary/fetch-billing-accounts.service.test.js index a8a85f467f..984aab1707 100644 --- a/test/services/bill-runs/supplementary/fetch-billing-accounts.service.test.js +++ b/test/services/bill-runs/supplementary/fetch-billing-accounts.service.test.js @@ -8,7 +8,7 @@ const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const BillingAccountHelper = require('../../../support/helpers/crm-v2/billing-account.helper.js') +const BillingAccountHelper = require('../../../support/helpers/billing-account.helper.js') const DatabaseHelper = require('../../../support/helpers/database.helper.js') // Thing under test @@ -33,20 +33,20 @@ describe('Fetch Billing Accounts service', () => { expectedResult = [ { - invoiceAccountId: billingAccounts[0].invoiceAccountId, - invoiceAccountNumber: billingAccounts[0].invoiceAccountNumber + id: billingAccounts[0].id, + accountNumber: billingAccounts[0].accountNumber }, { - invoiceAccountId: billingAccounts[1].invoiceAccountId, - invoiceAccountNumber: billingAccounts[1].invoiceAccountNumber + id: billingAccounts[1].id, + accountNumber: billingAccounts[1].accountNumber } ] }) it('fetches the billing accounts that the charge versions link to', async () => { const result = await FetchBillingAccountsService.go([ - { invoiceAccountId: billingAccounts[0].invoiceAccountId }, - { invoiceAccountId: billingAccounts[1].invoiceAccountId } + { invoiceAccountId: billingAccounts[0].id }, + { invoiceAccountId: billingAccounts[1].id } ]) expect(result).to.have.length(2).and.contain(expectedResult) diff --git a/test/services/bill-runs/supplementary/fetch-previous-transactions.service.test.js b/test/services/bill-runs/supplementary/fetch-previous-transactions.service.test.js index 60be4017cb..a22d48771a 100644 --- a/test/services/bill-runs/supplementary/fetch-previous-transactions.service.test.js +++ b/test/services/bill-runs/supplementary/fetch-previous-transactions.service.test.js @@ -9,7 +9,7 @@ const { expect } = Code // Test helpers const BillHelper = require('../../../support/helpers/water/bill.helper.js') -const BillingAccountHelper = require('../../../support/helpers/crm-v2/billing-account.helper.js') +const BillingAccountHelper = require('../../../support/helpers/billing-account.helper.js') const BillLicenceHelper = require('../../../support/helpers/water/bill-licence.helper.js') const BillRunHelper = require('../../../support/helpers/water/bill-run.helper.js') const DatabaseHelper = require('../../../support/helpers/database.helper.js') @@ -23,7 +23,7 @@ describe('Fetch Previous Transactions service', () => { const chargeCategoryCode = '4.3.1' const financialYearEnding = 2023 const invoiceAccountId = '4fe996c9-7641-4edc-9f42-0700dcde37b5' - const invoiceAccountNumber = BillingAccountHelper.generateInvoiceAccountNumber() + const invoiceAccountNumber = BillingAccountHelper.generateAccountNumber() const licenceId = '4492f1e2-f58c-4d4f-88a1-d4f9eb26fcba' const licenceRef = LicenceHelper.generateLicenceRef() diff --git a/test/services/bill-runs/supplementary/generate-bill.service.test.js b/test/services/bill-runs/supplementary/generate-bill.service.test.js index 6565c0bab4..1bc2677d1c 100644 --- a/test/services/bill-runs/supplementary/generate-bill.service.test.js +++ b/test/services/bill-runs/supplementary/generate-bill.service.test.js @@ -8,7 +8,7 @@ const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const BillingAccountHelper = require('../../../support/helpers/crm-v2/billing-account.helper.js') +const BillingAccountHelper = require('../../../support/helpers/billing-account.helper.js') const DatabaseHelper = require('../../../support/helpers/database.helper.js') // Thing under test @@ -27,9 +27,9 @@ describe('Generate Bill service', () => { billingAccount = await BillingAccountHelper.add() expectedResult = { - invoiceAccountId: billingAccount.invoiceAccountId, + invoiceAccountId: billingAccount.id, address: {}, - invoiceAccountNumber: billingAccount.invoiceAccountNumber, + invoiceAccountNumber: billingAccount.accountNumber, billingBatchId: billRunId, financialYearEnding, isCredit: false diff --git a/test/services/bill-runs/supplementary/pre-generate-billing-data.service.test.js b/test/services/bill-runs/supplementary/pre-generate-billing-data.service.test.js index e09386ea8d..b045b1ba1c 100644 --- a/test/services/bill-runs/supplementary/pre-generate-billing-data.service.test.js +++ b/test/services/bill-runs/supplementary/pre-generate-billing-data.service.test.js @@ -23,12 +23,12 @@ describe('Pre-generate billing data service', () => { const billingAccounts = [ { - invoiceAccountId: '235bae72-01f7-4a21-b8a3-d2b5fb2eff91', - invoiceAccountNumber: 'T12345678A' + id: '235bae72-01f7-4a21-b8a3-d2b5fb2eff91', + accountNumber: 'T12345678A' }, { - invoiceAccountId: '1d407b9c-457a-487d-bfe1-a54b72ef0bb5', - invoiceAccountNumber: 'T87654321A' + id: '1d407b9c-457a-487d-bfe1-a54b72ef0bb5', + accountNumber: 'T87654321A' } ] @@ -46,10 +46,10 @@ describe('Pre-generate billing data service', () => { describe('when the service is called', () => { beforeEach(async () => { chargeVersions = [ - { invoiceAccountId: billingAccounts[0].invoiceAccountId, licence: licences[0] }, - { invoiceAccountId: billingAccounts[1].invoiceAccountId, licence: licences[0] }, - { invoiceAccountId: billingAccounts[1].invoiceAccountId, licence: licences[1] }, - { invoiceAccountId: billingAccounts[1].invoiceAccountId, licence: licences[1] } + { invoiceAccountId: billingAccounts[0].id, licence: licences[0] }, + { invoiceAccountId: billingAccounts[1].id, licence: licences[0] }, + { invoiceAccountId: billingAccounts[1].id, licence: licences[1] }, + { invoiceAccountId: billingAccounts[1].id, licence: licences[1] } ] Sinon.stub(FetchBillingAccountsService, 'go').resolves(billingAccounts) @@ -63,7 +63,7 @@ describe('Pre-generate billing data service', () => { expect(keys).to.have.length(2) }) - it('is keyed with the invoice account id', async () => { + it('is keyed with the billing account id', async () => { const { bills: result } = await PreGenerateBillingDataService.go(chargeVersions, billRunId, billingPeriod) const entries = Object.entries(result) @@ -79,10 +79,10 @@ describe('Pre-generate billing data service', () => { const entries = Object.entries(result) entries.forEach(([key, value]) => { - const matchingInvoiceAccount = billingAccounts.find((billingAccount) => { - return key === billingAccount.invoiceAccountId + const matchingBillingAccount = billingAccounts.find((billingAccount) => { + return key === billingAccount.id }) - expect(value.invoiceAccountNumber).to.equal(matchingInvoiceAccount.invoiceAccountNumber) + expect(value.invoiceAccountNumber).to.equal(matchingBillingAccount.accountNumber) }) }) }) diff --git a/test/services/bill-runs/supplementary/process-billing-period.service.test.js b/test/services/bill-runs/supplementary/process-billing-period.service.test.js index e487821ae9..e60a3aadfd 100644 --- a/test/services/bill-runs/supplementary/process-billing-period.service.test.js +++ b/test/services/bill-runs/supplementary/process-billing-period.service.test.js @@ -9,7 +9,7 @@ const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const BillingAccountHelper = require('../../../support/helpers/crm-v2/billing-account.helper.js') +const BillingAccountHelper = require('../../../support/helpers/billing-account.helper.js') const BillRunError = require('../../../../app/errors/bill-run.error.js') const BillRunHelper = require('../../../support/helpers/water/bill-run.helper.js') const BillRunModel = require('../../../../app/models/water/bill-run.model.js') @@ -79,7 +79,7 @@ describe('Process billing period service', () => { const { chargeVersionId } = await ChargeVersionHelper.add( { changeReasonId: changeReason.changeReasonId, - invoiceAccountId: billingAccount.invoiceAccountId, + invoiceAccountId: billingAccount.id, startDate: new Date(2022, 7, 1, 9), licenceId: licence.licenceId } @@ -154,7 +154,7 @@ describe('Process billing period service', () => { const { chargeVersionId } = await ChargeVersionHelper.add( { changeReasonId: changeReason.changeReasonId, - invoiceAccountId: billingAccount.invoiceAccountId, + invoiceAccountId: billingAccount.id, startDate: new Date(2022, 7, 1, 9), licenceId: licence.licenceId } @@ -189,7 +189,7 @@ describe('Process billing period service', () => { const { chargeVersionId } = await ChargeVersionHelper.add( { changeReasonId: changeReason.changeReasonId, - invoiceAccountId: billingAccount.invoiceAccountId, + invoiceAccountId: billingAccount.id, startDate: new Date(2022, 7, 1, 9), licenceId: licence.licenceId, status: 'superseded' @@ -225,7 +225,7 @@ describe('Process billing period service', () => { beforeEach(async () => { const { chargeVersionId } = await ChargeVersionHelper.add({ changeReasonId: changeReason.changeReasonId, - invoiceAccountId: billingAccount.invoiceAccountId, + invoiceAccountId: billingAccount.id, licenceId: licence.licenceId }) const { chargeElementId } = await ChargeReferenceHelper.add( diff --git a/test/services/billing-accounts/change-address.service.test.js b/test/services/billing-accounts/change-address.service.test.js index 2493bfdba3..a6656494bc 100644 --- a/test/services/billing-accounts/change-address.service.test.js +++ b/test/services/billing-accounts/change-address.service.test.js @@ -9,14 +9,14 @@ const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const AddressHelper = require('../../support/helpers/crm-v2/address.helper.js') -const AddressModel = require('../../../app/models/crm-v2/address.model.js') -const BillingAccountAddressHelper = require('../../support/helpers/crm-v2/billing-account-address.helper.js') -const BillingAccountAddressModel = require('../../../app/models/crm-v2/billing-account-address.model.js') -const BillingAccountHelper = require('../../support/helpers/crm-v2/billing-account.helper.js') -const CompanyHelper = require('../../support/helpers/crm-v2/company.helper.js') -const CompanyModel = require('../../../app/models/crm-v2/company.model.js') -const ContactModel = require('../../../app/models/crm-v2/contact.model.js') +const AddressHelper = require('../../support/helpers/address.helper.js') +const AddressModel = require('../../../app/models/address.model.js') +const BillingAccountAddressHelper = require('../../support/helpers/billing-account-address.helper.js') +const BillingAccountAddressModel = require('../../../app/models/billing-account-address.model.js') +const BillingAccountHelper = require('../../support/helpers/billing-account.helper.js') +const CompanyHelper = require('../../support/helpers/company.helper.js') +const CompanyModel = require('../../../app/models/company.model.js') +const ContactModel = require('../../../app/models/contact.model.js') const DatabaseHelper = require('../../support/helpers/database.helper.js') // Things we need to stub @@ -48,7 +48,7 @@ describe('Change address service', () => { const contactIndividual = { type: 'person', firstName: 'Margherita', lastName: 'Villar' } - const invoiceAccountId = '2e72bd15-1412-4329-bf92-5217f83d19c0' + const billingAccountId = '2e72bd15-1412-4329-bf92-5217f83d19c0' let address let agentCompany @@ -58,7 +58,7 @@ describe('Change address service', () => { beforeEach(async () => { await DatabaseHelper.clean() - billingAccount = await BillingAccountHelper.add({ invoiceAccountId }) + billingAccount = await BillingAccountHelper.add({ id: billingAccountId }) }) afterEach(() => { @@ -76,11 +76,11 @@ describe('Change address service', () => { }) it('creates the billing account address and address records and handles the null agent and contact', async () => { - const result = await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + const result = await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) const newAddress = await AddressModel.query().first() - expect(result.invoiceAccountAddress.addressId).to.equal(newAddress.addressId) + expect(result.billingAccountAddress.addressId).to.equal(newAddress.id) expect(result.address.address1).to.equal('62 High St') expect(result.agentCompany).to.be.null() expect(result.contact).to.be.null() @@ -102,26 +102,26 @@ describe('Change address service', () => { }) it('overwrites the existing address with the latest OS Places details', async () => { - await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) const reFetchedExistingAddress = await existingAddress.$query() - expect(reFetchedExistingAddress.addressId).to.equal(existingAddress.addressId) + expect(reFetchedExistingAddress.id).to.equal(existingAddress.id) expect(reFetchedExistingAddress.createdAt).to.equal(existingAddress.createdAt) expect(reFetchedExistingAddress.address1).to.equal(address.addressLine1) expect(reFetchedExistingAddress.updatedAt).not.to.equal(existingAddress.updatedAt) }) it('links the billing account address record to the existing address', async () => { - const result = await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + const result = await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) - expect(result.invoiceAccountAddress.addressId).to.equal(existingAddress.addressId) + expect(result.billingAccountAddress.addressId).to.equal(existingAddress.id) }) }) describe('and a matching address does not exist', () => { it('creates a new address record', async () => { - await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) const result = await AddressModel.query() @@ -131,11 +131,11 @@ describe('Change address service', () => { }) it('links the billing account address record to the new address', async () => { - const result = await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + const result = await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) const newAddress = await AddressModel.query().first() - expect(result.invoiceAccountAddress.addressId).to.equal(newAddress.addressId) + expect(result.billingAccountAddress.addressId).to.equal(newAddress.id) }) }) }) @@ -146,7 +146,7 @@ describe('Change address service', () => { }) it('creates a new address record', async () => { - await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) const result = await AddressModel.query() @@ -155,11 +155,11 @@ describe('Change address service', () => { }) it('links the billing account address record to the new address', async () => { - const result = await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + const result = await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) const newAddress = await AddressModel.query().first() - expect(result.invoiceAccountAddress.addressId).to.equal(newAddress.addressId) + expect(result.billingAccountAddress.addressId).to.equal(newAddress.id) }) }) @@ -169,24 +169,24 @@ describe('Change address service', () => { beforeEach(async () => { existingAddress = await AddressHelper.add() - address = { addressId: existingAddress.addressId } + address = { addressId: existingAddress.id } }) it('makes no changes to the existing address record', async () => { - await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) const reFetchedExistingAddress = await existingAddress.$query() - expect(reFetchedExistingAddress.addressId).to.equal(existingAddress.addressId) + expect(reFetchedExistingAddress.id).to.equal(existingAddress.id) expect(reFetchedExistingAddress.address1).to.equal(existingAddress.address1) expect(reFetchedExistingAddress.createdAt).to.equal(existingAddress.createdAt) expect(reFetchedExistingAddress.updatedAt).to.equal(existingAddress.updatedAt) }) it('links the billing account address record to the existing address', async () => { - const result = await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + const result = await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) - expect(result.invoiceAccountAddress.addressId).to.equal(existingAddress.addressId) + expect(result.billingAccountAddress.addressId).to.equal(existingAddress.id) }) }) }) @@ -206,26 +206,26 @@ describe('Change address service', () => { }) it('overwrites the existing company with the latest Companies House details', async () => { - await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) const reFetchedExistingCompany = await existingCompany.$query() - expect(reFetchedExistingCompany.companyId).to.equal(existingCompany.companyId) + expect(reFetchedExistingCompany.id).to.equal(existingCompany.id) expect(reFetchedExistingCompany.createdAt).to.equal(existingCompany.createdAt) expect(reFetchedExistingCompany.name).to.equal(agentCompany.name) expect(reFetchedExistingCompany.updatedAt).not.to.equal(existingCompany.updatedAt) }) it('links the billing account address record to the existing company', async () => { - const result = await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + const result = await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) - expect(result.invoiceAccountAddress.agentCompanyId).to.equal(existingCompany.companyId) + expect(result.billingAccountAddress.companyId).to.equal(existingCompany.id) }) }) describe('has a company number that does not match an existing record', () => { it('creates a new company record', async () => { - await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) const result = await CompanyModel.query() @@ -235,11 +235,11 @@ describe('Change address service', () => { }) it('links the billing account address record to the new company', async () => { - const result = await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + const result = await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) const newCompany = await CompanyModel.query().first() - expect(result.invoiceAccountAddress.agentCompanyId).to.equal(newCompany.companyId) + expect(result.billingAccountAddress.companyId).to.equal(newCompany.id) }) }) @@ -249,24 +249,24 @@ describe('Change address service', () => { beforeEach(async () => { existingCompany = await CompanyHelper.add() - agentCompany = { companyId: existingCompany.companyId } + agentCompany = { companyId: existingCompany.id } }) it('makes no changes to the existing company record', async () => { - await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) const reFetchedExistingCompany = await existingCompany.$query() - expect(reFetchedExistingCompany.companyId).to.equal(existingCompany.companyId) + expect(reFetchedExistingCompany.id).to.equal(existingCompany.id) expect(reFetchedExistingCompany.name).to.equal(existingCompany.name) expect(reFetchedExistingCompany.createdAt).to.equal(existingCompany.createdAt) expect(reFetchedExistingCompany.updatedAt).to.equal(existingCompany.updatedAt) }) it('links the billing account address record to the existing company', async () => { - const result = await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + const result = await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) - expect(result.invoiceAccountAddress.agentCompanyId).to.equal(existingCompany.companyId) + expect(result.billingAccountAddress.companyId).to.equal(existingCompany.id) }) }) }) @@ -282,7 +282,7 @@ describe('Change address service', () => { }) it('creates a new contact record', async () => { - await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) const result = await ContactModel.query() @@ -291,11 +291,11 @@ describe('Change address service', () => { }) it('links the billing account address record to the new contact', async () => { - const result = await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + const result = await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) const newContact = await ContactModel.query().first() - expect(result.invoiceAccountAddress.contactId).to.equal(newContact.contactId) + expect(result.billingAccountAddress.contactId).to.equal(newContact.id) }) }) @@ -305,7 +305,7 @@ describe('Change address service', () => { }) it('creates a new contact record', async () => { - await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) const result = await ContactModel.query() @@ -315,18 +315,18 @@ describe('Change address service', () => { }) it('links the billing account address record to the new contact', async () => { - const result = await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + const result = await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) const newContact = await ContactModel.query().first() - expect(result.invoiceAccountAddress.contactId).to.equal(newContact.contactId) + expect(result.billingAccountAddress.contactId).to.equal(newContact.id) }) }) }) describe("and the 'current' billing account address", () => { let clock - let existingInvoiceAccountAddress + let existingBillingAccountAddress beforeEach(() => { address = { ...addressFromManual } @@ -345,8 +345,8 @@ describe('Change address service', () => { const startDate = new Date(2023, 8, 4) const timestamp = new Date(2023, 8, 4, 9, 22, 57, 13) - existingInvoiceAccountAddress = await BillingAccountAddressHelper.add({ - invoiceAccountId, + existingBillingAccountAddress = await BillingAccountAddressHelper.add({ + billingAccountId, startDate, createdAt: timestamp, updatedAt: timestamp @@ -354,17 +354,17 @@ describe('Change address service', () => { }) it('overwrites the existing record and ensures the end date is null', async () => { - const result = await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + const result = await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) - const newInvoiceAccountAddress = await BillingAccountAddressModel.query() - .findById(result.invoiceAccountAddress.invoiceAccountAddressId) + const newBillingAccountAddress = await BillingAccountAddressModel.query() + .findById(result.billingAccountAddress.id) - expect(newInvoiceAccountAddress.invoiceAccountAddressId) - .to.equal(existingInvoiceAccountAddress.invoiceAccountAddressId) - expect(newInvoiceAccountAddress.addressId).not.to.equal(existingInvoiceAccountAddress.addressId) - expect(newInvoiceAccountAddress.endDate).to.be.null() - expect(newInvoiceAccountAddress.createdAt).to.equal(existingInvoiceAccountAddress.createdAt) - expect(newInvoiceAccountAddress.updatedAt).to.be.above(existingInvoiceAccountAddress.updatedAt) + expect(newBillingAccountAddress.id) + .to.equal(existingBillingAccountAddress.id) + expect(newBillingAccountAddress.addressId).not.to.equal(existingBillingAccountAddress.addressId) + expect(newBillingAccountAddress.endDate).to.be.null() + expect(newBillingAccountAddress.createdAt).to.equal(existingBillingAccountAddress.createdAt) + expect(newBillingAccountAddress.updatedAt).to.be.above(existingBillingAccountAddress.updatedAt) }) }) @@ -373,8 +373,8 @@ describe('Change address service', () => { const startDate = new Date(2023, 7, 3) const timestamp = new Date(2023, 7, 3, 14, 46, 3, 18) - existingInvoiceAccountAddress = await BillingAccountAddressHelper.add({ - invoiceAccountId, + existingBillingAccountAddress = await BillingAccountAddressHelper.add({ + billingAccountId, startDate, createdAt: timestamp, updatedAt: timestamp @@ -382,27 +382,27 @@ describe('Change address service', () => { }) it('creates a new billing account record with a null end date', async () => { - const result = await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + const result = await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) - const newInvoiceAccountAddress = await BillingAccountAddressModel.query() - .findById(result.invoiceAccountAddress.invoiceAccountAddressId) + const newBillingAccountAddress = await BillingAccountAddressModel.query() + .findById(result.billingAccountAddress.id) - expect(newInvoiceAccountAddress.invoiceAccountAddressId) - .not.to.equal(existingInvoiceAccountAddress.invoiceAccountAddressId) - expect(newInvoiceAccountAddress.addressId).not.to.equal(existingInvoiceAccountAddress.addressId) - expect(newInvoiceAccountAddress.endDate).to.be.null() - expect(newInvoiceAccountAddress.createdAt).to.be.above(existingInvoiceAccountAddress.createdAt) - expect(newInvoiceAccountAddress.updatedAt).to.be.above(existingInvoiceAccountAddress.updatedAt) + expect(newBillingAccountAddress.id) + .not.to.equal(existingBillingAccountAddress.id) + expect(newBillingAccountAddress.addressId).not.to.equal(existingBillingAccountAddress.addressId) + expect(newBillingAccountAddress.endDate).to.be.null() + expect(newBillingAccountAddress.createdAt).to.be.above(existingBillingAccountAddress.createdAt) + expect(newBillingAccountAddress.updatedAt).to.be.above(existingBillingAccountAddress.updatedAt) }) it("updates the end date of the existing record to yesterday's date", async () => { - await ChangeAddressService.go(invoiceAccountId, address, agentCompany, contact) + await ChangeAddressService.go(billingAccountId, address, agentCompany, contact) - const reFetchedExistingInvoiceAccountAddress = await existingInvoiceAccountAddress.$query() + const reFetchedExistingInvoiceAccountAddress = await existingBillingAccountAddress.$query() const yesterday = new Date(2023, 8, 3) expect(reFetchedExistingInvoiceAccountAddress.endDate).to.equal(yesterday) - expect(reFetchedExistingInvoiceAccountAddress.updatedAt).to.be.above(existingInvoiceAccountAddress.updatedAt) + expect(reFetchedExistingInvoiceAccountAddress.updatedAt).to.be.above(existingBillingAccountAddress.updatedAt) }) }) }) diff --git a/test/services/bills/fetch-billing-account.service.test.js b/test/services/bills/fetch-billing-account.service.test.js index a96782a9ba..fa061fbe0f 100644 --- a/test/services/bills/fetch-billing-account.service.test.js +++ b/test/services/bills/fetch-billing-account.service.test.js @@ -8,16 +8,16 @@ const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const AddressHelper = require('../../support/helpers/crm-v2/address.helper.js') -const AddressModel = require('../../../app/models/crm-v2/address.model.js') -const BillingAccountAddressHelper = require('../../support/helpers/crm-v2/billing-account-address.helper.js') -const BillingAccountAddressModel = require('../../../app/models/crm-v2/billing-account-address.model.js') -const BillingAccountHelper = require('../../support/helpers/crm-v2/billing-account.helper.js') -const BillingAccountModel = require('../../../app/models/crm-v2/billing-account.model.js') -const CompanyHelper = require('../../support/helpers/crm-v2/company.helper.js') -const CompanyModel = require('../../../app/models/crm-v2/company.model.js') -const ContactHelper = require('../../support/helpers/crm-v2/contact.helper.js') -const ContactModel = require('../../../app/models/crm-v2/contact.model.js') +const AddressHelper = require('../../support/helpers/address.helper.js') +const AddressModel = require('../../../app/models/address.model.js') +const BillingAccountAddressHelper = require('../../support/helpers/billing-account-address.helper.js') +const BillingAccountAddressModel = require('../../../app/models/billing-account-address.model.js') +const BillingAccountHelper = require('../../support/helpers/billing-account.helper.js') +const BillingAccountModel = require('../../../app/models/billing-account.model.js') +const CompanyHelper = require('../../support/helpers/company.helper.js') +const CompanyModel = require('../../../app/models/company.model.js') +const ContactHelper = require('../../support/helpers/contact.helper.js') +const ContactModel = require('../../../app/models/contact.model.js') const DatabaseHelper = require('../../support/helpers/database.helper.js') // Thing under test @@ -32,7 +32,7 @@ describe('Fetch Billing Account service', () => { linkedCompany = await CompanyHelper.add() - testBillingAccount = await BillingAccountHelper.add({ companyId: linkedCompany.companyId }) + testBillingAccount = await BillingAccountHelper.add({ companyId: linkedCompany.id }) }) describe('when a billing account with a matching ID exists', () => { @@ -40,21 +40,21 @@ describe('Fetch Billing Account service', () => { let linkedAddress beforeEach(async () => { - const { invoiceAccountId } = testBillingAccount + const { id: billingAccountId } = testBillingAccount linkedAddress = await AddressHelper.add() - const { addressId } = linkedAddress + const { id: addressId } = linkedAddress await Promise.all([ - BillingAccountAddressHelper.add({ invoiceAccountId, addressId, endDate: new Date('2023-09-18') }), - BillingAccountAddressHelper.add({ invoiceAccountId, addressId, startDate: new Date('2023-09-19') }) + BillingAccountAddressHelper.add({ billingAccountId, addressId, endDate: new Date('2023-09-18') }), + BillingAccountAddressHelper.add({ billingAccountId, addressId, startDate: new Date('2023-09-19') }) ]) }) it('only returns the current one', async () => { - const result = await FetchBillingAccountService.go(testBillingAccount.invoiceAccountId) + const result = await FetchBillingAccountService.go(testBillingAccount.id) - expect(result.invoiceAccountId).to.equal(testBillingAccount.invoiceAccountId) + expect(result.id).to.equal(testBillingAccount.id) expect(result).to.be.an.instanceOf(BillingAccountModel) expect(result.billingAccountAddresses).to.have.length(1) @@ -64,14 +64,14 @@ describe('Fetch Billing Account service', () => { // All account address records are linked to an address it('returns details for the linked address', async () => { - const result = await FetchBillingAccountService.go(testBillingAccount.invoiceAccountId) + const result = await FetchBillingAccountService.go(testBillingAccount.id) - expect(result.invoiceAccountId).to.equal(testBillingAccount.invoiceAccountId) + expect(result.id).to.equal(testBillingAccount.id) expect(result).to.be.an.instanceOf(BillingAccountModel) const returnedAddress = result.billingAccountAddresses[0].address - expect(returnedAddress.addressId).to.equal(linkedAddress.addressId) + expect(returnedAddress.id).to.equal(linkedAddress.id) expect(returnedAddress).to.be.an.instanceof(AddressModel) }) }) @@ -81,21 +81,21 @@ describe('Fetch Billing Account service', () => { let linkedAgentCompany beforeEach(async () => { - const { invoiceAccountId } = testBillingAccount + const { id: billingAccountId } = testBillingAccount linkedAgentCompany = await CompanyHelper.add() - await BillingAccountAddressHelper.add({ invoiceAccountId, agentCompanyId: linkedAgentCompany.companyId }) + await BillingAccountAddressHelper.add({ billingAccountId, companyId: linkedAgentCompany.id }) }) it('returns details for the linked agent company', async () => { - const result = await FetchBillingAccountService.go(testBillingAccount.invoiceAccountId) + const result = await FetchBillingAccountService.go(testBillingAccount.id) - expect(result.invoiceAccountId).to.equal(testBillingAccount.invoiceAccountId) + expect(result.id).to.equal(testBillingAccount.id) expect(result).to.be.an.instanceOf(BillingAccountModel) - const returnedAgentCompany = result.billingAccountAddresses[0].agentCompany + const returnedAgentCompany = result.billingAccountAddresses[0].company - expect(returnedAgentCompany.companyId).to.equal(linkedAgentCompany.companyId) + expect(returnedAgentCompany.id).to.equal(linkedAgentCompany.id) expect(returnedAgentCompany).to.be.an.instanceof(CompanyModel) }) }) @@ -105,35 +105,35 @@ describe('Fetch Billing Account service', () => { let linkedContact beforeEach(async () => { - const { invoiceAccountId } = testBillingAccount + const { id: billingAccountId } = testBillingAccount linkedContact = await ContactHelper.add() - await BillingAccountAddressHelper.add({ invoiceAccountId, contactId: linkedContact.contactId }) + await BillingAccountAddressHelper.add({ billingAccountId, contactId: linkedContact.id }) }) it('returns details for the linked contact', async () => { - const result = await FetchBillingAccountService.go(testBillingAccount.invoiceAccountId) + const result = await FetchBillingAccountService.go(testBillingAccount.id) - expect(result.invoiceAccountId).to.equal(testBillingAccount.invoiceAccountId) + expect(result.id).to.equal(testBillingAccount.id) expect(result).to.be.an.instanceOf(BillingAccountModel) const returnedContact = result.billingAccountAddresses[0].contact - expect(returnedContact.contactId).to.equal(linkedContact.contactId) + expect(returnedContact.id).to.equal(linkedContact.id) expect(returnedContact).to.be.an.instanceof(ContactModel) }) }) // All billing account records have a linked company it('returns details for the linked company', async () => { - const result = await FetchBillingAccountService.go(testBillingAccount.invoiceAccountId) + const result = await FetchBillingAccountService.go(testBillingAccount.id) - expect(result.invoiceAccountId).to.equal(testBillingAccount.invoiceAccountId) + expect(result.id).to.equal(testBillingAccount.id) expect(result).to.be.an.instanceOf(BillingAccountModel) const returnedCompany = result.company - expect(returnedCompany.contactId).to.equal(returnedCompany.companyId) + expect(returnedCompany.id).to.equal(returnedCompany.id) expect(returnedCompany).to.be.an.instanceof(CompanyModel) }) }) diff --git a/test/support/helpers/bill.helper.js b/test/support/helpers/bill.helper.js index 58048e14b8..a0dd52fbb8 100644 --- a/test/support/helpers/bill.helper.js +++ b/test/support/helpers/bill.helper.js @@ -6,7 +6,7 @@ const BillModel = require('../../../app/models/bill.model.js') const { generateUUID } = require('../../../app/lib/general.lib.js') -const { generateInvoiceAccountNumber } = require('./crm-v2/billing-account.helper.js') +const { generateAccountNumber } = require('./billing-account.helper.js') /** * Add a new bill @@ -43,7 +43,7 @@ function defaults (data = {}) { const defaults = { billingAccountId: generateUUID(), address: {}, - accountNumber: generateInvoiceAccountNumber(), + accountNumber: generateAccountNumber(), billRunId: generateUUID(), financialYearEnding: 2023 } diff --git a/test/support/helpers/crm-v2/address.helper.js b/test/support/helpers/crm-v2/address.helper.js deleted file mode 100644 index 5e9c932dcb..0000000000 --- a/test/support/helpers/crm-v2/address.helper.js +++ /dev/null @@ -1,70 +0,0 @@ -'use strict' - -/** - * @module AddressHelper - */ - -const AddressModel = require('../../../../app/models/crm-v2/address.model.js') -const { randomInteger } = require('../general.helper.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` - [randomly generated - 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 - * - * 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: generateUprn() - } - - return { - ...defaults, - ...data - } -} - -function generateUprn () { - return randomInteger(100, 999999) -} - -module.exports = { - add, - defaults, - generateUprn -} diff --git a/test/support/helpers/crm-v2/billing-account-address.helper.js b/test/support/helpers/crm-v2/billing-account-address.helper.js deleted file mode 100644 index 0c9d3770b3..0000000000 --- a/test/support/helpers/crm-v2/billing-account-address.helper.js +++ /dev/null @@ -1,55 +0,0 @@ -'use strict' - -/** - * @module BillingAccountAddressHelper - */ - -const BillingAccountAddressModel = require('../../../../app/models/crm-v2/billing-account-address.model.js') -const { generateUUID } = require('../../../../app/lib/general.lib.js') - -/** - * Add a new billing account address - * - * If no `data` is provided, default values will be used. These are - * - * - `invoiceAccountId` - [random UUID] - * - `addressId` - [random UUID] - * - `startDate` - 2023-08-18 - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:BillingAccountAddressModel} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return BillingAccountAddressModel.query() - .insert({ ...insertData }) - .returning('*') -} - -/** - * Returns the defaults used - * - * 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 = { - invoiceAccountId: generateUUID(), - addressId: generateUUID(), - startDate: new Date('2023-08-18') - } - - return { - ...defaults, - ...data - } -} - -module.exports = { - add, - defaults -} diff --git a/test/support/helpers/crm-v2/billing-account.helper.js b/test/support/helpers/crm-v2/billing-account.helper.js deleted file mode 100644 index 652947f0c6..0000000000 --- a/test/support/helpers/crm-v2/billing-account.helper.js +++ /dev/null @@ -1,58 +0,0 @@ -'use strict' - -/** - * @module BillingAccountHelper - */ - -const { randomInteger } = require('../general.helper.js') -const BillingAccountModel = require('../../../../app/models/crm-v2/billing-account.model.js') - -/** - * Add a new billing account - * - * If no `data` is provided, default values will be used. These are - * - * - `invoiceAccountNumber` - [randomly generated - T12345678A] - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:BillingAccountModel} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return BillingAccountModel.query() - .insert({ ...insertData }) - .returning('*') -} - -/** - * Returns the defaults used - * - * 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 = { - invoiceAccountNumber: generateInvoiceAccountNumber() - } - - return { - ...defaults, - ...data - } -} - -function generateInvoiceAccountNumber () { - const numbering = randomInteger(10000000, 99999999) - - return `T${numbering}A` -} - -module.exports = { - add, - defaults, - generateInvoiceAccountNumber -} diff --git a/test/support/helpers/crm-v2/company.helper.js b/test/support/helpers/crm-v2/company.helper.js deleted file mode 100644 index d875cbd30c..0000000000 --- a/test/support/helpers/crm-v2/company.helper.js +++ /dev/null @@ -1,62 +0,0 @@ -'use strict' - -/** - * @module CompanyHelper - */ - -const CompanyModel = require('../../../../app/models/crm-v2/company.model.js') -const { randomInteger } = require('../general.helper.js') - -/** - * Add a new company - * - * If no `data` is provided, default values will be used. These are - * - * - `name` - Example Trading Ltd - * - `type` - organisation - * - `companyNumber` - [randomly generated - 24296934] - * - `organisationType` - limitedCompany - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:CompanyModel} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return CompanyModel.query() - .insert({ ...insertData }) - .returning('*') -} - -/** - * Returns the defaults used - * - * 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 = { - name: 'Example Trading Ltd', - type: 'organisation', - companyNumber: generateCompanyNumber(), - organisationType: 'limitedCompany' - } - - return { - ...defaults, - ...data - } -} - -function generateCompanyNumber () { - return randomInteger(1000000, 9999999).toString() -} - -module.exports = { - add, - defaults, - generateCompanyNumber -} diff --git a/test/support/helpers/crm-v2/contact.helper.js b/test/support/helpers/crm-v2/contact.helper.js deleted file mode 100644 index fb39f978b3..0000000000 --- a/test/support/helpers/crm-v2/contact.helper.js +++ /dev/null @@ -1,58 +0,0 @@ -'use strict' - -/** - * @module ContactHelper - */ - -const ContactModel = require('../../../../app/models/crm-v2/contact.model.js') - -/** - * Add a new contact - * - * If no `data` is provided, default values will be used. These are - * - * - `firstName` - Amara - * - `surname` - Gupta - * - `dataSource` - wrls - * - `contactType` - person - * - `email` - amara.gupta@example.com - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:ContactModel} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return ContactModel.query() - .insert({ ...insertData }) - .returning('*') -} - -/** - * Returns the defaults used - * - * 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 = { - firstName: 'Amara', - lastName: 'Gupta', - dataSource: 'wrls', - contactType: 'person', - email: 'amara.gupta@example.com' - } - - return { - ...defaults, - ...data - } -} - -module.exports = { - add, - defaults -} diff --git a/test/support/helpers/water/bill.helper.js b/test/support/helpers/water/bill.helper.js index fca791d843..e8cbba7e0b 100644 --- a/test/support/helpers/water/bill.helper.js +++ b/test/support/helpers/water/bill.helper.js @@ -6,7 +6,7 @@ const BillModel = require('../../../../app/models/water/bill.model.js') const { generateUUID } = require('../../../../app/lib/general.lib.js') -const { generateInvoiceAccountNumber } = require('../crm-v2/billing-account.helper.js') +const { generateAccountNumber } = require('../billing-account.helper.js') /** * Add a new bill @@ -43,7 +43,7 @@ function defaults (data = {}) { const defaults = { invoiceAccountId: generateUUID(), address: {}, - invoiceAccountNumber: generateInvoiceAccountNumber(), + invoiceAccountNumber: generateAccountNumber(), billingBatchId: generateUUID(), financialYearEnding: 2023 }