|
| 1 | +const { randomItem } = require('../lib/utils/random-item.js') |
| 2 | +const { dateFromYearMonthDay } = require('../lib/utils/date-from-year-month-day.js') |
| 3 | + |
| 4 | + |
1 | 5 | module.exports = router => { |
2 | 6 |
|
| 7 | + const listOfFirstNames = ["Scott", "Siera", "Ramsey", "Blair", "Gretchen", "Kelli", "Sheridan", "Anya", "Alexis", "Kegan", "Jamia", "Sunny", "Haley", "Elsa", "Ayanna", "Chiara", "Zander", "Oswaldo", "Paris", "Bennett", "Reyna", "Camryn", "Nehemiah", "Craig", "Jalil", "Derick", "Easton", "Mohammed", "Arnold", "Linnea", "Edna", "Cameron", "Gissell", "Melina", "Annalise", "Jalin", "Aric", "Kentrell", "Nyla", "Leslie", "Maranda", "Kinley", "Montana", "Britney", "Uriah", "Raul", "Vincent", "Dustin", "Grant", "Kia"] |
| 8 | + |
| 9 | + const listOfLastNames = ["Menendez", "Salisbury", "Mateo", "Alarcon", "Lenz", "Potter", "Kramer", "Trevino", "Singleton", "Batchelor", "Witte", "Rhoades", "Barragan", "Watson", "Fiore", "Beattie", "Parr", "Traylor", "Gillette", "Kim", "Fennell", "Eubanks", "Ko", "Mcfarlane", "Waite", "Gaines", "Rosado", "Rao", "Bynum", "Wentz", "Cheng", "Loera", "Hyman", "Ferrell", "Nixon", "Pierre", "Strand", "Wirth", "Delagarza", "Dixon", "Yoon", "Hines", "Hinds", "Barron", "Bruce", "Pease", "Rhodes", "Doss", "Marsh", "France"] |
3 | 10 |
|
4 | 11 | router.get('/lists', (req, res) => { |
| 12 | + const data = req.session.data |
| 13 | + const currentOrganisation = res.locals.currentOrganisation |
| 14 | + const lists = data.lists.filter((list) => list.organisationId === currentOrganisation.id) |
5 | 15 |
|
6 | | - const currentOrganisation = res.locals.currentOrganisation; |
7 | | - const lists = currentOrganisation.lists || [] |
| 16 | + const vaccinesAdded = data.vaccineStock.length |
| 17 | + |
| 18 | + if (vaccinesAdded === 0) { return res.render('lists/no-vaccines-added') } |
| 19 | + if (lists.length === 0) { return res.render('lists/no-lists-created') } |
| 20 | + |
| 21 | + const listSiteIds = [...new Set(lists.map((list) => list.siteId))] |
| 22 | + const sites = currentOrganisation.sites.filter((site) => listSiteIds.includes(site.id)) |
8 | 23 |
|
9 | 24 | res.render('lists/index', { |
10 | | - lists |
| 25 | + sites |
| 26 | + }) |
| 27 | + |
| 28 | + }) |
| 29 | + |
| 30 | + router.get('/lists/site/:siteId', (req, res) => { |
| 31 | + const currentOrganisation = res.locals.currentOrganisation |
| 32 | + const siteId = req.params.siteId |
| 33 | + const site = currentOrganisation.sites.find((site) => site.id === siteId) |
| 34 | + const lists = req.session.data.lists.filter((list) => list.organisationId === currentOrganisation.id).filter((list) => list.siteId === siteId) |
| 35 | + |
| 36 | + let justAddedList = null |
| 37 | + if (req.query.justAddedId) { |
| 38 | + justAddedList = req.session.data.lists.find((list) => list.id == req.query.justAddedId) |
| 39 | + } |
| 40 | + |
| 41 | + res.render('lists/team-lists', { |
| 42 | + lists, |
| 43 | + site, |
| 44 | + justAddedList |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + router.get('/lists/team', (req, res) => { |
| 49 | + const data = req.session.data |
| 50 | + let sites = res.locals.currentOrganisation.sites || [] |
| 51 | + |
| 52 | + const vaccineStock = data.vaccineStock |
| 53 | + const siteIdsWithVaccines = [...new Set(vaccineStock.map((vaccineAdded) => vaccineAdded.siteId))] |
| 54 | + |
| 55 | + sites = sites.filter((site) => siteIdsWithVaccines.includes(site.id)) |
| 56 | + |
| 57 | + res.render('lists/team', { |
| 58 | + sites |
| 59 | + }) |
| 60 | + |
| 61 | + }) |
| 62 | + |
| 63 | + router.get('/lists/date', (req, res) => { |
| 64 | + const today = new Date() |
| 65 | + const day = 86400000 // number of milliseconds in a day |
| 66 | + const daysToInclude = 5 |
| 67 | + |
| 68 | + let dates = [] |
| 69 | + |
| 70 | + for (let i = 0; i < daysToInclude; i++) { |
| 71 | + let date = new Date(today.getTime() - (i * day)) |
| 72 | + |
| 73 | + dates.push(date.toISOString().substring(0,10)) |
| 74 | + } |
| 75 | + |
| 76 | + res.render('lists/date', { |
| 77 | + dates |
11 | 78 | }) |
12 | 79 |
|
13 | 80 | }) |
14 | 81 |
|
15 | | - router.post('/lists/add-numbers', (req, res) => { |
| 82 | + router.post('/lists/date-answer', (req, res) => { |
| 83 | + const date = req.session.data.date |
| 84 | + |
| 85 | + if (date === '') { |
| 86 | + |
| 87 | + res.redirect('/lists/name') |
| 88 | + |
| 89 | + } else { |
| 90 | + |
| 91 | + res.redirect('/lists/add-nhs-numbers') |
| 92 | + |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + router.post('/lists/create', (req, res) => { |
| 97 | + const data = req.session.data |
| 98 | + const currentOrganisation = res.locals.currentOrganisation |
16 | 99 |
|
17 | | - const currentOrganisation = res.locals.currentOrganisation; |
18 | | - currentOrganisation.lists ||= [] |
| 100 | + const date = data.date |
| 101 | + const otherDate = data.otherDate |
| 102 | + const name = data.name |
| 103 | + const siteId = data.siteId |
| 104 | + const nhsNumbers = data.nhsNumbers.split(/\n/) |
| 105 | + const id = Math.floor(Math.random() * 10000000).toString() |
19 | 106 |
|
20 | | - // This bit isn’t working yet so faked for now. |
21 | | - currentOrganisation.lists.push({ |
22 | | - siteCode: "TODO" |
| 107 | + // Only 1 of these is set |
| 108 | + let listDate, listName |
| 109 | + |
| 110 | + if (date === "") { |
| 111 | + // Name based list |
| 112 | + listDate = null |
| 113 | + listName = name |
| 114 | + } else { |
| 115 | + // Date based list |
| 116 | + listName = null |
| 117 | + |
| 118 | + if (date === "other-date") { |
| 119 | + listDate = dateFromYearMonthDay(otherDate.year, otherDate.month, otherDate.day).toISOString().substring(0,10) |
| 120 | + } else { |
| 121 | + listDate = date |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + const patients = nhsNumbers.map(function(nhsNumber) { |
| 126 | + return { |
| 127 | + nhsNumber: nhsNumber, |
| 128 | + firstName: randomItem(listOfFirstNames), |
| 129 | + lastName: randomItem(listOfLastNames), |
| 130 | + dateOfBirth: "1945-01-18" |
| 131 | + } |
| 132 | + }) |
| 133 | + |
| 134 | + data.lists.push({ |
| 135 | + id: id, |
| 136 | + organisationId: currentOrganisation.id, |
| 137 | + siteId: siteId, |
| 138 | + date: listDate, |
| 139 | + name: listName, |
| 140 | + patients: patients |
23 | 141 | }) |
24 | 142 |
|
25 | | - res.redirect('/lists/list') |
| 143 | + // reset values |
| 144 | + data.date = null |
| 145 | + data.otherDate = null |
| 146 | + data.name = null |
| 147 | + data.nhsNumbers = null |
| 148 | + |
| 149 | + res.redirect(`/lists/site/${siteId}?justAddedId=${id}`) |
26 | 150 |
|
27 | 151 | }) |
28 | 152 |
|
| 153 | + router.get('/lists/list/:id', (req, res) => { |
| 154 | + const data = req.session.data |
| 155 | + const id = req.params.id |
| 156 | + const q = req.query.q |
| 157 | + const patientList = data.lists.find((list) => list.id === id) |
| 158 | + |
| 159 | + if (!patientList) { return res.redirect('/lists') } |
| 160 | + |
| 161 | + const totalPatients = patientList.patients.length |
| 162 | + |
| 163 | + let patients = patientList.patients |
| 164 | + |
| 165 | + if (q && q !== "") { |
| 166 | + |
| 167 | + patients = patients.filter(function(patient) { |
| 168 | + return ( |
| 169 | + patient.firstName.toLowerCase().startsWith(q.toLowerCase()) || |
| 170 | + patient.lastName.toLowerCase().startsWith(q.toLowerCase()) || |
| 171 | + (patient.firstName + " " + patient.lastName).toLowerCase().startsWith(q.toLowerCase()) || |
| 172 | + patient.nhsNumber === q |
| 173 | + ) |
| 174 | + |
| 175 | + }) |
| 176 | + } |
| 177 | + |
| 178 | + res.render('lists/list', { |
| 179 | + patientList, |
| 180 | + patients, |
| 181 | + totalPatients |
| 182 | + }) |
| 183 | + }) |
| 184 | + |
| 185 | + router.get('/lists/list/:id/edit-name', (req, res) => { |
| 186 | + const data = req.session.data |
| 187 | + const id = req.params.id |
| 188 | + const patientList = data.lists.find((list) => list.id === id) |
| 189 | + |
| 190 | + if (!patientList) { return res.redirect('/lists') } |
| 191 | + |
| 192 | + res.render('lists/edit-name', { |
| 193 | + patientList |
| 194 | + }) |
| 195 | + }) |
| 196 | + |
| 197 | + router.post('/lists/:id/update-name', (req, res) => { |
| 198 | + const data = req.session.data |
| 199 | + const id = req.params.id |
| 200 | + const patientList = data.lists.find((list) => list.id === id) |
| 201 | + |
| 202 | + if (!patientList) { return res.redirect('/lists') } |
| 203 | + |
| 204 | + patientList.name = data.name |
| 205 | + |
| 206 | + res.redirect(`/lists/list/${patientList.id}`) |
| 207 | + }) |
| 208 | + |
| 209 | + router.get('/lists/list/:id/add', (req, res) => { |
| 210 | + const data = req.session.data |
| 211 | + const id = req.params.id |
| 212 | + const patientList = data.lists.find((list) => list.id === id) |
| 213 | + |
| 214 | + if (!patientList) { return res.redirect('/lists') } |
| 215 | + |
| 216 | + res.render('lists/add-more-nhs-numbers', { |
| 217 | + patientList |
| 218 | + }) |
| 219 | + }) |
| 220 | + |
| 221 | + router.post('/lists/:id/nhs-numbers-added', (req, res) => { |
| 222 | + const data = req.session.data |
| 223 | + const id = req.params.id |
| 224 | + const patientList = data.lists.find((list) => list.id === id) |
| 225 | + |
| 226 | + if (!patientList) { return res.redirect('/lists') } |
| 227 | + |
| 228 | + const nhsNumbers = data.nhsNumbers.split(/\n/) |
| 229 | + |
| 230 | + for (nhsNumber of nhsNumbers) { |
| 231 | + patientList.patients.push({ |
| 232 | + nhsNumber: nhsNumber, |
| 233 | + firstName: randomItem(listOfFirstNames), |
| 234 | + lastName: randomItem(listOfLastNames), |
| 235 | + dateOfBirth: "1945-01-18" |
| 236 | + }) |
| 237 | + } |
| 238 | + |
| 239 | + // reset values |
| 240 | + data.date = null |
| 241 | + data.otherDate = {} |
| 242 | + data.name = null |
| 243 | + data.nhsNumbers = null |
| 244 | + |
| 245 | + res.redirect(`/lists/list/${patientList.id}`) |
| 246 | + |
| 247 | + }) |
| 248 | + |
| 249 | + router.get('/lists/list/:id/delete', (req, res) => { |
| 250 | + const data = req.session.data |
| 251 | + const id = req.params.id |
| 252 | + const patientList = data.lists.find((list) => list.id === id) |
| 253 | + |
| 254 | + if (!patientList) { return res.redirect('/lists') } |
| 255 | + |
| 256 | + res.render('lists/delete', { |
| 257 | + patientList |
| 258 | + }) |
| 259 | + }) |
| 260 | + |
| 261 | + router.post('/lists/:id/deleted', (req, res) => { |
| 262 | + const data = req.session.data |
| 263 | + const id = req.params.id |
| 264 | + const patientList = data.lists.find((list) => list.id === id) |
| 265 | + const siteId = patientList.siteId |
| 266 | + |
| 267 | + if (!patientList) { return res.redirect('/lists') } |
| 268 | + |
| 269 | + // Remove item from array using 'splice', which takes an index |
| 270 | + data.lists.splice(data.lists.indexOf(patientList), 1) |
| 271 | + |
| 272 | + res.redirect(`/lists/site/${siteId}?justDeleted=true`) |
| 273 | + |
| 274 | + }) |
| 275 | + |
| 276 | + |
29 | 277 | } |
0 commit comments