From 907199a48e14cd81ec0c4f3e9fd1762d368b6f7f Mon Sep 17 00:00:00 2001 From: Yun Cheng <129205442+ycheng-kickstarter@users.noreply.github.com> Date: Wed, 6 Mar 2024 12:03:29 -0500 Subject: [PATCH] MBL-1252 Add a createAttributionEvent method to the Apollo client (#1970) * MBL-1252 Add a createAttributionEvent method to the Apollo client --- .../ExternalApplicationModule.java | 4 +- .../InternalApplicationModule.java | 4 +- .../mock/services/MockApolloClient.kt | 7 +- .../kickstarter/services/KSApolloClientV2.kt | 71 ++++++++++++++++++- 4 files changed, 80 insertions(+), 6 deletions(-) diff --git a/app/src/external/java/com/kickstarter/ExternalApplicationModule.java b/app/src/external/java/com/kickstarter/ExternalApplicationModule.java index 56d6ddff5c..2f5db3a0cb 100644 --- a/app/src/external/java/com/kickstarter/ExternalApplicationModule.java +++ b/app/src/external/java/com/kickstarter/ExternalApplicationModule.java @@ -68,7 +68,7 @@ static ApolloClientType provideApolloClientType(final @NonNull ApolloClient apol @Provides @Singleton @NonNull - static ApolloClientTypeV2 provideApolloClientTypeV2(final @NonNull ApolloClient apolloClient) { - return Secrets.IS_OSS ? new MockApolloClientV2() : new KSApolloClientV2(apolloClient); + static ApolloClientTypeV2 provideApolloClientTypeV2(final @NonNull ApolloClient apolloClient, final @NonNull Gson gson) { + return Secrets.IS_OSS ? new MockApolloClientV2() : new KSApolloClientV2(apolloClient, gson); } } diff --git a/app/src/internal/java/com/kickstarter/InternalApplicationModule.java b/app/src/internal/java/com/kickstarter/InternalApplicationModule.java index 8235f6ae51..e14f0ad610 100644 --- a/app/src/internal/java/com/kickstarter/InternalApplicationModule.java +++ b/app/src/internal/java/com/kickstarter/InternalApplicationModule.java @@ -64,8 +64,8 @@ static ApolloClientType provideApolloClientType(final @NonNull ApolloClient apol @Provides @Singleton @NonNull - static ApolloClientTypeV2 provideApolloClientTypeV2(final @NonNull ApolloClient apolloClient) { - return Secrets.IS_OSS ? new MockApolloClientV2() : new KSApolloClientV2(apolloClient); + static ApolloClientTypeV2 provideApolloClientTypeV2(final @NonNull ApolloClient apolloClient, final @NonNull Gson gson) { + return Secrets.IS_OSS ? new MockApolloClientV2() : new KSApolloClientV2(apolloClient, gson); } @Provides diff --git a/app/src/main/java/com/kickstarter/mock/services/MockApolloClient.kt b/app/src/main/java/com/kickstarter/mock/services/MockApolloClient.kt index b74f9d2862..c86d661e93 100644 --- a/app/src/main/java/com/kickstarter/mock/services/MockApolloClient.kt +++ b/app/src/main/java/com/kickstarter/mock/services/MockApolloClient.kt @@ -44,6 +44,7 @@ import com.kickstarter.services.apiresponses.DiscoverEnvelope import com.kickstarter.services.apiresponses.ShippingRulesEnvelope import com.kickstarter.services.apiresponses.commentresponse.CommentEnvelope import com.kickstarter.services.apiresponses.updatesresponse.UpdatesGraphQlEnvelope +import com.kickstarter.services.mutations.CreateAttributionEventData import com.kickstarter.services.mutations.CreateBackingData import com.kickstarter.services.mutations.CreateCheckoutData import com.kickstarter.services.mutations.PostCommentData @@ -273,7 +274,7 @@ open class MockApolloClientV2 : ApolloClientTypeV2 { return io.reactivex.Observable.empty() } - override fun createPaymentIntent(createPaymentIntentInput: CreatePaymentIntentInput): io.reactivex.Observable { + override fun createAttributionEvent(eventInput: CreateAttributionEventData): io.reactivex.Observable { return io.reactivex.Observable.empty() } @@ -292,6 +293,10 @@ open class MockApolloClientV2 : ApolloClientTypeV2 { ): io.reactivex.Observable { return io.reactivex.Observable.empty() } + + override fun createPaymentIntent(createPaymentIntentInput: CreatePaymentIntentInput): io.reactivex.Observable { + return io.reactivex.Observable.empty() + } } open class MockApolloClient : ApolloClientType { diff --git a/app/src/main/java/com/kickstarter/services/KSApolloClientV2.kt b/app/src/main/java/com/kickstarter/services/KSApolloClientV2.kt index d6e2805f31..8ce058c80c 100644 --- a/app/src/main/java/com/kickstarter/services/KSApolloClientV2.kt +++ b/app/src/main/java/com/kickstarter/services/KSApolloClientV2.kt @@ -1,13 +1,50 @@ package com.kickstarter.services +import CancelBackingMutation +import ClearUserUnseenActivityMutation import CompleteOnSessionCheckoutMutation +import CreateAttributionEventMutation +import CreateBackingMutation +import CreateCheckoutMutation +import CreateCommentMutation +import CreateFlaggingMutation +import CreatePasswordMutation import CreatePaymentIntentMutation +import CreateSetupIntentMutation +import DeletePaymentSourceMutation +import ErroredBackingsQuery +import FetchCategoryQuery +import FetchProjectQuery +import GetBackingQuery +import GetCommentQuery +import GetProjectAddOnsQuery +import GetProjectBackingQuery +import GetProjectCommentsQuery +import GetProjectUpdateCommentsQuery +import GetProjectUpdatesQuery +import GetRepliesForCommentQuery +import GetRootCategoriesQuery +import GetShippingRulesForRewardIdQuery +import ProjectCreatorDetailsQuery +import SavePaymentMethodMutation +import SendEmailVerificationMutation +import SendMessageMutation +import TriggerThirdPartyEventMutation +import UnwatchProjectMutation +import UpdateBackingMutation +import UpdateUserCurrencyMutation +import UpdateUserEmailMutation +import UpdateUserPasswordMutation +import UserPaymentsQuery +import UserPrivacyQuery import ValidateCheckoutQuery +import WatchProjectMutation import android.util.Pair import com.apollographql.apollo.ApolloCall import com.apollographql.apollo.ApolloClient import com.apollographql.apollo.api.Response import com.apollographql.apollo.exception.ApolloException +import com.google.gson.Gson import com.kickstarter.libs.utils.extensions.isNotNull import com.kickstarter.models.Backing import com.kickstarter.models.Category @@ -28,6 +65,7 @@ import com.kickstarter.services.apiresponses.ShippingRulesEnvelope import com.kickstarter.services.apiresponses.commentresponse.CommentEnvelope import com.kickstarter.services.apiresponses.commentresponse.PageInfoEnvelope import com.kickstarter.services.apiresponses.updatesresponse.UpdatesGraphQlEnvelope +import com.kickstarter.services.mutations.CreateAttributionEventData import com.kickstarter.services.mutations.CreateBackingData import com.kickstarter.services.mutations.CreateCheckoutData import com.kickstarter.services.mutations.PostCommentData @@ -39,6 +77,7 @@ import com.kickstarter.services.transformers.commentTransformer import com.kickstarter.services.transformers.complexRewardItemsTransformer import com.kickstarter.services.transformers.decodeRelayId import com.kickstarter.services.transformers.encodeRelayId +import com.kickstarter.services.transformers.getCreateAttributionEventMutation import com.kickstarter.services.transformers.getTriggerThirdPartyEventMutation import com.kickstarter.services.transformers.projectTransformer import com.kickstarter.services.transformers.rewardTransformer @@ -147,12 +186,14 @@ interface ApolloClientTypeV2 { paymentIntentClientSecret: String, paymentSourceId: String ): Observable + + fun createAttributionEvent(eventInput: CreateAttributionEventData): Observable } private const val PAGE_SIZE = 25 private const val REPLIES_PAGE_SIZE = 7 -class KSApolloClientV2(val service: ApolloClient) : ApolloClientTypeV2 { +class KSApolloClientV2(val service: ApolloClient, val gson: Gson) : ApolloClientTypeV2 { override fun getProject(project: Project): Observable { return getProject(project.slug() ?: "") } @@ -1544,4 +1585,32 @@ class KSApolloClientV2(val service: ApolloClient) : ApolloClientTypeV2 { return@defer ps } } + + override fun createAttributionEvent(eventInput: CreateAttributionEventData): Observable { + return Observable.defer { + val ps = PublishSubject.create() + + val mutation = getCreateAttributionEventMutation(eventInput, gson) + + service.mutate(mutation) + .enqueue(object : ApolloCall.Callback() { + override fun onFailure(exception: ApolloException) { + ps.onError(exception) + } + + override fun onResponse(response: Response) { + if (response.hasErrors()) { + ps.onError(Exception(response.errors?.first()?.message ?: "")) + } + + response.data?.let { + val isSuccess = it.createAttributionEvent()?.successful() ?: false + ps.onNext(isSuccess) + } + ps.onComplete() + } + }) + return@defer ps + } + } }