Skip to content

Commit

Permalink
Merge pull request #1918 from HedvigInsurance/fix/ssn-format
Browse files Browse the repository at this point in the history
String builders for name and ssn
  • Loading branch information
hugokallstrom authored Dec 11, 2023
2 parents 56a44a0 + 41c6227 commit 61c6b61
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.hedvig.android.core.common

fun formatSsn(ssn: String): String {
return if (ssn.length == 12) {
buildString {
append(ssn.substring(0..7))
append("-")
append(ssn.substring(8..11))
}
} else {
ssn
}
}

fun formatShortSsn(ssn: String): String {
val formattedSSN = ssn.replace("-", "")
if (formattedSSN.length == 10) {
val ssnLastTwoDigitsOfYear = formattedSSN.substring(0, 2)
val currentYear = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR)
val firstTwoDigitsOfTheYear = currentYear / 100
val lastTwoDigitsOfTheYear = currentYear % 100

val ssnLastTwoDigits = ssnLastTwoDigitsOfYear.toIntOrNull()

return if (ssnLastTwoDigits != null && ssnLastTwoDigits > lastTwoDigitsOfTheYear) {
"${firstTwoDigitsOfTheYear - 1}$ssn"
} else {
"$firstTwoDigitsOfTheYear$ssn"
}
} else {
return ssn
}
}

