From 4520ff17aa0f3234020e5ca0eedd6a0b169c4b9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20Sch=C3=A4fer?= <40192999+MaxSchaefer@users.noreply.github.com> Date: Thu, 26 Oct 2023 18:41:08 +0200 Subject: [PATCH] fix: workaround for GetRecentPatients (#482) Until https://github.com/helpwave/services/issues/458 is fixed --- .../task-svc/internal/models/patient_models.go | 3 +++ services/task-svc/internal/patient/patient.go | 15 +++++++++++++++ .../internal/repositories/patient_repository.go | 15 +++++++++++++++ .../000019_patients_created_updated_at.down.sql | 4 ++++ .../000019_patients_created_updated_at.up.sql | 3 +++ 5 files changed, 40 insertions(+) create mode 100644 services/task-svc/migrations/000019_patients_created_updated_at.down.sql create mode 100644 services/task-svc/migrations/000019_patients_created_updated_at.up.sql diff --git a/services/task-svc/internal/models/patient_models.go b/services/task-svc/internal/models/patient_models.go index 6def4ca45..5a6e449f2 100644 --- a/services/task-svc/internal/models/patient_models.go +++ b/services/task-svc/internal/models/patient_models.go @@ -3,6 +3,7 @@ package models import ( "github.com/google/uuid" "gorm.io/plugin/soft_delete" + "time" ) type PatientBase struct { @@ -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 } diff --git a/services/task-svc/internal/patient/patient.go b/services/task-svc/internal/patient/patient.go index 662b16a8a..d361b86ac 100644 --- a/services/task-svc/internal/patient/patient.go +++ b/services/task-svc/internal/patient/patient.go @@ -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) @@ -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) diff --git a/services/task-svc/internal/repositories/patient_repository.go b/services/task-svc/internal/repositories/patient_repository.go index 31a483864..767b274fb 100644 --- a/services/task-svc/internal/repositories/patient_repository.go +++ b/services/task-svc/internal/repositories/patient_repository.go @@ -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 { @@ -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. diff --git a/services/task-svc/migrations/000019_patients_created_updated_at.down.sql b/services/task-svc/migrations/000019_patients_created_updated_at.down.sql new file mode 100644 index 000000000..1d348ac9b --- /dev/null +++ b/services/task-svc/migrations/000019_patients_created_updated_at.down.sql @@ -0,0 +1,4 @@ +ALTER TABLE patients + DROP COLUMN created_at, + DROP COLUMN updated_at; + diff --git a/services/task-svc/migrations/000019_patients_created_updated_at.up.sql b/services/task-svc/migrations/000019_patients_created_updated_at.up.sql new file mode 100644 index 000000000..5ca224af8 --- /dev/null +++ b/services/task-svc/migrations/000019_patients_created_updated_at.up.sql @@ -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;