-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthcrm.go
585 lines (453 loc) · 15.7 KB
/
healthcrm.go
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
package healthcrm
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"github.com/savannahghi/serverutils"
)
var (
// BaseURL represents the health CRM's base URL
BaseURL = serverutils.MustGetEnvVar("HEALTH_CRM_BASE_URL")
)
const (
facilitiesPath = "/v1/facilities/facilities/"
)
// HealthCRMLib interacts with the healthcrm APIs
type HealthCRMLib struct {
client *client
}
// NewHealthCRMLib initializes a new instance of healthCRM SDK
func NewHealthCRMLib() (*HealthCRMLib, error) {
client, err := newClient()
if err != nil {
return nil, err
}
return &HealthCRMLib{
client: client,
}, nil
}
// CreateFacility is used to create facility in health CRM service
func (h *HealthCRMLib) CreateFacility(ctx context.Context, facility *Facility) (*FacilityOutput, error) {
path := "/v1/facilities/facilities/"
response, err := h.client.MakeRequest(ctx, http.MethodPost, path, nil, facility)
if err != nil {
return nil, err
}
defer response.Body.Close()
respBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("could not read response: %w", err)
}
if response.StatusCode != http.StatusCreated {
return nil, errors.New(string(respBytes))
}
var facilityResponse *FacilityOutput
err = json.Unmarshal(respBytes, &facilityResponse)
if err != nil {
return nil, err
}
return facilityResponse, nil
}
// GetFacilityByID is used to fetch facilities from health crm facility registry using its ID
func (h *HealthCRMLib) GetFacilityByID(ctx context.Context, id string) (*FacilityOutput, error) {
path := fmt.Sprintf("/v1/facilities/facilities/%s/", id)
response, err := h.client.MakeRequest(ctx, http.MethodGet, path, nil, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
respBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("could not read response: %w", err)
}
if response.StatusCode != http.StatusOK {
return nil, errors.New(string(respBytes))
}
var facilityOutput *FacilityOutput
err = json.Unmarshal(respBytes, &facilityOutput)
if err != nil {
return nil, err
}
return facilityOutput, nil
}
// UpdateFacility is used to update facility's data
func (h *HealthCRMLib) UpdateFacility(ctx context.Context, id string, updatePayload *Facility) (*FacilityOutput, error) {
path := fmt.Sprintf("/v1/facilities/facilities/%s/", id)
response, err := h.client.MakeRequest(ctx, http.MethodPatch, path, nil, updatePayload)
if err != nil {
return nil, err
}
defer response.Body.Close()
respBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("could not read response: %w", err)
}
if response.StatusCode != http.StatusOK {
return nil, errors.New(string(respBytes))
}
var facilityOutput *FacilityOutput
err = json.Unmarshal(respBytes, &facilityOutput)
if err != nil {
return nil, err
}
return facilityOutput, nil
}
// GetServices retrieves a list of healthcare services provided by facilities
// that are owned by a specific SIL service, such as Mycarehub or Advantage.
func (h *HealthCRMLib) GetServices(ctx context.Context, pagination *Pagination, crmServiceCode string) (*FacilityServicePage, error) {
path := "/v1/facilities/services/"
queryParams := url.Values{}
if pagination != nil {
queryParams.Add("page_size", pagination.PageSize)
queryParams.Add("page", pagination.Page)
}
queryParams.Add("crm_service_code", crmServiceCode)
response, err := h.client.MakeRequest(ctx, http.MethodGet, path, queryParams, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
respBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("could not read response: %w", err)
}
if response.StatusCode != http.StatusOK {
return nil, errors.New(string(respBytes))
}
var facilityServicePage FacilityServicePage
err = json.Unmarshal(respBytes, &facilityServicePage)
if err != nil {
return nil, err
}
return &facilityServicePage, nil
}
// GetFacilitiesOfferingAService fetches the facilities that offer a particular service
func (h *HealthCRMLib) GetFacilitiesOfferingAService(ctx context.Context, serviceID string, pagination *Pagination) (*FacilityPage, error) {
path := "/v1/facilities/facilities/"
queryParams := url.Values{}
queryParams.Add("service", serviceID)
if pagination != nil {
queryParams.Add("page_size", pagination.PageSize)
queryParams.Add("page", pagination.Page)
}
response, err := h.client.MakeRequest(ctx, http.MethodGet, path, queryParams, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
respBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("could not read response: %w", err)
}
if response.StatusCode != http.StatusOK {
return nil, errors.New(string(respBytes))
}
var output *FacilityPage
err = json.Unmarshal(respBytes, &output)
if err != nil {
return nil, err
}
return output, nil
}
// CreateService is used to create a new service in health crm
func (h *HealthCRMLib) CreateService(ctx context.Context, input FacilityServiceInput) (*FacilityService, error) {
path := "/v1/facilities/services/"
response, err := h.client.MakeRequest(ctx, http.MethodPost, path, nil, input)
if err != nil {
return nil, err
}
defer response.Body.Close()
respBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("could not read response: %w", err)
}
if response.StatusCode != http.StatusOK {
return nil, errors.New(string(respBytes))
}
var output *FacilityService
err = json.Unmarshal(respBytes, &output)
if err != nil {
return nil, err
}
return output, nil
}
// LinkServiceToFacility is used to link a service to a facility
func (h *HealthCRMLib) LinkServiceToFacility(ctx context.Context, facilityID string, input []*FacilityServiceInput) (*FacilityService, error) {
path := fmt.Sprintf("/v1/facilities/facilities/%s/add_services/", facilityID)
response, err := h.client.MakeRequest(ctx, http.MethodPost, path, nil, input)
if err != nil {
return nil, err
}
defer response.Body.Close()
respBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("could not read response: %w", err)
}
if response.StatusCode != http.StatusCreated {
return nil, errors.New(string(respBytes))
}
var output *FacilityService
err = json.Unmarshal(respBytes, &output)
if err != nil {
return nil, err
}
return output, nil
}
// GetFacilities retrieves a list of facilities associated with MyCareHub
// stored in HealthCRM. The method allows for filtering facilities by location proximity and services offered.
//
// Parameters:
// - location: A Location struct that represents the reference location.
// If provided, facilities will be filtered based on proximity
// to this location.
// - pagination: A Pagination struct containing options for paginating
// the results.
// - serviceIDs: A parameter that allows specifying one or more
// service IDs. Facilities offering these services will be
// included in the results. You can pass multiple service
// IDs as separate arguments (e.g., GetFacilities(ctx, location, pagination, []string{"1234", "178"})).
// - searchParameter: A parameter used to search a facility by the facility name or a service name
// Note that this parameter cannot be passed together with the serviceIDs
//
// Usage:
// Example 1: Retrieve facilities by location and service IDs:
// --> E.g If we are searching with service ID that represents Chemotherapy, the response
// will be a list of facilities that offer Chemotherapy and it will be ordered with proximity
//
// Example 2: Retrieve facilities by location without specifying services:
// This will return a list of all facilities ordered by the proximity
//
// Example 3: Retrieve all facilities without specifying location or services:
func (h *HealthCRMLib) GetFacilities(ctx context.Context, location *Coordinates, serviceIDs []string, searchParameter string, pagination *Pagination, crmServiceCode string) (*FacilityPage, error) {
queryParams := url.Values{}
if pagination != nil {
queryParams.Add("page_size", pagination.PageSize)
queryParams.Add("page", pagination.Page)
}
if location != nil {
coordinateString, err := location.ToString()
if err != nil {
return nil, err
}
queryParams.Add("ref_location", coordinateString)
if location.Radius != "" {
queryParams.Add("distance", location.Radius)
}
}
if len(serviceIDs) > 0 && searchParameter != "" {
return nil, errors.New("both service IDs and search parameter cannot be provided simultaneously")
}
if len(serviceIDs) > 0 {
for _, id := range serviceIDs {
queryParams.Add("service", id)
}
}
if searchParameter != "" {
queryParams.Add("search", searchParameter)
}
queryParams.Add("crm_service_code", crmServiceCode)
response, err := h.client.MakeRequest(ctx, http.MethodGet, facilitiesPath, queryParams, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
respBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("could not read response: %w", err)
}
if response.StatusCode != http.StatusOK {
// TODO: Get the exact error message (should be formatted well)
return nil, errors.New(string(respBytes))
}
var facilityPage *FacilityPage
err = json.Unmarshal(respBytes, &facilityPage)
if err != nil {
return nil, err
}
return facilityPage, nil
}
// GetService is used to fetch a single service given its ID
func (h *HealthCRMLib) GetService(ctx context.Context, serviceID string) (*FacilityService, error) {
path := fmt.Sprintf("/v1/facilities/services/%s", serviceID)
response, err := h.client.MakeRequest(ctx, http.MethodGet, path, nil, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
respBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("could not read response: %w", err)
}
if response.StatusCode != http.StatusOK {
return nil, errors.New(string(respBytes))
}
var service FacilityService
err = json.Unmarshal(respBytes, &service)
if err != nil {
return nil, err
}
return &service, nil
}
// CreateProfile is used to create profile in health CRM service
func (h *HealthCRMLib) CreateProfile(ctx context.Context, profile *ProfileInput) (*ProfileOutput, error) {
path := "/v1/identities/profiles/"
response, err := h.client.MakeRequest(ctx, http.MethodPost, path, nil, profile)
if err != nil {
return nil, err
}
defer response.Body.Close()
respBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("could not read response: %w", err)
}
if response.StatusCode != http.StatusAccepted {
return nil, errors.New(string(respBytes))
}
var profileResponse *ProfileOutput
err = json.Unmarshal(respBytes, &profileResponse)
if err != nil {
return nil, err
}
return profileResponse, nil
}
// GetMultipleServices is used to fetch multiple services
//
// Parameters:
// - serviceIDs: A parameter that is a list of IDs specifying one or more
// service IDs. Service identifiers identifying these services will be
// included in the results. You can **ONLY** pass a single or multiple service
// IDs which should be of type **UUID** (e.g., GetMultipleServices(ctx, []string{"0fee2792-dffc-40d3-a744-2a70732b1053",
// "56c62083-c7b4-4055-8d44-6cc7446ac1d0", "8474ea55-8ede-4bc6-aa67-f53ed5456a03"})).
func (h *HealthCRMLib) GetMultipleServices(ctx context.Context, servicesIDs []string) ([]*FacilityService, error) {
if len(servicesIDs) < 1 || servicesIDs == nil {
return nil, fmt.Errorf("no service IDs provided")
}
searchParameter := servicesIDs[0]
for idx, id := range servicesIDs {
if idx == 0 {
continue
}
searchParameter += fmt.Sprintf(",%s", id)
}
path := fmt.Sprintf("/v1/facilities/services?service_ids=%s", searchParameter)
response, err := h.client.MakeRequest(ctx, http.MethodGet, path, nil, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
respBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("could not read response: %w", err)
}
if response.StatusCode != http.StatusOK {
return nil, errors.New(string(respBytes))
}
var services *FacilityServices
err = json.Unmarshal(respBytes, &services)
if err != nil {
return nil, err
}
return services.Results, nil
}
// GetMultipleFacilities is used to fetch multiple facilities
//
// Parameters:
// - facilityIDs: A parameter that is a list of IDs specifying one or more
// facility IDs. Facility identifiers, contacts, services and business hours linked to a facility will be
// included in the results. You can **ONLY** pass a single or multiple facility
// IDs which should be of type **UUID** (e.g., GetMultipleFacilities(ctx, []string{"0fee2792-dffc-40d3-a744-2a70732b1053",
// "56c62083-c7b4-4055-8d44-6cc7446ac1d0", "8474ea55-8ede-4bc6-aa67-f53ed5456a03"})).
func (h *HealthCRMLib) GetMultipleFacilities(ctx context.Context, facilityIDs []string) ([]*FacilityOutput, error) {
if len(facilityIDs) < 1 || facilityIDs == nil {
return nil, fmt.Errorf("no facility IDs provided")
}
searchParameter := facilityIDs[0]
for idx, id := range facilityIDs {
if idx == 0 {
continue
}
searchParameter += fmt.Sprintf(",%s", id)
}
path := fmt.Sprintf("/v1/facilities/facilities?facility_ids=%s", searchParameter)
response, err := h.client.MakeRequest(ctx, http.MethodGet, path, nil, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
respBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("could not read response: %w", err)
}
if response.StatusCode != http.StatusOK {
return nil, errors.New(string(respBytes))
}
var facilities *FacilityOutputs
err = json.Unmarshal(respBytes, &facilities)
if err != nil {
return nil, err
}
return facilities.Results, nil
}
// GetPersonIdentifiers fetches a persons identifiers using their HealthID, a
// filter for identifier_type can be passed
func (h *HealthCRMLib) GetPersonIdentifiers(ctx context.Context, healthID string, identifierType IdentifierType) ([]*ProfileIdentifierOutput, error) {
if healthID == "" {
return nil, errors.New("no health ID provided")
}
path := fmt.Sprintf("/v1/identities/persons/%s/identifiers/", healthID)
var queryParams url.Values
if identifierType != "" {
if !identifierType.IsValid() {
return nil, fmt.Errorf("invalid identifier passed: %s", identifierType)
}
queryParams = url.Values{}
queryParams.Add("identifier_type", identifierType.String())
}
response, err := h.client.MakeRequest(ctx, http.MethodGet, path, queryParams, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
respBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("could not read response: %w", err)
}
if response.StatusCode != http.StatusOK {
return nil, errors.New(string(respBytes))
}
var identifiers *ProfileIdentifierOutputs
err = json.Unmarshal(respBytes, &identifiers)
if err != nil {
return nil, err
}
return identifiers.Results, nil
}
// GetPersonContacts fetches a persons Contacts using their HealthID
func (h *HealthCRMLib) GetPersonContacts(ctx context.Context, healthID string) ([]*ProfileContactOutput, error) {
if healthID == "" {
return nil, errors.New("no health ID provided")
}
path := fmt.Sprintf("/v1/identities/persons/%s/contacts/", healthID)
response, err := h.client.MakeRequest(ctx, http.MethodGet, path, nil, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
respBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("could not read response: %w", err)
}
if response.StatusCode != http.StatusOK {
return nil, errors.New(string(respBytes))
}
var identifiers *ProfileContactOutputs
err = json.Unmarshal(respBytes, &identifiers)
if err != nil {
return nil, err
}
return identifiers.Results, nil
}