Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -30,10 +30,10 @@ interface ClientIdentifiersRepository {

suspend fun deleteClientIdentifier(clientId: Long, identifierId: Long): GenericResponse

suspend fun createClientIdentifier(
fun createClientIdentifier(
clientId: Long,
identifierPayload: IdentifierPayload,
): HttpResponse
): Flow<DataState<HttpResponse>>

suspend fun updateClientIdentifier(
clientId: Long,
Expand Down
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 @@ -43,11 +43,11 @@ class ClientIdentifiersRepositoryImp(
return dataManagerIdentifiers.deleteClientIdentifier(clientId, identifierId)
}

override suspend fun createClientIdentifier(
override fun createClientIdentifier(
clientId: Long,
identifierPayload: IdentifierPayload,
): HttpResponse {
return dataManagerIdentifiers.createClientIdentifier(clientId, identifierPayload)
): Flow<DataState<HttpResponse>> {
return dataManagerIdentifiers.createClientIdentifier(clientId, identifierPayload).asDataStateFlow()
}

override suspend fun updateClientIdentifier(
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 @@ -10,20 +10,17 @@
package com.mifos.core.domain.useCases

import com.mifos.core.common.utils.DataState
import com.mifos.core.common.utils.asDataStateFlow
import com.mifos.core.data.repository.ClientIdentifiersRepository
import com.mifos.core.model.objects.noncoreobjects.IdentifierPayload
import io.ktor.client.statement.HttpResponse
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

class CreateClientIdentifierUseCase(
private val repository: ClientIdentifiersRepository,
) {
operator fun invoke(
clientId: Long,
identifierPayload: IdentifierPayload,
): Flow<DataState<HttpResponse>> = flow {
emit(repository.createClientIdentifier(clientId, identifierPayload))
}.asDataStateFlow()
): Flow<DataState<HttpResponse>> =
repository.createClientIdentifier(clientId, identifierPayload)
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ interface ClientIdentifierApi {
* @return [GenericResponse] indicating the result of the create operation.
*/
@POST(APIEndPoint.CLIENTS + "/{clientId}/" + APIEndPoint.IDENTIFIERS)
suspend fun createClientIdentifier(
fun createClientIdentifier(
@Path("clientId") clientId: Long,
@Body identifierPayload: IdentifierPayload,
): HttpResponse
): Flow<HttpResponse>

/**
* Updates an existing client identifier for a given client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
*/
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
import kotlinx.coroutines.flow.map

/**
* Created by Arin Yadav on 12/09/25.
Expand Down Expand Up @@ -88,11 +91,20 @@ class DataManagerIdentifiers(
* @param identifierPayload The payload containing identifier details.
* @return [GenericResponse] indicating the result of the create operation.
*/
suspend fun createClientIdentifier(
fun createClientIdentifier(
clientId: Long,
identifierPayload: IdentifierPayload,
): HttpResponse {
return mBaseApiManager.clientIdentifiersApi.createClientIdentifier(clientId, identifierPayload)
): Flow<HttpResponse> {
return mBaseApiManager.clientIdentifiersApi.createClientIdentifier(clientId, identifierPayload).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,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,24 @@ 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,
)
}
} else {
mutableStateFlow.update {
it.copy(
dialogState = ClientIdentifiersAddUpdateState.DialogState.Error(
dataState.message,
),
)
}
}
}

Expand All @@ -107,25 +117,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 +268,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 +314,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 +543,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 @@ -81,8 +81,6 @@ private fun ShareAccountScaffold(
}

MifosScaffold(
title = "Create Share Account",
onBackPressed = { onAction(ShareAccountAction.NavigateBack) },
modifier = modifier,
) { paddingValues ->
if (state.dialogState == null) {
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,
)

shareAccountDestination()
Expand Down
Loading