Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya-07 committed Sep 16, 2024
1 parent 905bc37 commit e582b1b
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private constructor(
Intent.ORDER -> OrderPhase(repository, requestResource)
else ->
throw IllegalArgumentException(
"Couldn't create the flow for ${requestResource.getIntent().name} intent. Supported intents are 'proposal', 'plan', 'order'",
"Couldn't create the flow for ${requestResource.getIntent().code} intent. Supported intents are 'proposal', 'plan' and 'order'.",
)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class PerformPhase<E : CPGEventResource<*>>(val repository: Repository, e: E) :
val inputRequest = (inputPhase as BaseRequestPhase<*>).getRequest()

check(inputRequest.getIntent() in AllowedIntents) {
"Event can't be created for a request with ${inputRequest.getIntent().name} intent."
"Event can't be created for a request with ${inputRequest.getIntent().code} intent."
}

check(inputRequest.getStatus() == Status.ACTIVE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class OrderPhase<R : CPGRequestResource<*>>(repository: Repository, r: R) :
val inputRequest = (inputPhase as Phase.RequestPhase<*>).getRequest()

check(inputRequest.getIntent() in AllowedIntents) {
"Order can't be created for a request with ${inputRequest.getIntent().name} intent."
"Order can't be created for a request with ${inputRequest.getIntent().code} intent."
}

check(inputRequest.getStatus() == Status.ACTIVE) {
Expand Down Expand Up @@ -110,7 +110,7 @@ class OrderPhase<R : CPGRequestResource<*>>(repository: Repository, r: R) :
}

require(inputOrder.getIntent() == Intent.ORDER) {
"Input request has '${inputOrder.getIntent().name}' intent."
"Input request has '${inputOrder.getIntent().code}' intent."
}

require(inputOrder.getStatus() in AllowedStatusForPhaseStart) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PlanPhase<R : CPGRequestResource<*>>(repository: Repository, r: R) :

val inputRequest = (inputPhase as Phase.RequestPhase<*>).getRequest()
check(inputRequest.getIntent() == Intent.PROPOSAL) {
"Plan can't be created for a request with ${inputRequest.getIntent().name} intent."
"Plan can't be created for a request with ${inputRequest.getIntent().code} intent."
}

check(inputRequest.getStatus() == Status.ACTIVE) {
Expand Down Expand Up @@ -107,7 +107,7 @@ class PlanPhase<R : CPGRequestResource<*>>(repository: Repository, r: R) :
}

require(draftPlan.getIntent() == Intent.PLAN) {
"Input request has '${draftPlan.getIntent().name}' intent."
"Input request has '${draftPlan.getIntent().code}' intent."
}

require(draftPlan.getStatus() in AllowedStatusForPhaseStart) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CPGCommunicationRequest(override val resource: CommunicationRequest) :
resource.getExtensionByUrl("http://hl7.org/fhir/StructureDefinition/request-intent")?.let {
Intent.of(it.value?.primitiveValue())
}
?: Intent.NULL
?: Intent.of(null)

override fun setStatus(status: Status, reason: String?) {
resource.status = CommunicationRequest.CommunicationRequestStatus.fromCode(status.string)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ sealed class CPGRequestResource<R>(internal open val resource: R) where R : Reso
val logicalId: String
get() = resource.logicalId

abstract fun setIntent(intent: Intent)
internal abstract fun setIntent(intent: Intent)

abstract fun getIntent(): Intent
internal abstract fun getIntent(): Intent

abstract fun setStatus(status: Status, reason: String? = null)

Expand All @@ -73,7 +73,7 @@ sealed class CPGRequestResource<R>(internal open val resource: R) where R : Reso

internal abstract fun copy(): CPGRequestResource<R>

fun copy(id: String, status: Status, intent: Intent): CPGRequestResource<R> {
internal fun copy(id: String, status: Status, intent: Intent): CPGRequestResource<R> {
val parent: CPGRequestResource<R> = this
return copy().apply {
resource.idElement = IdType.of(resource).setValue(id)
Expand Down Expand Up @@ -143,32 +143,28 @@ sealed class CPGRequestResource<R>(internal open val resource: R) where R : Reso
}
}

enum class Intent(val code: String?) {
PROPOSAL("proposal"),
PLAN("plan"),
DIRECTIVE("directive"),
ORDER("order"),
ORIGINALORDER("original-order"),
REFLEXORDER("reflex-order"),
FILLERORDER("filler-order"),
INSTANCEORDER("instance-order"),
OPTION("option"),
NULL(null),
;
/**
* PROPOSAL, PLAN and ORDER are the only intents we are interested in. All the other Request Intent
* values are represented by OTHER. See
* [codesystem-request-intent](https://www.hl7.org/FHIR/codesystem-request-intent.html) for the list
* of intents.
*/
internal sealed class Intent(val code: String?) {
data object PROPOSAL : Intent("proposal")

data object PLAN : Intent("plan")

data object ORDER : Intent("order")

class OTHER(code: String?) : Intent(code)

companion object {
fun of(code: String?): Intent {
return when (code) {
"proposal" -> PROPOSAL
"plan" -> PLAN
"directive" -> DIRECTIVE
"order" -> ORDER
"original-order" -> ORIGINALORDER
"reflex-order" -> REFLEXORDER
"filler-order" -> FILLERORDER
"instance-order" -> INSTANCEORDER
"option" -> OPTION
else -> NULL
else -> OTHER(code)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,18 @@ class ActivityFlowTest {
fun `communication request flow new api`(): Unit = runBlockingOnWorkerThread {
val cpgCommunicationRequest =
CPGRequestResource.of(
CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile("http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest")

addPayload().apply { content = StringType("Proposal") }
},
)

CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile(
"http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest",
)

addPayload().apply { content = StringType("Proposal") }
},
)
.apply { setIntent(Intent.PROPOSAL) }
val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine)
repository.create(cpgCommunicationRequest.resource)

Expand Down Expand Up @@ -422,15 +424,18 @@ class ActivityFlowTest {
fun `draftPlan is success when flow is in proposal phase`(): Unit = runBlockingOnWorkerThread {
val cpgCommunicationRequest =
CPGRequestResource.of(
CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile("http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest")

addPayload().apply { content = StringType("Proposal") }
},
)
CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile(
"http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest",
)

addPayload().apply { content = StringType("Proposal") }
},
)
.apply { setIntent(Intent.PROPOSAL) }
val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine)
repository.create(cpgCommunicationRequest.resource)

Expand Down Expand Up @@ -523,15 +528,18 @@ class ActivityFlowTest {
fun `draftOrder is success when flow is in proposal phase`(): Unit = runBlockingOnWorkerThread {
val cpgCommunicationRequest =
CPGRequestResource.of(
CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile("http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest")

addPayload().apply { content = StringType("Proposal") }
},
)
CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile(
"http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest",
)

addPayload().apply { content = StringType("Proposal") }
},
)
.apply { setIntent(Intent.PROPOSAL) }
val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine)
repository.create(cpgCommunicationRequest.resource)

Expand Down Expand Up @@ -624,15 +632,18 @@ class ActivityFlowTest {
fun `draftPerform is success when flow is in proposal phase`(): Unit = runBlockingOnWorkerThread {
val cpgCommunicationRequest =
CPGRequestResource.of(
CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile("http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest")

addPayload().apply { content = StringType("Proposal") }
},
)
CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile(
"http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest",
)

addPayload().apply { content = StringType("Proposal") }
},
)
.apply { setIntent(Intent.PROPOSAL) }
val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine)
repository.create(cpgCommunicationRequest.resource)

Expand Down Expand Up @@ -725,15 +736,18 @@ class ActivityFlowTest {
fun `getCurrentPhase should return the current phase of the flow`() = runBlockingOnWorkerThread {
val cpgCommunicationRequest =
CPGRequestResource.of(
CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile("http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest")

addPayload().apply { content = StringType("Proposal") }
},
)
CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile(
"http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest",
)

addPayload().apply { content = StringType("Proposal") }
},
)
.apply { setIntent(Intent.PROPOSAL) }
val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine)
repository.create(cpgCommunicationRequest.resource)

Expand Down

0 comments on commit e582b1b

Please sign in to comment.