fun formatName(firstName: String?, lastName: String?): String {
return buildString {
if (firstName != null) {
append(firstName)
}
if (firstName != null && lastName != null) {
append(" ")
}
if (lastName != null) {
append(lastName)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hedvig.android.feature.editcoinsured.data

import com.hedvig.android.core.common.formatName
import com.hedvig.android.core.common.formatSsn
import java.time.format.DateTimeFormatter
import java.util.UUID
import kotlinx.datetime.LocalDate
Expand All @@ -15,18 +17,13 @@ internal data class CoInsured(
) {
val id = "$firstName-$lastName-$birthDate-$ssn"

val displayName: String = buildString {
if (firstName != null) {
append(firstName)
}
if (firstName != null && lastName != null) {
append(" ")
}
if (lastName != null) {
append(lastName)
val displayName: String = formatName(firstName, lastName)

fun identifier(dateTimeFormatter: DateTimeFormatter): String? {
return if (ssn != null) {
formatSsn(ssn)
} else {
birthDate?.toJavaLocalDate()?.format(dateTimeFormatter)
}
}

fun identifier(dateTimeFormatter: DateTimeFormatter): String? =
ssn ?: birthDate?.toJavaLocalDate()?.format(dateTimeFormatter)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package com.hedvig.android.feature.editcoinsured.data

import com.hedvig.android.core.common.formatName
import com.hedvig.android.core.common.formatSsn

internal data class Member(
val firstName: String,
val lastName: String,
val ssn: String?,
) {
val displayName: String = "$firstName $lastName"
val displayName: String = formatName(firstName, lastName)

fun identifier(): String? {
return if (ssn != null) {
formatSsn(ssn)
} else {
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal fun CoInsuredList(
uiState.member?.let {
InsuredRow(
displayName = it.displayName,
identifier = it.ssn ?: "",
identifier = it.identifier() ?: "",
hasMissingInfo = false,
allowEdit = false,
isMember = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package com.hedvig.android.feature.editcoinsured.ui
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.Snapshot
import arrow.core.raise.either
import com.hedvig.android.core.common.formatShortSsn
import com.hedvig.android.core.common.safeCast
import com.hedvig.android.core.uidata.UiMoney
import com.hedvig.android.feature.editcoinsured.data.CoInsured
Expand Down Expand Up @@ -57,7 +59,7 @@ internal class EditCoInsuredPresenter(

var errorMessage by remember { mutableStateOf<String?>(null) }
var isLoading by remember { mutableStateOf(true) }
var ssnQuery by remember { mutableStateOf<String?>(null) }
var fetchInfoFromSsn by remember { mutableIntStateOf(0) }
var intentId by remember { mutableStateOf<String?>(null) }
var selectedCoInsuredId by remember { mutableStateOf<String?>(null) }
var commit by remember { mutableStateOf(false) }
Expand Down Expand Up @@ -98,26 +100,29 @@ internal class EditCoInsuredPresenter(
if (selectedCoInsured != null) {
editedCoInsuredList = addCoInsured(selectedCoInsuredId, selectedCoInsured, listState)
} else if (addBottomSheetState.shouldFetchInfo()) {
ssnQuery = addBottomSheetState.ssn
fetchInfoFromSsn++
} else {
editedCoInsuredList = addCoInsuredFromBottomSheet(selectedCoInsuredId, addBottomSheetState, listState)
}
}

is EditCoInsuredEvent.OnCoInsuredSelected ->
addBottomSheetState =
addBottomSheetState.copy(
selectedCoInsured = event.coInsured,
errorMessage = null,
)
addBottomSheetState = addBottomSheetState.copy(
selectedCoInsured = event.coInsured,
errorMessage = null,
)

is RemoveCoInsured -> {
editedCoInsuredList = listState.coInsured.filterNot { it == event.coInsured }.toImmutableList()
}

is EditCoInsuredEvent.OnSsnChanged ->
addBottomSheetState =
addBottomSheetState.copy(ssn = event.ssn, errorMessage = null)
addBottomSheetState = addBottomSheetState.copy(
ssn = event.ssn,
errorMessage = null,
firstName = null,
lastName = null,
)

is EditCoInsuredEvent.OnBirthDateChanged ->
addBottomSheetState =
Expand All @@ -132,7 +137,6 @@ internal class EditCoInsuredPresenter(
addBottomSheetState.copy(lastName = event.lastName, errorMessage = null)

is EditCoInsuredEvent.OnManualInputSwitchChanged -> {
ssnQuery = null
addBottomSheetState =
Loaded.AddBottomSheetState(
showManualInput = event.show,
Expand Down Expand Up @@ -172,7 +176,6 @@ internal class EditCoInsuredPresenter(

ResetAddBottomSheetState -> {
addBottomSheetState = Loaded.AddBottomSheetState()
ssnQuery = null
}

ResetRemoveBottomSheetState -> removeBottomSheetState = Loaded.RemoveBottomSheetState()
Expand All @@ -181,15 +184,17 @@ internal class EditCoInsuredPresenter(
}
}

LaunchedEffect(ssnQuery) {
LaunchedEffect(fetchInfoFromSsn) {
addBottomSheetState = addBottomSheetState.copy(errorMessage = null)
ssnQuery?.let { ssnQuery ->
val ssn = addBottomSheetState.ssn
if (ssn != null) {
val paddedSsn = formatShortSsn(ssn)
either {
val result = fetchCoInsuredPersonalInformationUseCase.invoke(ssnQuery).bind()
val result = fetchCoInsuredPersonalInformationUseCase.invoke(paddedSsn).bind()
addBottomSheetState = addBottomSheetState.copy(
firstName = result.firstName,
lastName = result.lastName,
ssn = ssnQuery,
ssn = paddedSsn,
errorMessage = null,
)
}.onLeft {
Expand Down Expand Up @@ -232,7 +237,6 @@ internal class EditCoInsuredPresenter(
),
)
selectedCoInsuredId = null
ssnQuery = null
addBottomSheetState = Loaded.AddBottomSheetState(show = false)
removeBottomSheetState = Loaded.RemoveBottomSheetState(show = false)
editedCoInsuredList = null
Expand All @@ -243,7 +247,7 @@ internal class EditCoInsuredPresenter(
}

if (commit) {
LaunchedEffect(commit) {
LaunchedEffect(Unit) {
intentId?.let {
listState = listState.copy(isCommittingUpdate = true)
commitMidtermChangeUseCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.apollographql.apollo3.cache.normalized.fetchPolicy
import com.hedvig.android.apollo.safeExecute
import com.hedvig.android.apollo.toEither
import com.hedvig.android.core.common.ErrorMessage
import com.hedvig.android.core.common.formatName
import com.hedvig.android.core.common.formatSsn
import com.hedvig.android.data.productVariant.android.toProductVariant
import com.hedvig.android.hanalytics.featureflags.FeatureManager
import com.hedvig.android.hanalytics.featureflags.flags.Feature
Expand Down Expand Up @@ -37,7 +39,7 @@ internal class GetInsuranceContractsUseCaseImpl(
val isEditCoInsuredEnabled = featureManager.isFeatureEnabled(Feature.EDIT_COINSURED)

val contractHolderDisplayName = insuranceQueryData.getContractHolderDisplayName()
val contractHolderSSN = insuranceQueryData.currentMember.ssn
val contractHolderSSN = insuranceQueryData.currentMember.ssn?.let { formatSsn(it) }

val terminatedContracts = insuranceQueryData.currentMember.terminatedContracts.map {
it.toContract(
Expand All @@ -60,8 +62,7 @@ internal class GetInsuranceContractsUseCaseImpl(
}
}

private fun InsuranceContractsQuery.Data.getContractHolderDisplayName(): String =
"${currentMember.firstName} ${currentMember.lastName}"
private fun InsuranceContractsQuery.Data.getContractHolderDisplayName(): String = formatName(currentMember.firstName, currentMember.lastName)

private fun ContractFragment.toContract(
isTerminated: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hedvig.android.feature.insurances.data

import com.hedvig.android.core.common.formatName
import com.hedvig.android.core.common.formatSsn
import com.hedvig.android.data.productvariant.ProductVariant
import java.time.format.DateTimeFormatter
import kotlinx.collections.immutable.ImmutableList
Expand Down Expand Up @@ -45,21 +47,15 @@ data class InsuranceAgreement(
val terminatesOn: LocalDate?,
val hasMissingInfo: Boolean,
) {
fun getDisplayName() = buildString {
if (firstName != null) {
append(firstName)
}
if (firstName != null && lastName != null) {
append(" ")
}
if (lastName != null) {
append(lastName)
}
}
fun getDisplayName() = formatName(firstName, lastName)

fun getSsnOrBirthDate(dateTimeFormatter: DateTimeFormatter) = ssn ?: birthDate
?.toJavaLocalDate()
?.let { dateTimeFormatter.format(it) }
fun getSsnOrBirthDate(dateTimeFormatter: DateTimeFormatter) = if (ssn != null) {
formatSsn(ssn)
} else {
birthDate
?.toJavaLocalDate()
?.let { dateTimeFormatter.format(it) }
}
}

enum class CreationCause {
Expand Down

0 comments on commit 61c6b61

Please sign in to comment.