Skip to content

Commit

Permalink
feat: Migrated savings service from retrofit to ktor (#1758)
Browse files Browse the repository at this point in the history
* getSavingsAccounts in SavingsAccountService migrated to ktor

* migrated savings account service to ktor
  • Loading branch information
PratyushSingh07 authored Sep 4, 2024
1 parent 7089ebc commit 5059e62
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,37 @@
*/
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(
private val mFineractRepository: FineractRepository,
) : UseCase<BlockUnblockCommand.RequestValues, BlockUnblockCommand.ResponseValue>() {

override fun executeUseCase(requestValues: RequestValues) {
mFineractRepository.blockUnblockAccount(requestValues.accountId, requestValues.command)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(
object : Subscriber<GenericResponse>() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -26,32 +27,31 @@ class FetchAccountTransaction @Inject constructor(
) :
UseCase<FetchAccountTransaction.RequestValues, FetchAccountTransaction.ResponseValue>() {
override fun executeUseCase(requestValues: RequestValues) {
fineractRepository.getSelfAccountTransactionFromId(
requestValues.accountId,
requestValues.transactionId,
)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(
object : Observer<Transactions?> {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FetchMerchants.RequestValues, FetchMerchants.ResponseValue>() {
override fun executeUseCase(requestValues: RequestValues) {
mFineractRepository.savingsAccounts
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(
object : Subscriber<Page<SavingsWithAssociations>>() {
override fun onCompleted() {}
override fun onError(e: Throwable) {
e.message?.let { useCaseCallback.onError(it) }
}

override fun onNext(savingsWithAssociationsPage: Page<SavingsWithAssociations>) {
val savingsWithAssociationsList = savingsWithAssociationsPage.pageItems
val merchantsList: MutableList<SavingsWithAssociations> = 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<SavingsWithAssociations> = 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ class FineractRepository @Inject constructor(
return fineractApiManager.clientsApi.getAccounts(clientId, Constants.SAVINGS)
}

val savingsAccounts: Observable<Page<SavingsWithAssociations>>
get() = fineractApiManager.savingsAccountsApi.getSavingsAccounts(-1)
suspend fun savingsAccounts(): Page<SavingsWithAssociations>
= fineractApiManager.ktorSavingsAccountApi.getSavingsAccounts(-1)

fun blockUnblockAccount(accountId: Long, command: String?): Observable<GenericResponse> {
return fineractApiManager.savingsAccountsApi.blockUnblockAccount(
suspend fun blockUnblockAccount(accountId: Long, command: String?): GenericResponse {
return fineractApiManager.ktorSavingsAccountApi.blockUnblockAccount(
accountId,
command,
)
Expand Down Expand Up @@ -293,11 +293,11 @@ class FineractRepository @Inject constructor(
)
}

fun getSelfAccountTransactionFromId(
suspend fun getSelfAccountTransactionFromId(
accountId: Long,
transactionId: Long,
): Observable<Transactions> {
return selfApiManager.savingAccountsListApi.getSavingAccountTransaction(
): Transactions {
return selfApiManager.ktorSavingsAccountApi.getSavingAccountTransaction(
accountId,
transactionId,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -93,4 +95,7 @@ class FineractApiManager @Inject constructor(

val standingInstructionApi: StandingInstructionService
get() = standingInstructionService

val ktorSavingsAccountApi: KtorSavingsAccountService
get() = ktorSavingsAccountService
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class NetworkModule {
@Named("FineractThirdPartyTransferService") thirdPartyTransferService: ThirdPartyTransferService,
standingInstructionService: StandingInstructionService,
notificationService: NotificationService,
ktorSavingsAccountService: KtorSavingsAccountService
): FineractApiManager {
return FineractApiManager(
authenticationService,
Expand All @@ -134,6 +135,7 @@ class NetworkModule {
thirdPartyTransferService,
standingInstructionService,
notificationService,
ktorSavingsAccountService
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,69 @@
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 {
parameters.append("associations", associationType)
}
}.body()
}

suspend fun getSavingsAccounts(limit: Int): Page<SavingsWithAssociations> {
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()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Page<SavingsWithAssociations>>

@POST(ApiEndPoints.SAVINGS_ACCOUNTS)
fun createSavingsAccount(@Body savingAccount: SavingAccount): Observable<GenericResponse>

@POST(ApiEndPoints.SAVINGS_ACCOUNTS + "/{accountId}")
fun blockUnblockAccount(
@Path("accountId") accountId: Long,
@Query("command") command: String?,
): Observable<GenericResponse>

@GET(
ApiEndPoints.SAVINGS_ACCOUNTS + "/{accountId}/" + ApiEndPoints.TRANSACTIONS +
"/{transactionId}",
)
fun getSavingAccountTransaction(
@Path("accountId") accountId: Long,
@Path("transactionId") transactionId: Long,
): Observable<Transactions>

@POST(
ApiEndPoints.SAVINGS_ACCOUNTS + "/{accountId}/" + ApiEndPoints.TRANSACTIONS +
"?command=deposit",
)
fun payViaMobile(@Path("accountId") accountId: Long): Observable<Transactions>
}

0 comments on commit 5059e62

Please sign in to comment.