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

Add copy from existing returns #1056

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
9323181
add copy from existing returns
robertparkinson May 29, 2024
dc6afb4
add unit test
robertparkinson May 30, 2024
91a5e6c
Merge branch 'main' into return-requirements-from-existing-returns
robertparkinson May 30, 2024
0dcb193
add return-version and return-requirement-point models and unit tests
robertparkinson May 30, 2024
364b441
Merge branch 'main' into return-requirements-from-existing-returns
robertparkinson May 30, 2024
07a6cc0
Merge branch 'main' into return-requirements-from-existing-returns
robertparkinson May 31, 2024
da78b1f
Merge branch 'main' into return-requirements-from-existing-returns
robertparkinson May 31, 2024
c6629cd
address review comments
robertparkinson May 31, 2024
75beb8e
Merge branch 'main' into return-requirements-from-existing-returns
robertparkinson May 31, 2024
ef33123
address review comments
robertparkinson May 31, 2024
464992e
Merge branch 'main' into return-requirements-from-existing-returns
robertparkinson May 31, 2024
b9b8936
Merge branch 'main' into return-requirements-from-existing-returns
robertparkinson May 31, 2024
e15bf61
Fix imports not alphabetical
Cruikshanks Jun 4, 2024
6a6b238
Fix comments being too long (120 chars)
Cruikshanks Jun 4, 2024
78564fa
Fix reference to view in model
Cruikshanks Jun 4, 2024
9e1029e
Fix model test titles
Cruikshanks Jun 4, 2024
38a0b50
Fix model test import order
Cruikshanks Jun 4, 2024
5f37be3
Fix incorrect model in JSDoc comment
Cruikshanks Jun 4, 2024
3e866c2
Fix other errors in the Rtn Req helper
Cruikshanks Jun 4, 2024
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
31 changes: 31 additions & 0 deletions app/models/return-requirement-point.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

/**
* Model for return_requirement_points (water.return_requirement_points)
* @module ReturnRequirementPointModel
*/

const { Model } = require('objection')

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

class ReturnRequirementPointModel extends BaseModel {
static get tableName () {
return 'returnRequirementPoints'
}

static get relationMappings () {
return {
returnRequirements: {
relation: Model.BelongsToOneRelation,
modelClass: 'return-requirement.model',
join: {
from: 'returnRequirements.id',
to: 'returnRequirementPoints.returnRequirementId'
}
}
}
}
}

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

/**
* Model for return_requirements (water.return_requirements)
* @module ReturnRequirementModel
*/

const { Model } = require('objection')

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

class ReturnRequirementModel extends BaseModel {
static get tableName () {
return 'returnRequirements'
}

static get relationMappings () {
return {
returnVersions: {
relation: Model.BelongsToOneRelation,
modelClass: 'return-version.model',
join: {
from: 'returnRequirements.returnVersionId',
to: 'returnVersions.id'
}
},
returnRequirementPoints: {
relation: Model.ManyToManyRelation,
modelClass: 'return-requirement-point.model',
join: {
from: 'returnRequirements.id',
to: 'returnRequirementPoints.returnRequirementId'
}
}
}
}
}

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

/**
* Model for return_versions (water.return_versions)
* @module ReturnVersionModel
*/

const { Model } = require('objection')

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

class ReturnVersionModel extends BaseModel {
static get tableName () {
return 'returnVersions'
}

static get relationMappings () {
return {
licence: {
relation: Model.BelongsToOneRelation,
modelClass: 'licence.model',
join: {
from: 'returnVersions.licenceId',
to: 'licences.id'
}
},
returnRequirementPoints: {
relation: Model.ManyToManyRelation,
modelClass: 'return-requirement-point.model',
join: {
from: 'returnVersions.id',
through: {
from: 'returnRequirements.returnVersionId',
to: 'returnRequirements.id'
},
to: 'returnRequirementPoints.returnRequirementId'
}
}
}
}
}

