-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsent.js
More file actions
195 lines (148 loc) · 5.13 KB
/
consent.js
File metadata and controls
195 lines (148 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import _ from 'lodash'
import { Consent, PatientSession, Patient, Session } from '../models.js'
import { getResults, getPagination } from '../utils/pagination.js'
export const consentController = {
read(request, response, next, consent_uuid) {
const { patient_uuid } = request.query
const { session_id } = request.params
const { referrer } = request.session
const consent = Consent.findOne(consent_uuid, request.session.data)
const back = session_id
? `/sessions/${consent.session_id}/consents`
: '/consents'
response.locals.back = referrer || back
response.locals.consent = consent
response.locals.patient = Patient.findOne(
patient_uuid,
request.session.data
)
response.locals.consentPath = session_id
? `/sessions/${consent.session_id}${consent.uri}`
: consent.uri
response.locals.consentsPath = session_id
? `/sessions/${session_id}/consents`
: '/consents'
delete request.session.referrer
next()
},
readAll(request, response, next) {
const { session_id } = request.params
let consents = Consent.findAll(request.session.data)
// Sort
consents = _.sortBy(consents, 'createdAt')
// Session consents
if (session_id) {
const session = Session.findOne(session_id, request.session.data)
consents = session.consents
response.locals.session = session
}
response.locals.consents = consents
response.locals.consentsPath = session_id
? `/sessions/${session_id}/consents`
: '/consents'
response.locals.results = getResults(consents, request.query)
response.locals.pages = getPagination(consents, request.query)
next()
},
show(request, response) {
const view = request.params.view || 'show'
response.render(`consent/${view}`)
},
list(request, response) {
response.render('consent/list')
},
readMatches(request, response, next) {
let { hasMissingNhsNumber, page, limit, q } = request.query
const { data } = request.session
let patients = Patient.findAll(data)
// Sort
patients = _.sortBy(patients, 'lastName')
// Paginate
page = parseInt(page) || 1
limit = parseInt(limit) || 50
// Query
if (q) {
patients = patients.filter((patient) =>
patient.tokenized.includes(String(q).toLowerCase())
)
}
// Filter by missing NHS number
if (hasMissingNhsNumber) {
patients = patients.filter((patient) => patient.hasMissingNhsNumber)
}
// Toggle initial view
response.locals.initial =
Object.keys(request.query).filter((key) => key !== 'referrer').length ===
0
// Results
response.locals.patients = patients
response.locals.results = getResults(patients, page, limit)
response.locals.pages = getPagination(patients, request.query)
// Clean up session data
delete data.hasMissingNhsNumber
delete data.q
next()
},
filterMatches(request, response) {
const { hasMissingNhsNumber, q } = request.body
const { consent } = response.locals
const params = new URLSearchParams()
if (q) {
params.append('q', String(q))
}
if (hasMissingNhsNumber?.includes('true')) {
params.append('hasMissingNhsNumber', 'true')
}
response.redirect(`${consent.uri}/match?${params}`)
},
link(request, response) {
const { consent_uuid } = request.params
const { data } = request.session
const { __, consent, patient, consentsPath } = response.locals
// Link consent with patient record
consent.linkToPatient(patient)
// Update session data
Consent.update(consent_uuid, consent, data)
Patient.update(patient.uuid, patient, data)
request.flash('success', __(`consent.link.success`, { consent, patient }))
response.redirect(consentsPath)
},
add(request, response) {
const { consent_uuid } = request.params
const { data } = request.session
const { __, consent, consentsPath } = response.locals
// Create patient
const patient = Patient.create(consent.child, data)
// Create and add patient session
const patientSession = PatientSession.create(
{
patient_uuid: patient.uuid,
session_id: consent.session_id
},
data
)
// Add to session
patient.addToSession(patientSession.session)
// Invite parent to give consent
patient.requestConsent(patientSession)
// Link consent with patient record
consent.linkToPatient(patient)
// Update session data
Consent.update(consent_uuid, consent, data)
Patient.update(patient.uuid, patient, data)
request.flash('success', __(`consent.add.success`, { consent, patient }))
response.redirect(consentsPath)
},
invalidate(request, response) {
const { note } = request.body.consent
const { consent_uuid } = request.params
const { data } = request.session
const { __, consentsPath } = response.locals
// Clean up session data
delete data.consent
// Update session data
const consent = Consent.update(consent_uuid, { invalid: true, note }, data)
request.flash('success', __(`consent.invalidate.success`, { consent }))
response.redirect(consentsPath)
}
}