Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.core.data.util
package com.mifos.core.common.utils

import com.mifos.core.model.objects.error.MifosError
import io.ktor.client.statement.HttpResponse
Expand All @@ -24,13 +24,8 @@ suspend fun extractErrorMessage(response: HttpResponse): String {
val errorResponse = json.decodeFromString<MifosError>(responseText)
errorResponse.errors.firstOrNull()?.defaultUserMessage
?: errorResponse.defaultUserMessage
?: Error.MSG_NOT_FOUND
?: "HTTP ${response.status.value} ${response.status.description}"
} catch (e: Exception) {
Error.FAILED_TO_PARSE_ERROR_RESPONSE
"HTTP ${response.status.value} ${response.status.description}"
}
}

data object Error {
const val MSG_NOT_FOUND = "Message Not Found"
const val FAILED_TO_PARSE_ERROR_RESPONSE = "Failed to parse error response"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
package com.mifos.core.data.repositoryImp

import com.mifos.core.common.utils.DataState
import com.mifos.core.common.utils.extractErrorMessage
import com.mifos.core.data.repository.ClientDetailsRepository
import com.mifos.core.data.util.extractErrorMessage
import com.mifos.core.model.objects.account.share.ShareAccounts
import com.mifos.core.network.datamanager.DataManagerClient
import com.mifos.core.network.model.ClientCloseTemplateResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
package com.mifos.core.domain.useCases

import com.mifos.core.common.utils.DataState
import com.mifos.core.common.utils.extractErrorMessage
import com.mifos.core.data.repository.ActivateRepository
import com.mifos.core.data.util.extractErrorMessage
import com.mifos.core.model.objects.clients.ActivatePayload

class ActivateGroupUseCase(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
*/
package com.mifos.core.network.datamanager

import com.mifos.core.common.utils.extractErrorMessage
import com.mifos.core.model.objects.noncoreobjects.Identifier
import com.mifos.core.model.objects.noncoreobjects.IdentifierPayload
import com.mifos.core.model.objects.noncoreobjects.IdentifierTemplate
import com.mifos.core.network.BaseApiManager
import com.mifos.core.network.GenericResponse
import io.ktor.client.statement.HttpResponse
import io.ktor.http.isSuccess
import kotlinx.coroutines.flow.Flow

/**
Expand Down Expand Up @@ -92,7 +94,14 @@ class DataManagerIdentifiers(
clientId: Long,
identifierPayload: IdentifierPayload,
): HttpResponse {
return mBaseApiManager.clientIdentifiersApi.createClientIdentifier(clientId, identifierPayload)
val response =
mBaseApiManager.clientIdentifiersApi.createClientIdentifier(clientId, identifierPayload)

if (!response.status.isSuccess()) {
throw IllegalStateException(extractErrorMessage(response))
}

return response
}

/**
Expand All @@ -111,6 +120,10 @@ class DataManagerIdentifiers(
identifierId: Long,
identifierPayload: IdentifierPayload,
): GenericResponse {
return mBaseApiManager.clientIdentifiersApi.updateClientIdentifier(clientId, identifierId, identifierPayload)
return mBaseApiManager.clientIdentifiersApi.updateClientIdentifier(
clientId,
identifierId,
identifierPayload,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package com.mifos.core.network.datamanager

import com.mifos.core.common.utils.extractErrorMessage
import com.mifos.core.datastore.UserPreferencesRepository
import com.mifos.core.model.objects.account.loan.LoanDisbursement
import com.mifos.core.network.BaseApiManager
Expand All @@ -23,11 +24,13 @@ import com.mifos.room.entities.templates.loans.LoanTemplate
import com.mifos.room.entities.templates.loans.LoanTransactionTemplate
import com.mifos.room.helper.LoanDaoHelper
import io.ktor.client.statement.HttpResponse
import io.ktor.http.isSuccess
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map

/**
* Created by Rajan Maurya on 15/07/16.
Expand Down Expand Up @@ -56,7 +59,14 @@ class DataManagerLoan(
fun getLoanById(loanId: Int): Flow<LoanWithAssociationsEntity?> {
return prefManager.userInfo.flatMapLatest { userData ->
when (userData.userStatus) {
false -> flow { emit(mBaseApiManager.loanService.getLoanByIdWithAllAssociations(loanId)) }
false -> flow {
emit(
mBaseApiManager.loanService.getLoanByIdWithAllAssociations(
loanId,
),
)
}

true ->
/**
* offline Mode, Return LoanWithAssociation from LoanDaoHelper.
Expand All @@ -77,7 +87,8 @@ class DataManagerLoan(
*/
fun syncLoanById(loanId: Int): Flow<LoanWithAssociationsEntity> {
return flow {
val loanWithAssociations = mBaseApiManager.loanService.getLoanByIdWithAllAssociations(loanId)
val loanWithAssociations =
mBaseApiManager.loanService.getLoanByIdWithAllAssociations(loanId)
loanDaoHelper.saveLoanById(loanWithAssociations)
emit(loanWithAssociations)
}
Expand All @@ -91,7 +102,16 @@ class DataManagerLoan(
}

fun createLoansAccount(loansPayload: LoansPayload?): Flow<HttpResponse> {
return mBaseApiManager.loanService.createLoansAccount(loansPayload)
return mBaseApiManager.loanService.createLoansAccount(loansPayload).map { response ->

if (!response.status.isSuccess()) {
val errorMessage = extractErrorMessage(response)

throw IllegalStateException(errorMessage)
}

response
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
*/
package com.mifos.core.network.datamanager

import com.mifos.core.common.utils.extractErrorMessage
import com.mifos.core.datastore.UserPreferencesRepository
import com.mifos.core.model.objects.account.loan.SavingsApproval
import com.mifos.core.model.objects.account.saving.SavingsAccountTransactionResponse
import com.mifos.core.model.objects.error.MifosError
import com.mifos.core.model.objects.organisations.ProductSavings
import com.mifos.core.model.objects.payloads.SavingsPayload
import com.mifos.core.network.BaseApiManager
Expand Down Expand Up @@ -290,22 +290,16 @@ class DataManagerSavings(

fun createSavingsAccount(savingsPayload: SavingsPayload?): Flow<Savings> {
return mBaseApiManager.savingsService.createSavingsAccount(savingsPayload).map { response ->
val responseText = response.bodyAsText()

val json = Json { ignoreUnknownKeys = true }

if (!response.status.isSuccess()) {
val errorMessage = try {
val errorResponse = json.decodeFromString<MifosError>(responseText)
errorResponse.errors.firstOrNull()?.defaultUserMessage
?: errorResponse.defaultUserMessage
?: "HTTP ${response.status.value} ${response.status.description}"
} catch (e: Exception) {
"HTTP ${response.status.value} ${response.status.description}"
}
val errorMessage = extractErrorMessage(response)

throw IllegalStateException(errorMessage)
}

json.decodeFromString<Savings>(responseText)
json.decodeFromString<Savings>(response.bodyAsText())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import com.mifos.core.common.utils.DataState
import com.mifos.core.common.utils.FileKitUtil
import com.mifos.core.data.repository.ClientIdentifiersRepository
import com.mifos.core.data.repository.DocumentCreateUpdateRepository
import com.mifos.core.data.util.Error
import com.mifos.core.data.util.NetworkMonitor
import com.mifos.core.data.util.extractErrorMessage
import com.mifos.core.domain.useCases.CreateClientIdentifierUseCase
import com.mifos.core.domain.useCases.DownloadDocumentUseCase
import com.mifos.core.domain.useCases.GetDocumentsListUseCase
Expand Down Expand Up @@ -88,12 +86,26 @@ class ClientIdentifiersAddUpdateViewModel(
.collect { dataState ->
when (dataState) {
is DataState.Error -> {
mutableStateFlow.update {
it.copy(
dialogState = ClientIdentifiersAddUpdateState.DialogState.Error(
dataState.message,
),
)
if (dataState.exception is IllegalStateException) {
mutableStateFlow.update {
it.copy(
dialogState = ClientIdentifiersAddUpdateState.DialogState.Error(
dataState.message.replace("unique", "document")
.replace("under", " under"),
),
handleServerResponse = true,
isOverlayLoading = false,
)
}
} else {
mutableStateFlow.update {
it.copy(
dialogState = ClientIdentifiersAddUpdateState.DialogState.Error(
dataState.message,
),
isOverlayLoading = false,
)
}
}
}

Expand All @@ -107,25 +119,12 @@ class ClientIdentifiersAddUpdateViewModel(
}

is DataState.Success -> {
val error = extractErrorMessage(dataState.data)

if (error == Error.MSG_NOT_FOUND) {
mutableStateFlow.update {
it.copy(
dialogState = null,
isOverlayLoading = false,
feature = Feature.ADD_UPDATE_DOCUMENT,
)
}
} else {
mutableStateFlow.update {
it.copy(
dialogState = ClientIdentifiersAddUpdateState.DialogState.Error(
error.replace("unique", "document").replace("under", " under"),
),
handleServerResponse = true,
)
}
mutableStateFlow.update {
it.copy(
dialogState = null,
isOverlayLoading = false,
feature = Feature.ADD_UPDATE_DOCUMENT,
)
}
}
}
Expand Down Expand Up @@ -271,7 +270,11 @@ class ClientIdentifiersAddUpdateViewModel(
}
}

private suspend fun createDocument(file: PlatformFile, name: String?, uniqueKeyForHandleDocument: String) {
private suspend fun createDocument(
file: PlatformFile,
name: String?,
uniqueKeyForHandleDocument: String,
) {
repository.createDocument(
Constants.ENTITY_TYPE_CLIENT_IDENTIFIERS,
route.clientId,
Expand Down Expand Up @@ -313,7 +316,11 @@ class ClientIdentifiersAddUpdateViewModel(
}
}

private suspend fun updateDocument(file: PlatformFile, name: String?, uniqueKeyForHandleDocument: String?) {
private suspend fun updateDocument(
file: PlatformFile,
name: String?,
uniqueKeyForHandleDocument: String?,
) {
state.documentId?.let { documentId ->
repository.updateDocument(
entityType = Constants.CLIENTS,
Expand Down Expand Up @@ -538,7 +545,8 @@ class ClientIdentifiersAddUpdateViewModel(
createDocument(
file = file,
name = state.documentName,
uniqueKeyForHandleDocument = route.uniqueKeyForHandleDocument ?: uniqueKey,
uniqueKeyForHandleDocument = route.uniqueKeyForHandleDocument
?: uniqueKey,
)
} else {
updateDocument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import com.mifos.feature.client.clientEditProfile.clientEditProfileDestination
import com.mifos.feature.client.clientEditProfile.navigateToClientProfileEditProfileRoute
import com.mifos.feature.client.clientGeneral.clientProfileGeneralDestination
import com.mifos.feature.client.clientGeneral.navigateToClientProfileGeneralRoute
import com.mifos.feature.client.clientGeneral.navigateToClientProfileGeneralRouteOnStatus
import com.mifos.feature.client.clientIdentifiersAddUpdate.clientIdentifiersAddUpdateDestination
import com.mifos.feature.client.clientIdentifiersAddUpdate.onNavigateToClientIdentifiersAddUpdateScreen
import com.mifos.feature.client.clientIdentifiersList.clientIdentifiersListDestination
Expand Down Expand Up @@ -340,7 +339,7 @@ fun NavGraphBuilder.clientNavGraph(
onBackPressed = navController::popBackStack,
loadMoreSavingsAccountInfo = navController::navigateToDataTable,
loadDocuments = navController::navigateToDocumentListScreen,
onFinish = navController::navigateToClientProfileGeneralRouteOnStatus,
onFinish = navController::popBackStack,
)

createShareAccountDestination(
Expand Down
Loading