module.exports = ReturnVersionModel
26 changes: 25 additions & 1 deletion app/presenters/return-requirements/setup.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,36 @@
*
* @returns {Object} - The data formatted for the view template
*/
function go (session) {
function go (session, existingData) {
const { id: sessionId, licence, setup } = session

const radioOptions = [{
value: 'use-abstraction-data',
text: 'Start by using abstraction data',
checked: setup === 'use-abstraction-data'
}]

if (existingData.length !== 0) {
radioOptions.push({
value: 'use-existing-requirements',
text: 'Copy existing requirements',
checked: setup === 'use-existing-requirements'
})
}

radioOptions.push({
divider: 'or'
},
{
value: 'set-up-manually',
text: 'Set up manually',
checked: setup === 'set-up-manually'
})

return {
backLink: `/system/return-requirements/${sessionId}/reason`,
licenceRef: licence.licenceRef,
radioOptions,
sessionId,
setup: setup ?? null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

/**
* Fetches existing return requirements needed for `/return-requirements/{sessionId}/setup` page
* @module FetchReturnRequirementsService
*/

const ReturnVersionModel = require('../../models/return-version.model.js')

/**
* Fetches existing return requirements needed for `/return-requirements/{sessionId}/setup` page
*
* @param {string} licenceId - The UUID for the licence to fetch
*
* @returns {Promise<Object>} The existing return requirements for the matching licenceId
*/
async function go (licenceId) {
const data = await _fetchReturnRequirements(licenceId)

return data
}

async function _fetchReturnRequirements (licenceId) {
const result = await ReturnVersionModel.query()
.where('licenceId', licenceId)
.withGraphFetched('returnRequirementPoints')
.modifyGraph('returnRequirementPoints', (builder) => {
robertparkinson marked this conversation as resolved.
Show resolved Hide resolved
builder.select([
'id'
])
})

return result
}

module.exports = {
go
}
11 changes: 7 additions & 4 deletions app/services/return-requirements/setup.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@
* @module SetupService
*/

const SetupPresenter = require('../../presenters/return-requirements/setup.presenter.js')
const FetchReturnRequirementsService = require('./fetch-return-requirements.service.js')
const SessionModel = require('../../models/session.model.js')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nit comment but these two need to be swapped round
const SessionModel = require('../../models/session.model.js')
const SetupPresenter = require('../../presenters/return-requirements/setup.presenter.js')

const SetupPresenter = require('../../presenters/return-requirements/setup.presenter.js')

/**
* Orchestrates fetching and presenting the data for `/return-requirements/{sessionId}/setup` page
*
* Supports generating the data needed for the select reason page in the return requirements setup journey. It
* fetches the current session record and combines it with the radio buttons and other information needed for the form.
* Supports generating the data needed for selecting how you want to set up the return in the return requirements setup
* journey. It fetches the current session record and combines it with the radio buttons and other information needed
* for the form.
*
* @param {string} sessionId - The UUID for return requirement setup session record
*
* @returns {Promise<Object>} page data needed by the view template
*/
async function go (sessionId) {
robertparkinson marked this conversation as resolved.
Show resolved Hide resolved
const session = await SessionModel.query().findById(sessionId)
const existingData = await FetchReturnRequirementsService.go(session.licence.id)

const formattedData = SetupPresenter.go(session)
const formattedData = SetupPresenter.go(session, existingData)

return {
activeNavBar: 'search',
Expand Down
10 changes: 8 additions & 2 deletions app/services/return-requirements/submit-setup.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @module SubmitSetupService
*/

const FetchReturnRequirementsService = require('./fetch-return-requirements.service.js')
const SessionModel = require('../../models/session.model.js')
const SetupPresenter = require('../../presenters/return-requirements/setup.presenter.js')
const SetupValidator = require('../../validators/return-requirements/setup.validator.js')
Expand All @@ -21,11 +22,12 @@ const SetupValidator = require('../../validators/return-requirements/setup.valid
* @param {string} sessionId - The UUID of the current session
* @param {Object} payload - The submitted form data
*
* @returns {Promise<Object>} If no errors a the url for where the user should be redirected else the page data for the
* @returns {Promise<Object>} If no errors return a url for where the user should be redirected else the page data for the
* setup page including the validation error details
*/
async function go (sessionId, payload) {
robertparkinson marked this conversation as resolved.
Show resolved Hide resolved
const session = await SessionModel.query().findById(sessionId)
const existingData = await FetchReturnRequirementsService.go(session.licence.id)

const validationResult = _validate(payload)

Expand All @@ -37,7 +39,7 @@ async function go (sessionId, payload) {
}
}

const formattedData = SetupPresenter.go(session, payload)
const formattedData = SetupPresenter.go(session, existingData)

return {
activeNavBar: 'search',
Expand All @@ -54,6 +56,10 @@ function _redirect (setup) {
endpoint = 'check'
}

if (setup === 'use-existing-requirements') {
endpoint = 'existing'
}

if (setup === 'set-up-manually') {
endpoint = 'purpose/0'
}
Expand Down
1 change: 1 addition & 0 deletions app/validators/return-requirements/setup.validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Joi = require('joi')

const VALID_VALUES = [
'use-abstraction-data',
'use-existing-requirements',
'set-up-manually'
]

Expand Down
16 changes: 1 addition & 15 deletions app/views/return-requirements/setup.njk
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,7 @@
classes: "govuk-fieldset__legend--l govuk-!-margin-bottom-6"
}
},
items: [
{
value: "use-abstraction-data",
text: "Start by using abstraction data",
checked: "use-abstraction-data" === setup
},
{
divider: "or"
},
{
value: "set-up-manually",
text: "Set up manually",
checked: "set-up-manually" === setup
}
]
items: radioOptions
}) }}

{{ govukButton({ text: "Continue", preventDoubleClick: true }) }}
Expand Down
5 changes: 4 additions & 1 deletion db/migrations/legacy/20221108007023-water-return-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ exports.up = function (knex) {
table.integer('version_number').notNullable()
table.date('start_date').notNullable()
table.date('end_date')
table.string('status water').notNullable()
table.string('status').notNullable()
table.string('external_id')
table.string('reason')
table.boolean('multiple_upload')
table.text('notes')

// Legacy timestamps
table.timestamp('date_created', { useTz: false }).notNullable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ exports.up = function (knex) {
table.string('description')
table.integer('legacy_id')
table.string('external_id')
table.string('collection_frequency')
table.boolean('gravity_fill')
table.boolean('reabstraction')
table.boolean('two_part_tariff')
table.boolean('fifty_six_exception')

// Legacy timestamps
table.timestamp('date_created', { useTz: false }).notNullable()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const tableName = 'return_requirement_points'

exports.up = function (knex) {
return knex
.schema
.withSchema('water')
.createTable(tableName, (table) => {
// Primary Key
table.uuid('id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.uuid('return_requirement_id').notNullable()
table.string('description').notNullable()
table.string('ngr_1').notNullable()
table.string('ngr_2')
table.string('ngr_3')
table.string('ngr_4')
table.string('external_id')
})
}

exports.down = function (knex) {
return knex
.schema
.withSchema('water')
.dropTableIfExists(tableName)
}
30 changes: 30 additions & 0 deletions db/migrations/public/20240527105914_water-return-version-view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

const viewName = 'return_versions'

exports.up = function (knex) {
return knex
.schema
.createView(viewName, (view) => {
view.as(knex('return_versions').withSchema('water').select([
'return_version_id AS id',
'licence_id',
'version_number',
'start_date',
'end_date',
'status',
'external_id',
'reason',
'multiple_upload',
'notes',
'date_created AS created_at',
'date_updated AS updated_at'
]))
})
}

exports.down = function (knex) {
return knex
.schema
.dropViewIfExists(viewName)
}
Loading
Loading