Skip to content

Commit

Permalink
TAV-170: Bug: Handle badly formed ids in requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ssaleem-ee committed Dec 17, 2020
1 parent 062c505 commit f7f8092
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions app/osgb/AddressLookupIdController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import uk.gov.hmrc.address.v2.AddressRecord
import uk.gov.hmrc.logging.SimpleLogger

import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try

class AddressLookupIdController @Inject()(addressSearch: AddressSearcher, responseProcessor: ResponseProcessor,
logger: SimpleLogger, ec: ExecutionContext, cc: ControllerComponents)
Expand All @@ -44,7 +45,7 @@ class AddressLookupIdController @Inject()(addressSearch: AddressSearcher, respon

private[osgb] def findByIdRequest[A](request: Request[A], id: String, marshall: AddressRecord => JsValue): Future[Result] = {
val origin = getOriginHeaderIfSatisfactory(request.headers)
addressSearch.findID(id).map {
Try(addressSearch.findID(id).map {
a =>
val list = a.toList
logEvent("LOOKUP", "origin" -> origin, "id" -> id, "matches" -> list.size.toString)
Expand All @@ -55,6 +56,6 @@ class AddressLookupIdController @Inject()(addressSearch: AddressSearcher, respon
else {
NotFound(s"id matched nothing")
}
}
}).getOrElse(Future.successful(BadRequest(s"Check the id supplied: $id")))
}
}
5 changes: 3 additions & 2 deletions app/repositories/AddressLookupRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import cats.effect.IO
import doobie.Transactor
import doobie.implicits._
import doobie.util.fragment

import javax.inject.Inject
import osgb.SearchParameters
import osgb.services.AddressSearcher
Expand All @@ -35,11 +36,11 @@ class AddressLookupRepository @Inject()(transactor: Transactor[IO]) extends Addr
import AddressLookupRepository._


override def findID(id: String): Future[Option[DbAddress]] = findUprn(id).map(_.headOption)
override def findID(id: String): Future[Option[DbAddress]] = findUprn(cleanUprn(id)).map(_.headOption)

override def findUprn(uprn: String): Future[List[DbAddress]] = {
val queryFragment = baseQuery ++
sql""" WHERE uprn = ${cleanUprn(uprn).toInt}"""
sql""" WHERE uprn = ${uprn.toInt}"""

queryFragment.query[SqlDbAddress].to[List].transact(transactor).unsafeToFuture().map(l => l.map(mapToDbAddress))
}
Expand Down

0 comments on commit f7f8092

Please sign in to comment.