forked from bluehalo/kyruus-node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery-builder.js
713 lines (636 loc) · 16.6 KB
/
query-builder.js
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
'use-strict';
const _ = require('lodash');
const FilterObject = require('./filter-object.js');
/**
* @typedef KyruusVector
* @summary A Kyruus vector is a search paramater that allows for more flexible matching techniques like partial matching
* and inverted word. Search results are also sorted by quality and relevance
*
* There are only 5 vectors that can be used:
* Name - first, last, or full provider name
* Specialty Synonym - Provider's specialty
* Clinical Experience - If available, patient condition or medical term
* Practice Group - If available, provider practice group
* Unified - Search on ALL previous vectors and allows for mispellings
*
* @property {string} field - which vector to use if any
* @property {string} value - what the vector should look for
*/
/**
* @class k
* @property {FilterObject} _filter - filter object used to build the Kyruus search filter
* @property {KyruusVector} _vector - which vector to use in the Kyruus search if any
* @property {string} [_location.location] - which location to use in the Kyruus search if any
* @property {string} [_location.distance] - what distance from a location to use in the Kyruus search if any
* @property {object} [_params] - Object of key:value pairs for kyruus api parameters
*/
class k {
// Constants
static get NAME() {
return 'name';
}
static get SPECIALTYSYNONYM() {
return 'specialtysynonym';
}
static get CLINICALEXPERIENCE() {
return 'clinical.experience';
}
static get PRACTICEGROUP() {
return 'practice.group';
}
static get UNIFIED() {
return 'unified';
}
constructor(api = null) {
this._filter = {};
// Kyruus only allows for one vector object, this vector object will enforce that
this._vector = {field: null, value: null};
this._location = {location: null, distance: null};
this._params = [];
this._currentFilter = '';
//function to run search queries on
this._api = api;
}
get filter() {
return this._filter;
}
/*
* Location functions
*/
/**
* @function location
* @summary Adds the special location filter to the query.
* @param {string} location - Central location to sort by
* @param {string} distance - Radius from the location in miles to match
* @return {k}
*/
location(location, distance) {
this._location = {location: location, distance: distance};
return this;
}
/**
* @function removeLocation
* @summary Removes the location while maintaining the location format
* @return {k}
*/
removeLocation() {
this._location = {location: null, distance: null};
return this;
}
/*
* Vector functions
*/
/**
* @function vector
* @summary Assigns a search vector to use in Kyruus. Currently, there are only 5 vectors that can be used
* which are expressed in the functions: name, specialtySynonym, clinincalExperience, practiceGroup, unified
* @param {string} field - which type field to use
* @param {string} value - value the vector should be looking for
* @return {k}
*/
vector(field, value) {
this._vector.field = field;
this._vector.value = value;
return this;
}
/**
* @function name
* @summary Changes the vector to a name vector and assigns the given value to the vector
* @param {string} name - value for the Name
* @return {k}
*/
name(name) {
return this.vector(k.NAME, name);
}
/**
* @function specialtySynonym
* @summary Changes the vector to a specialtySynonym vector and assigns the given value to the vector
* @param {string} synonym - value for the specialtySynonym
* @return {k}
*/
// Figure out of this is correct
specialtySynonym(synonym) {
return this.vector(k.SPECIALTYSYNONYM, synonym);
}
/**
* @function clinicalExperience
* @summary Changes the vector to a clinicalExperience vector and assigns the given value to the vector
* @param {string} experience - value for the clinicalExperience
* @return {k}
*/
// Figure out of this is correct
clinicalExperience(experience) {
return this.vector(k.CLINICALEXPERIENCE, experience);
}
/**
* @function practiceGroup
* @summary Changes the vector to a practiceGroup vector and assigns the given value to the vector
* @param {string} group - value for the practiceGroup
* @return {k}
*/
// Figure out of this is correct
practiceGroup(group) {
return this.vector(k.PRACTICEGROUP, group);
}
/**
* @function isEncoded
* @summary Checks if input string is already encoded.
* @param {string} uri
* @return {k}
*/
isEncoded(uri = '') {
try {
return uri !== decodeURIComponent(uri);
} catch(err) {
return false;
}
}
/**
* @function encode
* @summary encodes uri if it's not already encoded.
* @param {string} uri
* @return {k}
*/
encode(uri = '') {
if (!this.isEncoded(uri)) {
uri = encodeURIComponent(uri);
}
return uri;
}
/**
* @function unified
* @summary Changes the vector to a unified vector and assigns the given value to the vector
* @param {string} unified - value for the unified
* @return {k}
*/
// Figure out of this is correct
unified(unified) {
return this.vector(k.UNIFIED, this.encode(unified));
}
/*
* Standard filter functions
*/
/**
* @function filterOther
* @summary Adds
* @param {string} field - what filter to add the value to
* @param {string} value - what to value to filter against in the field
* @param {string} conjunction - conjuction symbol to seperate filters by. Default is 'or'. Other option is '^'
* @return {k}
*/
filterOther(field, value, conjunction = 'or') {
value = this.encode(value);
if (this._filter[field]) {
if (this._filter[field].checkType(conjunction)) {
this._filter[field].append(value);
}
else {
this._filter[field] = new FilterObject(this._filter[field], conjunction);
this._filter[field].append(value);
}
}
else {
this._filter[field] = new FilterObject(value, conjunction);
}
this._currentFilter = field;
return this;
}
/**
* @function param
* @summary Adds an additional parameter to the query
* @param {string} field - parameter key/field
* @param {string} value - value of the parameter
* @return {k}
*/
param(field, value) {
this._params[field] = this.encode(value);
return this;
}
/**
* @function clearVector
* @summary Resets vector to its empty format
* @return {k}
*/
clearVector() {
this._vector = {field: null, value: null};
return this;
}
/**
* @function removeFromFilter
* @summary Removes a value from a filter
* @param {string} field - filter field
* @param {string} value - value to remove from filter
* @return {k}
*/
removeFromFilter(field, value) {
if(this._filter[field] instanceof FilterObject) {
this._filter[field].remove(this.encode(value));
if (this._filter[field].size() === 0) {
delete this._filter[field];
}
}
return this;
}
/**
* @function remove
* @summary Removes a field from the query
* @param {string} field - field to remove
* @return {k}
*/
remove(field) {
delete this._filter[field];
delete this._params[field];
if (this._vector.field === field) {
this.clearVector();
}
return this;
}
/**
* @function npis
* @summary Adds npis to query
* @param {...string} npis - npis to add
* @return {k}
*/
npis(...npis) {
_.forEach(npis, (npi) => {
this.filterOther('npi', npi);
});
return this;
}
/**
* @function removeNpis
* @summary Adds npis to query
* @param {...string} npis - npis to add
* @return {k}
*/
removeNpis(...npis) {
_.forEach(npis, (npi) => {
this.removeFromFilter('npi', npi);
});
return this;
}
/**
* @function gender
* @summary Adds gender to query
* @param {string} gender - gender to add
* @return {k}
*/
gender(gender) {
return this.filterOther('gender', gender);
}
/**
* @function removeGender
* @summary Removes a gender from the query if it exists
* @param {...string} npis - npis to add
* @return {k}
*/
removeGender(gender) {
return this.removeFromFilter('gender', gender);
}
/**
* @function locationNames
* @summary Adds location names to query
* @param {...string} locations - location names to add
* @return {k}
*/
locationNames(...locations) {
_.forEach(locations, (location) => {
this.filterOther('locations.name', location);
});
return this;
}
/**
* @function removeLocationNames
* @summary Removes location names from query
* @param {...string} locations - locations to remove
* @return {k}
*/
removeLocationNames(...locations) {
_.forEach(locations, (location) => {
this.removeFromFilter('locations.name', location);
});
return this;
}
/**
* @function specialties
* @summary Adds specialties to query
* @param {...string} specialties - specialties to add
* @return {k}
*/
specialties(...specialties) {
_.forEach(specialties, (specialty) => {
this.filterOther('specialties.specialty.untouched', specialty);
});
return this;
}
/**
* @function removeSpecialties
* @summary Removes specialties from query
* @param {...string} specialties - specialties to remove
* @return {k}
*/
removeSpecialties(...specialties) {
_.forEach(specialties, (specialty) => {
this.removeFromFilter('specialties.specialty.untouched', specialty);
});
return this;
}
/**
* @function subSpecialties
* @summary Adds sub-specialties to query
* @param {...string} specialties - sub-specialties to add
* @return {k}
*/
subSpecialties(...specialties) {
_.forEach(specialties, (specialty) => {
this.filterOther('specialties.subspecialty.untouched', specialty);
});
return this;
}
/**
* @function removeSubSpecialties
* @summary Removes sub-specialties from query
* @param {...string} specialties - sub-specialties to remove
* @return {k}
*/
removeSubSpecialties(...specialties) {
_.forEach(specialties, (specialty) => {
this.removeFromFilter('specialties.subspecialty.untouched', specialty);
});
return this;
}
/**
* @function practiceFocus
* @summary Adds practice focus to query
* @param {string} focus - Practice focus to add
* @return {k}
*/
practiceFocus(focus) {
return this.filterOther('specialties.practice_focus.untouched', focus);
}
/**
* @function removePracticeFocus
* @summary Removes practice focus if it exists
* @param {string} focus - Practice focus to remove
* @return {k}
*/
removePracticeFocus(focus) {
return this.removeFromFilter('specialties.practice_focus.untouched', focus);
}
/**
* @function cityLocations
* @summary Adds city locations to query
* @param {...string} cities - cities to add
* @return {k}
*/
cityLocations(...cities) {
_.forEach(cities, (city) => {
this.filterOther('locations.city', city);
});
return this;
}
/**
* @function removeCityLocations
* @summary Removes city locations from query
* @param {...string} cities - cities to remove
* @return {k}
*/
removeCityLocations(...cities) {
_.forEach(cities, (city) => {
this.removeFromFilter('locations.city', city);
});
return this;
}
/**
* @function languages
* @summary Adds languages to query
* @param {...string} cities - languages to add
* @return {k}
*/
languages(...languages) {
_.forEach(languages, (language) => {
this.filterOther('languages.language', language);
});
return this;
}
/**
* @function removeLanguages
* @summary Removes languages from query
* @param {...string} cities - languages to remove
* @return {k}
*/
removeLanguages(...languages) {
_.forEach(languages, (language) => {
this.removeFromFilter('languages.language', language);
});
return this;
}
/**
* @function acceptingNewPatients
* @summary Adds accepting_new_patients filter to query
* @param {boolean} accepts - filter on true/false if the provider is accepting new patients (default true)
* @return {k}
*/
acceptingNewPatients(accepts = true) {
return this.filterOther('accepting_new_patients', accepts);
}
/**
* @function filterAcceptingNewPatients
* @summary Removes from accepting new patients if it exists
* @param {boolean} accepts - remove true/false accepting new patients if it's set to that value
* @return {k}
*/
removeAcceptingNewPatients(accepts = true) {
return this.removeFromFilter('accepting_new_patients', accepts);
}
/**
* @function or
* @summary Adds an additional value to the current filter as an or statement
* @param {...string} values - Value(s) to append to the current filter key
* @return {k}
*/
or(...values) {
_.forEach(values, (value) => {
this.filterOther(this._currentFilter, value, 'or');
});
return this;
}
/**
* @function with
* @summary Adds additional filters to the current filter as a kyruus and (^) statement. Note: and statements can only be applied to same-object types. See kyruus documentation for more detail
* @param {...object} keyValues - key value pairs to add. Key being the additional filter, and value being the string value on what to filter on.
* @return {k}
*/
with(field, value) {
this.filterOther(this._currentFilter, `${field}:${value}`, '^');
return this;
}
/*
* Search sorting and page selection
*/
/**
* @function shuffle
* @summary Adds a seed to shuffle return results
* @param {string} seed - String to seed the shuffle with
* @return {k}
*/
shuffle(seed) {
return this.param('shuffle_seed', seed);
}
/**
* @function removeShuffle
* @summary Removes shuffle seed
* @return {k}
*/
removeShuffle() {
return this.remove('shuffle_seed');
}
/**
* @function sort
* @summary Adds a field to sort results by
* @param {string} seed - Field to sort by
* @return {k}
*/
sort(field) {
return this.param('sort', field);
}
/**
* @function removeSort
* @summary Removes sorting on search results
* @return {k}
*/
removeSort() {
return this.remove('sort');
}
/**
* @function fields
* @summary specify fields to return
* @param {value} value - boolean
* @return {k}
*/
fields(value) {
return this.param('fields', value);
}
/**
* @function removeDebug
* @summary Removes debug
* @return {k}
*/
removeFields() {
return this.remove('fields');
}
/**
* @function facets
* @summary Adds facets to query
* @param {...string} facets - facets to add
* @return {k}
*/
facets(...facets) {
_.forEach(facets, (facet) => {
this.param('facet', facet);
});
return this;
}
/**
* @function removeFacets
* @summary Removes facets from query
* @return {k}
*/
removeFacets() {
this.remove('facet');
return this;
}
/**
* @function pageSize
* @summary Sets the page size for return results
* @param {string} size - Number of results per page
* @return {k}
*/
pageSize(size) {
return this.param('per_page', size);
}
/**
* @function removePageSize
* @summary Removes page size parameter
* @param {string} size - Number of results per page
* @return {k}
*/
removePageSize(size) {
return this.remove('per_page', size);
}
/**
* @function removePageSize
* @summary Removes page size from query
* @return {k}
*/
removePageSize() {
return this.remove('per_page');
}
/**
* @function pageNumber
* @summary Adds the page number to return results. The results are going to be [pageSize*pageNumber...pageSize*pageNumber+pageNumber-1]
* @param {string} number - Page number
* @return {k}
*/
pageNumber(number) {
return this.param('page', number);
}
/**
* @function removePageNumber
* @summary Removes page number from query
* @return {k}
*/
removePageNumber(number) {
return this.remove('page');
}
/**
* @function toString
* @summary Converts object into a string formatted filter string for kyruus api calls
* @return {string}
*/
toString() {
let queryParams = []
let addQueryParam = function (val, key) {
if(val && key) {
queryParams.push(`${key}=${val}`);
}
}
_.forIn(this._params, (val, key) => {
if (_.isArray(val)) {
_.each(val, (v) => {
addQueryParam(v, key);
});
}
else {
addQueryParam(val, key);
}
});
let vectorKey = _.get(this,'_vector[field]', null);
let vectorValue = _.get(this,'_vector[value]', null);
if(vectorKey && vectorValue) {
addQueryParam(vectorValue, vectorKey);
}
let location = _.get(this,'_location.location', null);
let distance = _.get(this,'_location.distance', null);
if(location) {
addQueryParam(location, 'location');
addQueryParam(distance, 'distance');
}
_.forIn(this._filter, (value, key) => {
queryParams.push(`filter=${key}:${value}`);
});
return _.size(queryParams) > 0 ? '?'+_.join(queryParams, '&') : '';
}
/**
* @function search
* @summary Runs a kyruus search
* @return {Promise.<KyruusProviderSearch>|k}
*/
search() {
if(this._api) {
return this._api.search(this.toString());
}
return this;
}
}
module.exports = k;