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

Match CV data to returns data for 2PT billing #328

Merged
merged 8 commits into from
Aug 2, 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
1 change: 1 addition & 0 deletions app/models/legacy-base.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class LegacyBaseModel extends BaseModel {
const currentPath = __dirname
return [
currentPath,
path.join(currentPath, 'returns'),
path.join(currentPath, 'water'),
path.join(currentPath, 'crm-v2')
]
Expand Down
31 changes: 31 additions & 0 deletions app/models/returns/return.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

/**
* Model for return
* @module ReturnModel
*/

const ReturnsBaseModel = require('./returns-base.model.js')

class ReturnModel extends ReturnsBaseModel {
static get tableName () {
return 'returns'
}

static get idColumn () {
return 'returnId'
}

static get translations () {
return []
}

// Defining which fields contain json allows us to insert an object without needing to stringify it first
static get jsonAttributes () {
return [
'metadata'
]
}
}

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

/**
* Base class for all models based on the legacy 'returns' schema
* @module ReturnsBaseModel
*/

const LegacyBaseModel = require('../legacy-base.model.js')

class ReturnsBaseModel extends LegacyBaseModel {
static get schema () {
return 'returns'
}
}

module.exports = ReturnsBaseModel
34 changes: 22 additions & 12 deletions app/services/check/two-part.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,39 @@

/**
* Used to test the Two-part tariff matching logic
* @module SupplementaryDataService
* @module TwoPartService
*/

const ChargeVersion = require('../../models/water/charge-version.model.js')
const DetermineBillingPeriodsService = require('../billing/determine-billing-periods.service.js')
const FetchRegionService = require('../billing/fetch-region.service.js')
const LicenceModel = require('../../models/water/licence.model.js')
const ReturnModel = require('../../models/returns/return.model.js')

async function go (naldRegionId) {
const region = await FetchRegionService.go(naldRegionId)
const billingPeriods = DetermineBillingPeriodsService.go()

const billingPeriod = billingPeriods[1]

const licenceRefs = await ChargeVersion.query()
.select('licenceRef')
.innerJoinRelated('chargeElements')
.where('regionCode', naldRegionId)
const licences = await LicenceModel.query()
.select('licences.licenceRef', 'licences.licenceId')
.innerJoinRelated('chargeVersions.chargeElements')
.where('chargeVersions.regionCode', naldRegionId)
.where('chargeVersions.scheme', 'sroc')
.where('startDate', '<=', billingPeriod.endDate)
.where('isSection127AgreementEnabled', true)
.whereNot('status', 'draft')
.where('chargeVersions.startDate', '<=', billingPeriod.endDate)
.where('chargeVersions:chargeElements.isSection127AgreementEnabled', true)
.whereNot('chargeVersions.status', 'draft')
.withGraphFetched('chargeVersions.chargeElements.chargePurposes')

return [region.name, billingPeriod, licenceRefs]
for (const licence of licences) {
licence.returns = await ReturnModel.query()
.select('returnId', 'returnRequirement', 'startDate', 'endDate')
.where('licenceRef', licence.licenceRef)
// used in the service to filter out old returns here `src/lib/services/returns/api-connector.js`
.where('startDate', '>=', '2008-04-01')
.where('startDate', '<=', billingPeriod.endDate)
.where('endDate', '>=', billingPeriod.startDate)
}

return licences
}

module.exports = {
Expand Down
13 changes: 13 additions & 0 deletions db/migrations/20230802134043_create_returns_schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

exports.up = function (knex) {
return knex.raw(`
CREATE SCHEMA IF NOT EXISTS "returns";
`)
}

exports.down = function (knex) {
return knex.raw(`
DROP SCHEMA IF EXISTS "returns";
`)
}
42 changes: 42 additions & 0 deletions db/migrations/20230802134153_create_returns_returns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

const tableName = 'returns'

exports.up = async function (knex) {
await knex
.schema
.withSchema('returns')
.createTable(tableName, table => {
// Primary Key
table.string('return_id').primary()

// Data
table.string('regime')
table.string('licence_type')
table.string('licence_ref')
table.date('start_date')
table.date('end_date')
table.string('returns_frequency')
table.string('status')
table.string('source')
table.jsonb('metadata')
table.date('received_date')
table.string('return_requirement')
table.date('due_date')
table.boolean('under_query')
table.string('under_query_comment')
table.boolean('is_test')
table.uuid('return_cycle_id')

// Legacy timestamps
table.timestamp('created_at', { useTz: false }).notNullable().defaultTo(knex.fn.now())
table.timestamp('updated_at', { useTz: false }).notNullable().defaultTo(knex.fn.now())
})
}

exports.down = function (knex) {
return knex
.schema
.withSchema('returns')
.dropTableIfExists(tableName)
}
34 changes: 34 additions & 0 deletions test/models/returns/return.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'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 ReturnHelper = require('../../support/helpers/returns/return.helper.js')

// Thing under test
const ReturnModel = require('../../../app/models/returns/return.model.js')

describe('Return model', () => {
let testRecord

beforeEach(async () => {
await DatabaseHelper.clean()

testRecord = await ReturnHelper.add()
})

describe('Basic query', () => {
it('can successfully run a basic query', async () => {
const result = await ReturnModel.query().findById(testRecord.returnId)

expect(result).to.be.an.instanceOf(ReturnModel)
expect(result.returnId).to.equal(testRecord.returnId)
})
})
})
2 changes: 1 addition & 1 deletion test/support/helpers/database.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const { db } = require('../../../db/db.js')
* identity columns. For example, if a table relies on an incrementing ID the query will reset that to 1.
*/
async function clean () {
const schemas = ['water']
const schemas = ['water', 'returns']

for (const schema of schemas) {
const tables = await _tableNames(schema)
Expand Down
74 changes: 74 additions & 0 deletions test/support/helpers/returns/return.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict'

/**
* @module ReturnHelper
*/

const ReturnModel = require('../../../../app/models/returns/return.model.js')

/**
* Add a new return
*
* If no `data` is provided, default values will be used. These are
*
* - `returnId` - v1:1:9/99/99/99/9999:10021668:2022-04-01:2023-03-31
* - `regime` - water
* - `licenceType` - abstraction
* - `startDate` - 2022-04-01
* - `endDate` - 2023-03-31
* - `returnsFrequency` - month
* - `status` - completed
* - `source` - NALD
* - `metadata` - {}
* - `receivedDate` - 2023-04-12
* - `returnRequirement` - 99999
* - `dueDate` - 2023-04-28
* - `returnCycleId` - 2eb314fe-da45-4ae9-b418-7d89a8c49c51
*
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database
*
* @returns {module:ReturnModel} The instance of the newly created record
*/
function add (data = {}) {
const insertData = defaults(data)

return ReturnModel.query()
.insert({ ...insertData })
.returning('*')
}

/**
* Returns the defaults used when creating a new return
*
* 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 = {
returnId: 'v1:1:9/99/99/99/9999:10021668:2022-04-01:2023-03-31',
regime: 'water',
licenceType: 'abstraction',
startDate: '2022-04-01',
endDate: '2023-03-31',
returnsFrequency: 'month',
status: 'completed',
source: 'NALD',
metadata: {},
receivedDate: '2023-04-12',
returnRequirement: '99999',
dueDate: '2023-04-28',
returnCycleId: '2eb314fe-da45-4ae9-b418-7d89a8c49c51'
}

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

module.exports = {
add,
defaults
}