From bcb6de98a098c9ef0e36562795a95141c157b84c Mon Sep 17 00:00:00 2001 From: Paolo Angioletti <85297193+pangiole-hmrc@users.noreply.github.com> Date: Mon, 26 Jul 2021 08:46:18 +0100 Subject: [PATCH] Introduce ITSA tax identifier --- src/main/scala/uk/gov/hmrc/domain/Itsa.scala | 44 +++++++++++++++++++ .../scala/uk/gov/hmrc/domain/taxIds.scala | 4 +- .../scala/uk/gov/hmrc/domain/ItsaSpec.scala | 30 +++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/main/scala/uk/gov/hmrc/domain/Itsa.scala create mode 100644 src/test/scala/uk/gov/hmrc/domain/ItsaSpec.scala diff --git a/src/main/scala/uk/gov/hmrc/domain/Itsa.scala b/src/main/scala/uk/gov/hmrc/domain/Itsa.scala new file mode 100644 index 0000000..90eb933 --- /dev/null +++ b/src/main/scala/uk/gov/hmrc/domain/Itsa.scala @@ -0,0 +1,44 @@ +/* + * Copyright 2021 HM Revenue & Customs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.hmrc.domain + +import play.api.libs.json.{Reads, Writes} + +case class Itsa(itsa: String) extends TaxIdentifier with SimpleName { + require(Itsa.isValid(itsa), s"$itsa is not a valid itsa.") + override def toString = itsa + + private val LengthWithoutSuffix: Int = 8 + + def value = itsa + + val name = "itsa" + + def formatted = value.grouped(2).mkString(" ") + + def withoutSuffix = value.take(LengthWithoutSuffix) +} + +object Itsa extends (String => Itsa) { + implicit val itsaWrite: Writes[Itsa] = new SimpleObjectWrites[Itsa](_.value) + implicit val itsaRead: Reads[Itsa] = new SimpleObjectReads[Itsa]("itsa", Itsa.apply) + + // TODO Review the regular expression pattern for ITSA + private val validItsaFormat = "^[a-zA-Z0-9_]*$" + + def isValid(itsa: String) = itsa != null && itsa.matches(validItsaFormat) +} diff --git a/src/main/scala/uk/gov/hmrc/domain/taxIds.scala b/src/main/scala/uk/gov/hmrc/domain/taxIds.scala index 44b5789..212ac91 100644 --- a/src/main/scala/uk/gov/hmrc/domain/taxIds.scala +++ b/src/main/scala/uk/gov/hmrc/domain/taxIds.scala @@ -41,6 +41,7 @@ case class TaxIds(values: Set[TaxIds.TaxIdWithName]) { lazy val org = as[Org] lazy val agentBusinessUtr = as[AgentBusinessUtr] lazy val psaId = as[PsaId] + lazy val itsa = as[Itsa] } object TaxIds { @@ -89,7 +90,8 @@ object TaxIds { SerialisableTaxId("agentbusinessutr", AgentBusinessUtr.apply), SerialisableTaxId("psaid", PsaId.apply), SerialisableTaxId("HMRC-OBTDS-ORG", HmrcObtdsOrg.apply), - SerialisableTaxId("HMRC-MTD-VAT", HmrcMtdVat.apply) + SerialisableTaxId("HMRC-MTD-VAT", HmrcMtdVat.apply), + SerialisableTaxId("itsa", Itsa.apply), ) } diff --git a/src/test/scala/uk/gov/hmrc/domain/ItsaSpec.scala b/src/test/scala/uk/gov/hmrc/domain/ItsaSpec.scala new file mode 100644 index 0000000..0cc593c --- /dev/null +++ b/src/test/scala/uk/gov/hmrc/domain/ItsaSpec.scala @@ -0,0 +1,30 @@ +/* + * Copyright 2021 HM Revenue & Customs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.hmrc.domain + +import org.scalatest.{Matchers, WordSpec} + +class ItsaSpec extends WordSpec with Matchers { + import Itsa.isValid + + "The validation of an itsa" should { + "pass with valid alphanumerics" in { + isValid("AB123456C") shouldBe true + } + // TODO add more test cases here ... + } +}