Skip to content

Commit

Permalink
HAPP-1868: Handle special authority with no title
Browse files Browse the repository at this point in the history
spotless
  • Loading branch information
bsavoini-ey committed Mar 7, 2024
1 parent 65815bb commit e458279
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 9 deletions.
9 changes: 9 additions & 0 deletions app/src/main/java/ca/bc/gov/bchealth/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.work.WorkManager
import ca.bc.gov.bchealth.databinding.ActivityMainBinding
import ca.bc.gov.bchealth.ui.inappupdate.InAppUpdateActivity
import ca.bc.gov.bchealth.utils.InAppUpdateHelper
import ca.bc.gov.bchealth.utils.showPartialRecordsErrorMessage
import ca.bc.gov.bchealth.utils.showServiceDownMessage
import ca.bc.gov.bchealth.utils.viewBindings
import ca.bc.gov.bchealth.viewmodel.AnalyticsFeatureViewModel
Expand Down Expand Up @@ -169,6 +170,14 @@ class MainActivity : AppCompatActivity() {
binding.navHostFragment.showServiceDownMessage(this)
return
}

if (workData.getBoolean(
FetchAuthenticatedHealthRecordsWorker.FailureReason.PARTIAL_RECORDS_ERROR.value,
false
)
) {
binding.navHostFragment.showPartialRecordsErrorMessage(this)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.bc.gov.bchealth.usecases.records

import ca.bc.gov.common.exceptions.PartialRecordsException
import ca.bc.gov.common.model.AuthParametersDto
import ca.bc.gov.common.model.specialauthority.SpecialAuthorityDto
import ca.bc.gov.repository.di.IoDispatcher
Expand All @@ -20,7 +21,19 @@ class FetchSpecialAuthoritiesUseCase @Inject constructor(
authParameters, specialAuthorityRepository::getSpecialAuthority
)

insertSpecialAuthority(patientId, specialAuthorities)
var filteredOut = false
val filteredList = specialAuthorities?.filter {
val hasTitle = it.drugName.isNullOrBlank().not()
if (hasTitle.not()) {
filteredOut = true
}
hasTitle
}

insertSpecialAuthority(patientId, filteredList)
if (filteredOut) {
throw PartialRecordsException()
}
}

