Use JPA repo for local pincode resolution
All checks were successful
build-and-deploy / build-deploy (push) Successful in 40s

This commit is contained in:
androidlover5842
2026-02-05 19:56:59 +05:30
parent 1153193723
commit a0e354b464
4 changed files with 124 additions and 78 deletions

View File

@@ -1,76 +0,0 @@
package com.android.trisolarisserver.component.geo
import org.slf4j.LoggerFactory
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Component
@Component
class LocalPincodeDirectoryClient(
private val jdbcTemplate: JdbcTemplate
) {
private val logger = LoggerFactory.getLogger(LocalPincodeDirectoryClient::class.java)
fun resolve(pinCode: String): PincodeLookupResult {
val normalizedPin = pinCode.trim()
if (!PIN_REGEX.matches(normalizedPin)) {
return PincodeLookupResult(
resolvedCityState = null,
rawResponse = null,
status = "INVALID_PIN",
source = "local-db",
errorMessage = "PIN must be 6 digits"
)
}
return try {
val rows = jdbcTemplate.query(
"""
select city, state
from india_pincode_city_state
where pincode = ?
and nullif(trim(city), '') is not null
and nullif(trim(state), '') is not null
group by city, state
order by count(*) desc, city asc, state asc
limit 1
""".trimIndent(),
{ rs, _ ->
val city = rs.getString("city")?.trim()?.ifBlank { null }
val state = rs.getString("state")?.trim()?.ifBlank { null }
if (city == null || state == null) null else "$city, $state"
},
normalizedPin.toInt()
).filterNotNull()
val resolved = rows.firstOrNull()
if (resolved != null) {
PincodeLookupResult(
resolvedCityState = resolved,
rawResponse = null,
status = "OK",
source = "local-db"
)
} else {
PincodeLookupResult(
resolvedCityState = null,
rawResponse = null,
status = "ZERO_RESULTS",
source = "local-db"
)
}
} catch (ex: Exception) {
logger.warn("Local pincode lookup failed: {}", ex.message)
PincodeLookupResult(
resolvedCityState = null,
rawResponse = null,
status = "ERROR",
source = "local-db",
errorMessage = ex.message
)
}
}
companion object {
private val PIN_REGEX = Regex("\\d{6}")
}
}

View File

@@ -1,16 +1,21 @@
package com.android.trisolarisserver.component.geo
import com.android.trisolarisserver.repo.property.IndiaPincodeCityStateRepo
import org.slf4j.LoggerFactory
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Component
@Component
class PincodeResolver(
private val localPincodeDirectoryClient: LocalPincodeDirectoryClient,
private val indiaPincodeCityStateRepo: IndiaPincodeCityStateRepo,
private val dataGovPincodeClient: DataGovPincodeClient,
private val postalPincodeClient: PostalPincodeClient,
private val googleGeocodingClient: GoogleGeocodingClient
) {
private val logger = LoggerFactory.getLogger(PincodeResolver::class.java)
fun resolve(pinCode: String): PincodeResolveResult {
val primary = localPincodeDirectoryClient.resolve(pinCode)
val primary = resolveFromLocalDb(pinCode)
if (primary.status == "OK" && primary.resolvedCityState != null) {
return PincodeResolveResult(primary, null, null, null)
}
@@ -34,6 +39,53 @@ class PincodeResolver(
)
return PincodeResolveResult(primary, secondary, tertiary, quaternary)
}
private fun resolveFromLocalDb(pinCode: String): PincodeLookupResult {
val normalizedPin = pinCode.trim()
if (!PIN_REGEX.matches(normalizedPin)) {
return PincodeLookupResult(
resolvedCityState = null,
rawResponse = null,
status = "INVALID_PIN",
source = "local-db",
errorMessage = "PIN must be 6 digits"
)
}
return try {
val candidate = indiaPincodeCityStateRepo
.findCityStateCandidates(normalizedPin.toInt(), PageRequest.of(0, 1))
.firstOrNull()
if (candidate != null) {
PincodeLookupResult(
resolvedCityState = "${candidate.city}, ${candidate.state}",
rawResponse = null,
status = "OK",
source = "local-db"
)
} else {
PincodeLookupResult(
resolvedCityState = null,
rawResponse = null,
status = "ZERO_RESULTS",
source = "local-db"
)
}
} catch (ex: Exception) {
logger.warn("Local pincode lookup failed: {}", ex.message)
PincodeLookupResult(
resolvedCityState = null,
rawResponse = null,
status = "ERROR",
source = "local-db",
errorMessage = ex.message
)
}
}
companion object {
private val PIN_REGEX = Regex("\\d{6}")
}
}
data class PincodeResolveResult(