Skip to content

Commit

Permalink
Merge branch 'main' into 1522-pnc-resources-closure
Browse files Browse the repository at this point in the history
  • Loading branch information
dubdabasoduba authored Jun 23, 2023
2 parents 53239fd + 1c12391 commit 4a98423
Show file tree
Hide file tree
Showing 2 changed files with 333 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import ca.uhn.fhir.parser.IParser
import ca.uhn.fhir.rest.gclient.ReferenceClientParam
import com.google.android.fhir.FhirEngine
import com.google.android.fhir.datacapture.extensions.createQuestionnaireResponseItem
import com.google.android.fhir.get
import com.google.android.fhir.logicalId
import com.google.android.fhir.search.search
import java.time.Duration
Expand All @@ -29,15 +30,13 @@ import java.util.LinkedList
import java.util.Locale
import java.util.UUID
import kotlin.math.abs
import org.hl7.fhir.TaskStatus
import org.hl7.fhir.exceptions.FHIRException
import org.hl7.fhir.r4.model.Base
import org.hl7.fhir.r4.model.BaseDateTimeType
import org.hl7.fhir.r4.model.CodeableConcept
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Composition
import org.hl7.fhir.r4.model.Condition
import org.hl7.fhir.r4.model.Encounter
import org.hl7.fhir.r4.model.Extension
import org.hl7.fhir.r4.model.Group
import org.hl7.fhir.r4.model.HumanName
Expand All @@ -55,6 +54,7 @@ import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.StructureMap
import org.hl7.fhir.r4.model.Task
import org.hl7.fhir.r4.model.Timing
import org.joda.time.Instant
import org.json.JSONException
import org.json.JSONObject
import org.smartregister.fhircore.engine.data.local.DefaultRepository
Expand Down Expand Up @@ -348,49 +348,46 @@ suspend fun Task.updateDependentTaskDueDate(
dependantTask.partOf.forEach { _ ->
if (dependantTask.executionPeriod.hasStart() &&
dependantTask.hasInput() &&
this.status.equals(Task.TaskStatus.REQUESTED)
dependantTask.status.equals(Task.TaskStatus.REQUESTED)
) {
this.output.forEach { taskOp ->
try {
val taskOutReference = taskOp.value as Reference
if (taskOp.value != null &&
taskOutReference.extractType()?.equals(ResourceType.Encounter) == true
) {
val taskRef = taskOutReference.reference
val dependantImmunization =
fhirEngine.search<Immunization> {
filter(
referenceParameter = ReferenceClientParam(PARTOF),
{ value = taskRef.extractLogicalIdUuid() }
)
}
dependantImmunization.filter { it.hasEncounter() }.forEach { immunization ->
val dependentTaskStartDate = dependantTask.executionPeriod.start
val immunizationDate =
immunization.occurrenceDateTimeType.dateTimeValue().valueAsCalendar
dependantTask.input.onEach { input ->
val dependentTaskInputDate = input.value.toString().toInt()
val difference =
abs(
Duration.between(
immunizationDate?.toInstant(),
dependentTaskStartDate.toInstant()
)
.toDays()
)
if (difference < dependentTaskInputDate) {
dependantTask
.apply {
executionPeriod.start =
Date.from(immunizationDate?.toInstant())
.plusDays(dependentTaskInputDate)
}
.run {
defaultRepository.addOrUpdate(
addMandatoryTags = true,
resource = dependantTask
if (taskOp.value != null) {
if (taskOutReference.extractType()?.equals(ResourceType.Immunization) == true) {
val immunizationRef = taskOutReference.reference
val immunization =
fhirEngine.get<Immunization>(immunizationRef.extractLogicalIdUuid())
if (immunization.isResource && immunization.hasOccurrence()) {
val dependentTaskStartDate = dependantTask.executionPeriod.start
val immunizationDate =
Instant.parse(immunization.occurrence.valueToString()).toDate()
dependantTask.input.onEach { input ->
if (input.value.isPrimitive) {
val dependentTaskInputDuration = input.value.valueToString().toInt()
val difference =
abs(
Duration.between(
immunizationDate.toInstant(),
dependentTaskStartDate.toInstant()
)
.toDays()
)
if (difference < dependentTaskInputDuration) {
dependantTask
.apply {
executionPeriod.start =
Date.from(immunizationDate?.toInstant())
.plusDays(dependentTaskInputDuration)
}
.run {
defaultRepository.addOrUpdate(
addMandatoryTags = true,
resource = dependantTask
)
}
}
}
}
}
}
Expand All @@ -407,3 +404,4 @@ suspend fun Task.updateDependentTaskDueDate(

const val REFERENCE = "reference"
const val PARTOF = "part-of"
const val ENCOUNTER = "encounter"
Loading

0 comments on commit 4a98423

Please sign in to comment.