Skip to content

Commit

Permalink
Feat: Migrated Profile Module to KMP (#1788)
Browse files Browse the repository at this point in the history
  • Loading branch information
niyajali authored Oct 14, 2024
1 parent d78a798 commit 5d84fb5
Show file tree
Hide file tree
Showing 50 changed files with 1,723 additions and 1,301 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,22 @@ import org.mifospay.core.data.util.NetworkMonitor
import org.mifospay.core.data.util.TimeZoneMonitor

private val ioDispatcher = named(MifosDispatchers.IO.name)
private val unconfined = named(MifosDispatchers.Unconfined.name)

val RepositoryModule = module {
single<AccountRepository> { AccountRepositoryImpl(get(), get(ioDispatcher)) }
single<AuthenticationRepository> {
AuthenticationRepositoryImpl(get(), get(ioDispatcher))
}
single<BeneficiaryRepository> { BeneficiaryRepositoryImpl(get(), get(ioDispatcher)) }
single<ClientRepository> { ClientRepositoryImpl(get(), get(), get(ioDispatcher)) }
single<ClientRepository> {
ClientRepositoryImpl(
apiManager = get(),
fineractApiManager = get(),
ioDispatcher = get(ioDispatcher),
unconfinedDispatcher = get(unconfined),
)
}
single<DocumentRepository> { DocumentRepositoryImpl(get(), get(ioDispatcher)) }
single<InvoiceRepository> { InvoiceRepositoryImpl(get(), get(ioDispatcher)) }
single<KycLevelRepository> { KycLevelRepositoryImpl(get(), get(ioDispatcher)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,38 @@ package org.mifospay.core.data.mapper

import org.mifospay.core.model.client.Client
import org.mifospay.core.model.client.ClientAddress
import org.mifospay.core.model.client.ClientStatus
import org.mifospay.core.model.client.ClientTimeline
import org.mifospay.core.model.client.NewClient
import org.mifospay.core.model.client.UpdatedClient
import org.mifospay.core.network.model.entity.Page
import org.mifospay.core.network.model.entity.client.Address
import org.mifospay.core.network.model.entity.client.ClientEntity
import org.mifospay.core.network.model.entity.client.ClientTimelineEntity
import org.mifospay.core.network.model.entity.client.NewClientEntity
import org.mifospay.core.network.model.entity.client.Status
import org.mifospay.core.network.model.entity.client.UpdateClientEntity

fun ClientEntity.toModel(): Client {
return Client(
name = this.displayName ?: "",
clientId = this.id.toLong(),
externalId = this.externalId,
mobileNo = this.mobileNo,
displayName = this.displayName ?: "",
image = this.imageId.toString(),
id = id ?: 0,
accountNo = accountNo ?: "",
externalId = externalId ?: "",
active = active,
activationDate = activationDate,
firstname = firstname ?: "",
lastname = lastname ?: "",
displayName = displayName ?: "",
mobileNo = mobileNo ?: "",
emailAddress = emailAddress ?: "",
dateOfBirth = dateOfBirth,
isStaff = isStaff ?: false,
officeId = officeId ?: 0,
officeName = officeName ?: "",
savingsProductName = savingsProductName ?: "",
status = status?.toModel() ?: ClientStatus(),
timeline = timeline?.toModel() ?: ClientTimeline(),
legalForm = legalForm?.toModel() ?: ClientStatus(),
)
}

Expand Down Expand Up @@ -58,3 +76,31 @@ fun ClientAddress.toEntity(): Address {
addressTypeId = addressTypeId,
)
}

fun Status.toModel(): ClientStatus {
return ClientStatus(
id = id ?: 0,
code = code ?: "",
value = value ?: "",
)
}

fun ClientTimelineEntity.toModel(): ClientTimeline {
return ClientTimeline(
submittedOnDate = submittedOnDate,
activatedOnDate = activatedOnDate,
activatedByUsername = activatedByUsername,
activatedByFirstname = activatedByFirstname,
activatedByLastname = activatedByLastname,
)
}

fun UpdatedClient.toEntity(): UpdateClientEntity {
return UpdateClientEntity(
firstname = firstname,
lastname = lastname,
externalId = externalId,
mobileNo = mobileNo,
emailAddress = emailAddress,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@
package org.mifospay.core.data.repository

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import org.mifospay.core.common.Result
import org.mifospay.core.model.account.Account
import org.mifospay.core.model.client.Client
import org.mifospay.core.model.client.NewClient
import org.mifospay.core.model.client.UpdatedClient
import org.mifospay.core.network.model.entity.Page
import org.mifospay.core.network.model.entity.client.ClientAccountsEntity

interface ClientRepository {

fun getClientInfo(clientId: Long): StateFlow<Result<Client>>

suspend fun getClients(): Flow<Result<Page<Client>>>

suspend fun getClient(clientId: Long): Result<Client>

suspend fun updateClient(clientId: Long, client: Client): Flow<Result<Unit>>
suspend fun updateClient(clientId: Long, client: UpdatedClient): Result<String>

suspend fun getClientImage(clientId: Long): Flow<Result<String>>
fun getClientImage(clientId: Long): Flow<Result<String>>

suspend fun updateClientImage(clientId: Long, image: String): Flow<Result<Unit>>
suspend fun updateClientImage(clientId: Long, image: String): Result<String>

suspend fun getClientAccounts(clientId: Long): Flow<Result<ClientAccountsEntity>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
package org.mifospay.core.data.repositoryImp

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext
import org.mifospay.core.common.Result
import org.mifospay.core.common.asResult
Expand All @@ -23,6 +28,7 @@ import org.mifospay.core.data.repository.ClientRepository
import org.mifospay.core.model.account.Account
import org.mifospay.core.model.client.Client
import org.mifospay.core.model.client.NewClient
import org.mifospay.core.model.client.UpdatedClient
import org.mifospay.core.network.FineractApiManager
import org.mifospay.core.network.SelfServiceApiManager
import org.mifospay.core.network.model.entity.Page
Expand All @@ -32,34 +38,67 @@ class ClientRepositoryImpl(
private val apiManager: SelfServiceApiManager,
private val fineractApiManager: FineractApiManager,
private val ioDispatcher: CoroutineDispatcher,
unconfinedDispatcher: CoroutineDispatcher,
) : ClientRepository {
private val coroutineScope = CoroutineScope(unconfinedDispatcher)

override suspend fun getClients(): Flow<Result<Page<Client>>> {
return apiManager.clientsApi.clients().map { it.toModel() }.asResult().flowOn(ioDispatcher)
}

override fun getClientInfo(clientId: Long): StateFlow<Result<Client>> {
return fineractApiManager.clientsApi
.getClient(clientId)
.catch { Result.Error(it) }
.map { Result.Success(it.toModel()) }
.stateIn(
scope = coroutineScope,
started = SharingStarted.Eagerly,
initialValue = Result.Loading,
)
}

override suspend fun getClient(clientId: Long): Result<Client> {
return try {
val result = apiManager.clientsApi.getClientForId(clientId)
val result = fineractApiManager.clientsApi.getClientForId(clientId)
Result.Success(result.toModel())
} catch (e: Exception) {
Result.Error(e)
}
}

override suspend fun updateClient(clientId: Long, client: Client): Flow<Result<Unit>> {
return apiManager.clientsApi
.updateClient(clientId, client)
.asResult().flowOn(ioDispatcher)
override suspend fun updateClient(clientId: Long, client: UpdatedClient): Result<String> {
return try {
withContext(ioDispatcher) {
fineractApiManager.clientsApi.updateClient(clientId, client.toEntity())
}

Result.Success("Client updated successfully")
} catch (e: Exception) {
Result.Error(e)
}
}

override suspend fun getClientImage(clientId: Long): Flow<Result<String>> {
return apiManager.clientsApi.getClientImage(clientId).asResult().flowOn(ioDispatcher)
override fun getClientImage(clientId: Long): Flow<Result<String>> {
return fineractApiManager.clientsApi
.getClientImage(clientId)
.catch { Result.Error(it) }
.map { Result.Success(it) }
}

override suspend fun updateClientImage(clientId: Long, image: String): Flow<Result<Unit>> {
return apiManager.clientsApi
.updateClientImage(clientId, image)
.asResult().flowOn(ioDispatcher)
override suspend fun updateClientImage(clientId: Long, image: String): Result<String> {
return try {
withContext(ioDispatcher) {
fineractApiManager.clientsApi.updateClientImage(
clientId = clientId,
typedFile = "data:image/png;base64,$image",
)
}

Result.Success("Client image updated successfully")
} catch (e: Exception) {
Result.Error(e)
}
}

override suspend fun getClientAccounts(clientId: Long): Flow<Result<ClientAccountsEntity>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,39 @@ import kotlinx.serialization.Serializable

@Serializable
data class ClientPreferences(
val name: String,
val image: String,
val id: Long,
val accountNo: String,
val externalId: String,
val clientId: Long,
val active: Boolean,
val activationDate: List<Long>,
val firstname: String,
val lastname: String,
val displayName: String,
val mobileNo: String,
val emailAddress: String,
val dateOfBirth: List<Long>,
val isStaff: Boolean,
val officeId: Long,
val officeName: String,
val savingsProductName: String,
) {
companion object {
val DEFAULT = ClientPreferences(
name = "",
image = "",
id = 0L,
accountNo = "",
externalId = "",
clientId = 0,
active = false,
activationDate = emptyList(),
firstname = "",
lastname = "",
displayName = "",
mobileNo = "",
emailAddress = "",
dateOfBirth = emptyList(),
isStaff = false,
officeId = 0,
officeName = "",
savingsProductName = "",
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,41 @@ import org.mifospay.core.model.user.UserInfo

fun ClientPreferences.toClientInfo(): Client {
return Client(
name = name,
image = image,
id = id,
accountNo = accountNo,
externalId = externalId,
clientId = clientId,
active = active,
activationDate = activationDate,
firstname = firstname,
lastname = lastname,
displayName = displayName,
mobileNo = mobileNo,
emailAddress = emailAddress,
dateOfBirth = dateOfBirth,
isStaff = isStaff,
officeId = officeId,
officeName = officeName,
savingsProductName = savingsProductName,
)
}

fun Client.toClientPreferences(): ClientPreferences {
return ClientPreferences(
name = name,
image = image,
id = id,
accountNo = accountNo,
externalId = externalId,
clientId = clientId,
active = active,
activationDate = activationDate,
firstname = firstname,
lastname = lastname,
displayName = displayName,
mobileNo = mobileNo,
emailAddress = emailAddress,
dateOfBirth = dateOfBirth,
isStaff = isStaff,
officeId = officeId,
officeName = officeName,
savingsProductName = savingsProductName,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import kotlinx.serialization.ExperimentalSerializationApi
import org.mifospay.core.datastore.proto.ClientPreferences
import org.mifospay.core.datastore.proto.UserInfoPreferences
import org.mifospay.core.model.client.Client
import org.mifospay.core.model.client.UpdatedClient
import org.mifospay.core.model.user.UserInfo

private const val USER_INFO_KEY = "userInfo"
Expand Down Expand Up @@ -63,6 +64,8 @@ class UserPreferencesDataSource(

val clientInfo = _clientInfo.map(ClientPreferences::toClientInfo)

val clientId = _clientInfo.map { it.id }

suspend fun updateClientInfo(client: Client) {
withContext(dispatcher) {
settings.putClientPreference(client.toClientPreferences())
Expand All @@ -77,6 +80,22 @@ class UserPreferencesDataSource(
}
}

suspend fun updateClientProfile(client: UpdatedClient) {
withContext(dispatcher) {
val updatedClient = _clientInfo.value.copy(
firstname = client.firstname,
lastname = client.lastname,
displayName = client.firstname + " " + client.lastname,
emailAddress = client.emailAddress,
mobileNo = client.mobileNo,
externalId = client.externalId,
)

settings.putClientPreference(updatedClient)
_clientInfo.value = updatedClient
}
}

suspend fun updateToken(token: String) {
withContext(dispatcher) {
settings.putUserInfoPreference(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import org.mifospay.core.common.Result
import org.mifospay.core.model.client.Client
import org.mifospay.core.model.client.UpdatedClient
import org.mifospay.core.model.user.UserInfo

interface UserPreferencesRepository {
Expand All @@ -22,6 +23,8 @@ interface UserPreferencesRepository {

val client: StateFlow<Client?>

val clientId: StateFlow<Long?>

val authToken: String?

suspend fun updateToken(token: String): Result<Unit>
Expand All @@ -30,5 +33,7 @@ interface UserPreferencesRepository {

suspend fun updateClientInfo(client: Client): Result<Unit>

suspend fun updateClientProfile(client: UpdatedClient): Result<Unit>

suspend fun logOut(): Unit
}
Loading

0 comments on commit 5d84fb5

Please sign in to comment.