generated from DEFRA/ffc-template-node
-
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.
feat: refactor view application route (#34)
* feat: refactor view application route * fix: linting * feat: refactor applications route * feat: refactor applications route * skip: sonar codesmell * fix: missing filter
- Loading branch information
1 parent
3afcbba
commit db8e9f5
Showing
13 changed files
with
279 additions
and
244 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const appRefRegEx = /^vv-[\da-f]{4}-[\da-f]{4}$/i | ||
const validStatus = ['applied', 'withdrawn', 'data inputted', 'claimed', 'check', 'accepted', 'rejected', 'paid'] | ||
const sbiRegEx = /^[\0-9]{9}$/i | ||
|
||
module.exports = (searchText) => { | ||
let searchType | ||
searchText = (searchText ?? '').trim() | ||
switch (true) { | ||
case appRefRegEx.test(searchText): | ||
searchType = 'ref' | ||
break | ||
case validStatus.indexOf(searchText.toLowerCase()) !== -1: | ||
searchType = 'status' | ||
break | ||
case sbiRegEx.test(searchText): | ||
searchType = 'sbi' | ||
break | ||
} | ||
|
||
if (!searchType && searchText.length <= 0) { | ||
searchType = 'reset' | ||
} | ||
if (searchType) { | ||
return { | ||
searchText, | ||
searchType | ||
} | ||
} else { | ||
throw new Error('Invalid search. It should be application reference or status or sbi number.') | ||
} | ||
} |
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,11 @@ | ||
const { formatedDateToUk } = require('../../lib/display-helper') | ||
|
||
module.exports = (updatedAt) => { | ||
const formatedDate = formatedDateToUk(updatedAt) | ||
return { | ||
head: [{ text: 'Date' }, { text: 'Data requested' }, { text: 'Data entered' }], | ||
rows: [ | ||
[{ text: formatedDate }, { text: 'Details correct?' }, { text: 'Yes' }] | ||
] | ||
} | ||
} |
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,98 @@ | ||
const { getApplications } = require('../../api/applications') | ||
const { getPagination, getPagingData } = require('../../pagination') | ||
const { getAppSearch } = require('../../session') | ||
const getStyleClassByStatus = require('../../constants/status') | ||
const keys = require('../../session/keys') | ||
|
||
class ViewModel { | ||
constructor (request, page) { | ||
return (async () => { | ||
this.model = await createModel(request, page) | ||
return this | ||
})() | ||
} | ||
} | ||
|
||
async function createModel (request, page) { | ||
const viewTemplate = 'applications' | ||
const currentPath = `/${viewTemplate}` | ||
page = page ?? request.query.page ?? 1 | ||
const { limit, offset } = getPagination(page) | ||
const path = request.headers.path ?? '' | ||
const searchText = getAppSearch(request, keys.appSearch.searchText) | ||
const searchType = getAppSearch(request, keys.appSearch.searchType) | ||
const filterStatus = getAppSearch(request, keys.appSearch.filterStatus) ?? [] | ||
const apps = await getApplications(searchType, searchText, limit, offset, filterStatus) | ||
if (apps.total > 0) { | ||
let statusClass | ||
const applications = apps.applications.map(n => { | ||
statusClass = getStyleClassByStatus(n.status.status) | ||
return [ | ||
{ text: n.reference }, | ||
{ text: n.data?.organisation?.name }, | ||
{ | ||
text: n.data?.organisation?.sbi, | ||
format: 'numeric', | ||
attributes: { | ||
'data-sort-value': n.data?.organisation?.sbi | ||
} | ||
}, | ||
{ | ||
text: new Date(n.createdAt).toLocaleDateString('en-GB'), | ||
format: 'date', | ||
attributes: { | ||
'data-sort-value': n.createdAt | ||
} | ||
}, | ||
{ | ||
html: `<span class="govuk-tag ${statusClass}">${n.status.status}</span>`, | ||
attributes: { | ||
'data-sort-value': `${n.status.status}` | ||
} | ||
}, | ||
{ html: `<a href="view-application/${n.reference}">View application</a>` } | ||
] | ||
}) | ||
const pagingData = getPagingData(apps.total ?? 0, limit, page, path) | ||
const groupByStatus = apps.applicationStatus.map(s => { | ||
return { | ||
status: s.status, | ||
total: s.total, | ||
styleClass: getStyleClassByStatus(s.status), | ||
selected: filterStatus.filter(f => f === s.status).length > 0 | ||
} | ||
}) | ||
return { | ||
applications, | ||
...pagingData, | ||
searchText, | ||
availableStatus: groupByStatus, | ||
selectedStatus: groupByStatus.filter(s => s.selected === true).map(s => { | ||
return { | ||
href: `${currentPath}/remove/${s.status}`, | ||
classes: s.styleClass, | ||
text: s.status | ||
} | ||
}), | ||
filterStatus: groupByStatus.map(s => { | ||
return { | ||
value: s.status, | ||
html: `<div class="govuk-tag ${s.styleClass}" style="color:#104189;" >${s.status} (${s.total}) </div>`, | ||
checked: s.selected, | ||
styleClass: s.styleClass | ||
} | ||
}) | ||
} | ||
} else { | ||
return { | ||
applications: [], | ||
error: 'No Applications found.', | ||
searchText, | ||
availableStatus: [], | ||
selectedStatus: [], | ||
filterStatus: [] | ||
} | ||
} | ||
} | ||
|
||
module.exports = ViewModel |
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,7 @@ | ||
module.exports = (organisation) => { | ||
return [ | ||
{ key: { text: 'SBI number:' }, value: { text: organisation?.sbi } }, | ||
{ key: { text: 'Address:' }, value: { text: organisation?.address } }, | ||
{ key: { text: 'Email address:' }, value: { text: organisation?.email } } | ||
] | ||
} |
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,23 @@ | ||
const { formatedDateToUk } = require('../../lib/display-helper') | ||
|
||
module.exports = (payment) => { | ||
const { data, createdAt } = payment | ||
const formatedDate = formatedDateToUk(createdAt) | ||
const rows = [] | ||
data.invoiceLines.forEach(invoiceLine => { | ||
rows.push([{ text: formatedDate }, { text: invoiceLine.description }, { text: `£${data?.value?.toFixed(2)}` }]) | ||
}) | ||
|
||
if (data.frn) { | ||
rows.push([{ text: formatedDate }, { text: 'FRN number' }, { text: data.frn }]) | ||
} | ||
|
||
if (data.invoiceNumber) { | ||
rows.push([{ text: formatedDate }, { text: 'Invoice number' }, { text: data.invoiceNumber }]) | ||
} | ||
|
||
return { | ||
head: [{ text: 'Date' }, { text: 'Data requested' }, { text: 'Data entered' }], | ||
rows | ||
} | ||
} |
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,16 @@ | ||
const { formatedDateToUk, upperFirstLetter } = require('../../lib/display-helper') | ||
const speciesNumbers = require('../../../app/constants/species-numbers') | ||
|
||
module.exports = (application) => { | ||
const { data, createdAt } = application | ||
const formatedDate = formatedDateToUk(createdAt) | ||
return { | ||
head: [{ text: 'Date' }, { text: 'Data requested' }, { text: 'Data entered' }], | ||
rows: [ | ||
[{ text: formatedDate }, { text: 'Detail correct?' }, { text: upperFirstLetter(data.confirmCheckDetails) }], | ||
[{ text: formatedDate }, { text: 'Review type' }, { text: upperFirstLetter(data.whichReview) }], | ||
[{ text: formatedDate }, { text: 'Livestock number' }, { text: speciesNumbers[data.whichReview] }], | ||
[{ text: formatedDate }, { text: 'T&Cs agreed?' }, { text: data.declaration ? 'Yes' : 'No' }] | ||
] | ||
} | ||
} |
Oops, something went wrong.