Skip to content

Commit 9df7ef4

Browse files
authored
enhance new saving account flow (#2529)
1 parent 0c99c9b commit 9df7ef4

File tree

19 files changed

+404
-494
lines changed

19 files changed

+404
-494
lines changed

core/data/src/commonMain/kotlin/com/mifos/core/data/util/ErrorHandling.kt renamed to core/common/src/commonMain/kotlin/com/mifos/core/common/utils/ErrorHandling.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
99
*/
10-
package com.mifos.core.data.util
10+
package com.mifos.core.common.utils
1111

1212
import com.mifos.core.model.objects.error.MifosError
1313
import io.ktor.client.statement.HttpResponse
@@ -24,13 +24,8 @@ suspend fun extractErrorMessage(response: HttpResponse): String {
2424
val errorResponse = json.decodeFromString<MifosError>(responseText)
2525
errorResponse.errors.firstOrNull()?.defaultUserMessage
2626
?: errorResponse.defaultUserMessage
27-
?: Error.MSG_NOT_FOUND
27+
?: "HTTP ${response.status.value} ${response.status.description}"
2828
} catch (e: Exception) {
29-
Error.FAILED_TO_PARSE_ERROR_RESPONSE
29+
"HTTP ${response.status.value} ${response.status.description}"
3030
}
3131
}
32-
33-
data object Error {
34-
const val MSG_NOT_FOUND = "Message Not Found"
35-
const val FAILED_TO_PARSE_ERROR_RESPONSE = "Failed to parse error response"
36-
}

core/data/src/commonMain/kotlin/com/mifos/core/data/repositoryImp/ClientDetailsRepositoryImp.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
package com.mifos.core.data.repositoryImp
1111

1212
import com.mifos.core.common.utils.DataState
13+
import com.mifos.core.common.utils.extractErrorMessage
1314
import com.mifos.core.data.repository.ClientDetailsRepository
14-
import com.mifos.core.data.util.extractErrorMessage
1515
import com.mifos.core.model.objects.account.share.ShareAccounts
1616
import com.mifos.core.network.datamanager.DataManagerClient
1717
import com.mifos.core.network.model.ClientCloseTemplateResponse

core/domain/src/commonMain/kotlin/com/mifos/core/domain/useCases/ActivateGroupUseCase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
package com.mifos.core.domain.useCases
1111

1212
import com.mifos.core.common.utils.DataState
13+
import com.mifos.core.common.utils.extractErrorMessage
1314
import com.mifos.core.data.repository.ActivateRepository
14-
import com.mifos.core.data.util.extractErrorMessage
1515
import com.mifos.core.model.objects.clients.ActivatePayload
1616

