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

Alter return_logs view #781

Merged
merged 5 commits into from
Mar 4, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function _fetch (licenceRef, billingPeriod) {
const returnLogs = await ReturnLogModel.query()
.select([
'id',
'returnRequirement',
'returnReference',
ref('metadata:description').castText().as('description'),
'startDate',
'endDate',
Expand All @@ -47,7 +47,7 @@ async function _fetch (licenceRef, billingPeriod) {
.where('endDate', '>=', billingPeriod.startDate)
.whereJsonPath('metadata', '$.isTwoPartTariff', '=', true)
.orderBy('startDate', 'ASC')
.orderBy('returnRequirement', 'ASC')
.orderBy('returnReference', 'ASC')
.withGraphFetched('returnSubmissions')
.modifyGraph('returnSubmissions', builder => {
builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async function _persistReviewResult (
async function _persistReviewReturnResult (returnLog) {
const data = {
returnId: returnLog.id,
returnReference: returnLog.returnRequirement,
returnReference: returnLog.returnReference,
startDate: returnLog.startDate,
endDate: returnLog.endDate,
dueDate: returnLog.dueDate,
Expand Down
4 changes: 2 additions & 2 deletions app/services/check/scenario-formatter.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function _formatReturnLogs (returnLogs) {
return returnLogs.map((returnLog, index) => {
const {
id,
returnRequirement: requirement,
returnReference: reference,
description,
startDate,
endDate,
Expand Down Expand Up @@ -213,7 +213,7 @@ function _formatReturnLogs (returnLogs) {
return {
simpleId,
id,
requirement,
reference,
status,
description,
starts: startDate.toLocaleDateString('en-GB'),
Expand Down
63 changes: 63 additions & 0 deletions db/migrations/public/20240301151323_alter-return-logs-view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict'

const viewName = 'return_logs'

exports.up = function (knex) {
return knex
.schema
.dropViewIfExists(viewName)
.createView(viewName, (view) => {
// NOTE: We have commented out unused columns from the source table
view.as(knex('returns').withSchema('returns').select([
'return_id AS id',
// 'regime', // always 'water'
// 'licence_type', // always 'abstraction'
'licence_ref',
'start_date',
'end_date',
'returns_frequency',
'status',
// 'source', // always 'NALD'
'metadata',
'received_date',
'return_requirement as return_reference',
'due_date',
'under_query',
// 'under_query_comment',
// 'is_test',
// 'return_cycle_id' // is populated but links to a table that does not appear to be used
'created_at',
'updated_at'
]))
})
}

exports.down = function (knex) {
return knex
.schema
.dropViewIfExists(viewName)
.createView(viewName, (view) => {
// NOTE: We have commented out unused columns from the source table
view.as(knex('returns').withSchema('returns').select([
'return_id AS id',
// 'regime', // always 'water'
// 'licence_type', // always 'abstraction'
'licence_ref',
'start_date',
'end_date',
'returns_frequency',
'status',
// 'source', // always 'NALD'
'metadata',
'received_date',
'return_requirement',
'due_date',
'under_query',
// 'under_query_comment',
// 'is_test',
// 'return_cycle_id' // is populated but links to a table that does not appear to be used
'created_at',
'updated_at'
]))
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function _generateMatchingReturnsData () {
return [
{
id: 'v1:1:5/31/14/*S/0116A:10021668:2022-04-01:2023-03-31',
returnRequirement: '10021668',
returnReference: '10021668',
description: 'DRAINS ETC-DEEPING FEN AND OTHER LINKED SITES',
startDate: new Date('2022-04-01'),
endDate: new Date('2023-03-31'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('Persist Allocated Licence to Results service', () => {
)

expect(result[0].reviewReturnResults.returnId).to.equal(testLicence.returnLogs[0].id)
expect(result[0].reviewReturnResults.returnReference).to.equal(testLicence.returnLogs[0].returnRequirement)
expect(result[0].reviewReturnResults.returnReference).to.equal(testLicence.returnLogs[0].returnReference)
expect(result[0].reviewReturnResults.startDate).to.equal(testLicence.returnLogs[0].startDate)
expect(result[0].reviewReturnResults.endDate).to.equal(testLicence.returnLogs[0].endDate)
expect(result[0].reviewReturnResults.dueDate).to.equal(testLicence.returnLogs[0].dueDate)
Expand Down Expand Up @@ -106,7 +106,7 @@ describe('Persist Allocated Licence to Results service', () => {
expect(result[0].reviewChargeElementResults).to.be.null()

expect(result[0].reviewReturnResults.returnId).to.equal(testLicence.returnLogs[0].id)
expect(result[0].reviewReturnResults.returnReference).to.equal(testLicence.returnLogs[0].returnRequirement)
expect(result[0].reviewReturnResults.returnReference).to.equal(testLicence.returnLogs[0].returnReference)
expect(result[0].reviewReturnResults.startDate).to.equal(testLicence.returnLogs[0].startDate)
expect(result[0].reviewReturnResults.endDate).to.equal(testLicence.returnLogs[0].endDate)
expect(result[0].reviewReturnResults.dueDate).to.equal(testLicence.returnLogs[0].dueDate)
Expand Down Expand Up @@ -202,7 +202,7 @@ function _generateData (aggregate = null, returnMatched = true) {
returnLogs: [
{
id: returnId,
returnRequirement: '10021668',
returnReference: '10021668',
description: 'DRAINS ETC-DEEPING FEN AND OTHER LINKED SITES',
startDate: new Date('2022-04-01'),
endDate: new Date('2023-03-31'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Prepare Return Logs Service', () => {

expect(licence.returnLogs[0]).to.equal({
id: 'v1:1:01/977:14959864:2022-04-01:2023-03-31',
returnRequirement: '14959864',
returnReference: '14959864',
description: 'The Description',
startDate: new Date('2022-04-01'),
endDate: new Date('2023-03-31'),
Expand Down Expand Up @@ -226,7 +226,7 @@ function _testLicence () {
function _testReturnLog () {
return {
id: 'v1:1:01/977:14959864:2022-04-01:2023-03-31',
returnRequirement: '14959864',
returnReference: '14959864',
description: 'The Description',
startDate: new Date('2022-04-01T00:00:00.000Z'),
endDate: new Date('2023-03-31T00:00:00.000Z'),
Expand Down
18 changes: 9 additions & 9 deletions test/support/helpers/return-log.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ const ReturnLogModel = require('../../../app/models/return-log.model.js')
*
* If no `data` is provided, default values will be used. These are
*
* - `id` - v1:1:[the generated licenceRef]:[the generated returnRequirement]:2022-04-01:2023-03-31
* - `id` - v1:1:[the generated licenceRef]:[the generated returnReference]:2022-04-01:2023-03-31
* - `createdAt` - new Date()
* - `dueDate` - 2023-04-28
* - `endDate` - 2023-03-31
* - `licenceRef` - [randomly generated - 1/23/45/76/3672]
* - `metadata` - {}
* - `receivedDate` - 2023-04-12
* - `returnRequirement` - [randomly generated - 10000321]
* - `returnReference` - [randomly generated - 10000321]
* - `returnsFrequency` - month
* - `startDate` - 2022-04-01
* - `status` - completed
Expand Down Expand Up @@ -49,18 +49,18 @@ function add (data = {}) {
*/
function defaults (data = {}) {
const licenceRef = data.licenceRef ? data.licenceRef : generateLicenceRef()
const returnRequirement = data.returnRequirement ? data.returnRequirement : randomInteger(10000000, 19999999)
const returnReference = data.returnReference ? data.returnReference : randomInteger(10000000, 19999999)
const timestamp = timestampForPostgres()

const defaults = {
id: generateReturnLogId('2022-04-01', '2023-03-31', 1, licenceRef, returnRequirement),
id: generateReturnLogId('2022-04-01', '2023-03-31', 1, licenceRef, returnReference),
createdAt: timestamp,
dueDate: new Date('2023-04-28'),
endDate: new Date('2023-03-31'),
licenceRef,
metadata: {},
receivedDate: new Date('2023-04-12'),
returnRequirement,
returnReference,
returnsFrequency: 'month',
startDate: new Date('2022-04-01'),
status: 'completed',
Expand All @@ -78,17 +78,17 @@ function generateReturnLogId (
endDate = '2023-03-31',
version = 1,
licenceRef,
returnRequirement
returnReference
) {
if (!licenceRef) {
licenceRef = generateLicenceRef()
}

if (!returnRequirement) {
returnRequirement = randomInteger(10000000, 19999999)
if (!returnReference) {
returnReference = randomInteger(10000000, 19999999)
}

return `v${version}:1:${licenceRef}:${returnRequirement}:${startDate}:${endDate}`
return `v${version}:1:${licenceRef}:${returnReference}:${startDate}:${endDate}`
}

module.exports = {
Expand Down
6 changes: 3 additions & 3 deletions test/support/helpers/review-return-result.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const ReviewReturnResultModel = require('../../../app/models/review-return-resul
* If no `data` is provided, default values will be used. These are
*
* - `id` - [random UUID]
* - `returnId` - v1:1:[the generated licenceRef]:[the generated returnRequirement]:2022-04-01:2023-03-31
* - `returnId` - v1:1:[the generated licenceRef]:[the generated returnReference]:2022-04-01:2023-03-31
* - `returnReference` - `10031343`
* - `startDate` - 2022-04-01
* - `endDate` - 2022-05-06
Expand Down Expand Up @@ -53,11 +53,11 @@ function add (data = {}) {
*/
function defaults (data = {}) {
const licenceRef = data.licenceRef ? data.licenceRef : generateLicenceRef()
const returnRequirement = data.returnRequirement ? data.returnRequirement : randomInteger(10000000, 19999999)
const returnReference = data.returnReference ? data.returnReference : randomInteger(10000000, 19999999)

const defaults = {
id: generateUUID(),
returnId: generateReturnLogId('2022-04-01', '2023-03-31', 1, licenceRef, returnRequirement),
returnId: generateReturnLogId('2022-04-01', '2023-03-31', 1, licenceRef, returnReference),
returnReference: '10031343',
startDate: new Date('2022-04-01'),
endDate: new Date('2022-05-06'),
Expand Down
Loading