From 29b87d1f9cf64f0e6e7a37915745210caac21e90 Mon Sep 17 00:00:00 2001 From: Todd Kazakov Date: Fri, 6 Dec 2024 16:35:08 +0200 Subject: [PATCH] [BACK-3253] Add the ability to assign a tag to all clinic patients --- api/patients.go | 8 +++++++- go.mod | 2 +- patients/repo.go | 10 ++++++++-- patients/repo_test.go | 45 ++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/api/patients.go b/api/patients.go index 11542e78..d57b41bc 100644 --- a/api/patients.go +++ b/api/patients.go @@ -345,7 +345,13 @@ func (h *Handler) AssignPatientTagToClinicPatients(ec echo.Context, clinicId Cli return err } - err := h.Patients.AssignPatientTagToClinicPatients(ctx, string(clinicId), string(patientTagId), dto) + + // We pass an empty request body as nil which will target all clinic patients for tag assignment + if ec.Request().Body == http.NoBody { + dto = nil + } + + err := h.Patients.AssignPatientTagToClinicPatients(ctx, clinicId, patientTagId, dto) if err != nil { return err diff --git a/go.mod b/go.mod index 511dc5cd..7c4c86dc 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/tidepool-org/clinic -go 1.23 +go 1.23.0 require ( github.com/DataDog/datadog-agent/pkg/util/fxutil v0.59.0 diff --git a/patients/repo.go b/patients/repo.go index 3ce0b2fa..c742e2e3 100644 --- a/patients/repo.go +++ b/patients/repo.go @@ -696,7 +696,10 @@ func (r *repository) AssignPatientTagToClinicPatients(ctx context.Context, clini tagsFieldisNullSelector := bson.M{ "clinicId": clinicObjId, "tags": bson.M{"$type": bson.TypeNull}, - "userId": bson.M{"$in": patientIds}, + } + // Apply the tag to all patients if the slice is EXPLICITLY nil (and not just empty) + if patientIds != nil { + tagsFieldisNullSelector["userId"] = bson.M{"$in": patientIds} } tagsFieldisNullUpdate := bson.M{ @@ -713,7 +716,10 @@ func (r *repository) AssignPatientTagToClinicPatients(ctx context.Context, clini selector := bson.M{ "clinicId": clinicObjId, "tags": bson.M{"$nin": bson.A{patientTagId}}, - "userId": bson.M{"$in": patientIds}, + } + // Apply the tag to all patients if the slice is EXPLICITLY nil (and not just empty) + if patientIds != nil { + selector["userId"] = bson.M{"$in": patientIds} } update := bson.M{ diff --git a/patients/repo_test.go b/patients/repo_test.go index fda73abe..2b24b396 100644 --- a/patients/repo_test.go +++ b/patients/repo_test.go @@ -532,7 +532,7 @@ var _ = Describe("Patients Repository", func() { newPatientTag := primitive.NewObjectID() clinicId := primitive.NewObjectID() - // Add set common clinic ID for all patients + // Сet common clinic ID for all patients for _, patient := range allPatients { selector := bson.M{ "clinicId": patient.ClinicId, @@ -572,6 +572,49 @@ var _ = Describe("Patients Repository", func() { Expect(err).To(BeNil()) Expect(res).To(Equal(int64(2))) }) + + It("assigns the correct patient tag to all patients", func() { + newPatientTag := primitive.NewObjectID() + clinicId := primitive.NewObjectID() + + // Add set common clinic ID for all patients + for _, patient := range allPatients { + selector := bson.M{ + "clinicId": patient.ClinicId, + "userId": patient.UserId, + } + + update := bson.M{ + "$set": bson.M{ + "clinicId": clinicId, + }, + } + + _, err := collection.UpdateOne(context.Background(), selector, update) + Expect(err).ToNot(HaveOccurred()) + } + + selector := bson.M{ + "tags": newPatientTag, + } + + // No patients should be returned when querying for the new tag + res, err := collection.CountDocuments(context.Background(), selector) + Expect(err).To(BeNil()) + Expect(res).To(Equal(int64(0))) + + // Nil array will apply the tag to all patients + var patientIds []string + + // Perform the assign operation + err = repo.AssignPatientTagToClinicPatients(context.Background(), clinicId.Hex(), newPatientTag.Hex(), patientIds) + Expect(err).ToNot(HaveOccurred()) + + // All patients should have matching tag + res, err = collection.CountDocuments(context.Background(), selector) + Expect(err).To(BeNil()) + Expect(res).To(Equal(int64(len(allPatients)))) + }) }) Describe("Delete non-custodial patients", func() {