diff --git a/app/models/return-version.model.js b/app/models/return-version.model.js index eb6ab5188c..75a9bbcc0f 100644 --- a/app/models/return-version.model.js +++ b/app/models/return-version.model.js @@ -50,6 +50,82 @@ class ReturnVersionModel extends BaseModel { } } } + + /** + * Modifiers allow us to reuse logic in queries, eg. select the licence and everything to get the licence holder: + * + * return LicenceModel.query() + * .findById(licenceId) + * .modify('licenceHolder') + * + * See {@link https://vincit.github.io/objection.js/recipes/modifiers.html | Modifiers} for more details + */ + static get modifiers () { + return { + history (query) { + query + .withGraphFetched('modLogs') + .modifyGraph('modLogs', (builder) => { + builder.select([ + 'naldDate', + 'note', + 'reasonDescription', + 'userId' + ]) + .orderBy('externalId', 'asc') + }) + .withGraphFetched('user') + .modifyGraph('user', (builder) => { + builder.select([ + 'id', + 'username' + ]) + }) + } + } + } + + $createdBy () { + const firstModLog = this._firstModLog() + + return firstModLog?.userId ?? this.user?.username + } + + $createdDate () { + const firstModLog = this._firstModLog() + + return firstModLog?.naldDate ?? this.createdAt + } + + $notes () { + const notes = [] + + for (const modLog of this.modLogs) { + if (modLog.note) { + notes.push(modLog.note) + } + } + + if (this.notes) { + notes.push(this.notes) + } + + return notes + } + + $reason () { + const firstModLog = this._firstModLog() + + return firstModLog?.reasonDescription ?? this.reason + } + + _firstModLog () { + if (this.modLogs.length > 0) { + return this.modLogs[0] + } + + return null + } } module.exports = ReturnVersionModel diff --git a/app/services/return-requirements/fetch-return-version.service.js b/app/services/return-requirements/fetch-return-version.service.js index 6c6ae68ce8..2b51b11e28 100644 --- a/app/services/return-requirements/fetch-return-version.service.js +++ b/app/services/return-requirements/fetch-return-version.service.js @@ -33,13 +33,7 @@ async function _fetch (returnVersionId) { 'startDate', 'status' ]) - .withGraphFetched('user') - .modifyGraph('user', (builder) => { - builder.select([ - 'id', - 'username' - ]) - }) + .modify('history') .withGraphFetched('licence') .modifyGraph('licence', (builder) => { builder.select([ diff --git a/app/services/return-requirements/view.service.js b/app/services/return-requirements/view.service.js index 0fe02c5b12..24d6fde881 100644 --- a/app/services/return-requirements/view.service.js +++ b/app/services/return-requirements/view.service.js @@ -22,6 +22,11 @@ async function go (returnVersionId) { return { activeNavBar: 'search', + history: requirementsForReturns.modLogs, + NALDcreatedBy: requirementsForReturns.$createdBy(), + NALDcreatedAt: requirementsForReturns.$createdDate(), + NALDreason: requirementsForReturns.$reason(), + NALDnotes: requirementsForReturns.$notes(), ...data } } diff --git a/app/views/return-requirements/view.njk b/app/views/return-requirements/view.njk index 12659f09f7..c2fb84c0ff 100644 --- a/app/views/return-requirements/view.njk +++ b/app/views/return-requirements/view.njk @@ -26,6 +26,11 @@ {{ statusTag(status) }} +