1717
class ActivateGroupUseCase(

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
*/
1010
package com.mifos.core.network.datamanager
1111

12+
import com.mifos.core.common.utils.extractErrorMessage
1213
import com.mifos.core.model.objects.noncoreobjects.Identifier
1314
import com.mifos.core.model.objects.noncoreobjects.IdentifierPayload
1415
import com.mifos.core.model.objects.noncoreobjects.IdentifierTemplate
1516
import com.mifos.core.network.BaseApiManager
1617
import com.mifos.core.network.GenericResponse
1718
import io.ktor.client.statement.HttpResponse
19+
import io.ktor.http.isSuccess
1820
import kotlinx.coroutines.flow.Flow
1921

2022
/**
@@ -92,7 +94,14 @@ class DataManagerIdentifiers(
9294
clientId: Long,
9395
identifierPayload: IdentifierPayload,
9496
): HttpResponse {
95-
return mBaseApiManager.clientIdentifiersApi.createClientIdentifier(clientId, identifierPayload)
97+
val response =
98+
mBaseApiManager.clientIdentifiersApi.createClientIdentifier(clientId, identifierPayload)
99+
100+
if (!response.status.isSuccess()) {
101+
throw IllegalStateException(extractErrorMessage(response))
102+
}
103+
104+
return response
96105
}
97106

98107
/**
@@ -111,6 +120,10 @@ class DataManagerIdentifiers(
111120
identifierId: Long,
112121
identifierPayload: IdentifierPayload,
113122
): GenericResponse {
114-
return mBaseApiManager.clientIdentifiersApi.updateClientIdentifier(clientId, identifierId, identifierPayload)
123+
return mBaseApiManager.clientIdentifiersApi.updateClientIdentifier(
124+
clientId,
125+
identifierId,
126+
identifierPayload,
127+
)
115128
}
116129
}

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package com.mifos.core.network.datamanager
1111

12+
import com.mifos.core.common.utils.extractErrorMessage
1213
import com.mifos.core.datastore.UserPreferencesRepository
1314
import com.mifos.core.model.objects.account.loan.LoanDisbursement
1415
import com.mifos.core.network.BaseApiManager
@@ -23,11 +24,13 @@ import com.mifos.room.entities.templates.loans.LoanTemplate
2324
import com.mifos.room.entities.templates.loans.LoanTransactionTemplate
2425
import com.mifos.room.helper.LoanDaoHelper
2526
import io.ktor.client.statement.HttpResponse
27+
import io.ktor.http.isSuccess
2628
import kotlinx.coroutines.ExperimentalCoroutinesApi
2729
import kotlinx.coroutines.flow.Flow
2830
import kotlinx.coroutines.flow.first
2931
import kotlinx.coroutines.flow.flatMapLatest
3032
import kotlinx.coroutines.flow.flow
33+
import kotlinx.coroutines.flow.map
3134

3235
/**
3336
* Created by Rajan Maurya on 15/07/16.
@@ -56,7 +59,14 @@ class DataManagerLoan(
5659
fun getLoanById(loanId: Int): Flow<LoanWithAssociationsEntity?> {
5760
return prefManager.userInfo.flatMapLatest { userData ->
5861
when (userData.userStatus) {
59-
false -> flow { emit(mBaseApiManager.loanService.getLoanByIdWithAllAssociations(loanId)) }
62+
false -> flow {
63+
emit(
64+
mBaseApiManager.loanService.getLoanByIdWithAllAssociations(
65+
loanId,
66+
),
67+
)
68+
}
69+
6070
true ->
6171
/**
6272
* offline Mode, Return LoanWithAssociation from LoanDaoHelper.
@@ -77,7 +87,8 @@ class DataManagerLoan(
7787
*/
7888
fun syncLoanById(loanId: Int): Flow<LoanWithAssociationsEntity> {
7989
return flow {
80-
val loanWithAssociations = mBaseApiManager.loanService.getLoanByIdWithAllAssociations(loanId)
90+
val loanWithAssociations =
91+
mBaseApiManager.loanService.getLoanByIdWithAllAssociations(loanId)
8192
loanDaoHelper.saveLoanById(loanWithAssociations)
8293
emit(loanWithAssociations)
8394
}
@@ -91,7 +102,16 @@ class DataManagerLoan(
91102
}
92103

93104
fun createLoansAccount(loansPayload: LoansPayload?): Flow<HttpResponse> {
94-
return mBaseApiManager.loanService.createLoansAccount(loansPayload)
105+
return mBaseApiManager.loanService.createLoansAccount(loansPayload).map { response ->
106+
107+
if (!response.status.isSuccess()) {
108+
val errorMessage = extractErrorMessage(response)
109+
110+
throw IllegalStateException(errorMessage)
111+
}
112+
113+
response
114+
}
95115
}
96116

97117
/**

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
*/
1010
package com.mifos.core.network.datamanager
1111

12+
import com.mifos.core.common.utils.extractErrorMessage
1213
import com.mifos.core.datastore.UserPreferencesRepository
1314
import com.mifos.core.model.objects.account.loan.SavingsApproval
1415
import com.mifos.core.model.objects.account.saving.SavingsAccountTransactionResponse
15-
import com.mifos.core.model.objects.error.MifosError
1616
import com.mifos.core.model.objects.organisations.ProductSavings
1717
import com.mifos.core.model.objects.payloads.SavingsPayload
1818
import com.mifos.core.network.BaseApiManager
@@ -290,22 +290,16 @@ class DataManagerSavings(
290290

291291
fun createSavingsAccount(savingsPayload: SavingsPayload?): Flow<Savings> {
292292
return mBaseApiManager.savingsService.createSavingsAccount(savingsPayload).map { response ->
293-
val responseText = response.bodyAsText()
293+
294294
val json = Json { ignoreUnknownKeys = true }
295295

296296
if (!response.status.isSuccess()) {
297-
val errorMessage = try {
298-
val errorResponse = json.decodeFromString<MifosError>(responseText)
299-
errorResponse.errors.firstOrNull()?.defaultUserMessage
300-
?: errorResponse.defaultUserMessage
301-
?: "HTTP ${response.status.value} ${response.status.description}"
302-
} catch (e: Exception) {
303-
"HTTP ${response.status.value} ${response.status.description}"
304-
}
297+
val errorMessage = extractErrorMessage(response)
298+
305299
throw IllegalStateException(errorMessage)
306300
}
307301

308-
json.decodeFromString<Savings>(responseText)
302+
json.decodeFromString<Savings>(response.bodyAsText())
309303
}
310304
}
311305

feature/client/src/commonMain/kotlin/com/mifos/feature/client/clientIdentifiersAddUpdate/ClientIdentifiersAddUpdateViewModel.kt

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ import com.mifos.core.common.utils.DataState
1717
import com.mifos.core.common.utils.FileKitUtil
1818
import com.mifos.core.data.repository.ClientIdentifiersRepository
1919
import com.mifos.core.data.repository.DocumentCreateUpdateRepository
20-
import com.mifos.core.data.util.Error
2120
import com.mifos.core.data.util.NetworkMonitor
22-
import com.mifos.core.data.util.extractErrorMessage
2321
import com.mifos.core.domain.useCases.CreateClientIdentifierUseCase
2422
import com.mifos.core.domain.useCases.DownloadDocumentUseCase
2523
import com.mifos.core.domain.useCases.GetDocumentsListUseCase
@@ -88,12 +86,26 @@ class ClientIdentifiersAddUpdateViewModel(
8886
.collect { dataState ->
8987
when (dataState) {
9088
is DataState.Error -> {
91-
mutableStateFlow.update {
92-
it.copy(
93-
dialogState = ClientIdentifiersAddUpdateState.DialogState.Error(
94-
dataState.message,
95-
),
96-
)
89+
if (dataState.exception is IllegalStateException) {
90+
mutableStateFlow.update {
91+
it.copy(
92+
dialogState = ClientIdentifiersAddUpdateState.DialogState.Error(
93+
dataState.message.replace("unique", "document")
94+
.replace("under", " under"),
95+
),
96+
handleServerResponse = true,
97+
isOverlayLoading = false,
98+
)
99+
}
100+
} else {
101+
mutableStateFlow.update {
102+
it.copy(
103+
dialogState = ClientIdentifiersAddUpdateState.DialogState.Error(
104+
dataState.message,
105+
),
106+
isOverlayLoading = false,
107+
)
108+
}
97109
}
98110
}
99111

@@ -107,25 +119,12 @@ class ClientIdentifiersAddUpdateViewModel(
107119
}
108120

109121
is DataState.Success -> {
110-
val error = extractErrorMessage(dataState.data)
111-
112-
if (error == Error.MSG_NOT_FOUND) {
113-
mutableStateFlow.update {
114-
it.copy(
115-
dialogState = null,
116-
isOverlayLoading = false,
117-
feature = Feature.ADD_UPDATE_DOCUMENT,
118-
)
119-
}
120-
} else {
121-
mutableStateFlow.update {
122-
it.copy(
123-
dialogState = ClientIdentifiersAddUpdateState.DialogState.Error(
124-
error.replace("unique", "document").replace("under", " under"),
125-
),
126-
handleServerResponse = true,
127-
)
128-
}
122+
mutableStateFlow.update {
123+
it.copy(
124+
dialogState = null,
125+
isOverlayLoading = false,
126+
feature = Feature.ADD_UPDATE_DOCUMENT,
127+
)
129128
}
130129
}
131130
}
@@ -271,7 +270,11 @@ class ClientIdentifiersAddUpdateViewModel(
271270
}
272271
}
273272

274-
private suspend fun createDocument(file: PlatformFile, name: String?, uniqueKeyForHandleDocument: String) {
273+
private suspend fun createDocument(
274+
file: PlatformFile,
275+
name: String?,
276+
uniqueKeyForHandleDocument: String,
277+
) {
275278
repository.createDocument(
276279
Constants.ENTITY_TYPE_CLIENT_IDENTIFIERS,
277280
route.clientId,
@@ -313,7 +316,11 @@ class ClientIdentifiersAddUpdateViewModel(
313316
}
314317
}
315318

316-
private suspend fun updateDocument(file: PlatformFile, name: String?, uniqueKeyForHandleDocument: String?) {
319+
private suspend fun updateDocument(
320+
file: PlatformFile,
321+
name: String?,
322+
uniqueKeyForHandleDocument: String?,
323+
) {
317324
state.documentId?.let { documentId ->
318325
repository.updateDocument(
319326
entityType = Constants.CLIENTS,
@@ -538,7 +545,8 @@ class ClientIdentifiersAddUpdateViewModel(
538545
createDocument(
539546
file = file,
540547
name = state.documentName,
541-
uniqueKeyForHandleDocument = route.uniqueKeyForHandleDocument ?: uniqueKey,
548+
uniqueKeyForHandleDocument = route.uniqueKeyForHandleDocument
549+
?: uniqueKey,
542550
)
543551
} else {
544552
updateDocument(

feature/client/src/commonMain/kotlin/com/mifos/feature/client/navigation/ClientNavigation.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import com.mifos.feature.client.clientEditProfile.clientEditProfileDestination
4545
import com.mifos.feature.client.clientEditProfile.navigateToClientProfileEditProfileRoute
4646
import com.mifos.feature.client.clientGeneral.clientProfileGeneralDestination
4747
import com.mifos.feature.client.clientGeneral.navigateToClientProfileGeneralRoute
48-
import com.mifos.feature.client.clientGeneral.navigateToClientProfileGeneralRouteOnStatus
4948
import com.mifos.feature.client.clientIdentifiersAddUpdate.clientIdentifiersAddUpdateDestination
5049
import com.mifos.feature.client.clientIdentifiersAddUpdate.onNavigateToClientIdentifiersAddUpdateScreen
5150
import com.mifos.feature.client.clientIdentifiersList.clientIdentifiersListDestination
@@ -340,7 +339,7 @@ fun NavGraphBuilder.clientNavGraph(
340339
onBackPressed = navController::popBackStack,
341340
loadMoreSavingsAccountInfo = navController::navigateToDataTable,
342341
loadDocuments = navController::navigateToDocumentListScreen,
343-
onFinish = navController::navigateToClientProfileGeneralRouteOnStatus,
342+
onFinish = navController::popBackStack,
344343
)
345344

346345
createShareAccountDestination(

0 commit comments

Comments
 (0)