Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
Co-authored-by: Roble <[email protected]>
  • Loading branch information
Cruikshanks and rvsiyad committed Aug 19, 2024
1 parent 2781d2d commit ba07f67
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
76 changes: 76 additions & 0 deletions app/models/return-version.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
5 changes: 5 additions & 0 deletions app/services/return-requirements/view.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
5 changes: 5 additions & 0 deletions app/views/return-requirements/view.njk
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
</h1>
{{ statusTag(status) }}
</div>
<div>{{ history | dump }}</div>
<div>{{ NALDcreatedBy | dump }}</div>
<div>{{ NALDcreatedAt | dump }}</div>
<div>{{ NALDreason | dump }}</div>
<div>{{ NALDnotes | dump }}</div>

<div class="govuk-!-margin-bottom-9">
<hr class="govuk-section-break govuk-!-margin-bottom-2 govuk-section-break--visible">
Expand Down

0 comments on commit ba07f67

Please sign in to comment.