Skip to content

Commit

Permalink
fix: workaround for GetRecentPatients (#482)
Browse files Browse the repository at this point in the history
Until #458 is fixed
  • Loading branch information
MaxSchaefer authored Oct 26, 2023
1 parent 3d20029 commit 4520ff1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions services/task-svc/internal/models/patient_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
import (
"github.com/google/uuid"
"gorm.io/plugin/soft_delete"
"time"
)

type PatientBase struct {
Expand All @@ -18,4 +19,6 @@ type Patient struct {
Bed *Bed `gorm:"foreignKey:BedID"`
Tasks []Task `gorm:"foreignKey:PatientId"`
IsDischarged soft_delete.DeletedAt `gorm:"column:is_discharged;softDelete:flag;default:0"`
CreatedAt time.Time
UpdatedAt time.Time
}
15 changes: 15 additions & 0 deletions services/task-svc/internal/patient/patient.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ func (ServiceServer) GetRecentPatients(ctx context.Context, req *pb.GetRecentPat
patientRepo := repositories.PatientRepo(ctx)
log := zlog.Ctx(ctx)

organizationID, err := common.GetOrganizationID(ctx)
if err != nil {
return nil, err
}

// TODO: Auth

recentPatientIdsStrs, err := tracking.GetRecentPatientsForUser(ctx)
Expand All @@ -212,6 +217,16 @@ func (ServiceServer) GetRecentPatients(ctx context.Context, req *pb.GetRecentPat
return nil, status.Error(codes.Internal, err.Error())
}

// WORKAROUND: Until https://github.com/helpwave/services/issues/458 is fixed
if len(recentPatientIdsStrs) == 0 {
log.Debug().Msg("recentPatientIdsStrs was empty")
if patients, err := patientRepo.GetLastUpdatedPatientsForOrganization(4, organizationID); err == nil {
recentPatientIdsStrs = hwutil.Map(patients, func(patient models.Patient) string {
return patient.ID.String()
})
}
}

// parse all valid uuids into an array
recentPatientIds := hwutil.FlatMap(recentPatientIdsStrs, func(s string) *uuid.UUID {
parsedUUID, err := uuid.Parse(s)
Expand Down
15 changes: 15 additions & 0 deletions services/task-svc/internal/repositories/patient_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (r *PatientRepository) GetPatientsByIdsWithBedAndRoom(ids []uuid.UUID) ([]m
Where("id IN ?", ids).
Preload("Bed").
Preload("Bed.Room").
Order("updated_at DESC").
Find(&patients)

if err := query.Error; err != nil {
Expand Down Expand Up @@ -141,6 +142,20 @@ func (r *PatientRepository) GetRoomsWithBedsWithActivePatientsForWard(wardID uui
return rooms, nil
}

func (r *PatientRepository) GetLastUpdatedPatientsForOrganization(maxAmount uint32, organizationID uuid.UUID) ([]models.Patient, error) {
var patients []models.Patient
query := r.db.
Where("organization_id = ?", organizationID).
Limit(int(maxAmount)).
Order("updated_at DESC").
Find(&patients)

if err := query.Error; err != nil {
return nil, err
}
return patients, nil
}

func (r *PatientRepository) UpdatePatient(patientID uuid.UUID, updates map[string]interface{}) (*models.Patient, error) {
patient := &models.Patient{ID: patientID}
query := r.db.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE patients
DROP COLUMN created_at,
DROP COLUMN updated_at;

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE IF EXISTS patients
ADD COLUMN IF NOT EXISTS created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN IF NOT EXISTS updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP;

0 comments on commit 4520ff1

Please sign in to comment.