diff --git a/app/models/legacy-base.model.js b/app/models/legacy-base.model.js deleted file mode 100644 index e289a91575..0000000000 --- a/app/models/legacy-base.model.js +++ /dev/null @@ -1,224 +0,0 @@ -'use strict' - -/** - * Base class for all models based on tables in the legacy schemas - * @module LegacyBaseModel - */ - -const path = require('path') - -const BaseModel = require('./base.model.js') -const LegacyQueryBuilder = require('./legacy.query-builder.js') - -class LegacyBaseModel extends BaseModel { - /** - * Array of paths to search for models used in relationships - * - * > See `modelPaths()` in `BaseModel` for more details on this getter - * - * We override the one provided in `BaseModel` to include our legacy folders. - * - * @returns {Array} array of paths to search for related models - */ - static get modelPaths () { - const currentPath = __dirname - return [ - path.join(currentPath, 'water'), - path.join(currentPath, 'crm-v2'), - path.join(currentPath, 'idm') - ] - } - - /** - * Returns a custom `QueryBuilder` which supports declaring the schema to use when connecting to the DB - * - * See `schema()` for further details. - * - * @returns {module:LegacyQueryBuilder} a custom Objection `QueryBuilder` - */ - static get QueryBuilder () { - return LegacyQueryBuilder - } - - /** - * Name of the database schema a model belongs to - * - * All the legacy repos talk to the same PostgreSQL DB server but split their data using - * {@link https://www.postgresql.org/docs/current/ddl-schemas.html schemas}. Objection.js however assumes you are - * using the **public** schema which is the default in PostgreSQL if you don't specify one. - * - * So, when Objection is working with our legacy models it needs to know what schemas they belong to. There is no - * out-of-the-box support for specifying this. But the maintainer behind Objection.js suggested this solution in - * {@link https://github.com/Vincit/objection.js/issues/85#issuecomment-185183032 an issue} (which we've tweaked - * slightly). - * - * You extend Objection's QueryBuilder (see `SchemaQueryBuilder`) and call Knex's `withSchema()` within it (Knex - * _does_ understand schemas). It expects the Model generating the query to have a custom getter `schema` which - * returns the schema name. You then override the getter `QueryBuilder` in your model and return your - * {@link https://vincit.github.io/objection.js/recipes/custom-query-builder.html custom QueryBuilder}. This is then - * used to build any queries, for example, `MyModel.query.find()` ensuring the schema name is declared and our data - * layer knows where to find the data. - * - * > In this base model model we throw an exception to ensure any child classes override this getter. - * - * @returns {string} the schema name, for example, 'water' - */ - static get schema () { - throw new Error('defaultSchema() not implemented in child class') - } - - /** - * Translations for non-standard column names to our standardised ones - * - * The primary example of non-standard column names in the legacy data is timestamps. For example, we have found the - * column which has the timestamp for when a record is created has been named `date_created`, 'created', 'modified', - * and 'created_at`. - * - * Within our code we want to just be using `createdAt` (which would be `created_at` in the DB), as this is what the - * field would have been called if Knex had been used to generate the migrations and build the tables. - * - * All models that extend `LegacyBaseModel` are expected to implement this getter. It should always return an array. - * If there are no translations an empty array is valid. - * - * @example - * ```javascript - * static get translations () { - * return [ - * { database: 'created', model: 'createdAt' }, - * { database: 'modified', model: 'updatedAt' } - * ] - * } - * ``` - * - */ - static get translations () { - throw new Error('translations() not implemented in child class') - } - - /** - * Called when a Model is converted to database format. - * - * This is an Objection.js instance method which we are overriding. We use it to convert our standardised property - * names into the legacy ones, for example `createdAt` to `dateCreated`. Timestamps is a primary example - * where the legacy tables do not use a consistent naming convention. Various tables use different names for the same - * thing. But we want few surprises and lots of consistency in our code. So, this allows us to translate the names of - * properties before Objection.js finalises the database version. - * - * Each Model that extends `LegacyBaseModel` is expected to implement `static get translations` which defines the - * database to model translations needed. Any fields listed in `translations` not found will be ignored. - * - * For more details see - * {@link http://vincit.github.io/objection.js/api/model/instance-methods.html#parsedatabasejson $parseDatabaseJson()} - * and the - * {@link https://vincit.github.io/objection.js/api/model/overview.html#model-data-lifecycle Model data lifecycle} - * - * ------------------------------------------------------------------------------------------------------------------- - * - * **WARNING!** This translation method _only_ works for instances. We do not yet have a solution that will also - * work with static queries. Running the following will cause an exception because `createdAt` (automatically - * converted to `created_at` in the final query) does not exist in the `water.events` table. - * - * ```javascript - * EventModel.query().where('createdAt', '<', new Date().toISOString()) - * ``` - * - * Overriding - * {@link https://vincit.github.io/objection.js/api/model/static-properties.html#static-columnnamemappers columnNameMappers} - * had the same result as what we're doing. We also looked into - * {@link https://vincit.github.io/objection.js/api/objection/#knexidentifiermapping knexIdentifierMapping}. This - * would do the mapping at a lower level, in the same way we've used - * {@link https://vincit.github.io/objection.js/api/objection/#knexsnakecasemappers knexSnakeCaseMappers} to - * make changes before Objection.js takes over. - * - * The first problem we hit was that you can only use one of these; you can't apply both to the config object based to - * knex (see `knexfile.application.js` and `db/db.js` for how we currently do it). This is because both methods - * return an object which defines `wrapIdentifier()` and `postProcessResponse()`. So, whichever is added last - * overwrites what the previous one set. - * - * You can resolve this by importing `objection/lib/utils/identifierMapping.js` directly and doing something like this - * - * ```javascript - * // https://gitter.im/Vincit/objection.js?at=5e25f065a259cb0f060754ee - * const og = require('objection/lib/utils/identifierMapping'); - * const SNAKE_CASE_OVERRIDES = { col12: 'col_1_2' }; - * - * function snakeCase(str, { upperCase = false, underscoreBeforeDigits = false } = {}) { - * // if this column has a manual override, return the snake-cased column name from the override - * if (str.length && str in SNAKE_CASE_OVERRIDES) { - * return SNAKE_CASE_OVERRIDES[str]; - * } - * - * // otherwise simply call the original snakeCase() function - * return og.snakeCase(str, { upperCase: upperCase, underscoreBeforeDigits: underscoreBeforeDigits}); - * } - * - * function knexSnakeCaseMappers(opt = {}) { - * return og.knexIdentifierMappers({ - * parse: str => og.camelCase(str, opt), - * format: str => snakeCase(str, opt), // call overridden snakeCase() function - * }); - * } - * ``` - * - * But that leads us to the second problem; at this level translations are global. So, any reference to `date_created` - * would get translated to `created_at`, irrespective of the table referenced. If we only had to worry about database - * to model it would be okay. The problem is to make queries work we need to be able to translate back from our names - * to the database version. That is impossible where `created_at` might need to become `date_created`, `created`, - * or `modified`. With no access to the table name for context we cannot make that decision. - * - * This is why we have accepted we can only support translations when dealing with instances of a model for now. - * - * @param {Object} json The JSON POJO in internal format - * @returns {Object} The JSON POJO in database format - */ - $formatDatabaseJson (json) { - json = super.$formatDatabaseJson(json) - - for (const translation of this.constructor.translations) { - if (translation.model in json) { - json[translation.database] = json[translation.model] - - delete json[translation.model] - } - } - - return json - } - - /** - * Called when a Model instance is created from a database JSON object. This method converts the JSON object from the - * database format to the internal format. - * - * This is an Objection.js instance method which we are overriding. We use it to convert unconventional database - * column names into standardised ones, for example `dateCreated` to `createdAt`. Timestamps is a primary example - * where the legacy tables do not use a consistent naming convention. Various tables use different names for the same - * thing. But we want few surprises and lots of consistency in our code. So, this allows us to translate the names of - * columns before Objection.js finalises the model instance. - * - * Each Model that extends `LegacyBaseModel` is expected to implement `static get translations` which defines the - * database to model translations needed. Any fields listed in `translations` not found will be ignored. - * - * For more details see - * {@link http://vincit.github.io/objection.js/api/model/instance-methods.html#parsedatabasejson $parseDatabaseJson()} - * and the - * {@link https://vincit.github.io/objection.js/api/model/overview.html#model-data-lifecycle Model data lifecycle} - * - * @param {Object} json The JSON POJO in database format - * @returns {Object} The JSON POJO in internal format - */ - $parseDatabaseJson (json) { - json = super.$parseDatabaseJson(json) - - for (const translation of this.constructor.translations) { - if (translation.database in json) { - json[translation.model] = json[translation.database] - - delete json[translation.database] - } - } - - return json - } -} - -module.exports = LegacyBaseModel diff --git a/app/models/legacy.query-builder.js b/app/models/legacy.query-builder.js deleted file mode 100644 index fee0ef0711..0000000000 --- a/app/models/legacy.query-builder.js +++ /dev/null @@ -1,31 +0,0 @@ -'use strict' - -/** - * Wrapper around Objection.js QueryBuilder to add schema and alias support to Legacy Models - * @module LegacyBaseModel - */ - -const { QueryBuilder } = require('objection') - -class LegacyQueryBuilder extends QueryBuilder { - /** - * Wrapper around Objection.js QueryBuilder to add schema and alias support to Legacy Models - * - * @param {Object} modelClass The Objection legacy model to which this QueryBuilder will be applied - */ - constructor (modelClass) { - super(modelClass) - - // ALL legacy models must implement a `schema` property. So, we always call this - this.withSchema(modelClass.schema) - - // Only alias those that need to alias the table name to avoid errors, for example, needing to alias - // `charge_elements` as 'charge_references' so we can link them to `charge_purposes` but call purposes - // `ChargeElements`. - if (modelClass.alias) { - this.alias(modelClass.alias) - } - } -} - -module.exports = LegacyQueryBuilder diff --git a/app/models/water/bill-licence.model.js b/app/models/water/bill-licence.model.js deleted file mode 100644 index 6103bff242..0000000000 --- a/app/models/water/bill-licence.model.js +++ /dev/null @@ -1,58 +0,0 @@ -'use strict' - -/** - * Model for billing_invoice_licences - * @module BillLicenceModel - */ - -const { Model } = require('objection') - -const WaterBaseModel = require('./water-base.model.js') - -class BillLicenceModel extends WaterBaseModel { - static get tableName () { - return 'billingInvoiceLicences' - } - - static get idColumn () { - return 'billingInvoiceLicenceId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - bill: { - relation: Model.BelongsToOneRelation, - modelClass: 'bill.model', - join: { - from: 'billingInvoiceLicences.billingInvoiceId', - to: 'billingInvoices.billingInvoiceId' - } - }, - transactions: { - relation: Model.HasManyRelation, - modelClass: 'transaction.model', - join: { - from: 'billingInvoiceLicences.billingInvoiceLicenceId', - to: 'billingTransactions.billingInvoiceLicenceId' - } - }, - licence: { - relation: Model.BelongsToOneRelation, - modelClass: 'licence.model', - join: { - from: 'billingInvoiceLicences.licenceId', - to: 'licences.licenceId' - } - } - } - } -} - -module.exports = BillLicenceModel diff --git a/app/models/water/bill-run-volume.model.js b/app/models/water/bill-run-volume.model.js deleted file mode 100644 index d964d8ced2..0000000000 --- a/app/models/water/bill-run-volume.model.js +++ /dev/null @@ -1,75 +0,0 @@ -'use strict' - -/** - * Model for billing_batches - * @module BillRunModel - */ - -const { Model } = require('objection') - -const WaterBaseModel = require('./water-base.model.js') - -class BillRunVolumeModel extends WaterBaseModel { - static get tableName () { - return 'billingVolumes' - } - - static get idColumn () { - return 'billingVolumeId' - } - - static get translations () { - return [] - } - - static get relationMappings () { - return { - billRun: { - relation: Model.BelongsToOneRelation, - modelClass: 'bill-run.model', - join: { - from: 'billingVolumes.billingBatchId', - to: 'billingBatches.billingBatchId' - } - }, - chargeReference: { - relation: Model.BelongsToOneRelation, - modelClass: 'charge-reference.model', - join: { - from: 'billingVolumes.chargeElementId', - to: 'chargeElements.chargeElementId' - } - } - } - } - - // NOTE: When we checked the live data the only statuses we could find in use were; 10, 40, 50, 60, 70, 90 and 100 - static get twoPartTariffStatuses () { - return { - noReturnsSubmitted: 10, - underQuery: 20, - received: 30, - someReturnsDue: 40, - lateReturns: 50, - overAbstraction: 60, - noReturnsForMatching: 70, - notDueForBilling: 80, - returnLineOverlapsChargePeriod: 90, - noMatchingChargeElement: 100 - } - } - - $twoPartTariffStatus () { - const index = Object.values(BillRunVolumeModel.twoPartTariffStatuses).findIndex((value) => { - return value === this.twoPartTariffStatus - }) - - if (index !== -1) { - return Object.keys(BillRunVolumeModel.twoPartTariffStatuses)[index] - } - - return null - } -} - -module.exports = BillRunVolumeModel diff --git a/app/models/water/bill-run.model.js b/app/models/water/bill-run.model.js deleted file mode 100644 index f314ba31c0..0000000000 --- a/app/models/water/bill-run.model.js +++ /dev/null @@ -1,72 +0,0 @@ -'use strict' - -/** - * Model for billing_batches - * @module BillRunModel - */ - -const { Model } = require('objection') - -const WaterBaseModel = require('./water-base.model.js') - -class BillRunModel extends WaterBaseModel { - static get tableName () { - return 'billingBatches' - } - - static get idColumn () { - return 'billingBatchId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - region: { - relation: Model.BelongsToOneRelation, - modelClass: 'region.model', - join: { - from: 'billingBatches.regionId', - to: 'regions.regionId' - } - }, - bills: { - relation: Model.HasManyRelation, - modelClass: 'bill.model', - join: { - from: 'billingBatches.billingBatchId', - to: 'billingInvoices.billingBatchId' - } - }, - billRunVolumes: { - relation: Model.HasManyRelation, - modelClass: 'bill-run-volume.model', - join: { - from: 'billingBatches.billingBatchId', - to: 'billingVolumes.billingBatchId' - } - } - } - } - - static get errorCodes () { - return { - failedToPopulateChargeVersions: 10, - failedToProcessChargeVersions: 20, - failedToPrepareTransactions: 30, - failedToCreateCharge: 40, - failedToCreateBillRun: 50, - failedToDeleteInvoice: 60, - failedToProcessTwoPartTariff: 70, - failedToGetChargeModuleBillRunSummary: 80, - failedToProcessRebilling: 90 - } - } -} - -module.exports = BillRunModel diff --git a/app/models/water/bill.model.js b/app/models/water/bill.model.js deleted file mode 100644 index 1b198da7cb..0000000000 --- a/app/models/water/bill.model.js +++ /dev/null @@ -1,50 +0,0 @@ -'use strict' - -/** - * Model for billing_invoices - * @module BillModel - */ - -const { Model } = require('objection') - -const WaterBaseModel = require('./water-base.model.js') - -class BillModel extends WaterBaseModel { - static get tableName () { - return 'billingInvoices' - } - - static get idColumn () { - return 'billingInvoiceId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - billRun: { - relation: Model.BelongsToOneRelation, - modelClass: 'bill-run.model', - join: { - from: 'billingInvoices.billingBatchId', - to: 'billingBatches.billingBatchId' - } - }, - billLicences: { - relation: Model.HasManyRelation, - modelClass: 'bill-licence.model', - join: { - from: 'billingInvoices.billingInvoiceId', - to: 'billingInvoiceLicences.billingInvoiceId' - } - } - } - } -} - -module.exports = BillModel diff --git a/app/models/water/change-reason.model.js b/app/models/water/change-reason.model.js deleted file mode 100644 index 7f7dbca8ca..0000000000 --- a/app/models/water/change-reason.model.js +++ /dev/null @@ -1,42 +0,0 @@ -'use strict' - -/** - * Model for changeReasons - * @module ChangeReasonModel - */ - -const { Model } = require('objection') - -const WaterBaseModel = require('./water-base.model.js') - -class ChangeReasonModel extends WaterBaseModel { - static get tableName () { - return 'changeReasons' - } - - static get idColumn () { - return 'changeReasonId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - chargeVersions: { - relation: Model.HasManyRelation, - modelClass: 'charge-version.model', - join: { - from: 'changeReasons.changeReasonId', - to: 'chargeVersions.changeReasonId' - } - } - } - } -} - -module.exports = ChangeReasonModel diff --git a/app/models/water/charge-category.model.js b/app/models/water/charge-category.model.js deleted file mode 100644 index 41a72f8843..0000000000 --- a/app/models/water/charge-category.model.js +++ /dev/null @@ -1,42 +0,0 @@ -'use strict' - -/** - * Model for billing_charge_categories - * @module ChargeCategoryModel - */ - -const { Model } = require('objection') - -const WaterBaseModel = require('./water-base.model.js') - -class ChargeCategoryModel extends WaterBaseModel { - static get tableName () { - return 'billingChargeCategories' - } - - static get idColumn () { - return 'billingChargeCategoryId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - chargeReferences: { - relation: Model.HasManyRelation, - modelClass: 'charge-reference.model', - join: { - from: 'billingChargeCategories.billingChargeCategoryId', - to: 'chargeElements.billingChargeCategoryId' - } - } - } - } -} - -module.exports = ChargeCategoryModel diff --git a/app/models/water/charge-element.model.js b/app/models/water/charge-element.model.js deleted file mode 100644 index 69396a3d24..0000000000 --- a/app/models/water/charge-element.model.js +++ /dev/null @@ -1,50 +0,0 @@ -'use strict' - -/** - * Model for charge_purposes - * @module ChargeElementModel - */ - -const { Model } = require('objection') - -const WaterBaseModel = require('./water-base.model.js') - -class ChargeElementModel extends WaterBaseModel { - static get tableName () { - return 'chargePurposes' - } - - static get idColumn () { - return 'chargePurposeId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - chargeReference: { - relation: Model.BelongsToOneRelation, - modelClass: 'charge-reference.model', - join: { - from: 'chargePurposes.chargeElementId', - to: 'chargeElements.chargeElementId' - } - }, - purpose: { - relation: Model.BelongsToOneRelation, - modelClass: 'purpose.model', - join: { - from: 'chargePurposes.purposeUseId', - to: 'purposesUses.purposeUseId' - } - } - } - } -} - -module.exports = ChargeElementModel diff --git a/app/models/water/charge-reference.model.js b/app/models/water/charge-reference.model.js deleted file mode 100644 index a7d5bb9732..0000000000 --- a/app/models/water/charge-reference.model.js +++ /dev/null @@ -1,86 +0,0 @@ -'use strict' - -/** - * Model for charge_elements - * @module ChargeReferenceModel - */ - -const { Model } = require('objection') - -const WaterBaseModel = require('./water-base.model.js') - -class ChargeReferenceModel extends WaterBaseModel { - static get tableName () { - return 'chargeElements' - } - - static get idColumn () { - return 'chargeElementId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get alias () { - return 'chargeReferences' - } - - static get relationMappings () { - return { - billRunVolumes: { - relation: Model.HasManyRelation, - modelClass: 'bill-run-volume.model', - join: { - from: 'chargeElements.chargeElementId', - to: 'billingVolumes.chargeElementId' - } - }, - chargeVersion: { - relation: Model.BelongsToOneRelation, - modelClass: 'charge-version.model', - join: { - from: 'chargeElements.chargeVersionId', - to: 'chargeVersions.chargeVersionId' - } - }, - chargeCategory: { - relation: Model.BelongsToOneRelation, - modelClass: 'charge-category.model', - join: { - from: 'chargeElements.billingChargeCategoryId', - to: 'billingChargeCategories.billingChargeCategoryId' - } - }, - chargeElements: { - relation: Model.HasManyRelation, - modelClass: 'charge-element.model', - join: { - from: 'chargeElements.chargeElementId', - to: 'chargePurposes.chargeElementId' - } - }, - purpose: { - relation: Model.BelongsToOneRelation, - modelClass: 'purpose.model', - join: { - from: 'chargeElements.purposeUseId', - to: 'purposesUses.purposeUseId' - } - }, - transactions: { - relation: Model.HasManyRelation, - modelClass: 'transaction.model', - join: { - from: 'chargeElements.chargeElementId', - to: 'billingTransactions.chargeElementId' - } - } - } - } -} - -module.exports = ChargeReferenceModel diff --git a/app/models/water/charge-version.model.js b/app/models/water/charge-version.model.js deleted file mode 100644 index 351b6f144d..0000000000 --- a/app/models/water/charge-version.model.js +++ /dev/null @@ -1,58 +0,0 @@ -'use strict' - -/** - * Model for charge_versions - * @module ChargeVersionModel - */ - -const { Model } = require('objection') - -const WaterBaseModel = require('./water-base.model.js') - -class ChargeVersionModel extends WaterBaseModel { - static get tableName () { - return 'chargeVersions' - } - - static get idColumn () { - return 'chargeVersionId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - licence: { - relation: Model.BelongsToOneRelation, - modelClass: 'licence.model', - join: { - from: 'chargeVersions.licenceId', - to: 'licences.licenceId' - } - }, - changeReason: { - relation: Model.BelongsToOneRelation, - modelClass: 'change-reason.model', - join: { - from: 'chargeVersions.changeReasonId', - to: 'changeReasons.changeReasonId' - } - }, - chargeReferences: { - relation: Model.HasManyRelation, - modelClass: 'charge-reference.model', - join: { - from: 'chargeVersions.chargeVersionId', - to: 'chargeElements.chargeVersionId' - } - } - } - } -} - -module.exports = ChargeVersionModel diff --git a/app/models/water/event.model.js b/app/models/water/event.model.js deleted file mode 100644 index f5a90df302..0000000000 --- a/app/models/water/event.model.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict' - -/** - * Model for events - * @module EventModel - */ - -const WaterBaseModel = require('./water-base.model.js') - -class EventModel extends WaterBaseModel { - static get tableName () { - return 'events' - } - - static get idColumn () { - return 'eventId' - } - - static get translations () { - return [ - { database: 'created', model: 'createdAt' }, - { database: 'modified', model: 'updatedAt' } - ] - } -} - -module.exports = EventModel diff --git a/app/models/water/licence-version.model.js b/app/models/water/licence-version.model.js deleted file mode 100644 index bbf6b393cd..0000000000 --- a/app/models/water/licence-version.model.js +++ /dev/null @@ -1,42 +0,0 @@ -'use strict' - -/** - * Model for licenceVersions - * @module LicenceVersionModel - */ - -const { Model } = require('objection') - -const WaterBaseModel = require('./water-base.model.js') - -class LicenceVersionModel extends WaterBaseModel { - static get tableName () { - return 'licenceVersions' - } - - static get idColumn () { - return 'licenceVersionId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - licence: { - relation: Model.BelongsToOneRelation, - modelClass: 'licence.model', - join: { - from: 'licenceVersions.licenceId', - to: 'licences.licenceId' - } - } - } - } -} - -module.exports = LicenceVersionModel diff --git a/app/models/water/licence.model.js b/app/models/water/licence.model.js deleted file mode 100644 index 04eb18b864..0000000000 --- a/app/models/water/licence.model.js +++ /dev/null @@ -1,74 +0,0 @@ -'use strict' - -/** - * Model for licences - * @module LicenceModel - */ - -const { Model } = require('objection') - -const WaterBaseModel = require('./water-base.model.js') - -class LicenceModel extends WaterBaseModel { - static get tableName () { - return 'licences' - } - - static get idColumn () { - return 'licenceId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - chargeVersions: { - relation: Model.HasManyRelation, - modelClass: 'charge-version.model', - join: { - from: 'licences.licenceId', - to: 'chargeVersions.licenceId' - } - }, - region: { - relation: Model.BelongsToOneRelation, - modelClass: 'region.model', - join: { - from: 'licences.regionId', - to: 'regions.regionId' - } - }, - billLicences: { - relation: Model.HasManyRelation, - modelClass: 'bill-licence.model', - join: { - from: 'licences.licenceId', - to: 'billingInvoiceLicences.licenceId' - } - }, - workflows: { - relation: Model.HasManyRelation, - modelClass: 'workflow.model', - join: { - from: 'licences.licenceId', - to: 'chargeVersionWorkflows.licenceId' - } - }, - licenceVersions: { - relation: Model.HasManyRelation, - modelClass: 'licence-version.model', - join: { - from: 'licences.licenceId', - to: 'licenceVersions.licenceId' - } - } - } - } -} - -module.exports = LicenceModel diff --git a/app/models/water/purpose.model.js b/app/models/water/purpose.model.js deleted file mode 100644 index ad037d8218..0000000000 --- a/app/models/water/purpose.model.js +++ /dev/null @@ -1,50 +0,0 @@ -'use strict' - -/** - * Model for purposes_uses - * @module PurposeModel - */ - -const { Model } = require('objection') - -const WaterBaseModel = require('./water-base.model.js') - -class PurposeModel extends WaterBaseModel { - static get tableName () { - return 'purposesUses' - } - - static get idColumn () { - return 'purposeUseId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - chargeElements: { - relation: Model.HasManyRelation, - modelClass: 'charge-element.model', - join: { - from: 'purposesUses.purposeUseId', - to: 'chargePurposes.purposeUseId' - } - }, - chargeReferences: { - relation: Model.HasManyRelation, - modelClass: 'charge-reference.model', - join: { - from: 'purposesUses.purposeUseId', - to: 'chargeElements.purposeUseId' - } - } - } - } -} - -module.exports = PurposeModel diff --git a/app/models/water/region.model.js b/app/models/water/region.model.js deleted file mode 100644 index 616222cb32..0000000000 --- a/app/models/water/region.model.js +++ /dev/null @@ -1,50 +0,0 @@ -'use strict' - -/** - * Model for regions - * @module RegionModel - */ - -const { Model } = require('objection') - -const WaterBaseModel = require('./water-base.model.js') - -class RegionModel extends WaterBaseModel { - static get tableName () { - return 'regions' - } - - static get idColumn () { - return 'regionId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - licences: { - relation: Model.HasManyRelation, - modelClass: 'licence.model', - join: { - from: 'regions.regionId', - to: 'licences.regionId' - } - }, - billRuns: { - relation: Model.HasManyRelation, - modelClass: 'bill-run.model', - join: { - from: 'regions.regionId', - to: 'billingBatches.regionId' - } - } - } - } -} - -module.exports = RegionModel diff --git a/app/models/water/transaction.model.js b/app/models/water/transaction.model.js deleted file mode 100644 index 538007e769..0000000000 --- a/app/models/water/transaction.model.js +++ /dev/null @@ -1,60 +0,0 @@ -'use strict' - -/** - * Model for billing_transactions - * @module TransactionModel - */ - -const { Model } = require('objection') - -const WaterBaseModel = require('./water-base.model.js') - -class TransactionModel extends WaterBaseModel { - static get tableName () { - return 'billingTransactions' - } - - static get idColumn () { - return 'billingTransactionId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - chargeReference: { - relation: Model.BelongsToOneRelation, - modelClass: 'charge-reference.model', - join: { - from: 'billingTransactions.chargeElementId', - to: 'chargeElements.chargeElementId' - } - }, - billLicence: { - relation: Model.BelongsToOneRelation, - modelClass: 'bill-licence.model', - join: { - from: 'billingTransactions.billingInvoiceLicenceId', - to: 'billingInvoiceLicences.billingInvoiceLicenceId' - } - } - } - } - - // Defining which fields contain json allows us to insert an object without needing to stringify it first - static get jsonAttributes () { - return [ - 'abstractionPeriod', - 'grossValuesCalculated', - 'metadata', - 'purposes' - ] - } -} - -module.exports = TransactionModel diff --git a/app/models/water/water-base.model.js b/app/models/water/water-base.model.js deleted file mode 100644 index ed429e8eeb..0000000000 --- a/app/models/water/water-base.model.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict' - -/** - * Base class for all models based on the legacy 'water' schema - * @module WaterBaseModel - */ - -const LegacyBaseModel = require('../legacy-base.model.js') - -class WaterBaseModel extends LegacyBaseModel { - static get schema () { - return 'water' - } -} - -module.exports = WaterBaseModel diff --git a/app/models/water/workflow.model.js b/app/models/water/workflow.model.js deleted file mode 100644 index acb30941c9..0000000000 --- a/app/models/water/workflow.model.js +++ /dev/null @@ -1,42 +0,0 @@ -'use strict' - -/** - * Model for charge_version_workflows - * @module WorkflowModel - */ - -const { Model } = require('objection') - -const WaterBaseModel = require('./water-base.model.js') - -class WorkflowModel extends WaterBaseModel { - static get tableName () { - return 'chargeVersionWorkflows' - } - - static get idColumn () { - return 'chargeVersionWorkflowId' - } - - static get translations () { - return [ - { database: 'dateCreated', model: 'createdAt' }, - { database: 'dateUpdated', model: 'updatedAt' } - ] - } - - static get relationMappings () { - return { - licence: { - relation: Model.BelongsToOneRelation, - modelClass: 'licence.model', - join: { - from: 'chargeVersionWorkflows.licenceId', - to: 'licences.licenceId' - } - } - } - } -} - -module.exports = WorkflowModel diff --git a/test/models/legacy-base.model.test.js b/test/models/legacy-base.model.test.js deleted file mode 100644 index 032857efc6..0000000000 --- a/test/models/legacy-base.model.test.js +++ /dev/null @@ -1,159 +0,0 @@ -'use strict' - -// Test framework dependencies -const Lab = require('@hapi/lab') -const Code = require('@hapi/code') - -const { describe, it } = exports.lab = Lab.script() -const { expect } = Code - -// Test helpers -const { DBError } = require('objection') -const EventModel = require('../../app/models/water/event.model.js') - -// Thing under test -const LegacyBaseModel = require('../../app/models/legacy-base.model.js') - -describe('Legacy Base model', () => { - describe('.schema', () => { - describe('when the getter is not overridden', () => { - class BadModel extends LegacyBaseModel { - static get translations () { - return [] - } - } - - it('throws an error when called', () => { - expect(() => BadModel.query()).to.throw() - }) - }) - - describe('when the getter is overridden', () => { - class GoodModel extends LegacyBaseModel { - static get schema () { - return 'water' - } - - static get translations () { - return [] - } - } - - it('does not throw an error when called', () => { - expect(() => GoodModel.query()).not.to.throw() - }) - }) - }) - - describe('.translations', () => { - describe('when the getter is not overridden', () => { - class BadModel extends LegacyBaseModel { - static get schema () { - return 'water' - } - } - - it('throws an error when called', () => { - const instance = new BadModel() - expect(() => instance.$toJson()).not.to.throw() - }) - }) - - describe('when the getter is overridden', () => { - class GoodModel extends LegacyBaseModel { - static get schema () { - return 'water' - } - - static get translations () { - return [] - } - } - - it('does not throw an error when called', () => { - const instance = new GoodModel() - expect(() => instance.$toJson()).not.to.throw() - }) - }) - }) - - describe('when working with Legacy model instances', () => { - describe('and the legacy table a model is based on has non-standard columns', () => { - class YeOldeBillingModel extends LegacyBaseModel { - static get translations () { - return [ - { database: 'yehAlrightLove', model: 'greeting' }, - { database: 'doesNotExist', model: 'notHere' } - ] - } - } - - const dummyDatabaseJson = { - billingId: '76353750-5ace-411a-b00c-4e4a9ac934fb', - yehAlrightLove: 'Hello' - } - - const dummyModelJson = { - billingId: '76353750-5ace-411a-b00c-4e4a9ac934fb', - greeting: 'Hello' - } - - describe('when translating from the database table', () => { - it('translates them to standard model properties', () => { - const yeOldeBilling = new YeOldeBillingModel() - - // You would never call this directly. $parseDatabaseJson() is an objection method we're overloading in - // LegacyBaseModel. But it gives us the JSON Objection.js will then use to build the model so it works for this - // test. The JSON it expects as a param is generated by Objection based on the table in the DB. - // Note: The spread operator is used to create a shallow clone as $parseDatabaseJson() amends the value passed - // in. That would break subsequent tests - const result = yeOldeBilling.$parseDatabaseJson({ ...dummyDatabaseJson }) - - expect(result).to.equal(dummyModelJson) - }) - - it('ignores any translations that do not exist', () => { - const yeOldeBilling = new YeOldeBillingModel() - - const result = yeOldeBilling.$parseDatabaseJson({ ...dummyDatabaseJson }) - - expect(result.notHere).not.to.exist() - }) - }) - - describe('when translating back from the model instance', () => { - it('translates the model properties back to non-standard columns', () => { - const yeOldeBilling = new YeOldeBillingModel() - - // You would never call this directly. $formatDatabaseJson() is an objection method we're overloading in - // LegacyBaseModel. But it gives us the JSON Objection.js will then use to build to build the query so it works - // for this test. The JSON it expects as a param is generated by Objection based on the model instance. - // Note: The spread operator is used to create a shallow clone as $formatDatabaseJson() amends the value passed - // in. That would break subsequent tests - const result = yeOldeBilling.$formatDatabaseJson({ ...dummyModelJson }) - - expect(result).to.equal(dummyDatabaseJson) - }) - - it('ignores any translations that do not exist', () => { - const yeOldeBilling = new YeOldeBillingModel() - - const result = yeOldeBilling.$formatDatabaseJson({ ...dummyModelJson }) - - expect(result.doesNotExist).not.to.exist() - }) - }) - }) - }) - - describe('when working with Legacy model classes', () => { - describe('and the legacy table a model is based on has non-standard columns', () => { - it('throws an error when a query uses the standard model property name', async () => { - const timeNow = new Date().toISOString() - - const error = await expect(EventModel.query().where('createdAt', '<', timeNow)).to.reject() - expect(error).to.be.an.instanceOf(DBError) - }) - }) - }) -}) diff --git a/test/models/legacy.query-builder.test.js b/test/models/legacy.query-builder.test.js deleted file mode 100644 index d7dc28fdb4..0000000000 --- a/test/models/legacy.query-builder.test.js +++ /dev/null @@ -1,66 +0,0 @@ -'use strict' - -'use strict' - -// Test framework dependencies -const Lab = require('@hapi/lab') -const Code = require('@hapi/code') - -const { describe, it } = exports.lab = Lab.script() -const { expect } = Code - -// Test helpers -const LegacyBaseModel = require('../../app/models/legacy-base.model.js') - -// Thing under test -const LegacyQueryBuilder = require('../../app/models/legacy.query-builder.js') - -describe('Legacy Base model', () => { - describe('when given a model class with only a schema', () => { - class SchemaLegacyModel extends LegacyBaseModel { - static get tableName () { - return 'schemaTable' - } - - static get schema () { - return 'water' - } - - static get QueryBuilder () { - return LegacyQueryBuilder - } - } - - it('adds the schema name to the table in the queries it generates', () => { - const result = SchemaLegacyModel.query().toKnexQuery().toQuery() - - expect(result).to.endWith('from "water"."schema_table"') - }) - }) - - describe('when given a model class with an alias', () => { - class AliasLegacyModel extends LegacyBaseModel { - static get tableName () { - return 'aliasTable' - } - - static get schema () { - return 'water' - } - - static get alias () { - return 'chargeReferences' - } - - static get QueryBuilder () { - return LegacyQueryBuilder - } - } - - it('aliases the table in the queries it generates', () => { - const result = AliasLegacyModel.query().toKnexQuery().toQuery() - - expect(result).to.endWith('from "water"."alias_table" as "charge_references"') - }) - }) -}) diff --git a/test/models/water/bill-licence.model.test.js b/test/models/water/bill-licence.model.test.js deleted file mode 100644 index 91262a89d4..0000000000 --- a/test/models/water/bill-licence.model.test.js +++ /dev/null @@ -1,138 +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 BillHelper = require('../../support/helpers/water/bill.helper.js') -const BillModel = require('../../../app/models/water/bill.model.js') -const BillLicenceHelper = require('../../support/helpers/water/bill-licence.helper.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') -const LicenceHelper = require('../../support/helpers/water/licence.helper.js') -const LicenceModel = require('../../../app/models/water/licence.model.js') -const TransactionHelper = require('../../support/helpers/water/transaction.helper.js') -const TransactionModel = require('../../../app/models/water/transaction.model.js') - -// Thing under test -const BillLicenceModel = require('../../../app/models/water/bill-licence.model.js') - -describe('Bill Licence model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - - testRecord = await BillLicenceHelper.add() - }) - - describe('Basic query', () => { - it('can successfully run a basic query', async () => { - const result = await BillLicenceModel.query().findById(testRecord.billingInvoiceLicenceId) - - expect(result).to.be.an.instanceOf(BillLicenceModel) - expect(result.billingInvoiceLicenceId).to.equal(testRecord.billingInvoiceLicenceId) - }) - }) - - describe('Relationships', () => { - describe('when linking to bill', () => { - let testBill - - beforeEach(async () => { - testBill = await BillHelper.add() - - const { billingInvoiceId } = testBill - testRecord = await BillLicenceHelper.add({ billingInvoiceId }) - }) - - it('can successfully run a related query', async () => { - const query = await BillLicenceModel.query() - .innerJoinRelated('bill') - - expect(query).to.exist() - }) - - it('can eager load the bill', async () => { - const result = await BillLicenceModel.query() - .findById(testRecord.billingInvoiceLicenceId) - .withGraphFetched('bill') - - expect(result).to.be.instanceOf(BillLicenceModel) - expect(result.billingInvoiceLicenceId).to.equal(testRecord.billingInvoiceLicenceId) - - expect(result.bill).to.be.an.instanceOf(BillModel) - expect(result.bill).to.equal(testBill) - }) - }) - - describe('when linking to transactions', () => { - let testTransactions - - beforeEach(async () => { - testRecord = await BillLicenceHelper.add() - const { billingInvoiceLicenceId } = testRecord - - testTransactions = [] - for (let i = 0; i < 2; i++) { - const transaction = await TransactionHelper.add({ billingInvoiceLicenceId }) - testTransactions.push(transaction) - } - }) - - it('can successfully run a related query', async () => { - const query = await BillLicenceModel.query() - .innerJoinRelated('transactions') - - expect(query).to.exist() - }) - - it('can eager load the transactions', async () => { - const result = await BillLicenceModel.query() - .findById(testRecord.billingInvoiceLicenceId) - .withGraphFetched('transactions') - - expect(result).to.be.instanceOf(BillLicenceModel) - expect(result.billingInvoiceLicenceId).to.equal(testRecord.billingInvoiceLicenceId) - - expect(result.transactions).to.be.an.array() - expect(result.transactions[0]).to.be.an.instanceOf(TransactionModel) - expect(result.transactions).to.include(testTransactions[0]) - expect(result.transactions).to.include(testTransactions[1]) - }) - }) - - describe('when linking to licence', () => { - let testLicence - - beforeEach(async () => { - testLicence = await LicenceHelper.add() - - const { licenceId } = testLicence - testRecord = await BillLicenceHelper.add({ licenceId }) - }) - - it('can successfully run a related query', async () => { - const query = await BillLicenceModel.query() - .innerJoinRelated('licence') - - expect(query).to.exist() - }) - - it('can eager load the licence', async () => { - const result = await BillLicenceModel.query() - .findById(testRecord.billingInvoiceLicenceId) - .withGraphFetched('licence') - - expect(result).to.be.instanceOf(BillLicenceModel) - expect(result.billingInvoiceLicenceId).to.equal(testRecord.billingInvoiceLicenceId) - - expect(result.licence).to.be.an.instanceOf(LicenceModel) - expect(result.licence).to.equal(testLicence) - }) - }) - }) -}) diff --git a/test/models/water/bill-run-volume.model.test.js b/test/models/water/bill-run-volume.model.test.js deleted file mode 100644 index 0c8980110e..0000000000 --- a/test/models/water/bill-run-volume.model.test.js +++ /dev/null @@ -1,138 +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 BillRunHelper = require('../../support/helpers/water/bill-run.helper.js') -const BillRunModel = require('../../../app/models/water/bill-run.model.js') -const BillRunVolumeHelper = require('../../support/helpers/water/bill-run-volume.helper.js') -const ChargeReferenceHelper = require('../../support/helpers/water/charge-reference.helper.js') -const ChargeReferenceModel = require('../../../app/models/water/charge-reference.model.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') - -// Thing under test -const BillRunVolumeModel = require('../../../app/models/water/bill-run-volume.model.js') - -describe('Bill Run Volume model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - }) - - describe('Basic query', () => { - beforeEach(async () => { - testRecord = await BillRunVolumeHelper.add() - }) - - it('can successfully run a basic query', async () => { - const result = await BillRunVolumeModel.query().findById(testRecord.billingVolumeId) - - expect(result).to.be.an.instanceOf(BillRunVolumeModel) - expect(result.billingVolumeId).to.equal(testRecord.billingVolumeId) - }) - }) - - describe('Relationships', () => { - describe('when linking to bill run', () => { - let testBillRun - - beforeEach(async () => { - testBillRun = await BillRunHelper.add() - - const { billingBatchId } = testBillRun - testRecord = await BillRunVolumeHelper.add({ billingBatchId }) - }) - - it('can successfully run a related query', async () => { - const query = await BillRunVolumeModel.query() - .innerJoinRelated('billRun') - - expect(query).to.exist() - }) - - it('can eager load the bill run', async () => { - const result = await BillRunVolumeModel.query() - .findById(testRecord.billingVolumeId) - .withGraphFetched('billRun') - - expect(result).to.be.instanceOf(BillRunVolumeModel) - expect(result.billingVolumeId).to.equal(testRecord.billingVolumeId) - - expect(result.billRun).to.be.an.instanceOf(BillRunModel) - expect(result.billRun).to.equal(testBillRun) - }) - }) - - describe('when linking to charge reference', () => { - let testChargeReference - - beforeEach(async () => { - testChargeReference = await ChargeReferenceHelper.add() - - const { chargeElementId } = testChargeReference - testRecord = await BillRunVolumeHelper.add({ chargeElementId }) - }) - - it('can successfully run a related query', async () => { - const query = await BillRunVolumeModel.query() - .innerJoinRelated('chargeReference') - - expect(query).to.exist() - }) - - it('can eager load the charge reference', async () => { - const result = await BillRunVolumeModel.query() - .findById(testRecord.billingVolumeId) - .withGraphFetched('chargeReference') - - expect(result).to.be.instanceOf(BillRunVolumeModel) - expect(result.billingVolumeId).to.equal(testRecord.billingVolumeId) - - expect(result.chargeReference).to.be.an.instanceOf(ChargeReferenceModel) - expect(result.chargeReference).to.equal(testChargeReference) - }) - }) - }) - - describe('Static getters', () => { - describe('Two Part Tariff status codes', () => { - it('returns the requested status code', async () => { - const result = BillRunVolumeModel.twoPartTariffStatuses.noReturnsSubmitted - - expect(result).to.equal(10) - }) - }) - }) - - describe('$twoPartTariffStatus', () => { - describe('when the two-part tariff status is set', () => { - beforeEach(async () => { - testRecord = await BillRunVolumeHelper.add({ twoPartTariffStatus: 90 }) - }) - - it('returns the status', () => { - const result = testRecord.$twoPartTariffStatus() - - expect(result).to.equal('returnLineOverlapsChargePeriod') - }) - }) - - describe('when the two-part tariff status is not set', () => { - beforeEach(async () => { - testRecord = await BillRunVolumeHelper.add() - }) - - it('returns null', () => { - const result = testRecord.$twoPartTariffStatus() - - expect(result).to.be.null() - }) - }) - }) -}) diff --git a/test/models/water/bill-run.model.test.js b/test/models/water/bill-run.model.test.js deleted file mode 100644 index 514a8adcf6..0000000000 --- a/test/models/water/bill-run.model.test.js +++ /dev/null @@ -1,154 +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 BillHelper = require('../../support/helpers/water/bill.helper.js') -const BillModel = require('../../../app/models/water/bill.model.js') -const BillRunHelper = require('../../support/helpers/water/bill-run.helper.js') -const BillRunVolumeHelper = require('../../support/helpers/water/bill-run-volume.helper.js') -const BillRunVolumeModel = require('../../../app/models/water/bill-run-volume.model.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') -const RegionHelper = require('../../support/helpers/water/region.helper.js') -const RegionModel = require('../../../app/models/water/region.model.js') - -// Thing under test -const BillRunModel = require('../../../app/models/water/bill-run.model.js') - -describe('Bill Run model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - }) - - describe('Basic query', () => { - beforeEach(async () => { - testRecord = await BillRunHelper.add() - }) - - it('can successfully run a basic query', async () => { - const result = await BillRunModel.query().findById(testRecord.billingBatchId) - - expect(result).to.be.an.instanceOf(BillRunModel) - expect(result.billingBatchId).to.equal(testRecord.billingBatchId) - }) - }) - - describe('Relationships', () => { - describe('when linking to region', () => { - let testRegion - - beforeEach(async () => { - testRegion = await RegionHelper.add() - testRecord = await BillRunHelper.add({ regionId: testRegion.regionId }) - }) - - it('can successfully run a related query', async () => { - const query = await BillRunModel.query() - .innerJoinRelated('region') - - expect(query).to.exist() - }) - - it('can eager load the region', async () => { - const result = await BillRunModel.query() - .findById(testRecord.billingBatchId) - .withGraphFetched('region') - - expect(result).to.be.instanceOf(BillRunModel) - expect(result.billingBatchId).to.equal(testRecord.billingBatchId) - - expect(result.region).to.be.an.instanceOf(RegionModel) - expect(result.region).to.equal(testRegion) - }) - }) - - describe('when linking to bills', () => { - let testBills - - beforeEach(async () => { - testRecord = await BillRunHelper.add() - const { billingBatchId } = testRecord - - testBills = [] - for (let i = 0; i < 2; i++) { - const bill = await BillHelper.add({ financialYearEnding: 2023, billingBatchId }) - testBills.push(bill) - } - }) - - it('can successfully run a related query', async () => { - const query = await BillRunModel.query() - .innerJoinRelated('bills') - - expect(query).to.exist() - }) - - it('can eager load the bills', async () => { - const result = await BillRunModel.query() - .findById(testRecord.billingBatchId) - .withGraphFetched('bills') - - expect(result).to.be.instanceOf(BillRunModel) - expect(result.billingBatchId).to.equal(testRecord.billingBatchId) - - expect(result.bills).to.be.an.array() - expect(result.bills[0]).to.be.an.instanceOf(BillModel) - expect(result.bills).to.include(testBills[0]) - expect(result.bills).to.include(testBills[1]) - }) - }) - - describe('when linking to bill run volumes', () => { - let testBillRunVolumes - - beforeEach(async () => { - testRecord = await BillRunHelper.add() - const { billingBatchId } = testRecord - - testBillRunVolumes = [] - for (let i = 0; i < 2; i++) { - const billRunVolume = await BillRunVolumeHelper.add({ billingBatchId }) - testBillRunVolumes.push(billRunVolume) - } - }) - - it('can successfully run a related query', async () => { - const query = await BillRunModel.query() - .innerJoinRelated('billRunVolumes') - - expect(query).to.exist() - }) - - it('can eager load the bills', async () => { - const result = await BillRunModel.query() - .findById(testRecord.billingBatchId) - .withGraphFetched('billRunVolumes') - - expect(result).to.be.instanceOf(BillRunModel) - expect(result.billingBatchId).to.equal(testRecord.billingBatchId) - - expect(result.billRunVolumes).to.be.an.array() - expect(result.billRunVolumes[0]).to.be.an.instanceOf(BillRunVolumeModel) - expect(result.billRunVolumes).to.include(testBillRunVolumes[0]) - expect(result.billRunVolumes).to.include(testBillRunVolumes[1]) - }) - }) - }) - - describe('Static getters', () => { - describe('Error codes', () => { - it('returns the requested error code', async () => { - const result = BillRunModel.errorCodes.failedToCreateBillRun - - expect(result).to.equal(50) - }) - }) - }) -}) diff --git a/test/models/water/bill.model.test.js b/test/models/water/bill.model.test.js deleted file mode 100644 index d4a278e0da..0000000000 --- a/test/models/water/bill.model.test.js +++ /dev/null @@ -1,106 +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 BillRunModel = require('../../../app/models/water/bill-run.model.js') -const BillHelper = require('../../support/helpers/water/bill.helper.js') -const BillLicenceHelper = require('../../support/helpers/water/bill-licence.helper.js') -const BillLicenceModel = require('../../../app/models/water/bill-licence.model.js') -const BillRunHelper = require('../../support/helpers/water/bill-run.helper.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') - -// Thing under test -const BillModel = require('../../../app/models/water/bill.model.js') - -describe('Bill model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - }) - - describe('Basic query', () => { - beforeEach(async () => { - testRecord = await BillHelper.add() - }) - - it('can successfully run a basic query', async () => { - const result = await BillModel.query().findById(testRecord.billingInvoiceId) - - expect(result).to.be.an.instanceOf(BillModel) - expect(result.billingInvoiceId).to.equal(testRecord.billingInvoiceId) - }) - }) - - describe('Relationships', () => { - describe('when linking to bill run', () => { - let testBillRun - - beforeEach(async () => { - testBillRun = await BillRunHelper.add() - testRecord = await BillHelper.add({ billingBatchId: testBillRun.billingBatchId }) - }) - - it('can successfully run a related query', async () => { - const query = await BillModel.query() - .innerJoinRelated('billRun') - - expect(query).to.exist() - }) - - it('can eager load the bill run', async () => { - const result = await BillModel.query() - .findById(testRecord.billingInvoiceId) - .withGraphFetched('billRun') - - expect(result).to.be.instanceOf(BillModel) - expect(result.billingInvoiceId).to.equal(testRecord.billingInvoiceId) - - expect(result.billRun).to.be.an.instanceOf(BillRunModel) - expect(result.billRun).to.equal(testBillRun) - }) - }) - - describe('when linking to bill licences', () => { - let testBillLicences - - beforeEach(async () => { - testRecord = await BillHelper.add() - const { billingInvoiceId } = testRecord - - testBillLicences = [] - for (let i = 0; i < 2; i++) { - const billLicence = await BillLicenceHelper.add({ billingInvoiceId }) - testBillLicences.push(billLicence) - } - }) - - it('can successfully run a related query', async () => { - const query = await BillModel.query() - .innerJoinRelated('billLicences') - - expect(query).to.exist() - }) - - it('can eager load the bill licences', async () => { - const result = await BillModel.query() - .findById(testRecord.billingInvoiceId) - .withGraphFetched('billLicences') - - expect(result).to.be.instanceOf(BillModel) - expect(result.billingInvoiceId).to.equal(testRecord.billingInvoiceId) - - expect(result.billLicences).to.be.an.array() - expect(result.billLicences[0]).to.be.an.instanceOf(BillLicenceModel) - expect(result.billLicences).to.include(testBillLicences[0]) - expect(result.billLicences).to.include(testBillLicences[1]) - }) - }) - }) -}) diff --git a/test/models/water/change-reason.model.test.js b/test/models/water/change-reason.model.test.js deleted file mode 100644 index 33f99d2bbf..0000000000 --- a/test/models/water/change-reason.model.test.js +++ /dev/null @@ -1,73 +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 ChangeReasonHelper = require('../../support/helpers/water/change-reason.helper.js') -const ChargeVersionHelper = require('../../support/helpers/water/charge-version.helper.js') -const ChargeVersionModel = require('../../../app/models/water/charge-version.model.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') - -// Thing under test -const ChangeReasonModel = require('../../../app/models/water/change-reason.model.js') - -describe('Change Reason model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - - testRecord = await ChangeReasonHelper.add() - }) - - describe('Basic query', () => { - it('can successfully run a basic query', async () => { - const result = await ChangeReasonModel.query().findById(testRecord.changeReasonId) - - expect(result).to.be.an.instanceOf(ChangeReasonModel) - expect(result.changeReasonId).to.equal(testRecord.changeReasonId) - }) - }) - - describe('Relationships', () => { - describe('when linking to charge versions', () => { - let testChargeVersions - - beforeEach(async () => { - const { changeReasonId } = testRecord - - testChargeVersions = [] - for (let i = 0; i < 2; i++) { - const chargeVersion = await ChargeVersionHelper.add({ changeReasonId }) - testChargeVersions.push(chargeVersion) - } - }) - - it('can successfully run a related query', async () => { - const query = await ChangeReasonModel.query() - .innerJoinRelated('chargeVersions') - - expect(query).to.exist() - }) - - it('can eager load the charge versions', async () => { - const result = await ChangeReasonModel.query() - .findById(testRecord.changeReasonId) - .withGraphFetched('chargeVersions') - - expect(result).to.be.instanceOf(ChangeReasonModel) - expect(result.changeReasonId).to.equal(testRecord.changeReasonId) - - expect(result.chargeVersions).to.be.an.array() - expect(result.chargeVersions[0]).to.be.an.instanceOf(ChargeVersionModel) - expect(result.chargeVersions).to.include(testChargeVersions[0]) - expect(result.chargeVersions).to.include(testChargeVersions[1]) - }) - }) - }) -}) diff --git a/test/models/water/charge-category.model.test.js b/test/models/water/charge-category.model.test.js deleted file mode 100644 index 78b96f70a9..0000000000 --- a/test/models/water/charge-category.model.test.js +++ /dev/null @@ -1,73 +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 ChargeCategoryHelper = require('../../support/helpers/water/charge-category.helper.js') -const ChargeReferenceHelper = require('../../support/helpers/water/charge-reference.helper.js') -const ChargeReferenceModel = require('../../../app/models/water/charge-reference.model.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') - -// Thing under test -const ChargeCategoryModel = require('../../../app/models/water/charge-category.model.js') - -describe('Charge Category model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - - testRecord = await ChargeCategoryHelper.add() - }) - - describe('Basic query', () => { - it('can successfully run a basic query', async () => { - const result = await ChargeCategoryModel.query().findById(testRecord.billingChargeCategoryId) - - expect(result).to.be.an.instanceOf(ChargeCategoryModel) - expect(result.billingChargeCategoryId).to.equal(testRecord.billingChargeCategoryId) - }) - }) - - describe('Relationships', () => { - describe('when linking to charge references', () => { - let testChargeReferences - - beforeEach(async () => { - const { billingChargeCategoryId } = testRecord - - testChargeReferences = [] - for (let i = 0; i < 2; i++) { - const chargeReference = await ChargeReferenceHelper.add({ description: `CE ${i}`, billingChargeCategoryId }) - testChargeReferences.push(chargeReference) - } - }) - - it('can successfully run a related query', async () => { - const query = await ChargeCategoryModel.query() - .innerJoinRelated('chargeReferences') - - expect(query).to.exist() - }) - - it('can eager load the charge references', async () => { - const result = await ChargeCategoryModel.query() - .findById(testRecord.billingChargeCategoryId) - .withGraphFetched('chargeReferences') - - expect(result).to.be.instanceOf(ChargeCategoryModel) - expect(result.billingChargeCategoryId).to.equal(testRecord.billingChargeCategoryId) - - expect(result.chargeReferences).to.be.an.array() - expect(result.chargeReferences[0]).to.be.an.instanceOf(ChargeReferenceModel) - expect(result.chargeReferences).to.include(testChargeReferences[0]) - expect(result.chargeReferences).to.include(testChargeReferences[1]) - }) - }) - }) -}) diff --git a/test/models/water/charge-element.model.test.js b/test/models/water/charge-element.model.test.js deleted file mode 100644 index bfbaca7d10..0000000000 --- a/test/models/water/charge-element.model.test.js +++ /dev/null @@ -1,100 +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 ChargeElementHelper = require('../../support/helpers/water/charge-element.helper.js') -const ChargeReferenceHelper = require('../../support/helpers/water/charge-reference.helper.js') -const ChargeReferenceModel = require('../../../app/models/water/charge-reference.model.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') -const PurposeModel = require('../../../app/models/water/purpose.model.js') -const PurposeHelper = require('../../support/helpers/water/purpose.helper.js') - -// Thing under test -const ChargeElementModel = require('../../../app/models/water/charge-element.model.js') - -describe('Charge Element model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - - testRecord = await ChargeElementHelper.add() - }) - - describe('Basic query', () => { - it('can successfully run a basic query', async () => { - const result = await ChargeElementModel.query().findById(testRecord.chargePurposeId) - - expect(result).to.be.an.instanceOf(ChargeElementModel) - expect(result.chargePurposeId).to.equal(testRecord.chargePurposeId) - }) - }) - - describe('Relationships', () => { - describe('when linking to charge reference', () => { - let testChargeReference - - beforeEach(async () => { - testChargeReference = await ChargeReferenceHelper.add() - - const { chargeElementId } = testChargeReference - testRecord = await ChargeElementHelper.add({ chargeElementId }) - }) - - it('can successfully run a related query', async () => { - const query = await ChargeElementModel.query() - .innerJoinRelated('chargeReference') - - expect(query).to.exist() - }) - - it('can eager load the charge reference', async () => { - const result = await ChargeElementModel.query() - .findById(testRecord.chargePurposeId) - .withGraphFetched('chargeReference') - - expect(result).to.be.instanceOf(ChargeElementModel) - expect(result.chargePurposeId).to.equal(testRecord.chargePurposeId) - - expect(result.chargeReference).to.be.an.instanceOf(ChargeReferenceModel) - expect(result.chargeReference).to.equal(testChargeReference) - }) - }) - - describe('when linking to purpose', () => { - let testPurpose - - beforeEach(async () => { - testPurpose = await PurposeHelper.add() - - const { purposeUseId } = testPurpose - testRecord = await ChargeElementHelper.add({ purposeUseId }) - }) - - it('can successfully run a related query', async () => { - const query = await ChargeElementModel.query() - .innerJoinRelated('purpose') - - expect(query).to.exist() - }) - - it('can eager load the purposes use', async () => { - const result = await ChargeElementModel.query() - .findById(testRecord.chargePurposeId) - .withGraphFetched('purpose') - - expect(result).to.be.instanceOf(ChargeElementModel) - expect(result.chargePurposeId).to.equal(testRecord.chargePurposeId) - - expect(result.purpose).to.be.an.instanceOf(PurposeModel) - expect(result.purpose).to.equal(testPurpose) - }) - }) - }) -}) diff --git a/test/models/water/charge-reference.model.test.js b/test/models/water/charge-reference.model.test.js deleted file mode 100644 index 74e793d9e7..0000000000 --- a/test/models/water/charge-reference.model.test.js +++ /dev/null @@ -1,244 +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 BillRunVolumeHelper = require('../../support/helpers/water/bill-run-volume.helper.js') -const BillRunVolumeModel = require('../../../app/models/water/bill-run-volume.model.js') -const ChargeCategoryHelper = require('../../support/helpers/water/charge-category.helper.js') -const ChargeCategoryModel = require('../../../app/models/water/charge-category.model.js') -const ChargeElementHelper = require('../../support/helpers/water/charge-element.helper.js') -const ChargeElementModel = require('../../../app/models/water/charge-element.model.js') -const ChargeReferenceHelper = require('../../support/helpers/water/charge-reference.helper.js') -const ChargeVersionHelper = require('../../support/helpers/water/charge-version.helper.js') -const ChargeVersionModel = require('../../../app/models/water/charge-version.model.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') -const PurposeModel = require('../../../app/models/water/purpose.model.js') -const PurposeHelper = require('../../support/helpers/water/purpose.helper.js') -const TransactionHelper = require('../../support/helpers/water/transaction.helper.js') -const TransactionModel = require('../../../app/models/water/transaction.model.js') - -// Thing under test -const ChargeReferenceModel = require('../../../app/models/water/charge-reference.model.js') - -describe('Charge Reference model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - - testRecord = await ChargeReferenceHelper.add() - }) - - describe('Basic query', () => { - it('can successfully run a basic query', async () => { - const result = await ChargeReferenceModel.query().findById(testRecord.chargeElementId) - - expect(result).to.be.an.instanceOf(ChargeReferenceModel) - expect(result.chargeElementId).to.equal(testRecord.chargeElementId) - }) - }) - - describe('Relationships', () => { - describe('when linking to bill run volumes', () => { - let testBillRunVolumes - - beforeEach(async () => { - testRecord = await ChargeReferenceHelper.add() - const { chargeElementId } = testRecord - - testBillRunVolumes = [] - for (let i = 0; i < 2; i++) { - const billRunVolume = await BillRunVolumeHelper.add({ chargeElementId }) - testBillRunVolumes.push(billRunVolume) - } - }) - - it('can successfully run a related query', async () => { - const query = await ChargeReferenceModel.query() - .innerJoinRelated('billRunVolumes') - - expect(query).to.exist() - }) - - it('can eager load the bills', async () => { - const result = await ChargeReferenceModel.query() - .findById(testRecord.chargeElementId) - .withGraphFetched('billRunVolumes') - - expect(result).to.be.instanceOf(ChargeReferenceModel) - expect(result.chargeElementId).to.equal(testRecord.chargeElementId) - - expect(result.billRunVolumes).to.be.an.array() - expect(result.billRunVolumes[0]).to.be.an.instanceOf(BillRunVolumeModel) - expect(result.billRunVolumes).to.include(testBillRunVolumes[0]) - expect(result.billRunVolumes).to.include(testBillRunVolumes[1]) - }) - }) - - describe('when linking to charge category', () => { - let testChargeCategory - - beforeEach(async () => { - testChargeCategory = await ChargeCategoryHelper.add() - - const { billingChargeCategoryId } = testChargeCategory - testRecord = await ChargeReferenceHelper.add({ billingChargeCategoryId }) - }) - - it('can successfully run a related query', async () => { - const query = await ChargeReferenceModel.query() - .innerJoinRelated('chargeCategory') - - expect(query).to.exist() - }) - - it('can eager load the charge category', async () => { - const result = await ChargeReferenceModel.query() - .findById(testRecord.chargeElementId) - .withGraphFetched('chargeCategory') - - expect(result).to.be.instanceOf(ChargeReferenceModel) - expect(result.chargeElementId).to.equal(testRecord.chargeElementId) - - expect(result.chargeCategory).to.be.an.instanceOf(ChargeCategoryModel) - expect(result.chargeCategory).to.equal(testChargeCategory) - }) - }) - - describe('when linking to charge elements', () => { - let testChargeElements - - beforeEach(async () => { - const { chargeElementId } = testRecord - - testChargeElements = [] - for (let i = 0; i < 2; i++) { - const chargeElement = await ChargeElementHelper.add({ description: `CP ${i}`, chargeElementId }) - testChargeElements.push(chargeElement) - } - }) - - it('can successfully run a related query', async () => { - const query = await ChargeReferenceModel.query() - .innerJoinRelated('chargeElements') - - expect(query).to.exist() - }) - - it('can eager load the charge elements', async () => { - const result = await ChargeReferenceModel.query() - .findById(testRecord.chargeElementId) - .withGraphFetched('chargeElements') - - expect(result).to.be.instanceOf(ChargeReferenceModel) - expect(result.chargeElementId).to.equal(testRecord.chargeElementId) - - expect(result.chargeElements).to.be.an.array() - expect(result.chargeElements[0]).to.be.an.instanceOf(ChargeElementModel) - expect(result.chargeElements).to.include(testChargeElements[0]) - expect(result.chargeElements).to.include(testChargeElements[1]) - }) - }) - - describe('when linking to purpose', () => { - let testPurpose - - beforeEach(async () => { - testPurpose = await PurposeHelper.add() - - const { purposeUseId } = testPurpose - testRecord = await ChargeReferenceHelper.add({ purposeUseId }) - }) - - it('can successfully run a related query', async () => { - const query = await ChargeReferenceModel.query() - .innerJoinRelated('purpose') - - expect(query).to.exist() - }) - - it('can eager load the purpose', async () => { - const result = await ChargeReferenceModel.query() - .findById(testRecord.chargeElementId) - .withGraphFetched('purpose') - - expect(result).to.be.instanceOf(ChargeReferenceModel) - expect(result.chargePurposeId).to.equal(testRecord.chargePurposeId) - - expect(result.purpose).to.be.an.instanceOf(PurposeModel) - expect(result.purpose).to.equal(testPurpose) - }) - }) - - describe('when linking to transactions', () => { - let testTransactions - - beforeEach(async () => { - const { chargeElementId } = testRecord - - testTransactions = [] - for (let i = 0; i < 2; i++) { - const transaction = await TransactionHelper.add({ description: `TEST TRANSACTION ${i}`, chargeElementId }) - testTransactions.push(transaction) - } - }) - - it('can successfully run a related query', async () => { - const query = await ChargeReferenceModel.query() - .innerJoinRelated('transactions') - - expect(query).to.exist() - }) - - it('can eager load the transactions', async () => { - const result = await ChargeReferenceModel.query() - .findById(testRecord.chargeElementId) - .withGraphFetched('transactions') - - expect(result).to.be.instanceOf(ChargeReferenceModel) - expect(result.chargeElementId).to.equal(testRecord.chargeElementId) - - expect(result.transactions).to.be.an.array() - expect(result.transactions[0]).to.be.an.instanceOf(TransactionModel) - expect(result.transactions).to.include(testTransactions[0]) - expect(result.transactions).to.include(testTransactions[1]) - }) - }) - - describe('when linking to charge version', () => { - let testChargeVersion - - beforeEach(async () => { - testChargeVersion = await ChargeVersionHelper.add() - - const { chargeVersionId } = testChargeVersion - testRecord = await ChargeReferenceHelper.add({ chargeVersionId }) - }) - - it('can successfully run a related query', async () => { - const query = await ChargeReferenceModel.query() - .innerJoinRelated('chargeVersion') - - expect(query).to.exist() - }) - - it('can eager load the charge version', async () => { - const result = await ChargeReferenceModel.query() - .findById(testRecord.chargeElementId) - .withGraphFetched('chargeVersion') - - expect(result).to.be.instanceOf(ChargeReferenceModel) - expect(result.chargeElementId).to.equal(testRecord.chargeElementId) - - expect(result.chargeVersion).to.be.an.instanceOf(ChargeVersionModel) - expect(result.chargeVersion).to.equal(testChargeVersion) - }) - }) - }) -}) diff --git a/test/models/water/charge-version.model.test.js b/test/models/water/charge-version.model.test.js deleted file mode 100644 index c5b00adb44..0000000000 --- a/test/models/water/charge-version.model.test.js +++ /dev/null @@ -1,137 +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 ChangeReasonHelper = require('../../support/helpers/water/change-reason.helper.js') -const ChangeReasonModel = require('../../../app/models/water/change-reason.model.js') -const ChargeReferenceHelper = require('../../support/helpers/water/charge-reference.helper.js') -const ChargeReferenceModel = require('../../../app/models/water/charge-reference.model.js') -const ChargeVersionHelper = require('../../support/helpers/water/charge-version.helper.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') -const LicenceHelper = require('../../support/helpers/water/licence.helper.js') -const LicenceModel = require('../../../app/models/water/licence.model.js') - -// Thing under test -const ChargeVersionModel = require('../../../app/models/water/charge-version.model.js') - -describe('Charge Version model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - - testRecord = await ChargeVersionHelper.add() - }) - - describe('Basic query', () => { - it('can successfully run a basic query', async () => { - const result = await ChargeVersionModel.query().findById(testRecord.chargeVersionId) - - expect(result).to.be.an.instanceOf(ChargeVersionModel) - expect(result.chargeVersionId).to.equal(testRecord.chargeVersionId) - }) - }) - - describe('Relationships', () => { - describe('when linking to licence', () => { - let testLicence - - beforeEach(async () => { - testLicence = await LicenceHelper.add() - - const { licenceId } = testLicence - testRecord = await ChargeVersionHelper.add({ licenceId }) - }) - - it('can successfully run a related query', async () => { - const query = await ChargeVersionModel.query() - .innerJoinRelated('licence') - - expect(query).to.exist() - }) - - it('can eager load the licence', async () => { - const result = await ChargeVersionModel.query() - .findById(testRecord.chargeVersionId) - .withGraphFetched('licence') - - expect(result).to.be.instanceOf(ChargeVersionModel) - expect(result.chargeVersionId).to.equal(testRecord.chargeVersionId) - - expect(result.licence).to.be.an.instanceOf(LicenceModel) - expect(result.licence).to.equal(testLicence) - }) - }) - - describe('when linking to change reason', () => { - let testChangeReason - - beforeEach(async () => { - testChangeReason = await ChangeReasonHelper.add() - - const { changeReasonId } = testChangeReason - testRecord = await ChargeVersionHelper.add({ changeReasonId }) - }) - - it('can successfully run a related query', async () => { - const query = await ChargeVersionModel.query() - .innerJoinRelated('changeReason') - - expect(query).to.exist() - }) - - it('can eager load the change reason', async () => { - const result = await ChargeVersionModel.query() - .findById(testRecord.chargeVersionId) - .withGraphFetched('changeReason') - - expect(result).to.be.instanceOf(ChargeVersionModel) - expect(result.chargeVersionId).to.equal(testRecord.chargeVersionId) - - expect(result.changeReason).to.be.an.instanceOf(ChangeReasonModel) - expect(result.changeReason).to.equal(testChangeReason) - }) - }) - - describe('when linking to charge references', () => { - let testChargeReferences - - beforeEach(async () => { - const { chargeVersionId } = testRecord - - testChargeReferences = [] - for (let i = 0; i < 2; i++) { - const chargeReference = await ChargeReferenceHelper.add({ description: `CE ${i}`, chargeVersionId }) - testChargeReferences.push(chargeReference) - } - }) - - it('can successfully run a related query', async () => { - const query = await ChargeVersionModel.query() - .innerJoinRelated('chargeReferences') - - expect(query).to.exist() - }) - - it('can eager load the charge references', async () => { - const result = await ChargeVersionModel.query() - .findById(testRecord.chargeVersionId) - .withGraphFetched('chargeReferences') - - expect(result).to.be.instanceOf(ChargeVersionModel) - expect(result.chargeVersionId).to.equal(testRecord.chargeVersionId) - - expect(result.chargeReferences).to.be.an.array() - expect(result.chargeReferences[0]).to.be.an.instanceOf(ChargeReferenceModel) - expect(result.chargeReferences).to.include(testChargeReferences[0]) - expect(result.chargeReferences).to.include(testChargeReferences[1]) - }) - }) - }) -}) diff --git a/test/models/water/event.model.test.js b/test/models/water/event.model.test.js deleted file mode 100644 index ff724b2909..0000000000 --- a/test/models/water/event.model.test.js +++ /dev/null @@ -1,34 +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 DatabaseHelper = require('../../support/helpers/database.helper.js') -const EventHelper = require('../../support/helpers/water/event.helper.js') - -// Thing under test -const EventModel = require('../../../app/models/water/event.model.js') - -describe('Event model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - - testRecord = await EventHelper.add() - }) - - describe('Basic query', () => { - it('can successfully run a basic query', async () => { - const result = await EventModel.query().findById(testRecord.eventId) - - expect(result).to.be.an.instanceOf(EventModel) - expect(result.eventId).to.equal(testRecord.eventId) - }) - }) -}) diff --git a/test/models/water/licence-version.model.test.js b/test/models/water/licence-version.model.test.js deleted file mode 100644 index 9e5ddd6de3..0000000000 --- a/test/models/water/licence-version.model.test.js +++ /dev/null @@ -1,68 +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 DatabaseHelper = require('../../support/helpers/database.helper.js') -const LicenceHelper = require('../../support/helpers/water/licence.helper.js') -const LicenceModel = require('../../../app/models/water/licence.model.js') -const LicenceVersionHelper = require('../../support/helpers/water/licence-version.helper.js') - -// Thing under test -const LicenceVersionModel = require('../../../app/models/water/licence-version.model.js') - -describe('Licence Version model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - - testRecord = await LicenceVersionHelper.add() - }) - - describe('Basic query', () => { - it('can successfully run a basic query', async () => { - const result = await LicenceVersionModel.query().findById(testRecord.licenceVersionId) - - expect(result).to.be.an.instanceOf(LicenceVersionModel) - expect(result.licenceVersionId).to.equal(testRecord.licenceVersionId) - }) - }) - - describe('Relationships', () => { - describe('when linking to licence', () => { - let testLicence - - beforeEach(async () => { - testLicence = await LicenceHelper.add() - - const { licenceId } = testLicence - testRecord = await LicenceVersionHelper.add({ licenceId }) - }) - - it('can successfully run a related query', async () => { - const query = await LicenceVersionModel.query() - .innerJoinRelated('licence') - - expect(query).to.exist() - }) - - it('can eager load the licence', async () => { - const result = await LicenceVersionModel.query() - .findById(testRecord.licenceVersionId) - .withGraphFetched('licence') - - expect(result).to.be.instanceOf(LicenceVersionModel) - expect(result.licenceVersionId).to.equal(testRecord.licenceVersionId) - - expect(result.licence).to.be.an.instanceOf(LicenceModel) - expect(result.licence).to.equal(testLicence) - }) - }) - }) -}) diff --git a/test/models/water/licence.model.test.js b/test/models/water/licence.model.test.js deleted file mode 100644 index 75351c6392..0000000000 --- a/test/models/water/licence.model.test.js +++ /dev/null @@ -1,216 +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 BillLicenceHelper = require('../../support/helpers/water/bill-licence.helper.js') -const BillLicenceModel = require('../../../app/models/water/bill-licence.model.js') -const ChargeVersionHelper = require('../../support/helpers/water/charge-version.helper.js') -const ChargeVersionModel = require('../../../app/models/water/charge-version.model.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') -const LicenceHelper = require('../../support/helpers/water/licence.helper.js') -const LicenceVersionHelper = require('../../support/helpers/water/licence-version.helper.js') -const LicenceVersionModel = require('../../../app/models/water/licence-version.model.js') -const RegionHelper = require('../../support/helpers/water/region.helper.js') -const RegionModel = require('../../../app/models/water/region.model.js') -const WorkflowHelper = require('../../support/helpers/water/workflow.helper.js') -const WorkflowModel = require('../../../app/models/water/workflow.model.js') - -// Thing under test -const LicenceModel = require('../../../app/models/water/licence.model.js') - -describe('Licence model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - - testRecord = await LicenceHelper.add() - }) - - describe('Basic query', () => { - it('can successfully run a basic query', async () => { - const result = await LicenceModel.query().findById(testRecord.licenceId) - - expect(result).to.be.an.instanceOf(LicenceModel) - expect(result.licenceId).to.equal(testRecord.licenceId) - }) - }) - - describe('Relationships', () => { - describe('when linking to charge versions', () => { - let testChargeVersions - - beforeEach(async () => { - const { licenceId, licenceRef } = testRecord - - testChargeVersions = [] - for (let i = 0; i < 2; i++) { - const chargeVersion = await ChargeVersionHelper.add({ licenceRef, licenceId }) - testChargeVersions.push(chargeVersion) - } - }) - - it('can successfully run a related query', async () => { - const query = await LicenceModel.query() - .innerJoinRelated('chargeVersions') - - expect(query).to.exist() - }) - - it('can eager load the charge versions', async () => { - const result = await LicenceModel.query() - .findById(testRecord.licenceId) - .withGraphFetched('chargeVersions') - - expect(result).to.be.instanceOf(LicenceModel) - expect(result.licenceId).to.equal(testRecord.licenceId) - - expect(result.chargeVersions).to.be.an.array() - expect(result.chargeVersions[0]).to.be.an.instanceOf(ChargeVersionModel) - expect(result.chargeVersions).to.include(testChargeVersions[0]) - expect(result.chargeVersions).to.include(testChargeVersions[1]) - }) - }) - - describe('when linking to region', () => { - let testRegion - - beforeEach(async () => { - testRegion = await RegionHelper.add() - - const { regionId } = testRegion - testRecord = await LicenceHelper.add({ regionId }) - }) - - it('can successfully run a related query', async () => { - const query = await LicenceModel.query() - .innerJoinRelated('region') - - expect(query).to.exist() - }) - - it('can eager load the region', async () => { - const result = await LicenceModel.query() - .findById(testRecord.licenceId) - .withGraphFetched('region') - - expect(result).to.be.instanceOf(LicenceModel) - expect(result.licenceId).to.equal(testRecord.licenceId) - - expect(result.region).to.be.an.instanceOf(RegionModel) - expect(result.region).to.equal(testRegion) - }) - }) - - describe('when linking to bill licences', () => { - let testBillLicences - - beforeEach(async () => { - const { licenceId, licenceRef } = testRecord - - testBillLicences = [] - for (let i = 0; i < 2; i++) { - const billLicence = await BillLicenceHelper.add({ licenceRef, licenceId }) - testBillLicences.push(billLicence) - } - }) - - it('can successfully run a related query', async () => { - const query = await LicenceModel.query() - .innerJoinRelated('billLicences') - - expect(query).to.exist() - }) - - it('can eager load the bill licences', async () => { - const result = await LicenceModel.query() - .findById(testRecord.licenceId) - .withGraphFetched('billLicences') - - expect(result).to.be.instanceOf(LicenceModel) - expect(result.licenceId).to.equal(testRecord.licenceId) - - expect(result.billLicences).to.be.an.array() - expect(result.billLicences[0]).to.be.an.instanceOf(BillLicenceModel) - expect(result.billLicences).to.include(testBillLicences[0]) - expect(result.billLicences).to.include(testBillLicences[1]) - }) - }) - - describe('when linking to workflows', () => { - let testWorkflows - - beforeEach(async () => { - const { licenceId } = testRecord - - testWorkflows = [] - for (let i = 0; i < 2; i++) { - const workflow = await WorkflowHelper.add({ licenceId }) - testWorkflows.push(workflow) - } - }) - - it('can successfully run a related query', async () => { - const query = await LicenceModel.query() - .innerJoinRelated('workflows') - - expect(query).to.exist() - }) - - it('can eager load the workflows', async () => { - const result = await LicenceModel.query() - .findById(testRecord.licenceId) - .withGraphFetched('workflows') - - expect(result).to.be.instanceOf(LicenceModel) - expect(result.licenceId).to.equal(testRecord.licenceId) - - expect(result.workflows).to.be.an.array() - expect(result.workflows[0]).to.be.an.instanceOf(WorkflowModel) - expect(result.workflows).to.include(testWorkflows[0]) - expect(result.workflows).to.include(testWorkflows[1]) - }) - }) - - describe('when linking to licence versions', () => { - let testLicenceVersions - - beforeEach(async () => { - const { licenceId } = testRecord - - testLicenceVersions = [] - for (let i = 0; i < 2; i++) { - const licenceVersion = await LicenceVersionHelper.add({ licenceId }) - testLicenceVersions.push(licenceVersion) - } - }) - - it('can successfully run a related query', async () => { - const query = await LicenceModel.query() - .innerJoinRelated('licenceVersions') - - expect(query).to.exist() - }) - - it('can eager load the licence versions', async () => { - const result = await LicenceModel.query() - .findById(testRecord.licenceId) - .withGraphFetched('licenceVersions') - - expect(result).to.be.instanceOf(LicenceModel) - expect(result.licenceId).to.equal(testRecord.licenceId) - - expect(result.licenceVersions).to.be.an.array() - expect(result.licenceVersions[0]).to.be.an.instanceOf(LicenceVersionModel) - expect(result.licenceVersions).to.include(testLicenceVersions[0]) - expect(result.licenceVersions).to.include(testLicenceVersions[1]) - }) - }) - }) -}) diff --git a/test/models/water/purpose.model.test.js b/test/models/water/purpose.model.test.js deleted file mode 100644 index b4475d4d2d..0000000000 --- a/test/models/water/purpose.model.test.js +++ /dev/null @@ -1,110 +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 ChargeElementHelper = require('../../support/helpers/water/charge-element.helper.js') -const ChargeElementModel = require('../../../app/models/water/charge-element.model.js') -const ChargeReferenceHelper = require('../../support/helpers/water/charge-reference.helper.js') -const ChargeReferenceModel = require('../../../app/models/water/charge-reference.model.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') -const PurposeHelper = require('../../support/helpers/water/purpose.helper.js') - -// Thing under test -const PurposeModel = require('../../../app/models/water/purpose.model.js') - -describe('Purpose model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - - testRecord = await PurposeHelper.add() - }) - - describe('Basic query', () => { - it('can successfully run a basic query', async () => { - const result = await PurposeModel.query().findById(testRecord.purposeUseId) - - expect(result).to.be.an.instanceOf(PurposeModel) - expect(result.purposeUseId).to.equal(testRecord.purposeUseId) - }) - }) - - describe('Relationships', () => { - describe('when linking to charge elements', () => { - let testChargeElements - - beforeEach(async () => { - const { purposeUseId } = testRecord - - testChargeElements = [] - for (let i = 0; i < 2; i++) { - const chargeElement = await ChargeElementHelper.add({ purposeUseId }) - testChargeElements.push(chargeElement) - } - }) - - it('can successfully run a related query', async () => { - const query = await PurposeModel.query() - .innerJoinRelated('chargeElements') - - expect(query).to.exist() - }) - - it('can eager load the charge elements', async () => { - const result = await PurposeModel.query() - .findById(testRecord.purposeUseId) - .withGraphFetched('chargeElements') - - expect(result).to.be.instanceOf(PurposeModel) - expect(result.purposeUseId).to.equal(testRecord.purposeUseId) - - expect(result.chargeElements).to.be.an.array() - expect(result.chargeElements[0]).to.be.an.instanceOf(ChargeElementModel) - expect(result.chargeElements).to.include(testChargeElements[0]) - expect(result.chargeElements).to.include(testChargeElements[1]) - }) - }) - - describe('when linking to charge references', () => { - let testChargeReferences - - beforeEach(async () => { - const { purposeUseId } = testRecord - - testChargeReferences = [] - for (let i = 0; i < 2; i++) { - const chargeReference = await ChargeReferenceHelper.add({ purposeUseId }) - testChargeReferences.push(chargeReference) - } - }) - - it('can successfully run a related query', async () => { - const query = await PurposeModel.query() - .innerJoinRelated('chargeReferences') - - expect(query).to.exist() - }) - - it('can eager load the charge references', async () => { - const result = await PurposeModel.query() - .findById(testRecord.purposeUseId) - .withGraphFetched('chargeReferences') - - expect(result).to.be.instanceOf(PurposeModel) - expect(result.purposeUseId).to.equal(testRecord.purposeUseId) - - expect(result.chargeReferences).to.be.an.array() - expect(result.chargeReferences[0]).to.be.an.instanceOf(ChargeReferenceModel) - expect(result.chargeReferences).to.include(testChargeReferences[0]) - expect(result.chargeReferences).to.include(testChargeReferences[1]) - }) - }) - }) -}) diff --git a/test/models/water/region.model.test.js b/test/models/water/region.model.test.js deleted file mode 100644 index 4d5d48edb9..0000000000 --- a/test/models/water/region.model.test.js +++ /dev/null @@ -1,110 +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 BillRunHelper = require('../../support/helpers/water/bill-run.helper.js') -const BillRunModel = require('../../../app/models/water/bill-run.model.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') -const LicenceHelper = require('../../support/helpers/water/licence.helper.js') -const LicenceModel = require('../../../app/models/water/licence.model.js') -const RegionHelper = require('../../support/helpers/water/region.helper.js') - -// Thing under test -const RegionModel = require('../../../app/models/water/region.model.js') - -describe('Region model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - - testRecord = await RegionHelper.add() - }) - - describe('Basic query', () => { - it('can successfully run a basic query', async () => { - const result = await RegionModel.query().findById(testRecord.regionId) - - expect(result).to.be.an.instanceOf(RegionModel) - expect(result.regionId).to.equal(testRecord.regionId) - }) - }) - - describe('Relationships', () => { - describe('when linking to bill runs', () => { - let testBillRuns - - beforeEach(async () => { - const { regionId } = testRecord - - testBillRuns = [] - for (let i = 0; i < 2; i++) { - const billRun = await BillRunHelper.add({ regionId }) - testBillRuns.push(billRun) - } - }) - - it('can successfully run a related query', async () => { - const query = await RegionModel.query() - .innerJoinRelated('billRuns') - - expect(query).to.exist() - }) - - it('can eager load the bill runs', async () => { - const result = await RegionModel.query() - .findById(testRecord.regionId) - .withGraphFetched('billRuns') - - expect(result).to.be.instanceOf(RegionModel) - expect(result.regionId).to.equal(testRecord.regionId) - - expect(result.billRuns).to.be.an.array() - expect(result.billRuns[0]).to.be.an.instanceOf(BillRunModel) - expect(result.billRuns).to.include(testBillRuns[0]) - expect(result.billRuns).to.include(testBillRuns[1]) - }) - }) - - describe('when linking to licences', () => { - let testLicences - - beforeEach(async () => { - const { regionId } = testRecord - - testLicences = [] - for (let i = 0; i < 2; i++) { - const licence = await LicenceHelper.add({ licenceRef: `0${i}/123`, regionId }) - testLicences.push(licence) - } - }) - - it('can successfully run a related query', async () => { - const query = await RegionModel.query() - .innerJoinRelated('licences') - - expect(query).to.exist() - }) - - it('can eager load the licences', async () => { - const result = await RegionModel.query() - .findById(testRecord.regionId) - .withGraphFetched('licences') - - expect(result).to.be.instanceOf(RegionModel) - expect(result.regionId).to.equal(testRecord.regionId) - - expect(result.licences).to.be.an.array() - expect(result.licences[0]).to.be.an.instanceOf(LicenceModel) - expect(result.licences).to.include(testLicences[0]) - expect(result.licences).to.include(testLicences[1]) - }) - }) - }) -}) diff --git a/test/models/water/transaction.model.test.js b/test/models/water/transaction.model.test.js deleted file mode 100644 index b8d6365d94..0000000000 --- a/test/models/water/transaction.model.test.js +++ /dev/null @@ -1,131 +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 BillLicenceModel = require('../../../app/models/water/bill-licence.model.js') -const BillLicenceHelper = require('../../support/helpers/water/bill-licence.helper.js') -const ChargeReferenceHelper = require('../../support/helpers/water/charge-reference.helper.js') -const ChargeReferenceModel = require('../../../app/models/water/charge-reference.model.js') -const DatabaseHelper = require('../../support/helpers/database.helper.js') -const TransactionHelper = require('../../support/helpers/water/transaction.helper.js') - -// Thing under test -const TransactionModel = require('../../../app/models/water/transaction.model.js') - -describe('Transaction model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - - testRecord = await TransactionHelper.add() - }) - - describe('Basic query', () => { - it('can successfully run a basic query', async () => { - const result = await TransactionModel.query().findById(testRecord.billingTransactionId) - - expect(result).to.be.an.instanceOf(TransactionModel) - expect(result.billingTransactionId).to.equal(testRecord.billingTransactionId) - }) - }) - - describe('Relationships', () => { - describe('when linking to bill licence', () => { - let testBillLicence - - beforeEach(async () => { - testBillLicence = await BillLicenceHelper.add() - - const { billingInvoiceLicenceId } = testBillLicence - testRecord = await TransactionHelper.add({ billingInvoiceLicenceId }) - }) - - it('can successfully run a related query', async () => { - const query = await TransactionModel.query() - .innerJoinRelated('billLicence') - - expect(query).to.exist() - }) - - it('can eager load the bill licence', async () => { - const result = await TransactionModel.query() - .findById(testRecord.billingTransactionId) - .withGraphFetched('billLicence') - - expect(result).to.be.instanceOf(TransactionModel) - expect(result.billingTransactionId).to.equal(testRecord.billingTransactionId) - - expect(result.billLicence).to.be.an.instanceOf(BillLicenceModel) - expect(result.billLicence).to.equal(testBillLicence) - }) - }) - - describe('when linking to charge reference', () => { - let testChargeReference - - beforeEach(async () => { - testChargeReference = await ChargeReferenceHelper.add() - - const { chargeElementId } = testChargeReference - testRecord = await TransactionHelper.add({ chargeElementId }) - }) - - it('can successfully run a related query', async () => { - const query = await TransactionModel.query() - .innerJoinRelated('chargeReference') - - expect(query).to.exist() - }) - - it('can eager load the charge reference', async () => { - const result = await TransactionModel.query() - .findById(testRecord.billingTransactionId) - .withGraphFetched('chargeReference') - - expect(result).to.be.instanceOf(TransactionModel) - expect(result.billingTransactionId).to.equal(testRecord.billingTransactionId) - - expect(result.chargeReference).to.be.an.instanceOf(ChargeReferenceModel) - expect(result.chargeReference).to.equal(testChargeReference) - }) - }) - }) - - describe('Inserting', () => { - // Objection doesn't normally allow us to insert an object directly into a json field unless we stringify it first. - // However if we define jsonAttributes in our model with the json fields then we don't need to stringify the object. - // This test is therefore to check whether jsonAttributes is correctly working. - it('can insert an object directly into a json field', async () => { - await expect( - TransactionModel.query().insert({ - ...TransactionHelper.defaults(), - purposes: [{ test: 'TEST' }] - }) - ).to.not.reject() - }) - - it('returns an object from a json field regardless of whether the inserted object was stringified first', async () => { - const objectTransaction = await TransactionModel.query().insert({ - ...TransactionHelper.defaults(), - purposes: [{ test: 'TEST' }] - }) - const stringifyTransaction = await TransactionModel.query().insert({ - ...TransactionHelper.defaults(), - purposes: JSON.stringify([{ test: 'TEST' }]) - }) - - const objectResult = await TransactionModel.query().findById(objectTransaction.billingTransactionId) - const stringifyResult = await TransactionModel.query().findById(stringifyTransaction.billingTransactionId) - - expect(objectResult.purposes).to.equal([{ test: 'TEST' }]) - expect(stringifyResult.purposes).to.equal([{ test: 'TEST' }]) - }) - }) -}) diff --git a/test/models/water/workflow.model.test.js b/test/models/water/workflow.model.test.js deleted file mode 100644 index 779c69db96..0000000000 --- a/test/models/water/workflow.model.test.js +++ /dev/null @@ -1,68 +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 DatabaseHelper = require('../../support/helpers/database.helper.js') -const LicenceHelper = require('../../support/helpers/water/licence.helper.js') -const LicenceModel = require('../../../app/models/water/licence.model.js') -const WorkflowHelper = require('../../support/helpers/water/workflow.helper.js') - -// Thing under test -const WorkflowModel = require('../../../app/models/water/workflow.model.js') - -describe('Workflow model', () => { - let testRecord - - beforeEach(async () => { - await DatabaseHelper.clean() - - testRecord = await WorkflowHelper.add() - }) - - describe('Basic query', () => { - it('can successfully run a basic query', async () => { - const result = await WorkflowModel.query().findById(testRecord.chargeVersionWorkflowId) - - expect(result).to.be.an.instanceOf(WorkflowModel) - expect(result.chargeVersionWorkflowId).to.equal(testRecord.chargeVersionWorkflowId) - }) - }) - - describe('Relationships', () => { - describe('when linking to licence', () => { - let testLicence - - beforeEach(async () => { - testLicence = await LicenceHelper.add() - - const { licenceId } = testLicence - testRecord = await WorkflowHelper.add({ licenceId }) - }) - - it('can successfully run a related query', async () => { - const query = await WorkflowModel.query() - .innerJoinRelated('licence') - - expect(query).to.exist() - }) - - it('can eager load the licence', async () => { - const result = await WorkflowModel.query() - .findById(testRecord.chargeVersionWorkflowId) - .withGraphFetched('licence') - - expect(result).to.be.instanceOf(WorkflowModel) - expect(result.chargeVersionWorkflowId).to.equal(testRecord.chargeVersionWorkflowId) - - expect(result.licence).to.be.an.instanceOf(LicenceModel) - expect(result.licence).to.equal(testLicence) - }) - }) - }) -}) diff --git a/test/support/helpers/water/bill-licence.helper.js b/test/support/helpers/water/bill-licence.helper.js deleted file mode 100644 index ef61c1abcd..0000000000 --- a/test/support/helpers/water/bill-licence.helper.js +++ /dev/null @@ -1,56 +0,0 @@ -'use strict' - -/** - * @module BillLicenceHelper - */ - -const BillLicenceModel = require('../../../../app/models/water/bill-licence.model.js') -const { generateUUID } = require('../../../../app/lib/general.lib.js') -const LicenceHelper = require('./licence.helper.js') - -/** - * Add a new bill licence - * - * If no `data` is provided, default values will be used. These are - * - * - `billingInvoiceId` - [random UUID] - * - `licenceRef` - [randomly generated - 01/123] - * - `licenceId` - [random UUID] - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:BillLicenceModel} The instance of the newly created record - */ -async function add (data = {}) { - const insertData = defaults(data) - - return BillLicenceModel.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 = { - billingInvoiceId: generateUUID(), - licenceRef: LicenceHelper.generateLicenceRef(), - licenceId: generateUUID() - } - - return { - ...defaults, - ...data - } -} - -module.exports = { - add, - defaults -} diff --git a/test/support/helpers/water/bill-run-volume.helper.js b/test/support/helpers/water/bill-run-volume.helper.js deleted file mode 100644 index 8250c35710..0000000000 --- a/test/support/helpers/water/bill-run-volume.helper.js +++ /dev/null @@ -1,57 +0,0 @@ -'use strict' - -/** - * @module BillRunVolumeHelper - */ - -const BillRunVolumeModel = require('../../../../app/models/water/bill-run-volume.model.js') -const { generateUUID } = require('../../../../app/lib/general.lib.js') - -/** - * Add a new bill run volume - * - * If no `data` is provided, default values will be used. These are - * - * - `chargeElementId` - [random UUID] - * - `financialYear` - 2023 - * - `isSummer` - false - * - `billingBatchId` - [random UUID] - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:BillRunModel} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return BillRunVolumeModel.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 = { - chargeElementId: generateUUID(), - financialYear: 2023, - isSummer: false, - billingBatchId: generateUUID() - } - - return { - ...defaults, - ...data - } -} - -module.exports = { - add, - defaults -} diff --git a/test/support/helpers/water/bill-run.helper.js b/test/support/helpers/water/bill-run.helper.js deleted file mode 100644 index da7a8be314..0000000000 --- a/test/support/helpers/water/bill-run.helper.js +++ /dev/null @@ -1,63 +0,0 @@ -'use strict' - -/** - * @module BillRunHelper - */ - -const BillRunModel = require('../../../../app/models/water/bill-run.model.js') -const { generateUUID } = require('../../../../app/lib/general.lib.js') - -/** - * Add a new bill run - * - * If no `data` is provided, default values will be used. These are - * - * - `regionId` - [random UUID] - * - `batchType` - supplementary - * - `fromFinancialYearEnding` - 2023 - * - `toFinancialYearEnding` - 2023 - * - `status` - queued - * - `scheme` - sroc - * - `source` - wrls - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:BillRunModel} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return BillRunModel.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 = { - regionId: generateUUID(), - batchType: 'supplementary', - fromFinancialYearEnding: 2023, - toFinancialYearEnding: 2023, - status: 'queued', - scheme: 'sroc', - source: 'wrls' - } - - 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 deleted file mode 100644 index e8cbba7e0b..0000000000 --- a/test/support/helpers/water/bill.helper.js +++ /dev/null @@ -1,60 +0,0 @@ -'use strict' - -/** - * @module BillingHelper - */ - -const BillModel = require('../../../../app/models/water/bill.model.js') -const { generateUUID } = require('../../../../app/lib/general.lib.js') -const { generateAccountNumber } = require('../billing-account.helper.js') - -/** - * Add a new bill - * - * If no `data` is provided, default values will be used. These are - * - * - `invoiceAccountId` - [random UUID] - * - `address` - {} - * - `invoiceAccountNumber` - [randomly generated - T12345678A] - * - `billingBatchId` - [random UUID] - * - `financialYearEnding` - 2023 - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:BillModel} The instance of the newly created record - */ -async function add (data = {}) { - const insertData = defaults(data) - - return BillModel.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(), - address: {}, - invoiceAccountNumber: generateAccountNumber(), - billingBatchId: generateUUID(), - financialYearEnding: 2023 - } - - return { - ...defaults, - ...data - } -} - -module.exports = { - add, - defaults -} diff --git a/test/support/helpers/water/change-reason.helper.js b/test/support/helpers/water/change-reason.helper.js deleted file mode 100644 index 81220e423e..0000000000 --- a/test/support/helpers/water/change-reason.helper.js +++ /dev/null @@ -1,61 +0,0 @@ -'use strict' - -/** - * @module ChangeReasonHelper - */ - -const ChangeReasonModel = require('../../../../app/models/water/change-reason.model.js') - -/** - * Add a new change reason - * - * If no `data` is provided, default values will be used. These are - * - * - `description` - Strategic review of charges (SRoC) - * - `type` - new_chargeable_charge_version - * - `isEnabledForNewChargeVersions` - true, - * - `createdAt` - 2022-02-23 - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:ChangeReasonModel} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return ChangeReasonModel.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 = { - description: 'Strategic review of charges (SRoC)', - type: 'new_chargeable_charge_version', - isEnabledForNewChargeVersions: true, - // INFO: The change_reasons table does not have a default for the date_created column. But it is set as 'not - // nullable'! So, we need to ensure we set it when creating a new record. Also, we can't use Date.now() because - // Javascript returns the time since the epoch in milliseconds, whereas a PostgreSQL timestamp field can only hold - // the seconds since the epoch. Pass it an ISO string though ('2022-02-23 09:19:39.953') and PostgreSQL can do the - // conversion https://stackoverflow.com/a/61912776/6117745 - createdAt: new Date('2022-02-23 09:19:39.953').toISOString() - } - - return { - ...defaults, - ...data - } -} - -module.exports = { - add, - defaults -} diff --git a/test/support/helpers/water/charge-category.helper.js b/test/support/helpers/water/charge-category.helper.js deleted file mode 100644 index 0816101b0d..0000000000 --- a/test/support/helpers/water/charge-category.helper.js +++ /dev/null @@ -1,86 +0,0 @@ -'use strict' - -/** - * @module ChargeCategoryHelper - */ - -const ChargeCategoryModel = require('../../../../app/models/water/charge-category.model.js') -const { randomInteger } = require('../general.helper.js') - -/** - * Add a new charge category - * - * If no `data` is provided, default values will be used. These are - * - * - `reference` - [randomly generated - 4.4.5] - * - `subsistenceCharge` - 12000 - * - `description` - Low loss non-tidal abstraction of restricted water up to and including 5,000 megalitres a year, - * where a Tier 1 model applies. - * - `shortDescription` - Low loss, non-tidal, restricted water, up to and including 5,000 ML/yr, Tier 1 model - * - `isTidal` - false - * - `lossFactor` - low - * - `modelTier` - tier 1 - * - `isRestrictedSource` - true - * - `minVolume` - 0 - * - `maxVolume` - 5000, - * - `dateCreated` - Date.now() - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:ChargeCategoryModel} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return ChargeCategoryModel.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 = { - reference: generateChargeReference(), - subsistenceCharge: 12000, - description: 'Low loss non-tidal abstraction of restricted water up to and including 5,000 megalitres a year, where a Tier 1 model applies.', - shortDescription: 'Low loss, non-tidal, restricted water, up to and including 5,000 ML/yr, Tier 1 model', - isTidal: false, - lossFactor: 'low', - modelTier: 'tier 1', - isRestrictedSource: true, - minVolume: 0, - maxVolume: 5000, - // INFO: The billing_charge_categories table does not have a default for the date_created column. But it is set as - // 'not nullable'! So, we need to ensure we set it when creating a new record, something we'll never actually need - // to do because it's a static table. Also, we can't use Date.now() because Javascript returns the time since the - // epoch in milliseconds, whereas a PostgreSQL timestamp field can only hold the seconds since the epoch. Pass it - // an ISO string though ('2023-01-05T08:37:05.575Z') and PostgreSQL can do the conversion - // https://stackoverflow.com/a/61912776/6117745 - createdAt: new Date().toISOString() - } - - return { - ...defaults, - ...data - } -} - -function generateChargeReference () { - const secondPart = randomInteger(1, 6) - const thirdPart = randomInteger(1, 42) - - return `4.${secondPart}.${thirdPart}` -} - -module.exports = { - add, - defaults, - generateChargeReference -} diff --git a/test/support/helpers/water/charge-element.helper.js b/test/support/helpers/water/charge-element.helper.js deleted file mode 100644 index 1efaba29d0..0000000000 --- a/test/support/helpers/water/charge-element.helper.js +++ /dev/null @@ -1,81 +0,0 @@ -'use strict' - -/** - * @module ChargeElementHelper - */ - -const ChargeElementModel = require('../../../../app/models/water/charge-element.model.js') -const { generateUUID } = require('../../../../app/lib/general.lib.js') - -/** - * Add a new charge element - * - * If no `data` is provided, default values will be used. These are - * - * - `chargeElementId` - [random UUID] - * - `abstractionPeriodStartDay` - 1 - * - `abstractionPeriodStartMonth` - 4 - * - `abstractionPeriodEndDay` - 31 - * - `abstractionPeriodEndMonth` - 3 - * - `authorisedAnnualQuantity` - 200 - * - `loss` - low - * - `factorsOverridden` - true - * - `billableAnnualQuantity` - 4.55 - * - `timeLimitedStartDate` - 2022-04-01 - * - `timeLimitedEndDate` - 2030-03-30 - * - `description` - Trickle Irrigation - Direct - * - `purposePrimaryId` - [random UUID] - * - `purposeSecondaryId` - [random UUID] - * - `purposeUseId` - [random UUID] - * - `isSection127AgreementEnabled` - true - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:ChargeElementModel} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return ChargeElementModel.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 = { - chargeElementId: generateUUID(), - abstractionPeriodStartDay: 1, - abstractionPeriodStartMonth: 4, - abstractionPeriodEndDay: 31, - abstractionPeriodEndMonth: 3, - authorisedAnnualQuantity: 200, - loss: 'low', - factorsOverridden: true, - billableAnnualQuantity: 4.55, - timeLimitedStartDate: new Date('2022-04-01'), - timeLimitedEndDate: new Date('2030-03-30'), - description: 'Trickle Irrigation - Direct', - purposePrimaryId: generateUUID(), - purposeSecondaryId: generateUUID(), - purposeUseId: generateUUID(), - isSection127AgreementEnabled: true - } - - return { - ...defaults, - ...data - } -} - -module.exports = { - add, - defaults -} diff --git a/test/support/helpers/water/charge-reference.helper.js b/test/support/helpers/water/charge-reference.helper.js deleted file mode 100644 index 5486872019..0000000000 --- a/test/support/helpers/water/charge-reference.helper.js +++ /dev/null @@ -1,83 +0,0 @@ -'use strict' - -/** - * @module ChargeReferenceHelper - */ - -const ChargeReferenceModel = require('../../../../app/models/water/charge-reference.model.js') -const { generateUUID } = require('../../../../app/lib/general.lib.js') - -/** - * Add a new charge reference - * - * If no `data` is provided, default values will be used. These are - * - * - `chargeVersionId` - [random UUID] - * - `source` - non-tidal - * - `loss` - low - * - `description` - Mineral washing - * - `isSection127AgreementEnabled` - true - * - `scheme` - sroc - * - `isRestrictedSource` - true - * - `waterModel` - no model - * - `volume` - 6.819 - * - `billingChargeCategoryId` - [random UUID] - * - `additionalCharges` - { isSupplyPublicWater: true } - * - `adjustments` - { s126: null, s127: false, s130: false, charge: null, winter: false, aggregate: 0.562114443 } - * - `eiucRegion` - Anglian - * - `abstractionPeriodStartDay` - 1 - * - `abstractionPeriodStartMonth` - 1 - * - `abstractionPeriodEndDay` - 31 - * - `abstractionPeriodEndMonth` - 12 - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:ChargeReferenceModel} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return ChargeReferenceModel.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 = { - chargeVersionId: generateUUID(), - source: 'non-tidal', - loss: 'low', - description: 'Mineral washing', - isSection127AgreementEnabled: true, - scheme: 'sroc', - isRestrictedSource: true, - waterModel: 'no model', - volume: 6.819, - billingChargeCategoryId: generateUUID(), - additionalCharges: { isSupplyPublicWater: true }, - adjustments: { s126: null, s127: false, s130: false, charge: null, winter: false, aggregate: '0.562114443' }, - eiucRegion: 'Anglian', - abstractionPeriodStartDay: 1, - abstractionPeriodStartMonth: 1, - abstractionPeriodEndDay: 31, - abstractionPeriodEndMonth: 12 - } - - return { - ...defaults, - ...data - } -} - -module.exports = { - add, - defaults -} diff --git a/test/support/helpers/water/charge-version.helper.js b/test/support/helpers/water/charge-version.helper.js deleted file mode 100644 index b60ecfbc8d..0000000000 --- a/test/support/helpers/water/charge-version.helper.js +++ /dev/null @@ -1,68 +0,0 @@ -'use strict' - -/** - * @module ChargeVersionHelper - */ - -const ChargeVersionModel = require('../../../../app/models/water/charge-version.model.js') -const { generateUUID } = require('../../../../app/lib/general.lib.js') -const { generateLicenceRef } = require('./licence.helper.js') - -/** - * Add a new charge version - * - * If no `data` is provided, default values will be used. These are - * - * - `licenceRef` - [randomly generated - 01/123] - * - `scheme` - sroc - * - `versionNumber` - 1 - * - `startDate` - 2022-04-01 - * - `regionCode` - 1 - * - `source` - wrls - * - `invoiceAccountId` - [random UUID] - * - `status` - current - * - `licenceId` - [random UUID] - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:ChargeVersionModel} The instance of the newly created record - */ -async function add (data = {}) { - const insertData = defaults(data) - - return ChargeVersionModel.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 = { - licenceRef: generateLicenceRef(), - scheme: 'sroc', - versionNumber: 1, - startDate: new Date('2022-04-01'), - regionCode: 1, - source: 'wrls', - invoiceAccountId: generateUUID(), - status: 'current', - licenceId: generateUUID() - } - - return { - ...defaults, - ...data - } -} - -module.exports = { - add, - defaults -} diff --git a/test/support/helpers/water/event.helper.js b/test/support/helpers/water/event.helper.js deleted file mode 100644 index ff16c3f447..0000000000 --- a/test/support/helpers/water/event.helper.js +++ /dev/null @@ -1,82 +0,0 @@ -'use strict' - -/** - * @module EventHelper - */ - -const EventModel = require('../../../../app/models/water/event.model.js') -const { generateUUID, timestampForPostgres } = require('../../../../app/lib/general.lib.js') - -/** - * Add a new event - * - * If no `data` is provided, default values will be used. These are - * - * - `type` - billing-batch - * - `subtype` - supplementary - * - `issuer` - test.user@defra.gov.uk - * - `metadata` - batch: { - id: [random UUID], - type: 'supplementary', - region: { - id: [random UUID] - }, - scheme: 'sroc' - } - } - * - `status` - start - * - `createdAt` - current Date and time as an ISO string - * - `updatedAt` - current Date and time as an ISO string - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:EventModel} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return EventModel.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 timestamp = timestampForPostgres() - - const defaults = { - type: 'billing-batch', - subtype: 'supplementary', - issuer: 'test.user@defra.gov.uk', - metadata: { - batch: { - id: generateUUID(), - type: 'supplementary', - region: { - id: generateUUID() - }, - scheme: 'sroc' - } - }, - status: 'start', - createdAt: timestamp, - updatedAt: timestamp - } - - return { - ...defaults, - ...data - } -} - -module.exports = { - add, - defaults -} diff --git a/test/support/helpers/water/licence-version.helper.js b/test/support/helpers/water/licence-version.helper.js deleted file mode 100644 index 59cd617266..0000000000 --- a/test/support/helpers/water/licence-version.helper.js +++ /dev/null @@ -1,68 +0,0 @@ -'use strict' - -/** - * @module LicenceVersionHelper - */ - -const { generateUUID, timestampForPostgres } = require('../../../../app/lib/general.lib.js') -const { randomInteger } = require('../general.helper.js') -const LicenceVersionModel = require('../../../../app/models/water/licence-version.model.js') - -/** - * Add a new licence version - * - * If no `data` is provided, default values will be used. These are - * - * - `licenceId` - [random UUID] - * - `issue` - 1 - * - `increment` - 0 - * - `status` - 'current' - * - `startDate` - new Date('2022-01-01') - * - `externalId` - [randomly generated - 9:99999:1:0] - * - `createdAt` - new Date() - * - `updatedAt` - new Date() - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:LicenceVersionModel} The instance of the newly created record - */ -async function add (data = {}) { - const insertData = defaults(data) - - return LicenceVersionModel.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 timestamp = timestampForPostgres() - - const defaults = { - licenceId: generateUUID(), - issue: 1, - increment: 0, - status: 'current', - startDate: new Date('2022-01-01'), - externalId: `9:${randomInteger(10000, 99999)}:1:0`, - createdAt: timestamp, - updatedAt: timestamp - } - - return { - ...defaults, - ...data - } -} - -module.exports = { - add, - defaults -} diff --git a/test/support/helpers/water/licence.helper.js b/test/support/helpers/water/licence.helper.js deleted file mode 100644 index d581a8f346..0000000000 --- a/test/support/helpers/water/licence.helper.js +++ /dev/null @@ -1,65 +0,0 @@ -'use strict' - -/** - * @module LicenceHelper - */ - -const { randomInteger } = require('../general.helper.js') -const { generateUUID } = require('../../../../app/lib/general.lib.js') -const LicenceModel = require('../../../../app/models/water/licence.model.js') - -/** - * Add a new licence - * - * If no `data` is provided, default values will be used. These are - * - * - `licenceRef` - [randomly generated - 01/123] - * - `regionId` - [random UUID] - * - `regions` - { historicalAreaCode: 'SAAR', regionalChargeArea: 'Southern' } - * - `startDate` - new Date('2022-01-01') - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:LicenceModel} The instance of the newly created record - */ -async function add (data = {}) { - const insertData = defaults(data) - - return LicenceModel.query() - .insert({ ...insertData }) - .returning('*') -} - -/** - * Returns the defaults used when creating a new licence - * - * 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 = { - licenceRef: generateLicenceRef(), - regionId: generateUUID(), - regions: { historicalAreaCode: 'SAAR', regionalChargeArea: 'Southern' }, - startDate: new Date('2022-01-01') - } - - return { - ...defaults, - ...data - } -} - -function generateLicenceRef () { - const secondPart = randomInteger(100, 999) - - return `01/${secondPart}` -} - -module.exports = { - add, - defaults, - generateLicenceRef -} diff --git a/test/support/helpers/water/purpose.helper.js b/test/support/helpers/water/purpose.helper.js deleted file mode 100644 index 611a0d94d0..0000000000 --- a/test/support/helpers/water/purpose.helper.js +++ /dev/null @@ -1,63 +0,0 @@ -'use strict' - -/** - * @module PurposeHelper - */ - -const { randomInteger } = require('../general.helper.js') -const PurposeModel = require('../../../../app/models/water/purpose.model.js') - -/** - * Add a new purpose - * - * If no `data` is provided, default values will be used. These are - * - * - `legacyId` - [randomly generated - 420] - * - `description` - Spray Irrigation - Storage - * - `lossFactor` - high - * - `isTwoPartTariff` - true - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:PurposeModel} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return PurposeModel.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 = { - legacyId: generateLegacyId(), - description: 'Spray Irrigation - Storage', - lossFactor: 'high', - isTwoPartTariff: true - } - - return { - ...defaults, - ...data - } -} - -function generateLegacyId () { - const numbering = randomInteger(10, 99) - - return `${numbering}0` -} - -module.exports = { - add, - defaults -} diff --git a/test/support/helpers/water/region.helper.js b/test/support/helpers/water/region.helper.js deleted file mode 100644 index f6ec0f465e..0000000000 --- a/test/support/helpers/water/region.helper.js +++ /dev/null @@ -1,64 +0,0 @@ -'use strict' - -/** - * @module RegionHelper - */ - -const { randomInteger } = require('../general.helper.js') -const RegionModel = require('../../../../app/models/water/region.model.js') - -/** - * Add a new region - * - * If no `data` is provided, default values will be used. These are - * - * - `chargeRegionId` - [selected based on randomly generated naldRegionId] - * - `naldRegionId` - [randomly generated - 8] - * - `name` - Kingdom of Avalon - * - `displayName` - Avalon - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:RegionModel} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return RegionModel.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 naldRegionId = randomInteger(1, 8) - const defaults = { - chargeRegionId: _chargeRegionId(naldRegionId), - naldRegionId, - name: 'Kingdom of Avalon', - displayName: 'Avalon' - } - - return { - ...defaults, - ...data - } -} - -function _chargeRegionId (naldRegionId) { - const chargeRegionIds = ['A', 'B', 'Y', 'N', 'E', 'S', 'T', 'W'] - - return chargeRegionIds[naldRegionId - 1] -} - -module.exports = { - add, - defaults -} diff --git a/test/support/helpers/water/transaction.helper.js b/test/support/helpers/water/transaction.helper.js deleted file mode 100644 index 587b807531..0000000000 --- a/test/support/helpers/water/transaction.helper.js +++ /dev/null @@ -1,86 +0,0 @@ -'use strict' - -/** - * @module TransactionHelper - */ - -const { generateChargeReference } = require('./charge-category.helper.js') -const { generateUUID } = require('../../../../app/lib/general.lib.js') -const TransactionModel = require('../../../../app/models/water/transaction.model.js') - -/** - * Add a new transaction - * - * If no `data` is provided, default values will be used. These are - * - * - `adjustmentFactor` - 1 - * - `aggregateFactor` - 1 - * - `authorisedDays` - 365 - * - `authorisedQuantity` - 11 - * - `billableDays` - 365 - * - `billableQuantity` - 11 - * - `billingInvoiceLicenceId` - [random UUID] - * - `chargeCategoryCode` - [randomly generated - 4.4.5] - * - `chargeCategoryDescription` - Medium loss, non-tidal, restricted water, up to and including 25 ML/yr, Tier 2 model - * - `chargeType` - standard, - * - `description` - Water abstraction charge: Agriculture other than spray irrigation at East Rudham - * - `isCredit` - false - * - `loss` - medium - * - `season` - all year - * - `section130Agreement` - false - * - `scheme` - sroc - * - `source` - non-tidal - * - `volume` - 11 - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:TransactionModel} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return TransactionModel.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 = { - adjustmentFactor: 1, - aggregateFactor: 1, - authorisedDays: 365, - authorisedQuantity: 11, - billableDays: 365, - billableQuantity: 11, - billingInvoiceLicenceId: generateUUID(), - chargeCategoryCode: generateChargeReference(), - chargeCategoryDescription: 'Medium loss, non-tidal, restricted water, up to and including 25 ML/yr, Tier 2 model', - chargeType: 'standard', - description: 'Water abstraction charge: Agriculture other than spray irrigation at East Rudham', - isCredit: false, - loss: 'medium', - season: 'all year', - section130Agreement: 'false', - scheme: 'sroc', - source: 'non-tidal', - volume: 11 - } - - return { - ...defaults, - ...data - } -} - -module.exports = { - add, - defaults -} diff --git a/test/support/helpers/water/workflow.helper.js b/test/support/helpers/water/workflow.helper.js deleted file mode 100644 index ddf67129b7..0000000000 --- a/test/support/helpers/water/workflow.helper.js +++ /dev/null @@ -1,62 +0,0 @@ -'use strict' - -/** - * @module WorkflowHelper - */ - -const { generateUUID } = require('../../../../app/lib/general.lib.js') -const WorkflowModel = require('../../../../app/models/water/workflow.model.js') - -/** - * Add a new workflow - * - * If no `data` is provided, default values will be used. These are - * - * - `licenceId` - [random UUID] - * - `status` - to_setup - Other possible values are: changes_requested & review - * - `data` - { chargeVersion: null }, - * - `createdAt` - the current date and time - * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {module:WorkflowModel} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return WorkflowModel.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 = { - licenceId: generateUUID(), - status: 'to_setup', - data: { chargeVersion: null }, - // INFO: The charge_version_workflows table does not have a default for the date_created column. But it is set as - // 'not nullable'! So, we need to ensure we set it when creating a new record. Also, we can't use Date.now() because - // Javascript returns the time since the epoch in milliseconds, whereas a PostgreSQL timestamp field can only hold - // the seconds since the epoch. Pass it an ISO string though ('2022-02-23 09:19:39.953') and PostgreSQL can do the - // conversion https://stackoverflow.com/a/61912776/6117745 - createdAt: new Date().toISOString() - } - - return { - ...defaults, - ...data - } -} - -module.exports = { - add, - defaults -}