Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create water schema models & helpers #560

Merged
merged 19 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading