-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a note to return submissions (#1662)
https://eaflood.atlassian.net/browse/WATER-4807 Create functionality to allow the user to add/edit/remove a note from a return submission. The link to allow the user to add/edit/remove a note will be on the "Check details and enter new volumes or readings" page. The migrations for the `notes` and `created_by` columns can be found in these PRs: - DEFRA/water-abstraction-returns#406 - #1661
- Loading branch information
Showing
18 changed files
with
993 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict' | ||
|
||
/** | ||
* Formats data for the `/return-logs/setup/{sessionId}/note` page | ||
* @module NotePresenter | ||
*/ | ||
|
||
/** | ||
* Formats data for the `/return-logs/setup/{sessionId}/note` page | ||
* | ||
* @param {module:SessionModel} session - The returns log session instance | ||
* | ||
* @returns {object} The data formatted for the view template | ||
*/ | ||
function go(session) { | ||
const { id: sessionId, note, returnReference } = session | ||
|
||
return { | ||
backLink: `/system/return-logs/setup/${sessionId}/check`, | ||
note: note ? note.content : null, | ||
pageTitle: 'Add a note', | ||
returnReference, | ||
sessionId | ||
} | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict' | ||
|
||
/** | ||
* Deletes the note from the return log currently being setup | ||
* @module DeleteNoteService | ||
*/ | ||
|
||
const SessionModel = require('../../../models/session.model.js') | ||
|
||
/** | ||
* Deletes the note from the return log currently being setup | ||
* | ||
* It first retrieves the session instance for the return log journey in progress. Then it removes the notes | ||
* data from the session. | ||
* | ||
* @param {string} sessionId - The id of the current session | ||
* @param {object} yar - The Hapi `request.yar` session manager passed on by the controller | ||
*/ | ||
async function go(sessionId, yar) { | ||
const session = await SessionModel.query().findById(sessionId) | ||
const notification = { | ||
title: 'Removed', | ||
text: 'Note removed' | ||
} | ||
|
||
yar.flash('notification', notification) | ||
|
||
await _save(session) | ||
} | ||
|
||
async function _save(session) { | ||
delete session.note | ||
|
||
return session.$update() | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict' | ||
|
||
/** | ||
* Orchestrates fetching and presenting the data for `/return-logs/setup/{sessionId}/note` page | ||
* @module NoteService | ||
*/ | ||
|
||
const NotePresenter = require('../../../presenters/return-logs/setup/note.presenter.js') | ||
const SessionModel = require('../../../models/session.model.js') | ||
|
||
/** | ||
* Orchestrates fetching and presenting the data for `/return-logs/setup/{sessionId}/note` page | ||
* | ||
* Supports generating the data needed for the note page in the return log setup journey. It fetches the | ||
* current session record and combines it with textarea information needed for the form. | ||
* | ||
* @param {string} sessionId - The UUID for return log setup session record | ||
* | ||
* @returns {Promise<object>} The view data for the note page | ||
*/ | ||
async function go(sessionId) { | ||
const session = await SessionModel.query().findById(sessionId) | ||
|
||
const formattedData = NotePresenter.go(session) | ||
|
||
return { | ||
activeNavBar: 'search', | ||
...formattedData | ||
} | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
'use strict' | ||
|
||
/** | ||
* Orchestrates validating the data for `/return-logs/setup/{sessionId}/note` page | ||
* @module SubmitNoteService | ||
*/ | ||
|
||
const NotePresenter = require('../../../presenters/return-logs/setup/note.presenter.js') | ||
const NoteValidator = require('../../../validators/return-logs/setup/note.validator.js') | ||
const SessionModel = require('../../../models/session.model.js') | ||
|
||
/** | ||
* Orchestrates validating the data for `/return-logs/setup/{sessionId}/note` page | ||
* | ||
* It first retrieves the session instance for the return log journey in progress. | ||
* | ||
* The validation result is then combined with the output of the presenter to generate the page data needed by the view. | ||
* If there was a validation error the controller will re-render the page so needs this information. If all is well the | ||
* controller will redirect to the next page in the journey. | ||
* | ||
* @param {string} sessionId - The id of the current session | ||
* @param {object} payload - The submitted form data | ||
* @param {object} user - The logged in user details | ||
* @param {object} yar - The Hapi `request.yar` session manager passed on by the controller | ||
* | ||
* @returns {Promise<object>} If no errors it returns an empty object else the page data for the note page including the | ||
* validation error details | ||
*/ | ||
async function go(sessionId, payload, user, yar) { | ||
const session = await SessionModel.query().findById(sessionId) | ||
const validationResult = _validate(payload) | ||
|
||
if (!validationResult) { | ||
const notification = _notification(session, payload.note) | ||
|
||
await _save(session, payload, user) | ||
|
||
if (notification) { | ||
yar.flash('notification', notification) | ||
} | ||
|
||
return {} | ||
} | ||
|
||
const submittedSessionData = _submittedSessionData(session, payload) | ||
|
||
return { | ||
activeNavBar: 'search', | ||
error: validationResult, | ||
...submittedSessionData | ||
} | ||
} | ||
|
||
function _notification(session, newNote) { | ||
const { | ||
data: { note } | ||
} = session | ||
|
||
if (!note && newNote) { | ||
return { | ||
text: 'Note added', | ||
title: 'Added' | ||
} | ||
} | ||
|
||
if (note?.content !== newNote) { | ||
return { | ||
text: 'Note updated', | ||
title: 'Updated' | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
async function _save(session, payload, user) { | ||
session.note = { | ||
content: payload.note, | ||
userEmail: user.username | ||
} | ||
|
||
return session.$update() | ||
} | ||
|
||
function _submittedSessionData(session, payload) { | ||
session.note = payload.note ? payload.note : null | ||
|
||
return NotePresenter.go(session) | ||
} | ||
|
||
function _validate(payload) { | ||
const validation = NoteValidator.go(payload) | ||
|
||
if (!validation.error) { | ||
return null | ||
} | ||
|
||
const { message } = validation.error.details[0] | ||
|
||
return { | ||
text: message | ||
} | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
Oops, something went wrong.