Skip to content

Commit c8a93dc

Browse files
Add a patient record from PDS
1 parent 921a024 commit c8a93dc

16 files changed

Lines changed: 737 additions & 4 deletions

File tree

app/controllers/pds-record.js

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import wizard from '@x-govuk/govuk-prototype-wizard'
2+
import _ from 'lodash'
3+
4+
import { generateChild } from '../generators/child.js'
5+
import { Patient, PDSRecord } from '../models.js'
6+
import { getResults, getPagination } from '../utils/pagination.js'
7+
8+
export const pdsRecordController = {
9+
redirect(request, response) {
10+
response.redirect('/patients')
11+
},
12+
13+
start(request, response) {
14+
const { data } = request.session
15+
16+
if (request.body.nhsn) {
17+
const child = generateChild()
18+
const pdsRecord = new PDSRecord({ ...child }, data)
19+
20+
// Add entered NHS number
21+
pdsRecord.nhsn = request.body.nhsn.replaceAll(' ', '')
22+
23+
// Add PDS record to wizard data
24+
PDSRecord.create(pdsRecord, data.wizard)
25+
26+
response.redirect(`/pds/${pdsRecord.uuid}/new/result`)
27+
} else {
28+
response.redirect(`/pds/new/search`)
29+
}
30+
},
31+
32+
read(request, response, next, pdsRecord_uuid) {
33+
const { data } = request.session
34+
35+
response.locals.pdsRecord = PDSRecord.findOne(pdsRecord_uuid, data)
36+
37+
next()
38+
},
39+
40+
update(request, response) {
41+
const { pdsRecord_uuid } = request.params
42+
const { data } = request.session
43+
const { __ } = response.locals
44+
45+
// Update session data
46+
const pdsRecord = PDSRecord.update(
47+
pdsRecord_uuid,
48+
data.wizard.pdsRecords[pdsRecord_uuid],
49+
data.wizard
50+
)
51+
52+
// Create patient record
53+
let patient = new Patient(pdsRecord, data)
54+
patient = Patient.create(patient, data)
55+
56+
// Clean up session data
57+
delete data.hasNhsNumber
58+
delete data.nhs
59+
delete data.school_id
60+
delete data.pdsRecord
61+
delete data.wizard
62+
63+
request.flash('success', __(`pdsRecord.new.success`, { patient }))
64+
65+
response.redirect(patient.uri)
66+
},
67+
68+
readAll(request, response, next) {
69+
const { q } = request.query
70+
const { data } = request.session
71+
72+
const pdsRecords = PDSRecord.findAll(data)
73+
74+
// Sort
75+
let results = _.sortBy(pdsRecords, 'lastName')
76+
77+
// Query
78+
if (q) {
79+
results = results.filter((pdsRecord) =>
80+
pdsRecord.tokenized.includes(String(q).toLowerCase())
81+
)
82+
}
83+
84+
// Results
85+
response.locals.pdsRecords = pdsRecords
86+
response.locals.results = getResults(results, request.query)
87+
response.locals.pages = getPagination(results, request.query)
88+
89+
// Clean up session data
90+
delete data.q
91+
92+
next()
93+
},
94+
95+
readForm(request, response, next) {
96+
const { pdsRecord_uuid } = request.params
97+
const { data, referrer } = request.session
98+
99+
// Setup wizard if not already setup
100+
let pdsRecord = PDSRecord.findOne(pdsRecord_uuid, data.wizard)
101+
if (!pdsRecord) {
102+
pdsRecord = PDSRecord.create(response.locals.pdsRecord, data.wizard)
103+
}
104+
response.locals.pdsRecord = new PDSRecord(pdsRecord, data)
105+
106+
const journey = {
107+
['/']: {},
108+
['/new/start']: {
109+
[`/${pdsRecord_uuid}/new/result`]: {
110+
data: 'hasNhsNumber',
111+
value: 'true'
112+
},
113+
['/new/search']: {
114+
data: 'hasNhsNumber',
115+
value: 'false'
116+
}
117+
},
118+
['/new/search']: {},
119+
['/new/results']: {},
120+
[`/${pdsRecord_uuid}/new/result`]: {
121+
[`/${pdsRecord_uuid}/new/school`]: {
122+
data: 'add',
123+
value: 'true'
124+
},
125+
['/new/search']: {
126+
data: 'add',
127+
value: 'false'
128+
}
129+
},
130+
[`/${pdsRecord_uuid}/new/school`]: {}
131+
}
132+
133+
response.locals.paths = {
134+
...wizard(journey, request),
135+
...(referrer && { back: referrer })
136+
}
137+
138+
next()
139+
},
140+
141+
showForm(request, response) {
142+
let { view } = request.params
143+
144+
response.render(`pds/form/${view}`)
145+
},
146+
147+
updateForm(request, response, next) {
148+
const { pdsRecord_uuid } = request.params
149+
const { data } = request.session
150+
const { paths } = response.locals
151+
152+
if (request.body.school_id && !request.body.pdsRecord?.school_id) {
153+
request.body.pdsRecord = {
154+
school_id: request.body.school_id
155+
}
156+
}
157+
158+
PDSRecord.update(pdsRecord_uuid, request.body.pdsRecord, data.wizard)
159+
160+
return paths?.next ? response.redirect(paths.next) : next()
161+
}
162+
}

