Skip to content

Commit

Permalink
Fix/474687 index access sorting (#349)
Browse files Browse the repository at this point in the history
* fix: 474697 - Change header text on police user list

* fix: 474704 - Handle singular users in list

* fix: 474687 - Update api call for indexAccess

* fix: 474687 - Switch sort order for userlist indexAccess

* chore: patch v0.96.48

---------

Co-authored-by: Chris Cole <whitewaterdesign>
  • Loading branch information
whitewaterdesign authored Nov 15, 2024
1 parent 54da169 commit 9278835
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/api/ddi-index-api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ const getUsers = async (options, callingUser) => {
url.searchParams.append('sortKey', 'policeForce')
sortOrder = options.sort.policeForce
} else if (options.sort?.indexAccess !== undefined) {
url.searchParams.append('sortKey', 'activated')
url.searchParams.append('activated', `${options.sort.indexAccess}`)
url.searchParams.append('sortKey', 'indexAccess')
url.searchParams.append('indexAccessSortOrder', `${options.sort.indexAccess ? 'Y' : 'N'}`)
}

if ([sort.ASC, sort.DESC].includes(sortOrder)) {
Expand Down
3 changes: 2 additions & 1 deletion app/models/admin/users/police/user-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ function ViewModel (details, options, backNav, errors) {

this.model = {
backLink: backNav?.backLink || routes.index.get,
userCount: `${details.count} police officer${details.count > 1 ? 's' : ''}`,
fieldset: {
legend: {
text: 'Police officers with access to the Index',
text: 'Police officer access to the Index',
isPageHeading: true,
classes: 'govuk-fieldset__legend--l govuk-!-margin-bottom-5'
}
Expand Down
16 changes: 12 additions & 4 deletions app/routes/admin/users/police/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const { getPoliceForces } = require('../../../../api/ddi-index-api/police-forces
const { policeOfficerListQuerySchema } = require('../../../../schema/portal/admin/users')
const { sort } = require('../../../../constants/api')

/**
* @param {'indexAccess'|'username'|'policeForce'} key
* @param {'ASC'|'DESC'} order
* @return {'ASC'|'DESC'}
*/
const getSortOrder = (key, order) => {
if (key === 'indexAccess') {
return order !== sort.DESC
}

if (order === sort.DESC) {
return sort.DESC
}
Expand All @@ -30,6 +31,13 @@ const getSortOptions = (key, order) => {
username: getSortOrder('username', order)
}
}

if (key === 'indexAccess') {
return {
indexAccess: order !== sort.ASC
}
}

return {
[key]: getSortOrder(key, order)
}
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/users/police/list.njk
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@


<h2 class="govuk-heading-m">
{{ model.count }} police officers
{{ model.userCount }}
</h2>
<table class="govuk-table defra-sortable-table">
<thead class="govuk-table__head">
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aphw-ddi-portal",
"version": "0.96.47",
"version": "0.96.48",
"description": "Web frontend for managing the dogs index",
"homepage": "https://github.com/DEFRA/aphw-ddi-portal",
"main": "app/index.js",
Expand Down
31 changes: 25 additions & 6 deletions test/integration/narrow/routes/admin/users/police/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,23 @@ describe('Police users page', () => {
count: 3
}

const userListSingle = {
users: [
buildUser({
id: 1,
username: '[email protected]',
policeForceId: 1,
policeForce: 'Dallas Police Department',
accepted: new Date('2024-11-12T00:00:00.000Z'),
active: true,
createdAt: new Date('2024-11-12T00:00:00.000Z'),
lastLogin: new Date('2024-11-12T00:00:00.000Z'),
activated: new Date('2024-11-12T00:00:00.000Z')
})
],
count: 1
}

getPoliceForces.mockResolvedValue([buildPoliceForce({})])

test('should get unfiltered list of police users', async () => {
Expand All @@ -174,7 +191,7 @@ describe('Police users page', () => {

expect(response.statusCode).toBe(200)
expect(getUsers).toHaveBeenCalledWith({}, expect.anything())
expect(document.querySelector('.govuk-fieldset__legend--l').textContent.trim()).toBe('Police officers with access to the Index')
expect(document.querySelector('.govuk-fieldset__legend--l').textContent.trim()).toBe('Police officer access to the Index')
const mainContent = document.querySelector('#main-content')
expect(mainContent.textContent).toContain('Officers by police force')
expect(mainContent.querySelector('.govuk-button').textContent.trim()).toBe('Select police force')
Expand All @@ -200,19 +217,21 @@ describe('Police users page', () => {
expect(getPoliceForces).toHaveBeenCalled()
})

test('should filter police users', async () => {
getUsers.mockResolvedValue(userList)
test('should filter police users and show single user correctly', async () => {
getUsers.mockResolvedValue(userListSingle)
const options = {
method: 'GET',
url: '/admin/users/police/list?policeForce=3',
auth
}

const response = await server.inject(options)
const { document } = new JSDOM(response.payload).window

expect(response.statusCode).toBe(200)

expect(getUsers).toHaveBeenCalledWith({ filter: { policeForceId: 3 } }, expect.anything())

expect(document.querySelector('#main-content h2.govuk-heading-m').textContent.trim()).toBe('1 police officer')
})

test('should not filter police users if empty', async () => {
Expand Down Expand Up @@ -317,7 +336,7 @@ describe('Police users page', () => {

expect(response.statusCode).toBe(200)

expect(getUsers).toHaveBeenCalledWith({ sort: { indexAccess: true } }, expect.anything())
expect(getUsers).toHaveBeenCalledWith({ sort: { indexAccess: false } }, expect.anything())
})

test('should sort by indexAccess descending', async () => {
Expand All @@ -332,7 +351,7 @@ describe('Police users page', () => {

expect(response.statusCode).toBe(200)

expect(getUsers).toHaveBeenCalledWith({ sort: { indexAccess: false } }, expect.anything())
expect(getUsers).toHaveBeenCalledWith({ sort: { indexAccess: true } }, expect.anything())
})

test('should not permit invalid query strings', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/api/ddi-index-api/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,15 +361,15 @@ describe('DDI API users', () => {

await getUsers({ sort: { indexAccess: true } }, user)

expect(get).toHaveBeenCalledWith(expect.objectContaining({ href: 'http://test.com/users?sortKey=activated&activated=true' }), user)
expect(get).toHaveBeenCalledWith(expect.objectContaining({ href: 'http://test.com/users?sortKey=indexAccess&indexAccessSortOrder=Y' }), user)
})

test('should return a list of users sorted by indexAccess No', async () => {
get.mockResolvedValue(usersResponse)

await getUsers({ sort: { indexAccess: false } }, user)

expect(get).toHaveBeenCalledWith(expect.objectContaining({ href: 'http://test.com/users?sortKey=activated&activated=false' }), user)
expect(get).toHaveBeenCalledWith(expect.objectContaining({ href: 'http://test.com/users?sortKey=indexAccess&indexAccessSortOrder=N' }), user)
})

test('should return a list of users called with multiple options', async () => {
Expand Down

0 comments on commit 9278835

Please sign in to comment.