Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ interface PirRunStateHandler {
val lastActionId: String,
val durationMs: Long,
val currentActionAttemptCount: Int,
val generatedEmail: String? = null,
) : PirRunState(broker)

data class BrokerRecordEmailConfirmationStarted(
Expand Down Expand Up @@ -626,11 +627,16 @@ class RealPirRunStateHandler @Inject constructor(
}

private suspend fun handleBrokerRecordEmailConfirmationNeeded(pirRunState: BrokerRecordEmailConfirmationNeeded) {
// Fall back to the generated email when the extracted profile has none. Brokers that use an
// explicit generateEmail action (e.g. SpyFly) store the address on the state's generatedEmailData
// rather than on extractedProfile, and the polling worker keys off this value to fetch the
// confirmation link.
val email = pirRunState.extractedProfile.email.ifEmpty { pirRunState.generatedEmail.orEmpty() }
jobRecordUpdater.markOptOutAsWaitingForEmailConfirmation(
profileQueryId = pirRunState.extractedProfile.profileQueryId,
extractedProfileId = pirRunState.extractedProfile.dbId,
brokerName = pirRunState.broker.name,
email = pirRunState.extractedProfile.email,
email = email,
attemptId = pirRunState.attemptId,
)
pixelSender.reportStagePendingEmailConfirmation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class BrokerStepCompletedEventHandler @Inject constructor(
lastActionId = currentBrokerStep.step.actions[state.currentActionIndex].id,
durationMs = currentTimeProvider.currentTimeMillis() - state.stageStatus.stageStartMs,
currentActionAttemptCount = state.actionRetryCount + 1,
generatedEmail = state.generatedEmailData?.emailAddress,
),
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,32 @@ class RealPirRunStateHandlerTest {
)
}

@Test
fun whenHandleBrokerRecordEmailConfirmationNeededAndExtractedProfileEmailEmptyThenUsesGeneratedEmail() =
runTest {
val state =
BrokerRecordEmailConfirmationNeeded(
broker = testBroker,
extractedProfile = testExtractedProfile.copy(email = ""),
attemptId = "c9982ded-021a-4251-9e03-2c58b130410f",
lastActionId = "hello82ded-021a-4251-9e03-2c58b130410f",
durationMs = testTotalTimeMillis,
currentActionAttemptCount = 1,
generatedEmail = "generated@duck.com",
)
whenever(mockRepository.getBrokerForName(testBrokerName)).thenReturn(testBroker)

testee.handleState(state)

verify(mockJobRecordUpdater).markOptOutAsWaitingForEmailConfirmation(
profileQueryId = testProfileQueryId,
extractedProfileId = testExtractedProfileId,
brokerName = testBrokerName,
email = "generated@duck.com",
attemptId = "c9982ded-021a-4251-9e03-2c58b130410f",
)
}

@Test
fun whenHandleBrokerRecordEmailConfirmationStartedThenUpdateEmailJobRecordAndEmitPixel() =
runTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,45 @@ class BrokerStepCompletedEventHandlerTest {
assertEquals("action-3", capturedState.firstValue.lastActionId)
assertEquals(testCurrentTimeInMillis - testStageStartMs, capturedState.firstValue.durationMs)
assertEquals(2, capturedState.firstValue.currentActionAttemptCount)
assertNull(capturedState.firstValue.generatedEmail)
}

@Test
fun whenNeedsEmailConfirmationTrueAndGeneratedEmailDataPresentThenForwardsGeneratedEmail() = runTest {
val optOutStep = OptOutStep(
broker = testBroker,
step = OptOutStepActions(
stepType = "optout",
actions = listOf(testAction1, testAction2, testAction3),
optOutType = "form",
),
profileToOptOut = testExtractedProfile,
)
val state = State(
runType = RunType.OPTOUT,
brokerStepsToExecute = listOf(optOutStep),
profileQuery = testProfileQuery,
currentBrokerStepIndex = 0,
currentActionIndex = 2,
actionRetryCount = 1,
attemptId = "attempt-123",
brokerStepStartTime = testBrokerStartTime,
generatedEmailData = testGeneratedEmailData,
stageStatus = PirStageStatus(
currentStage = PirStage.FILL_FORM,
stageStartMs = testStageStartMs,
),
)
val event = BrokerStepCompleted(
needsEmailConfirmation = true,
stepStatus = StepStatus.Success,
)

testee.invoke(state, event)

val capturedState = argumentCaptor<BrokerRecordEmailConfirmationNeeded>()
verify(mockPirRunStateHandler).handleState(capturedState.capture())
assertEquals("generated@example.com", capturedState.firstValue.generatedEmail)
}

@Test
Expand Down
Loading