Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for finding and getting practices #16

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Client struct {
MedicationSvc *MedicationService
PatientSvc *PatientService
PhysicianSvc *PhysicianService
PracticeSvc *PracticeService
ProblemSvc *ProblemService
ServiceLocationSvc *ServiceLocationService
SubscriptionSvc *SubscriptionService
Expand All @@ -64,6 +65,7 @@ func NewClient(httpClient *http.Client, tokenURL, clientID, clientSecret, baseUR
client.MedicationSvc = &MedicationService{client}
client.PatientSvc = &PatientService{client}
client.PhysicianSvc = &PhysicianService{client}
client.PracticeSvc = &PracticeService{client}
client.ProblemSvc = &ProblemService{client}
client.ServiceLocationSvc = &ServiceLocationService{client}
client.SubscriptionSvc = &SubscriptionService{client}
Expand Down
102 changes: 102 additions & 0 deletions practice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package elation

import (
"context"
"fmt"
"net/http"
"strconv"
"time"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

type PracticeServicer interface {
Find(ctx context.Context, opts *FindPracticesOptions) (*Response[[]*Practice], *http.Response, error)
Get(ctx context.Context, id int64) (*Practice, *http.Response, error)
}

var _ PracticeServicer = (*PracticeService)(nil)

type PracticeService struct {
client *Client
}

type Practice struct {
ID int64 `json:"id"`
Name string `json:"name"`
AddressLine1 string `json:"address_line1"`
AddressLine2 string `json:"address_line2"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
Timezone string `json:"timezone"`
ElationRootOid string `json:"elation_root_oid"`
Employers []*PracticeEmployer `json:"employers"`
Physicians []int64 `json:"physicians"`
ServiceLocations []*PracticeServiceLocation `json:"service_locations"`
Metadata any `json:"metadata"`
Status string `json:"status"`
}

type PracticeEmployer struct {
Code string `json:"code"`
Name string `json:"name"`
Description string `json:"description"`
}

type PracticeServiceLocation struct {
ID int `json:"id"`
Name string `json:"name"`
IsPrimary bool `json:"is_primary"`
PlaceOfService string `json:"place_of_service"`
AddressLine1 string `json:"address_line1"`
AddressLine2 string `json:"address_line2"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
Phone string `json:"phone"`
Email any `json:"email"`
Fax string `json:"fax"`
Practice int64 `json:"practice"`
CreatedDate time.Time `json:"created_date"`
DeletedDate any `json:"deleted_date"`
Status string `json:"status"`
}

type FindPracticesOptions struct {
*Pagination
}

func (s *PracticeService) Find(ctx context.Context, opts *FindPracticesOptions) (*Response[[]*Practice], *http.Response, error) {
ctx, span := s.client.tracer.Start(ctx, "find practices", trace.WithSpanKind(trace.SpanKindClient))
defer span.End()

out := &Response[[]*Practice]{}

res, err := s.client.request(ctx, http.MethodGet, "/practices", opts, nil, &out)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "error making request")
return nil, res, fmt.Errorf("making request: %w", err)
}

return out, res, nil
}

func (s *PracticeService) Get(ctx context.Context, id int64) (*Practice, *http.Response, error) {
ctx, span := s.client.tracer.Start(ctx, "get practice", trace.WithSpanKind(trace.SpanKindClient), trace.WithAttributes(attribute.Int64("elation.Practice_id", id)))
defer span.End()

out := &Practice{}

res, err := s.client.request(ctx, http.MethodGet, "/practices/"+strconv.FormatInt(id, 10), nil, nil, &out)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "error making request")
return nil, res, fmt.Errorf("making request: %w", err)
}

return out, res, nil
}
96 changes: 96 additions & 0 deletions practice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package elation

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
)

func TestPracticeService_Find(t *testing.T) {
assert := assert.New(t)

opts := &FindPracticesOptions{
Pagination: &Pagination{
Limit: 1,
Offset: 2,
},
}

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if tokenRequest(w, r) {
return
}

assert.Equal(http.MethodGet, r.Method)
assert.Equal("/practices", r.URL.Path)

limit := r.URL.Query().Get("limit")
offset := r.URL.Query().Get("offset")

assert.Equal(opts.Pagination.Limit, strToInt(limit))
assert.Equal(opts.Pagination.Offset, strToInt(offset))

b, err := json.Marshal(Response[[]*Practice]{
Results: []*Practice{
{
ID: 1,
},
{
ID: 2,
},
},
})
assert.NoError(err)

w.Header().Set("Content-Type", "application/json")
//nolint
w.Write(b)
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := PracticeService{client}

found, res, err := svc.Find(context.Background(), opts)
assert.NotNil(found)
assert.NotNil(res)
assert.NoError(err)
}

func TestPracticeService_Get(t *testing.T) {
assert := assert.New(t)

var id int64 = 1

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if tokenRequest(w, r) {
return
}

assert.Equal(http.MethodGet, r.Method)
assert.Equal("/practices/"+strconv.FormatInt(id, 10), r.URL.Path)

b, err := json.Marshal(&Practice{
ID: id,
})
assert.NoError(err)

w.Header().Set("Content-Type", "application/json")
//nolint
w.Write(b)
}))
defer srv.Close()

client := NewClient(srv.Client(), srv.URL+"/token", "", "", srv.URL)
svc := PracticeService{client}

found, res, err := svc.Get(context.Background(), id)
assert.NotNil(found)
assert.NotNil(res)
assert.NoError(err)
}