Skip to content

Commit 36d5b6d

Browse files
committed
refactor(savingsAccount): enhance error handling in createSavingsAccount, improve string resource usage and null safety
1 parent 414e794 commit 36d5b6d

File tree

4 files changed

+24
-29
lines changed

4 files changed

+24
-29
lines changed

core/network/src/commonMain/kotlin/com/mifos/core/network/datamanager/DataManagerSavings.kt

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import com.mifos.room.entities.client.Savings
2323
import com.mifos.room.entities.templates.savings.SavingProductsTemplate
2424
import com.mifos.room.entities.templates.savings.SavingsAccountTransactionTemplateEntity
2525
import com.mifos.room.helper.SavingsDaoHelper
26-
import io.ktor.client.statement.HttpResponse
2726
import io.ktor.client.statement.bodyAsText
2827
import io.ktor.http.isSuccess
2928
import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -290,29 +289,22 @@ class DataManagerSavings(
290289
get() = mBaseApiManager.savingsService.allSavingsAccounts()
291290

292291
fun createSavingsAccount(savingsPayload: SavingsPayload?): Flow<Savings> {
293-
suspend fun extractErrorMessage(response: HttpResponse): String {
292+
return mBaseApiManager.savingsService.createSavingsAccount(savingsPayload).map { response ->
294293
val responseText = response.bodyAsText()
295-
var result = ""
296-
try {
297-
if (!response.status.isSuccess()) {
298-
val json = Json { ignoreUnknownKeys = true }
294+
val json = Json { ignoreUnknownKeys = true }
295+
296+
if (!response.status.isSuccess()) {
297+
val errorMessage = try {
299298
val errorResponse = json.decodeFromString<MifosError>(responseText)
300-
result = errorResponse.errors.firstOrNull()?.defaultUserMessage
299+
errorResponse.errors.firstOrNull()?.defaultUserMessage
301300
?: errorResponse.defaultUserMessage
302-
?: "Message Not Found"
301+
?: "HTTP ${response.status.value} ${response.status.description}"
302+
} catch (e: Exception) {
303+
"HTTP ${response.status.value} ${response.status.description}"
303304
}
304-
} catch (e: Exception) {
305-
result = "Failed to parse error response"
305+
throw IllegalStateException(errorMessage)
306306
}
307-
return result
308-
}
309-
return mBaseApiManager.savingsService.createSavingsAccount(savingsPayload).map { response ->
310-
val errorMessage = extractErrorMessage(response)
311-
if (errorMessage.isNotEmpty()) {
312-
throw Exception(errorMessage)
313-
}
314-
val responseText = response.bodyAsText()
315-
val json = Json { ignoreUnknownKeys = true }
307+
316308
json.decodeFromString<Savings>(responseText)
317309
}
318310
}

feature/savings/src/commonMain/composeResources/values/feature_savings_strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
<string name="feature_savings_new_savings_account_submitted_success">New Savings Account Application Submitted Successfully</string>
149149
<string name="feature_savings_new_savings_account_submitted_failed">Failed to apply for new savings account with the provided details</string>
150150

151-
151+
<string name="feature_savings_charges_active_count">%1$d active charges</string>
152152

153153
</resources>
154154

feature/savings/src/commonMain/kotlin/com/mifos/feature/savings/savingsAccountv2/SavingsAccountViewModel.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,17 @@ internal class SavingsAccountViewModel(
131131
dateFormat = "dd-MM-yyyy"
132132
productId = state.savingProductOptions.getOrNull(state.savingsProductSelected)?.id
133133
clientId = state.clientId
134-
fieldOfficerId = state.fieldOfficerOptions[state.fieldOfficerIndex].id
134+
fieldOfficerId = state.fieldOfficerOptions.getOrNull(state.fieldOfficerIndex)?.id
135135
submittedOnDate = state.submissionDate
136136
externalId = state.externalId
137137
allowOverdraft = state.isCheckedOverdraftAllowed
138138
enforceMinRequiredBalance = state.isCheckedMinimumBalance
139139
minRequiredOpeningBalance = state.minimumOpeningBalance
140140
minRequiredBalance = state.monthlyMinimumBalance
141141
lockinPeriodFrequency = state.frequency.toIntOrNull()
142-
lockinPeriodFrequencyType = state.freqTypeIndex.let {
143-
if (it != -1 && state.frequency.toIntOrNull() != null) it else null
144-
}
142+
lockinPeriodFrequencyType = state.savingsProductTemplate?.lockinPeriodFrequencyTypeOptions
143+
?.getOrNull(state.freqTypeIndex)?.id
144+
.takeIf { state.frequency.toIntOrNull() != null }
145145
charges = state.addedCharges.map { charges ->
146146
ChargesPayload(
147147
chargeId = charges.id,

feature/savings/src/commonMain/kotlin/com/mifos/feature/savings/savingsAccountv2/pages/PreviewPage.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package com.mifos.feature.savings.savingsAccountv2.pages
1111

1212
import androidclient.feature.savings.generated.resources.Res
1313
import androidclient.feature.savings.generated.resources.feature_savings_back
14+
import androidclient.feature.savings.generated.resources.feature_savings_charges_active_count
1415
import androidclient.feature.savings.generated.resources.feature_savings_currency
1516
import androidclient.feature.savings.generated.resources.feature_savings_days_in_year
1617
import androidclient.feature.savings.generated.resources.feature_savings_external_id
@@ -24,7 +25,6 @@ import androidclient.feature.savings.generated.resources.feature_savings_submiss
2425
import androidclient.feature.savings.generated.resources.feature_savings_submit
2526
import androidclient.feature.savings.generated.resources.feature_savings_yes
2627
import androidclient.feature.savings.generated.resources.step_charges
27-
import androidclient.feature.savings.generated.resources.step_charges_active
2828
import androidclient.feature.savings.generated.resources.step_charges_view
2929
import androidclient.feature.savings.generated.resources.step_details
3030
import androidclient.feature.savings.generated.resources.step_terms
@@ -60,8 +60,10 @@ fun PreviewPage(
6060
modifier: Modifier = Modifier,
6161
) {
6262
val previewDetailsMap = mapOf(
63-
stringResource(Res.string.feature_savings_product_name) to state.savingProductOptions[state.savingsProductSelected].name,
64-
stringResource(Res.string.feature_savings_field_officer) to state.fieldOfficerOptions[state.fieldOfficerIndex].displayName,
63+
stringResource(Res.string.feature_savings_product_name) to
64+
state.savingProductOptions.getOrNull(state.savingsProductSelected)?.name.orEmpty(),
65+
stringResource(Res.string.feature_savings_field_officer) to
66+
state.fieldOfficerOptions.getOrNull(state.fieldOfficerIndex)?.displayName.orEmpty(),
6567
stringResource(Res.string.feature_savings_submission_date) to state.submissionDate,
6668
stringResource(Res.string.feature_savings_external_id) to state.externalId,
6769
)
@@ -173,8 +175,9 @@ fun PreviewPage(
173175
onAction(SavingsAccountAction.ShowCharges)
174176
},
175177
btnText = stringResource(Res.string.step_charges_view),
176-
text = state.addedCharges.size.toString() + " " + stringResource(Res.string.step_charges_active) + " " + stringResource(
177-
Res.string.step_charges,
178+
text = stringResource(
179+
Res.string.feature_savings_charges_active_count,
180+
state.addedCharges.size,
178181
),
179182
btnEnabled = state.addedCharges.isNotEmpty(),
180183
)

0 commit comments

Comments
 (0)