diff --git a/core/data/src/main/java/org/mifospay/core/data/domain/usecase/account/BlockUnblockCommand.kt b/core/data/src/main/java/org/mifospay/core/data/domain/usecase/account/BlockUnblockCommand.kt index 85951cd10..c43caada0 100644 --- a/core/data/src/main/java/org/mifospay/core/data/domain/usecase/account/BlockUnblockCommand.kt +++ b/core/data/src/main/java/org/mifospay/core/data/domain/usecase/account/BlockUnblockCommand.kt @@ -9,12 +9,13 @@ */ package org.mifospay.core.data.domain.usecase.account +import android.util.Log +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.mifospay.core.data.base.UseCase import org.mifospay.core.data.fineract.repository.FineractRepository -import org.mifospay.core.network.GenericResponse -import rx.Subscriber -import rx.android.schedulers.AndroidSchedulers -import rx.schedulers.Schedulers import javax.inject.Inject class BlockUnblockCommand @Inject constructor( @@ -22,23 +23,23 @@ class BlockUnblockCommand @Inject constructor( ) : UseCase() { override fun executeUseCase(requestValues: RequestValues) { - mFineractRepository.blockUnblockAccount(requestValues.accountId, requestValues.command) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe( - object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - useCaseCallback.onError( - "Error " + requestValues.command + "ing account", - ) - } - - override fun onNext(genericResponse: GenericResponse) { - useCaseCallback.onSuccess(ResponseValue) - } - }, - ) + CoroutineScope(Dispatchers.IO).launch { + try { + val res = mFineractRepository.blockUnblockAccount( + requestValues.accountId, + requestValues.command, + ) + withContext(Dispatchers.Main) { + Log.d("BlockUnblockCommand@@@@","$res") + useCaseCallback.onSuccess(ResponseValue) + } + } catch (e: Exception) { + Log.d("BlockUnblockCommand@@@@","${e.message}") + useCaseCallback.onError( + "Error " + requestValues.command + "ing account", + ) + } + } } data class RequestValues(val accountId: Long, val command: String) : UseCase.RequestValues diff --git a/core/data/src/main/java/org/mifospay/core/data/domain/usecase/account/FetchAccountTransaction.kt b/core/data/src/main/java/org/mifospay/core/data/domain/usecase/account/FetchAccountTransaction.kt index 196604e4e..9a6f494a4 100644 --- a/core/data/src/main/java/org/mifospay/core/data/domain/usecase/account/FetchAccountTransaction.kt +++ b/core/data/src/main/java/org/mifospay/core/data/domain/usecase/account/FetchAccountTransaction.kt @@ -9,15 +9,16 @@ */ package org.mifospay.core.data.domain.usecase.account +import android.util.Log import com.mifospay.core.model.domain.Transaction -import com.mifospay.core.model.entity.accounts.savings.Transactions +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.mifospay.core.data.base.UseCase import org.mifospay.core.data.fineract.entity.mapper.TransactionMapper import org.mifospay.core.data.fineract.repository.FineractRepository import org.mifospay.core.data.util.Constants -import rx.Observer -import rx.android.schedulers.AndroidSchedulers -import rx.schedulers.Schedulers import javax.inject.Inject class FetchAccountTransaction @Inject constructor( @@ -26,32 +27,31 @@ class FetchAccountTransaction @Inject constructor( ) : UseCase() { override fun executeUseCase(requestValues: RequestValues) { - fineractRepository.getSelfAccountTransactionFromId( - requestValues.accountId, - requestValues.transactionId, - ) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe( - object : Observer { - override fun onCompleted() {} - override fun onError(e: Throwable) { - if (e.message == "HTTP 401 Unauthorized") { - useCaseCallback.onError(Constants.UNAUTHORIZED_ERROR) - } else { - useCaseCallback.onError( - Constants.ERROR_FETCHING_REMOTE_ACCOUNT_TRANSACTIONS, - ) - } - } - - override fun onNext(transaction: Transactions?) { - useCaseCallback.onSuccess( - ResponseValue(transactionMapper.transformInvoice(transaction)), + CoroutineScope(Dispatchers.IO).launch { + try { + val res = fineractRepository.getSelfAccountTransactionFromId( + requestValues.accountId, + requestValues.transactionId, + ) + withContext(Dispatchers.Main) { + Log.d("FetchTransactions@@@@","$res") + useCaseCallback.onSuccess( + ResponseValue(transactionMapper.transformInvoice(res)), + ) + } + } catch (e: Exception) { + Log.d("FetchTransactions@@@@","${e.message}") + withContext(Dispatchers.Main) { + if (e.message == "HTTP 401 Unauthorized") { + useCaseCallback.onError(Constants.UNAUTHORIZED_ERROR) + } else { + useCaseCallback.onError( + Constants.ERROR_FETCHING_REMOTE_ACCOUNT_TRANSACTIONS, ) } - }, - ) + } + } + } } data class RequestValues( diff --git a/core/data/src/main/java/org/mifospay/core/data/domain/usecase/account/FetchMerchants.kt b/core/data/src/main/java/org/mifospay/core/data/domain/usecase/account/FetchMerchants.kt index a1da35556..2d255f28f 100644 --- a/core/data/src/main/java/org/mifospay/core/data/domain/usecase/account/FetchMerchants.kt +++ b/core/data/src/main/java/org/mifospay/core/data/domain/usecase/account/FetchMerchants.kt @@ -9,44 +9,42 @@ */ package org.mifospay.core.data.domain.usecase.account -import com.mifospay.core.model.entity.Page +import android.util.Log import com.mifospay.core.model.entity.accounts.savings.SavingsWithAssociations +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.mifospay.core.data.base.UseCase import org.mifospay.core.data.fineract.repository.FineractRepository import org.mifospay.core.data.util.Constants -import rx.Subscriber -import rx.android.schedulers.AndroidSchedulers -import rx.schedulers.Schedulers import javax.inject.Inject class FetchMerchants @Inject constructor( private val mFineractRepository: FineractRepository, ) : UseCase() { override fun executeUseCase(requestValues: RequestValues) { - mFineractRepository.savingsAccounts - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()) - .subscribe( - object : Subscriber>() { - override fun onCompleted() {} - override fun onError(e: Throwable) { - e.message?.let { useCaseCallback.onError(it) } - } - - override fun onNext(savingsWithAssociationsPage: Page) { - val savingsWithAssociationsList = savingsWithAssociationsPage.pageItems - val merchantsList: MutableList = ArrayList() - for (i in savingsWithAssociationsList.indices) { - if (savingsWithAssociationsList[i].savingsProductId == - Constants.MIFOS_MERCHANT_SAVINGS_PRODUCT_ID - ) { - merchantsList.add(savingsWithAssociationsList[i]) - } + CoroutineScope(Dispatchers.IO).launch { + try { + val res = mFineractRepository.savingsAccounts() + withContext(Dispatchers.Main) { + Log.d("FetchMerchants@@@@", "$res") + val savingsWithAssociationsList = res.pageItems + val merchantsList: MutableList = ArrayList() + for (i in savingsWithAssociationsList.indices) { + if (savingsWithAssociationsList[i].savingsProductId == + Constants.MIFOS_MERCHANT_SAVINGS_PRODUCT_ID + ) { + merchantsList.add(savingsWithAssociationsList[i]) } - useCaseCallback.onSuccess(ResponseValue(merchantsList)) } - }, - ) + useCaseCallback.onSuccess(ResponseValue(merchantsList)) + } + } catch (e: Exception) { + Log.d("FetchTransactions@@@@", "${e.message}") + e.message?.let { useCaseCallback.onError(it) } + } + } } class RequestValues : UseCase.RequestValues diff --git a/core/data/src/main/java/org/mifospay/core/data/fineract/repository/FineractRepository.kt b/core/data/src/main/java/org/mifospay/core/data/fineract/repository/FineractRepository.kt index 9c3db3c00..5f439c562 100644 --- a/core/data/src/main/java/org/mifospay/core/data/fineract/repository/FineractRepository.kt +++ b/core/data/src/main/java/org/mifospay/core/data/fineract/repository/FineractRepository.kt @@ -103,11 +103,11 @@ class FineractRepository @Inject constructor( return fineractApiManager.clientsApi.getAccounts(clientId, Constants.SAVINGS) } - val savingsAccounts: Observable> - get() = fineractApiManager.savingsAccountsApi.getSavingsAccounts(-1) + suspend fun savingsAccounts(): Page + = fineractApiManager.ktorSavingsAccountApi.getSavingsAccounts(-1) - fun blockUnblockAccount(accountId: Long, command: String?): Observable { - return fineractApiManager.savingsAccountsApi.blockUnblockAccount( + suspend fun blockUnblockAccount(accountId: Long, command: String?): GenericResponse { + return fineractApiManager.ktorSavingsAccountApi.blockUnblockAccount( accountId, command, ) @@ -293,11 +293,11 @@ class FineractRepository @Inject constructor( ) } - fun getSelfAccountTransactionFromId( + suspend fun getSelfAccountTransactionFromId( accountId: Long, transactionId: Long, - ): Observable { - return selfApiManager.savingAccountsListApi.getSavingAccountTransaction( + ): Transactions { + return selfApiManager.ktorSavingsAccountApi.getSavingAccountTransaction( accountId, transactionId, ) diff --git a/core/network/src/main/kotlin/org/mifospay/core/network/FineractApiManager.kt b/core/network/src/main/kotlin/org/mifospay/core/network/FineractApiManager.kt index 0b4b41062..a424d3d92 100644 --- a/core/network/src/main/kotlin/org/mifospay/core/network/FineractApiManager.kt +++ b/core/network/src/main/kotlin/org/mifospay/core/network/FineractApiManager.kt @@ -15,6 +15,7 @@ import org.mifospay.core.network.services.ClientService import org.mifospay.core.network.services.DocumentService import org.mifospay.core.network.services.InvoiceService import org.mifospay.core.network.services.KYCLevel1Service +import org.mifospay.core.network.services.KtorSavingsAccountService import org.mifospay.core.network.services.NotificationService import org.mifospay.core.network.services.RegistrationService import org.mifospay.core.network.services.RunReportService @@ -44,6 +45,7 @@ class FineractApiManager @Inject constructor( private val thirdPartyTransferService: ThirdPartyTransferService, private val standingInstructionService: StandingInstructionService, private val notificationService: NotificationService, + private val ktorSavingsAccountService: KtorSavingsAccountService, ) { val authenticationApi: AuthenticationService @@ -93,4 +95,7 @@ class FineractApiManager @Inject constructor( val standingInstructionApi: StandingInstructionService get() = standingInstructionService + + val ktorSavingsAccountApi: KtorSavingsAccountService + get() = ktorSavingsAccountService } diff --git a/core/network/src/main/kotlin/org/mifospay/core/network/di/NetworkModule.kt b/core/network/src/main/kotlin/org/mifospay/core/network/di/NetworkModule.kt index 3af4cd732..b896a9d50 100644 --- a/core/network/src/main/kotlin/org/mifospay/core/network/di/NetworkModule.kt +++ b/core/network/src/main/kotlin/org/mifospay/core/network/di/NetworkModule.kt @@ -116,6 +116,7 @@ class NetworkModule { @Named("FineractThirdPartyTransferService") thirdPartyTransferService: ThirdPartyTransferService, standingInstructionService: StandingInstructionService, notificationService: NotificationService, + ktorSavingsAccountService: KtorSavingsAccountService ): FineractApiManager { return FineractApiManager( authenticationService, @@ -134,6 +135,7 @@ class NetworkModule { thirdPartyTransferService, standingInstructionService, notificationService, + ktorSavingsAccountService ) } diff --git a/core/network/src/main/kotlin/org/mifospay/core/network/services/KtorSavingsAccountService.kt b/core/network/src/main/kotlin/org/mifospay/core/network/services/KtorSavingsAccountService.kt index 2f68f73b2..6f09605c8 100644 --- a/core/network/src/main/kotlin/org/mifospay/core/network/services/KtorSavingsAccountService.kt +++ b/core/network/src/main/kotlin/org/mifospay/core/network/services/KtorSavingsAccountService.kt @@ -1,19 +1,27 @@ package org.mifospay.core.network.services +import com.mifospay.core.model.entity.Page +import com.mifospay.core.model.entity.accounts.savings.SavingAccount import com.mifospay.core.model.entity.accounts.savings.SavingsWithAssociations +import com.mifospay.core.model.entity.accounts.savings.Transactions import io.ktor.client.HttpClient import io.ktor.client.call.body import io.ktor.client.request.get +import io.ktor.client.request.post +import io.ktor.client.request.setBody +import io.ktor.http.ContentType +import io.ktor.http.contentType import jakarta.inject.Inject import org.mifospay.core.network.ApiEndPoints import org.mifospay.core.network.BaseURL +import org.mifospay.core.network.GenericResponse class KtorSavingsAccountService @Inject constructor( - private val client: HttpClient + private val client: HttpClient, ) { suspend fun getSavingsWithAssociations( accountId: Long, - associationType: String + associationType: String, ): SavingsWithAssociations { return client.get("${BaseURL().selfServiceUrl}${ApiEndPoints.SAVINGS_ACCOUNTS}/$accountId") { url { @@ -21,4 +29,41 @@ class KtorSavingsAccountService @Inject constructor( } }.body() } + + suspend fun getSavingsAccounts(limit: Int): Page { + return client.get("${BaseURL().selfServiceUrl}${ApiEndPoints.SAVINGS_ACCOUNTS}") { + url { + parameters.append("limit", limit.toString()) + } + }.body() + } + + suspend fun createSavingsAccount(savingAccount: SavingAccount): GenericResponse { + return client.post("${BaseURL().selfServiceUrl}${ApiEndPoints.SAVINGS_ACCOUNTS}") { + contentType(ContentType.Application.Json) + setBody(savingAccount) + }.body() + } + + suspend fun blockUnblockAccount(accountId: Long, command: String?): GenericResponse { + return client.post("${BaseURL().selfServiceUrl}${ApiEndPoints.SAVINGS_ACCOUNTS}/$accountId") { + url { + parameters.append("command", command ?: "") + } + }.body() + } + + suspend fun getSavingAccountTransaction(accountId: Long, transactionId: Long): Transactions { + return client.get("${BaseURL().selfServiceUrl}${ApiEndPoints.SAVINGS_ACCOUNTS}/$accountId/${ApiEndPoints.TRANSACTIONS}/$transactionId") + .body() + } + + suspend fun payViaMobile(accountId: Long): Transactions { + return client.post("${BaseURL().selfServiceUrl}${ApiEndPoints.SAVINGS_ACCOUNTS}/$accountId/${ApiEndPoints.TRANSACTIONS}") { + url { + parameters.append("command", "deposit") + } + }.body() + } + } \ No newline at end of file diff --git a/core/network/src/main/kotlin/org/mifospay/core/network/services/SavingsAccountsService.kt b/core/network/src/main/kotlin/org/mifospay/core/network/services/SavingsAccountsService.kt index dbaa03ba8..d8071dfe5 100644 --- a/core/network/src/main/kotlin/org/mifospay/core/network/services/SavingsAccountsService.kt +++ b/core/network/src/main/kotlin/org/mifospay/core/network/services/SavingsAccountsService.kt @@ -9,47 +9,6 @@ */ package org.mifospay.core.network.services -import com.mifospay.core.model.entity.Page -import com.mifospay.core.model.entity.accounts.savings.SavingAccount -import com.mifospay.core.model.entity.accounts.savings.SavingsWithAssociations -import com.mifospay.core.model.entity.accounts.savings.Transactions -import org.mifospay.core.network.ApiEndPoints -import org.mifospay.core.network.GenericResponse -import retrofit2.http.Body -import retrofit2.http.GET -import retrofit2.http.POST -import retrofit2.http.Path -import retrofit2.http.Query -import rx.Observable - interface SavingsAccountsService { - @GET(ApiEndPoints.SAVINGS_ACCOUNTS) - fun getSavingsAccounts( - @Query("limit") limit: Int, - ): Observable> - - @POST(ApiEndPoints.SAVINGS_ACCOUNTS) - fun createSavingsAccount(@Body savingAccount: SavingAccount): Observable - - @POST(ApiEndPoints.SAVINGS_ACCOUNTS + "/{accountId}") - fun blockUnblockAccount( - @Path("accountId") accountId: Long, - @Query("command") command: String?, - ): Observable - - @GET( - ApiEndPoints.SAVINGS_ACCOUNTS + "/{accountId}/" + ApiEndPoints.TRANSACTIONS + - "/{transactionId}", - ) - fun getSavingAccountTransaction( - @Path("accountId") accountId: Long, - @Path("transactionId") transactionId: Long, - ): Observable - - @POST( - ApiEndPoints.SAVINGS_ACCOUNTS + "/{accountId}/" + ApiEndPoints.TRANSACTIONS + - "?command=deposit", - ) - fun payViaMobile(@Path("accountId") accountId: Long): Observable }