diff --git a/app/uk/gov/hmrc/agentclientrelationships/repository/RelationshipCopyRecordRepository.scala b/app/uk/gov/hmrc/agentclientrelationships/repository/RelationshipCopyRecordRepository.scala index f99ac977..1f7b6bb7 100644 --- a/app/uk/gov/hmrc/agentclientrelationships/repository/RelationshipCopyRecordRepository.scala +++ b/app/uk/gov/hmrc/agentclientrelationships/repository/RelationshipCopyRecordRepository.scala @@ -43,7 +43,13 @@ object SyncStatus extends Enumeration { import uk.gov.hmrc.agentclientrelationships.repository.SyncStatus._ -case class RelationshipCopyRecord(arn: String, clientIdentifier: String, clientIdentifierType: String, references: Option[Set[SaAgentReference]] = None, syncToETMPStatus: Option[SyncStatus] = None, syncToGGStatus: Option[SyncStatus] = None, dateTime: DateTime = DateTime.now()) +case class RelationshipCopyRecord(arn: String, + clientIdentifier: String, + clientIdentifierType: String, + references: Option[Set[SaAgentReference]] = None, + dateTime: DateTime = DateTime.now(), + syncToETMPStatus: Option[SyncStatus] = None, + syncToGGStatus: Option[SyncStatus] = None) object RelationshipCopyRecord extends ReactiveMongoFormats { implicit val formats: Format[RelationshipCopyRecord] = format[RelationshipCopyRecord] diff --git a/app/uk/gov/hmrc/agentclientrelationships/services/RelationshipsService.scala b/app/uk/gov/hmrc/agentclientrelationships/services/RelationshipsService.scala index ff020ab9..09bdb3fa 100644 --- a/app/uk/gov/hmrc/agentclientrelationships/services/RelationshipsService.scala +++ b/app/uk/gov/hmrc/agentclientrelationships/services/RelationshipsService.scala @@ -71,36 +71,29 @@ class RelationshipsService @Inject()(gg: GovernmentGatewayProxyConnector, agentCode: Future[AgentCode], references: Set[SaAgentReference])(implicit hc: HeaderCarrier): Future[Unit] = { - case class Identifiers(mtdItId: Future[MtdItId], nino: Future[Nino]) { - - def secondIdentifierFor(first: TaxIdentifier): Future[TaxIdentifier] = first match { - case MtdItId(_) => nino - case Nino(_) => mtdItId - } + val identifierData: Future[(String, String)] = taxIdentifier match { + case MtdItId(m) => Future.successful(m -> "MTDITID") + case Nino(n) => Future.successful(n -> "NINO") + case _ => Future.failed(new Exception("Invalid tax identifier found.")) } - def identifiers: Identifiers = taxIdentifier match { - case m@MtdItId(_) => Identifiers(Future.successful(m), des.getNinoFor(m)) - case n@Nino(_) => Identifiers(des.getMtdIdFor(n), Future.successful(n)) - case _ => throw new Exception("Invalid tax identifier found.") + val mtdItId: Future[MtdItId] = identifierData.flatMap { case (identifier, identifierType) => + if (identifierType == "MTDITID") + Future.successful(MtdItId(identifier)) + else + des.getMtdIdFor(Nino(identifier)) } - def typeOf(taxIdentifier: TaxIdentifier) = taxIdentifier match { - case MtdItId(_) => "MTDITID" - case Nino(_) => "NINO" + def createRelationshipRecord: Future[Unit] = (for { + (identifier, identifierType) <- identifierData + record = RelationshipCopyRecord(arn.value, identifier, identifierType, Some(references)) + _ <- repository.create(record) + } yield ()).recoverWith { + case ex => + Logger.warn(s"Inserting relationship record into mongo failed", ex) + Future.failed(ex) } - def createDatabaseRecord(syncToETMPStatus: Option[SyncStatus], syncToGGStatus: Option[SyncStatus]) - (taxIdentifier: TaxIdentifier): Future[Unit] = - repository.create( - RelationshipCopyRecord(arn.value, taxIdentifier.value, typeOf(taxIdentifier), Some(references), syncToETMPStatus, syncToGGStatus) - ) - .recoverWith { - case ex => - Logger.warn(s"Inserting relationship record into mongo failed", ex) - Future.failed(ex) - } - val updateEtmpSyncStatus = repository.updateEtmpSyncStatus(arn, taxIdentifier, _: SyncStatus) val updateGgSyncStatus = repository.updateGgSyncStatus(arn, taxIdentifier, _: SyncStatus) @@ -131,30 +124,12 @@ class RelationshipsService @Inject()(gg: GovernmentGatewayProxyConnector, updateGgSyncStatus(SyncStatus.Failed) } - val createRecordForMainIdentifier: Future[MtdItId] = for { - mtdItId <- identifiers.mtdItId - _ <- createDatabaseRecord(None, None)(taxIdentifier) - } yield mtdItId - - createRecordForMainIdentifier.flatMap { mtdItId => - - val allocateAgent: Future[Unit] = for { - _ <- createEtmpRecord(mtdItId) - _ <- createGgRecord(mtdItId) - } yield () - - def createRecordForSecondIdentifier = - repository.findBy(arn, taxIdentifier).flatMap { - case None => identifiers.secondIdentifierFor(taxIdentifier) - .flatMap(createDatabaseRecord(None, None)) - case Some(record) => identifiers.secondIdentifierFor(taxIdentifier) - .flatMap(createDatabaseRecord(record.syncToETMPStatus, record.syncToGGStatus)) - } - - allocateAgent - .flatMap(_ => createRecordForSecondIdentifier) - .recoverWith { case _ => createRecordForSecondIdentifier } - } + for { + mtdItId <- mtdItId + _ <- createRelationshipRecord + _ <- createEtmpRecord(mtdItId) + _ <- createGgRecord(mtdItId) + } yield () } private def getNinoFor(identifier: TaxIdentifier) diff --git a/it/uk/gov/hmrc/agentrelationships/controllers/RelationshipISpec.scala b/it/uk/gov/hmrc/agentrelationships/controllers/RelationshipISpec.scala index a7e5b5c7..49f398dc 100644 --- a/it/uk/gov/hmrc/agentrelationships/controllers/RelationshipISpec.scala +++ b/it/uk/gov/hmrc/agentrelationships/controllers/RelationshipISpec.scala @@ -89,9 +89,6 @@ class RelationshipISpec extends UnitSpec val identifier: String = if (isMtdItId) mtditid else nino val identifierType: String = if (isMtdItId) "MTDITID" else "NINO" - val identifier2: String = if (isMtdItId) nino else mtditid - val identifierType2: String = if (isMtdItId) "NINO" else "MTDITID" - //HAPPY PATH :-) "return 200 when relationship exists in gg" in { @@ -159,10 +156,8 @@ class RelationshipISpec extends UnitSpec givenAgentCanBeAllocatedInGovernmentGateway(mtditid, "bar") def query = repo.find("arn" -> arn, "clientIdentifier" -> identifier, "clientIdentifierType" -> identifierType) - def query2 = repo.find("arn" -> arn, "clientIdentifier" -> identifier2, "clientIdentifierType" -> identifierType2) await(query) shouldBe empty - await(query2) shouldBe empty val result = await(doRequest) result.status shouldBe 200 @@ -175,15 +170,6 @@ class RelationshipISpec extends UnitSpec 'syncToETMPStatus (Some(SyncStatus.Success)), 'syncToGGStatus (Some(SyncStatus.Success)) ) - - await(query2).head should have( - 'arn (arn), - 'clientIdentifier (identifier2), - 'clientIdentifierType (identifierType2), - 'references (Some(Set(SaAgentReference("foo")))), - 'syncToETMPStatus (Some(SyncStatus.Success)), - 'syncToGGStatus (Some(SyncStatus.Success)) - ) } "return 200 when agent credentials unknown but relationship exists in cesa" in { @@ -252,10 +238,8 @@ class RelationshipISpec extends UnitSpec givenAgentCanBeAllocatedInGovernmentGateway(mtditid, "bar") def query = repo.find("arn" -> arn, "clientIdentifier" -> identifier, "clientIdentifierType" -> identifierType) - def query2 = repo.find("arn" -> arn, "clientIdentifier" -> identifier2, "clientIdentifierType" -> identifierType2) await(query) shouldBe empty - await(query2) shouldBe empty val result = await(doRequest) result.status shouldBe 200 @@ -268,14 +252,6 @@ class RelationshipISpec extends UnitSpec 'syncToETMPStatus (Some(SyncStatus.Failed)), 'syncToGGStatus (None) ) - await(query2).head should have( - 'arn (arn), - 'clientIdentifier (identifier2), - 'clientIdentifierType (identifierType2), - 'references (Some(Set(SaAgentReference("foo")))), - 'syncToETMPStatus (Some(SyncStatus.Failed)), - 'syncToGGStatus (None) - ) } "return 200 when relationship exists only in cesa and relationship copy attempt fails because of gg" in {