Skip to content

Commit

Permalink
[BACK-3253] Add the ability to assign a tag to all clinic patients
Browse files Browse the repository at this point in the history
  • Loading branch information
toddkazakov committed Dec 6, 2024
1 parent 39f6472 commit 3254eb1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
8 changes: 7 additions & 1 deletion api/patients.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 8 additions & 2 deletions patients/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 nil
if patientIds != nil {
tagsFieldisNullSelector["userId"] = bson.M{"$in": patientIds}
}

tagsFieldisNullUpdate := bson.M{
Expand All @@ -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 nil
if patientIds != nil {
selector["userId"] = bson.M{"$in": patientIds}
}

update := bson.M{
Expand Down
45 changes: 44 additions & 1 deletion patients/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 3254eb1

Please sign in to comment.