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 11 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
* @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
* @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
* @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: 'billingVolumes.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
* @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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports.up = function (knex) {
// NOTE: We have commented out unused columns from the source table
view.as(knex('billing_volumes').withSchema('water').select([
'billing_volume_id AS id',
'charge_element_id AS charge_element_id',
'charge_element_id AS charge_reference_id',
'financial_year',
'is_summer AS summer',
'calculated_volume',
Expand Down
56 changes: 56 additions & 0 deletions test/support/helpers/bill-licence.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict'

/**
* @module BillLicenceHelper
*/

const BillLicenceModel = require('../../../app/models/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
*
* - `billId` - [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 = {
billId: generateUUID(),
licenceRef: LicenceHelper.generateLicenceRef(),
licenceId: generateUUID()
}

return {
...defaults,
...data
}
}

module.exports = {
add,
defaults
}
57 changes: 57 additions & 0 deletions test/support/helpers/bill-run-volume.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict'

/**
* @module BillRunVolumeHelper
*/

const BillRunVolumeModel = require('../../../app/models/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
*
* - `chargeReferenceId` - [random UUID]
* - `financialYear` - 2023
* - `summer` - false
* - `billRunId` - [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 = {
chargeReferenceId: generateUUID(),
financialYear: 2023,
summer: false,
billRunId: generateUUID()
}

return {
...defaults,
...data
}
}

module.exports = {
add,
defaults
}
Loading
Loading