Skip to content

Commit

Permalink
Merge pull request #1870 from HedvigInsurance/cleanup/octopus_redeed_…
Browse files Browse the repository at this point in the history
…campaign

Use octopus for redeeming a campaign code
  • Loading branch information
StylianosGakis authored Nov 8, 2023
2 parents eb990a0 + 9a8afa9 commit 8edf186
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 43 deletions.

This file was deleted.

10 changes: 9 additions & 1 deletion app/data/data-forever/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
plugins {
id("hedvig.android.ktlint")
id("hedvig.android.library")
alias(libs.plugins.apollo)
alias(libs.plugins.squareSortDependencies)
}

dependencies {
apolloMetadata(projects.apolloOctopusPublic)

implementation(libs.apollo.normalizedCache)
implementation(libs.arrow.core)
implementation(libs.koin.core)
implementation(projects.apolloCore)
implementation(projects.apolloGiraffePublic)
implementation(projects.apolloOctopusPublic)
implementation(projects.coreCommonPublic)
implementation(projects.coreDemoMode)
implementation(projects.languageCore)
}

apollo {
service("octopus") {
packageName.set("octopus")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mutation RedeemCampaignCode($campaignCode: String!) {
memberCampaignsRedeem(code: $campaignCode) {
member {
id
}
userError {
message
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package com.hedvig.android.data.forever

import arrow.core.Either
import com.hedvig.android.core.common.ErrorMessage
import giraffe.RedeemReferralCodeMutation
import octopus.ReferralsQuery

interface ForeverRepository {
suspend fun getReferralsData(): Either<ErrorMessage, ReferralsQuery.Data>
suspend fun updateCode(newCode: String): Either<ReferralError, String>
suspend fun redeemReferralCode(campaignCode: CampaignCode): Either<ErrorMessage, RedeemReferralCodeMutation.Data?>
suspend fun redeemReferralCode(campaignCode: CampaignCode): Either<ErrorMessage, Unit>
data class ReferralError(val message: String?)
}

@JvmInline
value class CampaignCode(val code: String)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import arrow.core.Either
import arrow.core.raise.either
import arrow.core.right
import com.hedvig.android.core.common.ErrorMessage
import giraffe.RedeemReferralCodeMutation
import octopus.ReferralsQuery
import octopus.type.CurrencyCode
import octopus.type.MemberReferralStatus
Expand Down Expand Up @@ -76,5 +75,5 @@ internal class ForeverRepositoryDemo : ForeverRepository {

override suspend fun redeemReferralCode(
campaignCode: CampaignCode,
): Either<ErrorMessage, RedeemReferralCodeMutation.Data?> = either { null }
): Either<ErrorMessage, Unit> = either { Unit }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,25 @@ import com.hedvig.android.apollo.safeExecute
import com.hedvig.android.apollo.toEither
import com.hedvig.android.core.common.ErrorMessage
import com.hedvig.android.data.forever.ForeverRepository.ReferralError
import com.hedvig.android.language.LanguageService
import giraffe.RedeemReferralCodeMutation
import com.hedvig.android.logger.LogPriority
import com.hedvig.android.logger.logcat
import octopus.MemberReferralInformationCodeUpdateMutation
import octopus.RedeemCampaignCodeMutation
import octopus.ReferralsQuery

@JvmInline
value class CampaignCode(val code: String)

internal class ForeverRepositoryImpl(
private val apolloClientOctopus: ApolloClient,
private val apolloClientGiraffe: ApolloClient,
private val languageService: LanguageService,
private val apolloClient: ApolloClient,
) : ForeverRepository {
private val referralsQuery = ReferralsQuery()

override suspend fun getReferralsData(): Either<ErrorMessage, ReferralsQuery.Data> = apolloClientOctopus
override suspend fun getReferralsData(): Either<ErrorMessage, ReferralsQuery.Data> = apolloClient
.query(referralsQuery)
.fetchPolicy(FetchPolicy.NetworkOnly)
.safeExecute()
.toEither(::ErrorMessage)

override suspend fun updateCode(newCode: String): Either<ReferralError, String> = either {
val result = apolloClientOctopus
val result = apolloClient
.mutation(MemberReferralInformationCodeUpdateMutation(newCode))
.safeExecute()
.toEither { message, _ -> ReferralError(message) }
Expand All @@ -52,10 +48,18 @@ internal class ForeverRepositoryImpl(
// TODO Move to payments module
override suspend fun redeemReferralCode(
campaignCode: CampaignCode,
): Either<ErrorMessage, RedeemReferralCodeMutation.Data?> {
return apolloClientGiraffe
.mutation(RedeemReferralCodeMutation(campaignCode.code, languageService.getGraphQLLocale()))
): Either<ErrorMessage, Unit> {
return apolloClient
.mutation(RedeemCampaignCodeMutation(campaignCode.code))
.safeExecute()
.toEither(::ErrorMessage)
.onLeft { logcat(LogPriority.WARN, it.throwable) { "redeemReferralCode failed. Message:${it.message}" } }
.onRight {
val userError = it.memberCampaignsRedeem.userError
if (userError != null) {
logcat(LogPriority.ERROR) { "redeemReferralCode failed. User error:${userError.message}" }
}
}
.map { Unit }
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.hedvig.android.data.forever.di
package com.hedvig.android.data.forever

import com.hedvig.android.core.demomode.DemoManager
import com.hedvig.android.core.demomode.ProdOrDemoProvider
import com.hedvig.android.data.forever.ForeverRepository

class ForeverRepositoryProvider(
override val demoManager: DemoManager,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package com.hedvig.android.data.forever.di

import com.apollographql.apollo3.ApolloClient
import com.hedvig.android.apollo.giraffe.di.giraffeClient
import com.hedvig.android.apollo.octopus.di.octopusClient
import com.hedvig.android.core.demomode.DemoManager
import com.hedvig.android.data.forever.ForeverRepositoryDemo
import com.hedvig.android.data.forever.ForeverRepositoryImpl
import com.hedvig.android.language.LanguageService
import com.hedvig.android.data.forever.ForeverRepositoryProvider
import org.koin.dsl.module

val foreverDataModule = module {
single<ForeverRepositoryImpl> {
ForeverRepositoryImpl(
apolloClientOctopus = get<ApolloClient>(octopusClient),
apolloClientGiraffe = get<ApolloClient>(giraffeClient),
languageService = get<LanguageService>(),
)
ForeverRepositoryImpl(apolloClient = get<ApolloClient>(octopusClient))
}
single<ForeverRepositoryDemo> {
ForeverRepositoryDemo()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.hedvig.android.feature.forever.di

import com.hedvig.android.data.forever.di.ForeverRepositoryProvider
import com.hedvig.android.data.forever.ForeverRepositoryProvider
import com.hedvig.android.feature.forever.ForeverViewModel
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.hedvig.android.apollo.giraffe.di.giraffeClient
import com.hedvig.android.apollo.octopus.di.octopusClient
import com.hedvig.android.auth.LogoutUseCase
import com.hedvig.android.core.demomode.DemoManager
import com.hedvig.android.data.forever.di.ForeverRepositoryProvider
import com.hedvig.android.data.forever.ForeverRepositoryProvider
import com.hedvig.android.data.settings.datastore.SettingsDataStore
import com.hedvig.android.feature.profile.aboutapp.AboutAppViewModel
import com.hedvig.android.feature.profile.data.ProfileRepositoryDemo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ internal class PaymentPresenter(
}

LaunchedEffect(newDiscountCodeBeingSubmitted) {
val newCode = newDiscountCodeBeingSubmitted ?: return@LaunchedEffect
referralsRepositoryProvider.provide().redeemReferralCode(newCode).fold(
val newCampaignCode = newDiscountCodeBeingSubmitted ?: return@LaunchedEffect
referralsRepositoryProvider.provide().redeemReferralCode(newCampaignCode).fold(
ifLeft = { error -> paymentUiState = paymentUiState.copy(discountError = error.message) },
ifRight = { loadIteration++ },
)
Expand Down

0 comments on commit 8edf186

Please sign in to comment.