-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.ts
More file actions
251 lines (219 loc) · 7.39 KB
/
content.ts
File metadata and controls
251 lines (219 loc) · 7.39 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import {BundleEntry, Task} from "fhir/r4"
import {validatePrescriptionID} from "../utils/prescriptionID"
import {validateNhsNumber} from "../utils/nhsNumber"
import {validateFields} from "./fields"
export type TaskValidation = (task: Task) => string | undefined;
export type BundleEntryValidation = (
bundleEntry: BundleEntry,
) => string | undefined;
export type ValidationOutcome = {
valid: boolean;
issues: string | undefined;
};
export const ONE_HOUR_IN_MS = 60 * 60 * 1000
export const ONE_DAY_IN_MS = 24 * ONE_HOUR_IN_MS
export const LINE_ITEM_ID_CODESYSTEM =
"https://fhir.nhs.uk/Id/prescription-order-item-number"
export const NHS_NUMBER_CODESYSTEM = "https://fhir.nhs.uk/Id/nhs-number"
export const ODS_CODE_CODESYSTEM =
"https://fhir.nhs.uk/Id/ods-organization-code"
export const PRESCRIPTION_ID_CODESYSTEM =
"https://fhir.nhs.uk/Id/prescription-order-number"
export const STATUS_CODESYSTEM =
"https://fhir.nhs.uk/CodeSystem/task-businessStatus-nppt"
const VALID_STATUSES = ["completed", "in-progress"]
const COMPLETED_ONLY_BUSINESS_STATUSES = [
"collected",
"not dispensed",
"dispatched"
]
const IN_PROGRESS_ONLY_BUSINESS_STATUSES = [
"with pharmacy",
"with pharmacy - preparing remainder",
"ready to collect - partial",
"ready to dispatch - partial"
]
const AGNOSTIC_BUSINESS_STATUSES = ["ready to dispatch", "ready to collect"]
export const BUSINESS_STATUSES = [
...COMPLETED_ONLY_BUSINESS_STATUSES,
...IN_PROGRESS_ONLY_BUSINESS_STATUSES,
...AGNOSTIC_BUSINESS_STATUSES
]
const VALID_COMPLETED_STATUSES = COMPLETED_ONLY_BUSINESS_STATUSES.concat(
AGNOSTIC_BUSINESS_STATUSES
)
const VALID_IN_PROGRESS_STATUSES = IN_PROGRESS_ONLY_BUSINESS_STATUSES.concat(
AGNOSTIC_BUSINESS_STATUSES
)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function transactionBundle(body: any): boolean {
return body.resourceType === "Bundle" && body.type === "transaction"
}
export function entryContent(entry: BundleEntry): Array<string> {
return `urn:uuid:${entry.resource!.id}` === entry.fullUrl!
? []
: ["Invalid entry fullUrl or task id."]
}
export function lastModified(task: Task): string | undefined {
const lastModified = new Date(task.lastModified!)
const hasMetaLastUpdated = Boolean(task.meta?.lastUpdated)
// 24 hours if meta.lastUpdated is not provided, otherwise 999 hours
const allowedHours = hasMetaLastUpdated ? 999 : 24
return isWithinHours(lastModified, allowedHours, "lastModified")
}
function isWithinHours(
date: Date,
hours: number,
fieldName: string
): string | undefined {
if (isNaN(date.getTime())) {
return `Date format provided for ${fieldName} is invalid.`
}
const now = new Date()
const limitMs = hours * ONE_HOUR_IN_MS
if (date.valueOf() - now.valueOf()> limitMs) {
return `Invalid ${fieldName} value provided.`
}
}
export function metaLastUpdated(task: Task): string | undefined {
if (!task.meta?.lastUpdated) {
return undefined
}
const parsed = new Date(task.meta.lastUpdated)
return isWithinHours(parsed, 24, "meta.lastUpdated")
}
export function prescriptionID(task: Task): string | undefined {
const message = "Prescription ID is invalid."
const prescriptionID = task.basedOn?.[0].identifier?.value
if (!prescriptionID) {
return message
}
return validatePrescriptionID(prescriptionID) ? undefined : message
}
export function nhsNumber(task: Task): string | undefined {
const message = "NHS number is invalid."
const nhsNumber = task.for?.identifier?.value
if (!nhsNumber) {
return message
}
return validateNhsNumber(nhsNumber) ? undefined : message
}
export function resourceType(task: Task): string | undefined {
const message = "Resource's resourceType is not 'Task'."
const isTask = task.resourceType === "Task"
if (!isTask) {
return message
}
}
export function codeSystems(task: Task): string | undefined {
const systems: Array<TaskValidation> = [
(t: Task) =>
t.focus!.identifier!.system === LINE_ITEM_ID_CODESYSTEM
? undefined
: "LineItemID",
(t: Task) =>
t.for!.identifier!.system === NHS_NUMBER_CODESYSTEM
? undefined
: "PatientNHSNumber",
(t: Task) =>
t.owner!.identifier!.system === ODS_CODE_CODESYSTEM
? undefined
: "PharmacyODSCode",
(t: Task) =>
t.basedOn![0].identifier!.system === PRESCRIPTION_ID_CODESYSTEM
? undefined
: "PrescriptionID",
(t: Task) =>
t.businessStatus!.coding![0].system === STATUS_CODESYSTEM
? undefined
: "Status"
]
const incorrectCodeSystems: Array<string> = []
for (const system of systems) {
const incorrect = system(task)
if (incorrect) {
incorrectCodeSystems.push(incorrect)
}
}
if (incorrectCodeSystems.length > 0) {
return `Invalid CodeSystem(s) - ${incorrectCodeSystems.join(", ")}.`
}
}
export function status(task: Task): string | undefined {
const status = task.status
if (!VALID_STATUSES.includes(status)) {
return `Unsupported Task.status '${status}'.`
}
}
export function businessStatus(task: Task): string | undefined {
const code: string = task.businessStatus!.coding![0].code!
if (!BUSINESS_STATUSES.includes(code.toLowerCase())) {
return "Invalid business status."
}
}
export function taskStatusAgainstBusinessStatus(
task: Task
): string | undefined {
const status = task.status
const businessStatus: string = task.businessStatus!.coding![0].code!
const lowercaseCode = businessStatus.toLowerCase()
const validStatus = BUSINESS_STATUSES.includes(lowercaseCode)
if (!validStatus) {
return `Unsupported Task.businessStatus '${businessStatus}'.`
}
const validCompleteStatus = VALID_COMPLETED_STATUSES.includes(lowercaseCode)
if (status === "completed" && !validCompleteStatus) {
// eslint-disable-next-line max-len
return `Task.status field set to '${status}' but Task.businessStatus value of '${businessStatus}' requires follow up action.`
}
const validInProgressStatus =
VALID_IN_PROGRESS_STATUSES.includes(lowercaseCode)
if (status === "in-progress" && !validInProgressStatus) {
// eslint-disable-next-line max-len
return `Task.status field set to '${status}' but Task.businessStatus value of '${businessStatus}' has no possible follow up action.`
}
return undefined
}
export function taskContent(task: Task): Array<string> {
const contentValidations: Array<TaskValidation> = [
status,
businessStatus,
lastModified,
metaLastUpdated,
nhsNumber,
prescriptionID,
resourceType,
taskStatusAgainstBusinessStatus,
codeSystems
]
const issues: Array<string> = []
contentValidations.forEach((validation: TaskValidation) => {
const issue = validation(task)
if (issue) {
issues.push(issue)
}
})
return issues
}
export function validateContent(entry: BundleEntry): ValidationOutcome {
const validationOutcome: ValidationOutcome = {
valid: true,
issues: undefined
}
const issues: Array<string> = []
const task = entry.resource as Task
entryContent(entry).forEach((f) => issues.push(f))
taskContent(task).forEach((f) => issues.push(f))
if (issues.length > 0) {
validationOutcome.valid = false
validationOutcome.issues = issues.join(" ")
}
return validationOutcome
}
export function validateEntry(entry: BundleEntry): ValidationOutcome {
const fieldsOutcome = validateFields(entry)
if (!fieldsOutcome.valid) {
return fieldsOutcome
}
return validateContent(entry)
}