Skip to content

Commit

Permalink
Create water schema models & helpers (#560)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4057

As part of the work we have been doing on two-part tariff we are going to be creating all our new tables in the default `public` schema.

We have also decided that when there is a legacy table that we are still going to need we will create a [View](https://www.postgresql.org/docs/current/sql-createview.html) of it in the `public` schema. This allows us to correct any issues with naming conventions, strip out unused fields, and join entities that are currently sat in different schemas. The first example of this approach was done in PR #531 .

This change adds the models and helpers for the views in the `water` schema that were created in PR #551

> The final step will then be to refactor the existing code to use the new models and delete the old legacy ones.
  • Loading branch information
Jozzey authored Dec 5, 2023
1 parent 77171ed commit 54b7aa9
Show file tree
Hide file tree
Showing 49 changed files with 3,707 additions and 1 deletion.
47 changes: 47 additions & 0 deletions app/models/bill-licence.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

/**
* Model for bill_licences (water.billing_invoice_licences)
* @module BillLicenceModel
*/

const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class BillLicenceModel extends BaseModel {
static get tableName () {
return 'billLicences'
}

static get relationMappings () {
return {
bill: {
relation: Model.BelongsToOneRelation,
modelClass: 'bill.model',
join: {
from: 'billLicences.billId',
to: 'bills.id'
}
},
transactions: {
relation: Model.HasManyRelation,
modelClass: 'transaction.model',
join: {
from: 'billLicences.id',
to: 'transactions.billLicenceId'
}
},
licence: {
relation: Model.BelongsToOneRelation,
modelClass: 'licence.model',
join: {
from: 'billLicences.licenceId',
to: 'licences.id'
}
}
}
}
}

module.exports = BillLicenceModel
67 changes: 67 additions & 0 deletions app/models/bill-run-volume.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict'

/**
* Model for bill_run_volumes (water.billing_volumes)
* @module BillRunVolumeModel
*/

const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class BillRunVolumeModel extends BaseModel {
static get tableName () {
return 'billRunVolumes'
}

static get relationMappings () {
return {
billRun: {
relation: Model.BelongsToOneRelation,
modelClass: 'bill-run.model',
join: {
from: 'billRunVolumes.billRunId',
to: 'billRuns.id'
}
},
chargeReference: {
relation: Model.BelongsToOneRelation,
modelClass: 'charge-reference.model',
join: {
from: 'billRunVolumes.chargeReferenceId',
to: 'chargeReferences.id'
}
}
}
}

// 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
61 changes: 61 additions & 0 deletions app/models/bill-run.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict'

/**
* Model for bill_runs (water.billing_batches)
* @module BillRunModel
*/

const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class BillRunModel extends BaseModel {
static get tableName () {
return 'billRuns'
}

static get relationMappings () {
return {
region: {
relation: Model.BelongsToOneRelation,
modelClass: 'region.model',
join: {
from: 'billRuns.regionId',
to: 'regions.id'
}
},
bills: {
relation: Model.HasManyRelation,
modelClass: 'bill.model',
join: {
from: 'billRuns.id',
to: 'bills.billRunId'
}
},
billRunVolumes: {
relation: Model.HasManyRelation,
modelClass: 'bill-run-volume.model',
join: {
from: 'billRuns.id',
to: 'billRunVolumes.billRunId'
}
}
}
}

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
39 changes: 39 additions & 0 deletions app/models/bill.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

/**
* Model for bills (water.billing_invoices)
* @module BillModel
*/

const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class BillModel extends BaseModel {
static get tableName () {
return 'bills'
}

static get relationMappings () {
return {
billRun: {
relation: Model.BelongsToOneRelation,
modelClass: 'bill-run.model',
join: {
from: 'bills.billRunId',
to: 'billRuns.id'
}
},
billLicences: {
relation: Model.HasManyRelation,
modelClass: 'bill-licence.model',
join: {
from: 'bills.id',
to: 'billLicences.billId'
}
}
}
}
}

module.exports = BillModel
31 changes: 31 additions & 0 deletions app/models/change-reason.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

/**
* Model for change_reasons (water.change_reasons)
* @module ChangeReasonModel
*/

const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class ChangeReasonModel extends BaseModel {
static get tableName () {
return 'changeReasons'
}

static get relationMappings () {
return {
chargeVersions: {
relation: Model.HasManyRelation,
modelClass: 'charge-version.model',
join: {
from: 'changeReasons.id',
to: 'chargeVersions.changeReasonId'
}
}
}
}
}

module.exports = ChangeReasonModel
31 changes: 31 additions & 0 deletions app/models/charge-category.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

/**
* Model for charge_categories (water.billing_charge_categories)
* @module ChargeCategoryModel
*/

const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class ChargeCategoryModel extends BaseModel {
static get tableName () {
return 'chargeCategories'
}

static get relationMappings () {
return {
chargeReferences: {
relation: Model.HasManyRelation,
modelClass: 'charge-reference.model',
join: {
from: 'chargeCategories.id',
to: 'chargeReferences.chargeCategoryId'
}
}
}
}
}

module.exports = ChargeCategoryModel
39 changes: 39 additions & 0 deletions app/models/charge-element.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

/**
* Model for charge_elements (water.charge_purposes)
* @module ChargeElementModel
*/

const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class ChargeElementModel extends BaseModel {
static get tableName () {
return 'chargeElements'
}

static get relationMappings () {
return {
chargeReference: {
relation: Model.BelongsToOneRelation,
modelClass: 'charge-reference.model',
join: {
from: 'chargeElements.chargeReferenceId',
to: 'chargeReferences.id'
}
},
purpose: {
relation: Model.BelongsToOneRelation,
modelClass: 'purpose.model',
join: {
from: 'chargeElements.purposeId',
to: 'purposes.id'
}
}
}
}
}

module.exports = ChargeElementModel
71 changes: 71 additions & 0 deletions app/models/charge-reference.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict'

/**
* Model for charge_references (water.charge_elements)
* @module ChargeReferenceModel
*/

const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class ChargeReferenceModel extends BaseModel {
static get tableName () {
return 'chargeReferences'
}

static get relationMappings () {
return {
billRunVolumes: {
relation: Model.HasManyRelation,
modelClass: 'bill-run-volume.model',
join: {
from: 'chargeReferences.id',
to: 'billRunVolumes.chargeReferenceId'
}
},
chargeVersion: {
relation: Model.BelongsToOneRelation,
modelClass: 'charge-version.model',
join: {
from: 'chargeReferences.chargeVersionId',
to: 'chargeVersions.id'
}
},
chargeCategory: {
relation: Model.BelongsToOneRelation,
modelClass: 'charge-category.model',
join: {
from: 'chargeReferences.chargeCategoryId',
to: 'chargeCategories.id'
}
},
chargeElements: {
relation: Model.HasManyRelation,
modelClass: 'charge-element.model',
join: {
from: 'chargeReferences.id',
to: 'chargeElements.chargeReferenceId'
}
},
purpose: {
relation: Model.BelongsToOneRelation,
modelClass: 'purpose.model',
join: {
from: 'chargeReferences.purposeId',
to: 'purposes.id'
}
},
transactions: {
relation: Model.HasManyRelation,
modelClass: 'transaction.model',
join: {
from: 'chargeReferences.id',
to: 'transactions.chargeReferenceId'
}
}
}
}
}

module.exports = ChargeReferenceModel
Loading

0 comments on commit 54b7aa9

Please sign in to comment.