Skip to content

Commit

Permalink
Fix set group member to use configured group member type (#3018)
Browse files Browse the repository at this point in the history
* Fix set group member to use configured group member type

Signed-off-by: Elly Kitoto <[email protected]>

* syntax fix

---------

Signed-off-by: Elly Kitoto <[email protected]>
Co-authored-by: pld <[email protected]>
  • Loading branch information
ellykits and pld authored Jan 26, 2024
1 parent 2d03207 commit e353865
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ constructor(
)
addMemberToGroup(
resource = this,
memberResourceType = questionnaireConfig.groupResource?.memberResourceType,
groupIdentifier = questionnaireConfig.groupResource?.groupIdentifier,
)

Expand Down Expand Up @@ -667,7 +668,11 @@ constructor(
* NOT the same as the retrieved [GroupResourceConfig.groupIdentifier] (Cannot add a [Group] as
* member of itself.
*/
suspend fun addMemberToGroup(resource: Resource, groupIdentifier: String?) {
suspend fun addMemberToGroup(
resource: Resource,
memberResourceType: ResourceType?,
groupIdentifier: String?,
) {
// Load the Group resource from the database to get the updated one
val group =
groupIdentifier?.extractLogicalIdUuid()?.let { loadResource(ResourceType.Group, it) }
Expand All @@ -692,7 +697,7 @@ constructor(
ResourceType.Practitioner,
ResourceType.PractitionerRole,
ResourceType.Specimen,
)
) && resource.resourceType == memberResourceType
) {
group.addMember(Group.GroupMemberComponent().apply { entity = reference })
defaultRepository.addOrUpdate(resource = group)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import org.hl7.fhir.r4.model.Group
import org.hl7.fhir.r4.model.IdType
import org.hl7.fhir.r4.model.IntegerType
import org.hl7.fhir.r4.model.ListResource
import org.hl7.fhir.r4.model.Location
import org.hl7.fhir.r4.model.Observation
import org.hl7.fhir.r4.model.Parameters
import org.hl7.fhir.r4.model.Patient
Expand Down Expand Up @@ -706,19 +707,30 @@ class QuestionnaireViewModelTest : RobolectricTest() {
active = true
}

questionnaireConfig =
questionnaireConfig.copy(
groupResource =
GroupResourceConfig(
groupIdentifier = group.logicalId,
memberResourceType = ResourceType.Patient,
),
)

coEvery { fhirEngine.get(ResourceType.Group, group.logicalId) } returns group
coEvery { fhirEngine.update(any()) } just runs

// Attempting to add Group as member of itself should fail
questionnaireViewModel.addMemberToGroup(
resource = group,
memberResourceType = questionnaireConfig.groupResource?.memberResourceType,
groupIdentifier = group.logicalId,
)
coVerify(exactly = 0) { defaultRepository.addOrUpdate(resource = group) }

// Should add member to existing group
questionnaireViewModel.addMemberToGroup(
resource = patient,
memberResourceType = questionnaireConfig.groupResource?.memberResourceType,
groupIdentifier = group.logicalId,
)

Expand All @@ -736,6 +748,14 @@ class QuestionnaireViewModelTest : RobolectricTest() {
coEvery { fhirEngine.get(ResourceType.Group, anotherGroup.logicalId) } returns anotherGroup
questionnaireViewModel.addMemberToGroup(
resource = patient,
memberResourceType = questionnaireConfig.groupResource?.memberResourceType,
groupIdentifier = group.logicalId,
)
coVerify(exactly = 0) { defaultRepository.addOrUpdate(resource = anotherGroup) }

questionnaireViewModel.addMemberToGroup(
resource = Location().apply { id = "some-loc-id" },
memberResourceType = questionnaireConfig.groupResource?.memberResourceType,
groupIdentifier = group.logicalId,
)
coVerify(exactly = 0) { defaultRepository.addOrUpdate(resource = anotherGroup) }
Expand Down

0 comments on commit e353865

Please sign in to comment.