2022-08-28 11:51:58 -06:00
|
|
|
package cigna
|
|
|
|
|
|
|
|
import (
|
2022-08-30 20:03:24 -06:00
|
|
|
"context"
|
2022-08-30 22:36:40 -06:00
|
|
|
"fmt"
|
2022-08-28 11:51:58 -06:00
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/config"
|
2022-08-30 20:03:24 -06:00
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/database"
|
2022-08-28 11:51:58 -06:00
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/hub/internal/fhir/base"
|
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/models"
|
2022-08-30 20:03:24 -06:00
|
|
|
"github.com/fastenhealth/gofhir-models/fhir401"
|
|
|
|
fhirutils "github.com/fastenhealth/gofhir-models/fhir401/utils"
|
2022-08-30 22:36:40 -06:00
|
|
|
"github.com/google/uuid"
|
2022-08-28 11:51:58 -06:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CignaClient struct {
|
2022-08-30 20:03:24 -06:00
|
|
|
*base.FHIR401Client
|
2022-08-28 11:51:58 -06:00
|
|
|
}
|
|
|
|
|
2022-08-30 20:03:24 -06:00
|
|
|
func NewClient(appConfig config.Interface, globalLogger logrus.FieldLogger, source models.Source, testHttpClient ...*http.Client) (base.Client, error) {
|
|
|
|
baseClient, err := base.NewFHIR401Client(appConfig, globalLogger, source, testHttpClient...)
|
2022-08-28 11:51:58 -06:00
|
|
|
return CignaClient{
|
|
|
|
baseClient,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
2022-08-30 20:03:24 -06:00
|
|
|
func (c CignaClient) SyncAll(db database.DatabaseRepository) error {
|
2022-08-28 11:51:58 -06:00
|
|
|
|
2022-08-30 20:03:24 -06:00
|
|
|
bundle, err := c.GetPatientEverything(c.Source.PatientId)
|
2022-08-28 20:09:39 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-30 20:03:24 -06:00
|
|
|
resources := []interface{}{}
|
|
|
|
for _, bundleEntry := range bundle.Entry {
|
|
|
|
resource, _ := fhirutils.MapToResource(bundleEntry.Resource, false)
|
|
|
|
resources = append(resources, resource)
|
|
|
|
}
|
|
|
|
|
2022-08-30 22:36:40 -06:00
|
|
|
resourceRefLookup := map[string]uuid.UUID{}
|
|
|
|
|
2022-08-30 21:24:37 -06:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// Patient
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
2022-08-30 20:03:24 -06:00
|
|
|
patientResources := []fhir401.Patient{}
|
|
|
|
for _, resource := range resources {
|
|
|
|
if patient, isPatient := resource.(fhir401.Patient); isPatient {
|
|
|
|
patientResources = append(patientResources, patient)
|
|
|
|
}
|
2022-08-28 20:09:39 -06:00
|
|
|
}
|
2022-08-30 22:36:40 -06:00
|
|
|
apiProfiles, err := c.ProcessPatients(patientResources)
|
|
|
|
for _, profile := range apiProfiles {
|
|
|
|
err = db.UpsertProfile(context.Background(), &profile)
|
2022-08-30 20:03:24 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-30 22:36:40 -06:00
|
|
|
//add upserted resource uuids to lookup
|
|
|
|
resourceRefLookup[fmt.Sprintf("%s/%s", profile.SourceResourceType, profile.SourceResourceID)] = profile.ID
|
2022-08-30 20:03:24 -06:00
|
|
|
}
|
|
|
|
|
2022-08-30 21:24:37 -06:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
2022-08-30 22:36:40 -06:00
|
|
|
// Organization
|
2022-08-30 21:24:37 -06:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2022-08-30 22:36:40 -06:00
|
|
|
organizations := []fhir401.Organization{}
|
|
|
|
for _, resource := range resources {
|
|
|
|
if org, isOrganization := resource.(fhir401.Organization); isOrganization {
|
|
|
|
organizations = append(organizations, org)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
apiOrgs, err := c.ProcessOrganizations(organizations)
|
|
|
|
for _, apiOrg := range apiOrgs {
|
|
|
|
err = db.UpsertOrganziation(context.Background(), &apiOrg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//add upserted resource uuids to lookup
|
|
|
|
resourceRefLookup[fmt.Sprintf("%s/%s", apiOrg.SourceResourceType, apiOrg.SourceResourceID)] = apiOrg.ID
|
|
|
|
}
|
|
|
|
|
2022-08-28 11:51:58 -06:00
|
|
|
return nil
|
|
|
|
}
|