Created
September 20, 2022 06:20
-
-
Save jsonkile/09ec516aba0bb053bd46fc3a4827bc91 to your computer and use it in GitHub Desktop.
Create Transaction Issue Ticket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.global.gomoney.data.repository.viewmodelrepository | |
import androidx.lifecycle.liveData | |
import com.bumptech.glide.Glide | |
import com.global.gomoney.coroutines.DispatcherProvider | |
import com.global.gomoney.data.manager.DataManager | |
import com.global.gomoney.data.model.NetworkStatus | |
import com.global.gomoney.data.model.User | |
import com.global.gomoney.root.MvpApp | |
import com.global.gomoney.utils.toSentenceCase | |
import com.zendesk.service.ErrorResponse | |
import com.zendesk.service.ZendeskCallback | |
import kotlinx.coroutines.async | |
import kotlinx.coroutines.awaitAll | |
import kotlinx.coroutines.suspendCancellableCoroutine | |
import kotlinx.coroutines.withContext | |
import zendesk.core.AnonymousIdentity | |
import zendesk.core.Zendesk | |
import zendesk.support.CreateRequest | |
import zendesk.support.Request | |
import zendesk.support.Support | |
import zendesk.support.UploadResponse | |
import java.io.File | |
import javax.inject.Inject | |
import kotlin.coroutines.resume | |
import kotlin.coroutines.resumeWithException | |
class ZendeskRepository @Inject constructor( | |
private val dataManager: DataManager, | |
private val dispatcherProvider: DispatcherProvider, | |
private val context: MvpApp | |
) { | |
suspend fun createTicket(file: File, message: String) = liveData<NetworkStatus<Unit>> { | |
val title = "Autogenerated BVN issue ticket" | |
emit(NetworkStatus.Loading()) | |
val user = withContext(dispatcherProvider.io()) { dataManager.getCurrentUser()!! } | |
setIdentity(user) | |
val wallet = withContext(dispatcherProvider.io()) { dataManager.getWallet()!! } | |
try { | |
val uploadResponse = uploadFile(file) | |
suspendCancellableCoroutine<Request> { cancellableContinuation -> | |
val provider = Support.INSTANCE.provider()!!.requestProvider() | |
val request = CreateRequest().apply { | |
subject = "Android ticket - ${wallet.account.nuban} - $title" | |
tags = listOf("android", "mobile", "bvn") | |
description = | |
"$message\nNuban - ${wallet.account.nuban}\nPhone Number - ${user.phoneNumber}" | |
attachments = listOf(uploadResponse.token) | |
} | |
provider.createRequest(request, object : ZendeskCallback<Request>() { | |
override fun onSuccess(p0: Request?) { | |
cancellableContinuation.resume(p0!!) | |
} | |
override fun onError(p0: ErrorResponse?) { | |
cancellableContinuation.resumeWithException( | |
RuntimeException( | |
p0?.reason | |
?: "An error occurred" | |
) | |
) | |
} | |
}) | |
} | |
emit(NetworkStatus.Success(Unit)) | |
} catch (e: Exception) { | |
emit(NetworkStatus.Error(e.message)) | |
} | |
} | |
suspend fun createTier3AccountUpgradeTicket(file: File, message: String) = | |
liveData<NetworkStatus<Unit>>(dispatcherProvider.io()) { | |
val title = "Autogenerated ID document upload issue ticket" | |
emit(NetworkStatus.Loading()) | |
val user = dataManager.getCurrentUser()!! | |
setIdentity(user) | |
val wallet = dataManager.getWallet()!! | |
val uploadProvider = Support.INSTANCE.provider()!!.uploadProvider() | |
try { | |
withContext(dispatcherProvider.io()) { | |
val selfie = async { | |
val selfieFile = | |
Glide.with(context).downloadOnly().load(user.selfie).submit().get() | |
uploadFile(selfieFile) | |
} | |
val idImage = async { uploadFile(file) } | |
val images = awaitAll(selfie, idImage) | |
suspendCancellableCoroutine<Request> { cancellableContinuation -> | |
val provider = Support.INSTANCE.provider()!!.requestProvider() | |
val request = CreateRequest().apply { | |
subject = "Android ticket - ${wallet.account.nuban} - $title" | |
tags = listOf("android", "mobile", "kyc_manual_upload") | |
description = | |
"$message\nNuban - ${wallet.account.nuban}\nPhone Number - ${user.phoneNumber}" | |
attachments = images.map { it.token } | |
} | |
provider.createRequest(request, object : ZendeskCallback<Request>() { | |
override fun onSuccess(p0: Request?) { | |
cancellableContinuation.resume(p0!!) | |
} | |
override fun onError(p0: ErrorResponse?) { | |
cancellableContinuation.resumeWithException( | |
RuntimeException( | |
p0?.reason | |
?: "An error occurred" | |
) | |
) | |
} | |
}) | |
} | |
} | |
emit(NetworkStatus.Success(Unit)) | |
} catch (e: Exception) { | |
emit(NetworkStatus.Error(e.message)) | |
} | |
} | |
/** | |
* Report issues with a transaction | |
* @param transactionID Ref to the transaction | |
* @param transactionType the type of transaction e.g Transfer, Split Payment... | |
* @param message the complaint entered by the user | |
*/ | |
suspend fun createTransactionIssueTicket( | |
transactionID: String, | |
transactionType: String, | |
message: String | |
) = | |
liveData<NetworkStatus<Unit>> { | |
val title = | |
"Transaction Issue Ticket (Ref: $transactionID, Transaction Type: $transactionType)" | |
emit(NetworkStatus.Loading()) | |
val user = withContext(dispatcherProvider.io()) { dataManager.getCurrentUser()!! } | |
setIdentity(user) | |
val wallet = withContext(dispatcherProvider.io()) { dataManager.getWallet()!! } | |
try { | |
suspendCancellableCoroutine<Request> { cancellableContinuation -> | |
val provider = Support.INSTANCE.provider()!!.requestProvider() | |
val request = CreateRequest().apply { | |
subject = "Android ticket - ${wallet.account.nuban} - $title" | |
tags = listOf("android", "mobile", "transaction") | |
description = | |
"$message\nNuban - ${wallet.account.nuban}\nPhone Number - ${user.phoneNumber}" | |
} | |
provider.createRequest(request, object : ZendeskCallback<Request>() { | |
override fun onSuccess(p0: Request?) { | |
cancellableContinuation.resume(p0!!) | |
} | |
override fun onError(p0: ErrorResponse?) { | |
cancellableContinuation.resumeWithException( | |
RuntimeException( | |
p0?.reason | |
?: "An error occurred" | |
) | |
) | |
} | |
}) | |
} | |
emit(NetworkStatus.Success(Unit)) | |
} catch (e: Exception) { | |
emit(NetworkStatus.Error(e.message)) | |
} | |
} | |
private suspend fun uploadFile(file: File, mimeType: String = "image/*"): UploadResponse { | |
val uploadProvider = Support.INSTANCE.provider()!!.uploadProvider() | |
return suspendCancellableCoroutine<UploadResponse> { cancellableContinuation -> | |
uploadProvider.uploadAttachment( | |
file.name, | |
file, | |
mimeType, | |
object : ZendeskCallback<UploadResponse>() { | |
override fun onSuccess(p0: UploadResponse?) { | |
cancellableContinuation.resume(p0!!) | |
} | |
override fun onError(p0: ErrorResponse?) { | |
cancellableContinuation.resumeWithException( | |
RuntimeException( | |
p0?.reason | |
?: "An error occurred" | |
) | |
) | |
} | |
}) | |
} | |
} | |
private fun setIdentity(user: User) { | |
val identity = AnonymousIdentity.Builder() | |
identity.withEmailIdentifier(user.email) | |
identity.withNameIdentifier(user.name().toSentenceCase()) | |
Zendesk.INSTANCE.setIdentity(identity.build()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment