Skip to content

Commit

Permalink
Merge pull request #22 from hmrc/APB-884-second-record
Browse files Browse the repository at this point in the history
Revert "[AO,RV][APB-884] create record for second identifier to prevent duplicate allocate calls"
  • Loading branch information
arturopala authored Jun 23, 2017
2 parents 10b2073 + 0438599 commit 654e5f2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 654e5f2

Please sign in to comment.