Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BACK-3253] Add the ability to assign a tag to all clinic patients #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 EXPLICITLY nil (and not just empty)
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 EXPLICITLY nil (and not just empty)
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