Skip to content

Commit 930f13f

Browse files
Add setup users and user pagination (#487)
1 parent 47d36d2 commit 930f13f

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

app/lib/utils/random-item.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Returns a random item from an array
3+
* @param {Array} array - The array to select from
4+
* @returns {*} A random item from the array, or null if array is empty/invalid
5+
*/
6+
function randomItem(array)
7+
{
8+
// Handle invalid input
9+
if (!Array.isArray(array) || array.length === 0)
10+
{
11+
return null
12+
}
13+
14+
// Generate random index and return the item
15+
const randomIndex = Math.floor(Math.random() * array.length)
16+
return array[randomIndex]
17+
}
18+
19+
module.exports.randomItem = randomItem

app/routes/helpers.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { randomItem } = require('../lib/utils/random-item.js')
2+
13
module.exports = router => {
24

35
router.get('/prototype-setup/setup-batches', (req, res) => {
@@ -114,4 +116,46 @@ module.exports = router => {
114116
res.redirect('/home')
115117
});
116118

119+
120+
router.post('/prototype-setup/add-users', (req, res) => {
121+
const data = req.session.data
122+
const currentOrganisation = res.locals.currentOrganisation
123+
124+
const listOfFirstNames = ["Susana", "Steven", "Aleah", "Kaylen", "Stephan", "Donavon", "Emely", "Kailee", "Brooks", "Brenton", "Miles", "Emanuel", "Jedidiah", "Glenn", "Jude", "Ivory", "Austen", "Alyson", "Jaime", "Jordin", "Chad", "Janay", "Tahj", "Reginald", "Enoch", "Amiyah", "Benito", "April", "Joelle", "Brant"]
125+
126+
const listOfLastNames = ["Ross", "Friedman", "Switzer", "Devore", "Dominguez", "Kohn", "Moreau", "Farrar", "Hogue", "Goldsmith", "Wilkins", "Cornwell", "Wimberly", "Messer", "Woods", "Forrest", "Aiello", "Kuykendall", "Trout", "Bigelow", "Moreland", "Lentz", "Hurst", "Quinonez", "Pak", "McNally", "Longo", "Hunt", "Villa", "Breaux"]
127+
128+
129+
const usersToAdd = parseInt(data.numberOfUsersToAdd);
130+
131+
const permissionLevels = ["Recorder", "Administrator", "Lead administrator"]
132+
133+
134+
for (let i = 0; i < usersToAdd; i++) {
135+
const generatedId = Math.floor(Math.random() * 10000000).toString()
136+
137+
const generatedEmailId = Math.floor(Math.random() * 10).toString()
138+
139+
const randomFirstName = randomItem(listOfFirstNames)
140+
const randomLastName = randomItem(listOfLastNames)
141+
142+
data.users.push({
143+
id: generatedId,
144+
email: `${randomFirstName.toLowerCase()}.${randomLastName.toLowerCase()}${generatedEmailId}@nhs.net`,
145+
organisations: [
146+
{
147+
id: currentOrganisation.id,
148+
permissionLevel: randomItem(permissionLevels),
149+
status: "Active",
150+
vaccinator: randomItem([true, true, false])
151+
}
152+
],
153+
firstName: randomFirstName,
154+
lastName: randomLastName
155+
})
156+
}
157+
158+
res.redirect('/user-admin')
159+
});
160+
117161
}

app/views/prototype-setup.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ <h1 class="nhsuk-heading-l">Prototype data setup</h1>
1616

1717
<ul class="nhsuk-list">
1818
<li><a href="/prototype-setup/setup-batches">Add batches for all vaccines</a></li>
19+
<li><a href="/prototype-setup/add-users">Add users</a></li>
1920
<li><a href="/prototype-setup/add-vaccinations">Record some vaccinations</a></li>
2021
</ul>
2122

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{% extends 'layout.html' %}
2+
3+
{% set pageName = "Add users" %}
4+
5+
{% block header %}
6+
{% include "includes/header-logged-out.html" %}
7+
{% endblock %}
8+
9+
{% block beforeContent %}
10+
{{ backLink({
11+
text: "Back",
12+
href: "/prototype-setup"
13+
}) }}
14+
{% endblock %}
15+
16+
{% block content %}
17+
<div class="nhsuk-grid-row">
18+
<div class="nhsuk-grid-column-two-thirds">
19+
20+
<form action="/prototype-setup/add-users" method="post" novalidate>
21+
22+
{{ input({
23+
name: "numberOfUsersToAdd",
24+
label: {
25+
text: "How many users would you like to add?",
26+
isPageHeading: true,
27+
classes: "nhsuk-label--l"
28+
},
29+
classes: "nhsuk-input--width-5",
30+
inputmode: "numeric"
31+
}) }}
32+
33+
{{ button({
34+
text: "Add users"
35+
}) }}
36+
37+
</form>
38+
39+
</div>
40+
</div>
41+
42+
{% endblock %}

app/views/user-admin/index.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ <h1 class="nhsuk-heading-xl">Manage users</h1>
6161
{% if q %}
6262
Showing {{ totalUsers | plural("active user") }} matching {{ q }}
6363
{% else %}
64+
65+
{% if totalPages > 1 %}
66+
Showing {{ ((page - 1) * 20) + 1 }} to {{ ((page - 1) * 20) + (users | length) }} of
67+
{% endif %}
6468
{{ totalUsers | plural("active user") }}
6569
{% endif %}
6670
</caption>
@@ -113,6 +117,35 @@ <h1 class="nhsuk-heading-xl">Manage users</h1>
113117
</tbody>
114118
</table>
115119

120+
{% set items = [] %}
121+
122+
{% set ellipsisAdded = false %}
123+
{% for i in range(1, totalPages + 1) -%}
124+
{% if i == 1 or i == (page -1) or (i == page) or (i == page + 1) or (i == totalPages) %}
125+
{% set items = (items.push({
126+
number: i,
127+
href: "/user-admin?page=" + i + "&q=" + (q if q else ""),
128+
current: (i === page)
129+
}), items) %}
130+
{% set ellipsisAdded = false %}
131+
{% elif ellipsisAdded == false %}
132+
{% set items = (items.push({
133+
ellipsis: true
134+
}), items) %}
135+
{% set ellipsisAdded = true %}
136+
{% endif %}
137+
{%- endfor %}
138+
139+
{% if totalPages > 1 %}
140+
141+
142+
{{ appPagination({
143+
previousUrl: "/user-admin?page=" + (page - 1) + "&q=" + (q if q else "") if page != 1,
144+
nextUrl: "/user-admin?page=" + (page + 1) + "&q=" + (q if q else "") if page != totalPages,
145+
items: items
146+
}) }}
147+
{% endif %}
148+
116149
{% else %}
117150

118151
<div class="nhsuk-u-margin-top-6 nhsuk-u-margin-bottom-6">

0 commit comments

Comments
 (0)