private suspend fun insertSpecialAuthority(
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/ca/bc/gov/bchealth/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ fun View.showNoInternetConnectionMessage(context: Context) {
showErrorSnackbar(context.getString(R.string.no_internet_connection))
}

fun View.showPartialRecordsErrorMessage(context: Context) {
showErrorSnackbar(context.getString(R.string.partial_records_error_message))
}

fun View?.showErrorSnackbar(message: String, anchor: View? = null) {
this ?: return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import ca.bc.gov.bchealth.usecases.records.FetchPatientDataUseCase
import ca.bc.gov.bchealth.usecases.records.FetchSpecialAuthoritiesUseCase
import ca.bc.gov.bchealth.usecases.records.FetchVaccinesUseCase
import ca.bc.gov.common.BuildConfig.LOCAL_API_VERSION
import ca.bc.gov.common.exceptions.PartialRecordsException
import ca.bc.gov.common.model.AuthParametersDto
import ca.bc.gov.common.model.dependents.DependentDto
import ca.bc.gov.repository.DependentsRepository
Expand Down Expand Up @@ -110,7 +111,11 @@ class FetchAuthenticatedHealthRecordsWorker @AssistedInject constructor(
return Result.failure()
}

val isApiFailed = loadRecords(patientId, authParameters, dependents)
val isApiFailed = try {
loadRecords(patientId, authParameters, dependents)
} catch (e: PartialRecordsException) {
return respondToFailure(FailureReason.PARTIAL_RECORDS_ERROR, true)
}

return if (isApiFailed) {
return respondToFailure(FailureReason.IS_RECORD_FETCH_FAILED, true)
Expand All @@ -130,7 +135,9 @@ class FetchAuthenticatedHealthRecordsWorker @AssistedInject constructor(
val tasks = mutableListOf<Deferred<ListenableWorker.Result>>()
val dataSetFlag = mobileConfigRepository.getPatientDataSetFeatureFlags()

if (dataSetFlag.isMedicationEnabled()) { tasks.add(async { fetchMedicationsUseCase.execute(patientId, authParameters) }) }
if (dataSetFlag.isMedicationEnabled()) {
tasks.add(async { fetchMedicationsUseCase.execute(patientId, authParameters) })
}

if (dataSetFlag.isClinicalDocumentEnabled()) {
tasks.add(
Expand All @@ -154,9 +161,27 @@ class FetchAuthenticatedHealthRecordsWorker @AssistedInject constructor(
)
}

if (dataSetFlag.isHealthVisitEnabled()) { tasks.add(runTaskAsync { fetchHealthVisitsUseCase.execute(patientId, authParameters) }) }
if (dataSetFlag.isHealthVisitEnabled()) {
tasks.add(
runTaskAsync {
fetchHealthVisitsUseCase.execute(
patientId,
authParameters
)
}
)
}

if (dataSetFlag.isHospitalVisitEnabled()) { tasks.add(runTaskAsync { fetchHospitalVisitsUseCase.execute(patientId, authParameters) }) }
if (dataSetFlag.isHospitalVisitEnabled()) {
tasks.add(
runTaskAsync {
fetchHospitalVisitsUseCase.execute(
patientId,
authParameters
)
}
)
}

if (dataSetFlag.isImmunizationEnabled()) {
tasks.add(
Expand All @@ -169,9 +194,20 @@ class FetchAuthenticatedHealthRecordsWorker @AssistedInject constructor(
)
}

if (dataSetFlag.isLabResultEnabled()) { tasks.add(runTaskAsync { fetchLabOrdersUseCase.execute(patientId, authParameters) }) }
if (dataSetFlag.isLabResultEnabled()) {
tasks.add(runTaskAsync { fetchLabOrdersUseCase.execute(patientId, authParameters) })
}

if (dataSetFlag.isSpecialAuthorityRequestEnabled()) { tasks.add(runTaskAsync { fetchSpecialAuthoritiesUseCase.execute(patientId, authParameters) }) }
if (dataSetFlag.isSpecialAuthorityRequestEnabled()) {
tasks.add(
runTaskAsync {
fetchSpecialAuthoritiesUseCase.execute(
patientId,
authParameters
)
}
)
}

tasks.add(
runTaskAsync {
Expand All @@ -185,7 +221,14 @@ class FetchAuthenticatedHealthRecordsWorker @AssistedInject constructor(

val taskResult = tasks.awaitAll()

isApiFailed = taskResult.contains(Result.failure())
val failedTasks = taskResult.filter { it != Result.success() }
failedTasks.forEach {
val data = it.outputData
data.getBoolean(FailureReason.PARTIAL_RECORDS_ERROR.value, true)
throw PartialRecordsException()
}

isApiFailed = failedTasks.isNotEmpty()
}
return isApiFailed
}
Expand Down Expand Up @@ -214,6 +257,10 @@ class FetchAuthenticatedHealthRecordsWorker @AssistedInject constructor(
try {
task.invoke()
Result.success()
} catch (e: PartialRecordsException) {
Log.e("RecordsWorker", "Handling Exception:")
e.printStackTrace()
respondToFailure(FailureReason.PARTIAL_RECORDS_ERROR, true)
} catch (e: Exception) {
Log.e("RecordsWorker", "Handling Exception:")
e.printStackTrace()
Expand All @@ -228,6 +275,7 @@ class FetchAuthenticatedHealthRecordsWorker @AssistedInject constructor(
enum class FailureReason(val value: String) {
APP_UPDATE_REQUIRED("appUpdateRequired"),
IS_HG_SERVICES_UP("isHgServicesUp"),
IS_RECORD_FETCH_FAILED("IS_RECORD_FETCH_FAILED")
IS_RECORD_FETCH_FAILED("IS_RECORD_FETCH_FAILED"),
PARTIAL_RECORDS_ERROR("PARTIAL_RECORDS_ERROR")
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@
<string name="service_down">Maintenance is underway. Please try later.</string>
<string name="dismiss">Dismiss</string>
<string name="no_internet_connection">Unable to refresh your data. Please check your internet connection.</string>
<string name="partial_records_error_message">Some partial records were not displayed.</string>
<string name="comments_error_max_character">Maximum 1000 characters</string>

<!--Timeline View-->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ca.bc.gov.common.exceptions

class PartialRecordsException(
message: String? = null
) : Exception(message)

0 comments on commit e458279

Please sign in to comment.