Skip to content

Commit

Permalink
Merge pull request #22 from hmrc/PE-3061
Browse files Browse the repository at this point in the history
PE-3061: Add ISO 3166 Alpha-2-code country code to the address
  • Loading branch information
fenallen authored Jul 6, 2017
2 parents 56f0d6d + 3af0de5 commit 4bd5389
Show file tree
Hide file tree
Showing 14 changed files with 328 additions and 300 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ trait UserInfoGenerator {
"""221B Baker Street
|London
|NW1 9NT
|Great Britain""".stripMargin, Some("NW1 9NT"), Some("Great Britain")))
|Great Britain""".stripMargin, Some("NW1 9NT"), Some("Great Britain"), Some("GB")))
val enrolments = Seq(Enrolment("IR-SA", List(EnrolmentIdentifier("UTR", "174371121"))))
val government_gateway: GovernmentGatewayDetails = GovernmentGatewayDetails(Some("32131"),Some(Token("ggToken")),Some("User"),Some("affinityGroup"))

Expand Down
6 changes: 5 additions & 1 deletion app/uk/gov/hmrc/openidconnect/userinfo/domain/UserInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import org.joda.time.LocalDate

case class Address(formatted: String,
postal_code: Option[String],
country: Option[String])
country: Option[String],
code: Option[String])

case class UserInfo(given_name: Option[String],
family_name: Option[String],
Expand Down Expand Up @@ -55,3 +56,6 @@ case class DesAddress(line1: Option[String],
line4: Option[String],
postcode: Option[String],
countryCode: Option[Int])

case class Country(shortName: Option[String],
alphaTwoCode: Option[String])
7 changes: 7 additions & 0 deletions app/uk/gov/hmrc/openidconnect/userinfo/domain/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ package object domain {

implicit val desAddress = Json.format[DesAddress]

implicit val country: Reads[Country] = (
(__ \ "short_name").readNullable[String] and
(__ \ "alpha_two_code").readNullable[String]
)(Country.apply _)

implicit val countriesMap = Reads.mapReads[Country]

implicit val desUserInfo : Reads[DesUserInfo] = (
(JsPath \ "names" \ "1").read[DesUserName] and
(JsPath \ "dateOfBirth").readNullable[LocalDate] and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@

package uk.gov.hmrc.openidconnect.userinfo.services

import play.api.libs.json.Json
import uk.gov.hmrc.openidconnect.userinfo.domain.Country

import scala.io.Source

trait CountryService {

val countries: Map[String, String]
val countries: Map[String, Country]

def getCountry(countryCode: Int): Option[String] = countries.get(countryCode.toString)
def getCountry(countryCode: Int): Option[Country] = countries.get(countryCode.toString)
}

object CountryService extends CountryService {
override val countries = loadCountriesFromFile("/resources/country.properties")
override val countries = loadCountriesFromFile("/resources/country.json")

private def loadCountriesFromFile(file: String) = {
val is = getClass.getResourceAsStream(file)
try {
val lines = Source.fromInputStream(is).getLines().toSeq
lines.map(_.split("=")).map(t => (t(0), t(1))).toMap
Json.parse(Source.fromInputStream(is).mkString).as[Map[String, Country]]
} finally is.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package uk.gov.hmrc.openidconnect.userinfo.services

import org.joda.time.LocalDate
import uk.gov.hmrc.openidconnect.userinfo.domain.{Address, Authority, DesAddress, DesUserInfo, Enrolment, GovernmentGatewayDetails, UserDetails, UserInfo}
import uk.gov.hmrc.openidconnect.userinfo.domain.{Address, Authority, Country, DesAddress, DesUserInfo, Enrolment, GovernmentGatewayDetails, UserDetails, UserInfo}
import uk.gov.hmrc.play.http.Token

trait UserInfoTransformer {
Expand All @@ -30,7 +30,9 @@ trait UserInfoTransformer {

def address = if (scopes.contains("address")) {
val country = desUserInfo flatMap (u => u.address.countryCode flatMap countryService.getCountry)
desUserInfo map (u => Address(formattedAddress(u.address, country), u.address.postcode, country))
val countryName = country flatMap {c => c.shortName}
val countryCode = country flatMap {c => c.alphaTwoCode}
desUserInfo map (u => Address(formattedAddress(u.address, country), u.address.postcode, countryName, countryCode))
} else None

val identifier = if (scopes.contains("openid:gov-uk-identifiers")) authority flatMap {a => a.nino map {n => n}} else None
Expand All @@ -54,8 +56,10 @@ trait UserInfoTransformer {
ggInfo)
}

private def formattedAddress(desAddress: DesAddress, country: Option[String]) = {
Seq(desAddress.line1, desAddress.line2, desAddress.line3, desAddress.line4, desAddress.postcode, country).flatten.mkString("\n")
private def formattedAddress(desAddress: DesAddress, country: Option[Country]) = {
val countryName = country flatMap {c => c.shortName}
val countryCode = country flatMap {c => c.alphaTwoCode}
Seq(desAddress.line1, desAddress.line2, desAddress.line3, desAddress.line4, desAddress.postcode, countryName, countryCode).flatten.mkString("\n")
}

private def formatGGInfo(authority: Option[Authority], userDetails: Option[UserDetails], token: Option[Token]): Option[GovernmentGatewayDetails] = {
Expand Down
Loading

0 comments on commit 4bd5389

Please sign in to comment.