-
Notifications
You must be signed in to change notification settings - Fork 324
/
match.ts
246 lines (228 loc) · 9.2 KB
/
match.ts
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
import { CodeableConcept, Coding, Identifier, Reference, Resource, SearchParameter } from '@medplum/fhirtypes';
import { evalFhirPath } from '../fhirpath/parse';
import { PropertyType, globalSchema } from '../types';
import { SearchParameterType, getSearchParameterDetails } from './details';
import { Filter, Operator, SearchRequest } from './search';
/**
* Determines if the resource matches the search request.
* @param resource - The resource that was created or updated.
* @param searchRequest - The subscription criteria as a search request.
* @returns True if the resource satisfies the search request.
*/
export function matchesSearchRequest(resource: Resource, searchRequest: SearchRequest): boolean {
if (searchRequest.resourceType !== resource.resourceType) {
return false;
}
if (searchRequest.filters) {
for (const filter of searchRequest.filters) {
if (!matchesSearchFilter(resource, searchRequest, filter)) {
return false;
}
}
}
return true;
}
/**
* Determines if the resource matches the search filter.
* @param resource - The resource that was created or updated.
* @param searchRequest - The search request.
* @param filter - One of the filters of a subscription criteria.
* @returns True if the resource satisfies the search filter.
*/
function matchesSearchFilter(resource: Resource, searchRequest: SearchRequest, filter: Filter): boolean {
const searchParam = globalSchema.types[searchRequest.resourceType]?.searchParams?.[filter.code];
if (!searchParam) {
throw new Error(`Unknown search parameter: ${filter.code} for resource type ${searchRequest.resourceType}`);
}
if (filter.operator === Operator.MISSING && searchParam) {
const values = evalFhirPath(searchParam.expression as string, resource);
return filter.value === 'true' ? !values.length : values.length > 0;
}
if (filter.operator === Operator.MISSING || filter.operator === Operator.PRESENT) {
return matchesMissingOrPresent(resource, filter, searchParam);
}
switch (searchParam.type) {
case 'reference':
return matchesReferenceFilter(resource, filter, searchParam);
case 'string':
case 'uri':
return matchesStringFilter(resource, filter, searchParam);
case 'token':
return matchesTokenFilter(resource, filter, searchParam);
case 'date':
return matchesDateFilter(resource, filter, searchParam);
default:
// Unknown search parameter type
// Default fail the check
return false;
}
}
function matchesMissingOrPresent(resource: Resource, filter: Filter, searchParam: SearchParameter): boolean {
const values = evalFhirPath(searchParam.expression as string, resource);
const exists = values.length > 0;
const desired =
(filter.operator === Operator.MISSING && filter.value === 'false') ||
(filter.operator === Operator.PRESENT && filter.value === 'true');
return desired === exists;
}
function matchesReferenceFilter(resource: Resource, filter: Filter, searchParam: SearchParameter): boolean {
const values = evalFhirPath(searchParam.expression as string, resource) as (Reference | string)[];
const negated = isNegated(filter.operator);
if (filter.value === '' && values.length === 0) {
// If the filter operator is "equals", then the filter matches.
// If the filter operator is "not equals", then the filter does not match.
return filter.operator === Operator.EQUALS;
}
// Normalize the values array into reference strings
const references = values.map((value) => (typeof value === 'string' ? value : value.reference));
for (const filterValue of filter.value.split(',')) {
let match = references.includes(filterValue);
if (!match && filter.code === '_compartment') {
// Backwards compability for compartment search parameter
// In previous versions, the resource type was not required in compartment values
// So, "123" would match "Patient/123"
// We need to maintain this behavior for backwards compatibility
match = references.some((reference) => reference?.endsWith('/' + filterValue));
}
if (match) {
return !negated;
}
}
// If "not equals" and no matches, then return true
// If "equals" and no matches, then return false
return negated;
}
function matchesTokenFilter(resource: Resource, filter: Filter, searchParam: SearchParameter): boolean {
const details = getSearchParameterDetails(resource.resourceType, searchParam);
if (details.type === SearchParameterType.BOOLEAN) {
return matchesBooleanFilter(resource, filter, searchParam);
} else {
return matchesStringFilter(resource, filter, searchParam, true);
}
}
function matchesBooleanFilter(resource: Resource, filter: Filter, searchParam: SearchParameter): boolean {
const values = evalFhirPath(searchParam.expression as string, resource);
const expected = filter.value === 'true';
const result = values.includes(expected);
return isNegated(filter.operator) ? !result : result;
}
function matchesStringFilter(
resource: Resource,
filter: Filter,
searchParam: SearchParameter,
asToken?: boolean
): boolean {
const details = getSearchParameterDetails(resource.resourceType, searchParam);
const searchParamElementType = details.elementDefinitions?.[0].type?.[0].code;
const resourceValues = evalFhirPath(searchParam.expression as string, resource);
const filterValues = filter.value.split(',');
const negated = isNegated(filter.operator);
for (const resourceValue of resourceValues) {
for (const filterValue of filterValues) {
let match;
if (searchParamElementType === PropertyType.Identifier) {
match = matchesTokenIdentifierValue(resourceValue as Identifier, filter.operator, filterValue);
} else if (searchParamElementType === PropertyType.CodeableConcept) {
match = matchesTokenCodeableConceptValue(resourceValue as Coding, filter.operator, filterValue);
} else {
match = matchesStringValue(resourceValue, filter.operator, filterValue, asToken);
}
if (match) {
return !negated;
}
}
}
// If "not equals" and no matches, then return true
// If "equals" and no matches, then return false
return negated;
}
function matchesStringValue(
resourceValue: unknown,
operator: Operator,
filterValue: string,
asToken?: boolean
): boolean {
if (asToken && filterValue.includes('|')) {
const [system, code] = filterValue.split('|');
return (
matchesStringValue(resourceValue, operator, system, false) &&
(!code || matchesStringValue(resourceValue, operator, code, false))
);
}
let str = '';
if (resourceValue) {
if (typeof resourceValue === 'string') {
str = resourceValue;
} else if (typeof resourceValue === 'object') {
str = JSON.stringify(resourceValue);
}
}
return str.toLowerCase().includes(filterValue.toLowerCase());
}
function matchesTokenIdentifierValue(resourceValue: Identifier, operator: Operator, filterValue: string): boolean {
if (filterValue.includes('|')) {
const [system, value] = filterValue.split('|');
return (
resourceValue.system?.toLowerCase() === system.toLowerCase() &&
resourceValue.value?.toLowerCase() === value.toLowerCase()
);
}
return resourceValue.value?.toLowerCase() === filterValue.toLowerCase();
}
function matchesTokenCodeableConceptValue(
resourceValue: CodeableConcept,
operator: Operator,
filterValue: string
): boolean {
if (filterValue.includes('|')) {
const [system, code] = filterValue.split('|');
return (
resourceValue.coding?.some(
(coding) =>
coding.system?.toLowerCase() === system.toLowerCase() && coding.code?.toLowerCase() === code.toLowerCase()
) ?? false
);
}
return (
resourceValue.text?.toLowerCase() === filterValue.toLowerCase() ||
(resourceValue.coding?.some((coding) => coding.code?.toLowerCase() === filterValue.toLowerCase()) ?? false)
);
}
function matchesDateFilter(resource: Resource, filter: Filter, searchParam: SearchParameter): boolean {
const resourceValues = evalFhirPath(searchParam.expression as string, resource);
const filterValues = filter.value.split(',');
const negated = isNegated(filter.operator);
for (const resourceValue of resourceValues) {
for (const filterValue of filterValues) {
const match = matchesDateValue(resourceValue as string, filter.operator, filterValue);
if (match) {
return !negated;
}
}
}
// If "not equals" and no matches, then return true
// If "equals" and no matches, then return false
return negated;
}
function matchesDateValue(resourceValue: string, operator: Operator, filterValue: string): boolean {
switch (operator) {
case Operator.STARTS_AFTER:
case Operator.GREATER_THAN:
return resourceValue > filterValue;
case Operator.GREATER_THAN_OR_EQUALS:
return resourceValue >= filterValue;
case Operator.ENDS_BEFORE:
case Operator.LESS_THAN:
return resourceValue < filterValue;
case Operator.LESS_THAN_OR_EQUALS:
return resourceValue <= filterValue;
case Operator.EQUALS:
case Operator.NOT_EQUALS:
return resourceValue === filterValue;
default:
return false;
}
}
function isNegated(operator: Operator): boolean {
return operator === Operator.NOT_EQUALS || operator === Operator.NOT;
}