app/data.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import moves from '../.data/moves.json' with { type: 'json' }
88
import notices from '../.data/notices.json' with { type: 'json' }
99
import patients from '../.data/patients.json' with { type: 'json' }
1010
import patientSessions from '../.data/patient-sessions.json' with { type: 'json' }
11+
import pdsRecords from '../.data/pds-records.json' with { type: 'json' }
1112
import programmes from '../.data/programmes.json' with { type: 'json' }
1213
import replies from '../.data/replies.json' with { type: 'json' }
1314
import schools from '../.data/schools.json' with { type: 'json' }
@@ -44,6 +45,7 @@ const data = {
4445
notices,
4546
patients,
4647
patientSessions,
48+
pdsRecords,
4749
programmes,
4850
replies,
4951
schools,

app/generators/pds-record.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { fakerEN_GB as faker } from '@faker-js/faker'
2+
3+
import { PDSRecord } from '../models.js'
4+
5+
import { generateChild } from './child.js'
6+
import { generateParent } from './parent.js'
7+
8+
/**
9+
* Generate fake PDS record
10+
*
11+
* @returns {PDSRecord} PDS record
12+
*/
13+
export function generatePDSRecord() {
14+
const child = generateChild()
15+
16+
// Parents
17+
const parent1 = generateParent(child.lastName, true)
18+
19+
// PDS records provide only a subset of parent data
20+
delete parent1.sms
21+
delete parent1.contactPreference
22+
delete parent1.contactPreferenceDetails
23+
24+
let parent2
25+
const addSecondParent = faker.datatype.boolean(0.5)
26+
if (addSecondParent) {
27+
parent2 = generateParent(child.lastName)
28+
29+
// PDS records provide only a subset of parent data
30+
delete parent2.sms
31+
delete parent2.contactPreference
32+
delete parent2.contactPreferenceDetails
33+
}
34+
35+
return new PDSRecord({
36+
...child,
37+
parent1,
38+
parent2
39+
})
40+
}

app/locales/en.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,97 @@ export const en = {
16371637
}
16381638
}
16391639
},
1640+
pdsRecord: {
1641+
label: 'Child record',
1642+
new: {
1643+
success:
1644+
'{{patient.fullName}} has been added to your list of children in Mavis'
1645+
},
1646+
start: {
1647+
label: 'Add a new child',
1648+
title: 'Do you have the child’s NHS number?'
1649+
},
1650+
search: {
1651+
title: 'Search for a child'
1652+
},
1653+
results:
1654+
'{count, plural, =0 {No children matching your search criteria were found} one {Showing <b>{from}</b> to <b>{to}</b> of <b>{count}</b> record} other {Showing <b>{from}</b> to <b>{to}</b> of <b>{count}</b> children}}',
1655+
result: {
1656+
title: 'Check and confirm %s’s details'
1657+
},
1658+
school: {
1659+
title: 'Do you know which school %s goes to?',
1660+
yes: {
1661+
label: 'Yes'
1662+
},
1663+
unknown: {
1664+
label: 'No'
1665+
},
1666+
'home-educated': {
1667+
label: 'They are home-educated'
1668+
}
1669+
},
1670+
school_id: {
1671+
label: 'School URN',
1672+
title: 'Select a school'
1673+
},
1674+
nhsn: {
1675+
label: 'NHS number',
1676+
title: 'Enter the child’s NHS number',
1677+
hint: 'For example, 485 777 3456'
1678+
},
1679+
fullName: {
1680+
label: 'Full name'
1681+
},
1682+
firstName: {
1683+
label: 'First name'
1684+
},
1685+
lastName: {
1686+
label: 'Last name'
1687+
},
1688+
dob: {
1689+
label: 'Date of birth',
1690+
hint: 'For example, 27 3 2012'
1691+
},
1692+
dobWithAge: {
1693+
label: 'Date of birth'
1694+
},
1695+
dod: {
1696+
label: 'Date of death'
1697+
},
1698+
gender: {
1699+
label: 'Gender'
1700+
},
1701+
adjustments: {
1702+
label: 'Reasonable adjustments'
1703+
},
1704+
impairments: {
1705+
label: 'Impairments'
1706+
},
1707+
address: {
1708+
label: 'Address',
1709+
title: 'What is the child’s home address?'
1710+
},
1711+
postalCode: {
1712+
label: 'Postcode'
1713+
},
1714+
gpSurgery: {
1715+
label: 'GP surgery',
1716+
title: 'Who is the child’s GP?'
1717+
},
1718+
parents: {
1719+
label: 'Parents or guardians'
1720+
},
1721+
add: {
1722+
label: 'Do you want to add this child?',
1723+
yes: {
1724+
label: 'Yes'
1725+
},
1726+
no: {
1727+
label: 'No – search for another child'
1728+
}
1729+
}
1730+
},
16401731
programme: {
16411732
label: 'Programme',
16421733
list: {

app/models.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export * from './models/parent.js'
2121
export * from './models/patient-programme.js'
2222
export * from './models/patient-session.js'
2323
export * from './models/patient.js'
24+
export * from './models/pds-record.js'
2425
export * from './models/programme.js'
2526
export * from './models/school.js'
2627
export * from './models/session.js'

app/models/patient.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ import {
4040
* @augments Child
4141
* @param {object} options - Options
4242
* @param {object} [context] - Global context
43-
* @property {string} uuid - UUID
44-
* @property {string} nhsn - NHS number
45-
* @property {boolean} invalid - Flagged as invalid
46-
* @property {boolean} sensitive - Flagged as sensitive
43+
* @property {string} [uuid] - UUID
44+
* @property {string} [nhsn] - NHS number
45+
* @property {boolean} [invalid] - Flagged as invalid
46+
* @property {boolean} [sensitive] - Flagged as sensitive
4747
* @property {object} [address] - Address
4848
* @property {Parent} [parent1] - Parent 1
4949
* @property {Parent} [parent2] - Parent 2

0 commit comments

Comments
 (0)