[cart-checkout] Geocode addresses locally when lat/long is missing

Explains why the delivery fee specifically never changed while other
address info (label, street) updated fine after the previous fix:
AddAddressFormView only sets latLong when the CEP lookup happens to
return coordinates (AddAddressFormView.swift:141-146) — plenty of saved
addresses have none. Without coordinates the backend can't distinguish
that address from the previous one, so the fee (and, in Checkout,
checkoutAddressWatchKey itself) never actually changes, without any
error surfacing since it likely falls back to some default fee instead
of rejecting.

Added LocationService.geocodeAddress(street:number:neighborhood:city:
state:zip:), a thin CLGeocoder wrapper, and call it in both
CartView.refreshDeliveryFee and CheckoutView+Logic.
validateDeliveryAddressIfNeeded whenever coordinates are missing,
persisting the result back into appState.address so it doesn't need to
re-geocode on every subsequent check.
This commit is contained in:
Daniel Arantes Loverde
2026-07-10 10:55:28 -03:00
parent f706492823
commit 6b77775238
3 changed files with 75 additions and 2 deletions

View File

@@ -349,8 +349,29 @@ struct CartView: View {
SessionStateStore.saveAddress(appState.address)
}
let payloadLat = appState.address.latitude ?? selectedCustomerAddress?.latLong?.first
let payloadLng = appState.address.longitude ?? selectedCustomerAddress?.latLong?.dropFirst().first
var payloadLat = appState.address.latitude ?? selectedCustomerAddress?.latLong?.first
var payloadLng = appState.address.longitude ?? selectedCustomerAddress?.latLong?.dropFirst().first
// Some saved addresses have no lat/long (CEP lookup at creation
// time didn't return coordinates). Without coordinates the
// backend can't tell this address apart from any other, so the
// fee silently never changes. Geocode locally as a fallback.
if payloadLat == nil || payloadLng == nil {
if let coordinate = await LocationService.geocodeAddress(
street: selectedCustomerAddress?.address,
number: selectedCustomerAddress?.number,
neighborhood: selectedCustomerAddress?.neighborhood,
city: selectedCustomerAddress?.city,
state: selectedCustomerAddress?.state,
zip: selectedCustomerAddress?.zipCode
) {
payloadLat = coordinate.0
payloadLng = coordinate.1
appState.address.latitude = coordinate.0
appState.address.longitude = coordinate.1
SessionStateStore.saveAddress(appState.address)
}
}
let payload = ValidateDeliveryAddressPayload(
address: ValidateDeliveryAddressDataPayload(