Skip to content

Commit

Permalink
[APB-884][AO,RV] Removed copying relationships in check nino endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rvijayan authored and arturopala committed Jun 23, 2017
1 parent 654e5f2 commit ffb2629
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,6 @@ class GovernmentGatewayProxyConnector @Inject()(@Named("government-gateway-proxy
})
}

def deallocateAgent(agentCode: AgentCode, mtdItId: MtdItId)(implicit hc: HeaderCarrier): Future[Unit] = {
monitor("ConsumedAPI-GGW-GsoAdminDeallocateAgent-POST") {
httpPost.POSTString(path("GsoAdminDeallocateAgent"), GsoAdminAllocateAgentXmlInput(mtdItId.value,"MTDITID",agentCode.value), Seq(CONTENT_TYPE -> XML))
}.map({ response =>
toXmlElement(response.body)
})
}

private def toXmlElement(xmlString: String): Elem = {
val factory = SAXParserFactory.newInstance("org.apache.xerces.jaxp.SAXParserFactoryImpl", this.getClass.getClassLoader)
factory.setFeature(SAX_FEATURE_PREFIX + EXTERNAL_GENERAL_ENTITIES_FEATURE, false)
Expand Down Expand Up @@ -152,16 +144,6 @@ class GovernmentGatewayProxyConnector @Inject()(@Named("government-gateway-proxy
</Identifiers>
<AgentCode>{agentCode}</AgentCode>
</GsoAdminAllocateAgentXmlInput>.toString()

private def GsoAdminDeallocateAgentXmlInput(identifier: String, identifierType: String, agentCode: String): String =
<GsoAdminDeallocateAgentXmlInput xmlns="urn:GSO-System-Services:external:1.65:GsoAdminDeallocateAgentXmlInput">
<ServiceName>HMRC-MTD-IT</ServiceName>
<Identifiers>
<Identifier IdentifierType={identifierType}>{identifier}</Identifier>
</Identifiers>
<AgentCode>{agentCode}</AgentCode>
</GsoAdminDeallocateAgentXmlInput>.toString()

}

case class RelationshipNotFound(errorCode: String) extends Exception
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import reactivemongo.api.indexes.IndexType.Ascending
import reactivemongo.bson.{BSONDocument, BSONObjectID}
import uk.gov.hmrc.agentclientrelationships.repository.RelationshipCopyRecord.formats
import uk.gov.hmrc.agentmtdidentifiers.model.{Arn, MtdItId}
import uk.gov.hmrc.domain.{Nino, SaAgentReference, TaxIdentifier}
import uk.gov.hmrc.domain.SaAgentReference
import uk.gov.hmrc.mongo.json.ReactiveMongoFormats
import uk.gov.hmrc.mongo.{AtomicUpdate, ReactiveRepository}

Expand Down Expand Up @@ -60,6 +60,8 @@ class RelationshipCopyRecordRepository @Inject()(mongoComponent: ReactiveMongoCo
ReactiveRepository[RelationshipCopyRecord, BSONObjectID]("relationship-copy-record",
mongoComponent.mongoConnector.db, formats, ReactiveMongoFormats.objectIdFormats) with AtomicUpdate[RelationshipCopyRecord] {

private val MtdItIdType = "MTDITID"

override def indexes = Seq(
Index(Seq("arn" -> Ascending, "clientIdentifier" -> Ascending, "clientIdentifierType" -> Ascending), Some("arnAndAgentReference"), unique = true)
)
Expand All @@ -70,38 +72,29 @@ class RelationshipCopyRecordRepository @Inject()(mongoComponent: ReactiveMongoCo
}
}

def findBy(arn: Arn, taxIdentifier: TaxIdentifier)(implicit ec: ExecutionContext): Future[Option[RelationshipCopyRecord]] = {
val (identifier, identifierType) = identifierData(taxIdentifier)
def findBy(arn: Arn, mtdItId: MtdItId)(implicit ec: ExecutionContext): Future[Option[RelationshipCopyRecord]] = {

find("arn" -> arn.value, "clientIdentifierType" -> identifierType, "clientIdentifier" -> identifier)
find("arn" -> arn.value, "clientIdentifier" -> mtdItId.value, "clientIdentifierType" -> MtdItIdType)
.map(_.headOption)
}

def updateEtmpSyncStatus(arn: Arn, taxIdentifier: TaxIdentifier, status: SyncStatus)(implicit ec: ExecutionContext): Future[Unit] = {
val (identifier, identifierType) = identifierData(taxIdentifier)
def updateEtmpSyncStatus(arn: Arn, mtdItId: MtdItId, status: SyncStatus)(implicit ec: ExecutionContext): Future[Unit] = {
atomicUpdate(
finder = BSONDocument("arn" -> arn.value, "clientIdentifier" -> identifier, "clientIdentifierType" -> identifierType),
finder = BSONDocument("arn" -> arn.value, "clientIdentifier" -> mtdItId.value, "clientIdentifierType" -> MtdItIdType),
modifierBson = BSONDocument("$set" -> BSONDocument("syncToETMPStatus" -> status.toString))
).map(_.foreach { update =>
update.writeResult.errMsg.foreach(error => Logger.warn(s"Updating ETMP sync status ($status) failed: $error"))
})
}

def updateGgSyncStatus(arn: Arn, taxIdentifier: TaxIdentifier, status: SyncStatus)(implicit ec: ExecutionContext): Future[Unit] = {
val (identifier, identifierType) = identifierData(taxIdentifier)
def updateGgSyncStatus(arn: Arn, mtdItId: MtdItId, status: SyncStatus)(implicit ec: ExecutionContext): Future[Unit] = {
atomicUpdate(
finder = BSONDocument("arn" -> arn.value, "clientIdentifier" -> identifier, "clientIdentifierType" -> identifierType),
finder = BSONDocument("arn" -> arn.value, "clientIdentifier" -> mtdItId.value, "clientIdentifierType" -> MtdItIdType),
modifierBson = BSONDocument("$set" -> BSONDocument("syncToGGStatus" -> status.toString))
).map(_.foreach { update =>
update.writeResult.errMsg.foreach(error => Logger.warn(s"Updating GG sync status ($status) failed: $error"))
})
}

private def identifierData(identifier: TaxIdentifier): (String, String) = identifier match {
case MtdItId(mtdItId) => (mtdItId, "MTDITID")
case Nino(nino) => (nino, "NINO")
case _ => ("-", "UNKNOWN")
}

override def isInsertion(newRecordId: BSONObjectID, oldRecord: RelationshipCopyRecord): Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,29 @@ class RelationshipsService @Inject()(gg: GovernmentGatewayProxyConnector,
mapping: MappingConnector,
repository: RelationshipCopyRecordRepository) {

private val MtdItIdType = "MTDITID"

def checkForOldRelationship(arn: Arn, identifier: TaxIdentifier, agentCode: Future[AgentCode])
(implicit hc: HeaderCarrier): Future[Boolean] = {
identifier match {
case mtdItId@MtdItId(_) => checkCesaForOldRelationshipAndCopy(arn, mtdItId, agentCode)
case nino@Nino(_) => checkCesaForOldRelationship(arn, nino).map(_.nonEmpty)
}
}

repository.findBy(arn, identifier).flatMap {
def checkCesaForOldRelationshipAndCopy(arn: Arn, mtdItId: MtdItId, agentCode: Future[AgentCode])
(implicit hc: HeaderCarrier): Future[Boolean] = {
repository.findBy(arn, mtdItId).flatMap {
case Some(_) =>
Logger.warn(s"Relationship has been already copied from CESA to MTD")
Future.failed(new Exception())
case None =>
checkCesaForOldRelationship(arn, identifier).flatMap { matchingReferences =>
if (matchingReferences.nonEmpty) {
copyRelationship(arn, identifier, agentCode, matchingReferences)
checkCesaForOldRelationship(arn, mtdItId).flatMap {
case references if references.nonEmpty =>
copyRelationship(arn, mtdItId, agentCode, references)
.map(_ => true)
.recover { case _ => true }
} else Future.successful(false)
case _ => Future.successful(false)
}
}
}
Expand All @@ -67,35 +76,21 @@ class RelationshipsService @Inject()(gg: GovernmentGatewayProxyConnector,
}

private def copyRelationship(arn: Arn,
taxIdentifier: TaxIdentifier,
agentCode: Future[AgentCode],
references: Set[SaAgentReference])(implicit hc: HeaderCarrier): Future[Unit] = {

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."))
}
mtdItId: MtdItId,
agentCode: Future[AgentCode],
references: Set[SaAgentReference])(implicit hc: HeaderCarrier): Future[Unit] = {

val mtdItId: Future[MtdItId] = identifierData.flatMap { case (identifier, identifierType) =>
if (identifierType == "MTDITID")
Future.successful(MtdItId(identifier))
else
des.getMtdIdFor(Nino(identifier))
}

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 createRelationshipRecord: Future[Unit] = {
val record = RelationshipCopyRecord(arn.value, mtdItId.value, MtdItIdType, Some(references))
repository.create(record).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)
val updateEtmpSyncStatus = repository.updateEtmpSyncStatus(arn, mtdItId, _: SyncStatus)
val updateGgSyncStatus = repository.updateGgSyncStatus(arn, mtdItId, _: SyncStatus)

def createEtmpRecord(mtdItId: MtdItId): Future[Unit] = (for {
_ <- updateEtmpSyncStatus(SyncStatus.InProgress)
Expand Down Expand Up @@ -125,7 +120,6 @@ class RelationshipsService @Inject()(gg: GovernmentGatewayProxyConnector,
}

for {
mtdItId <- mtdItId
_ <- createRelationshipRecord
_ <- createEtmpRecord(mtdItId)
_ <- createGgRecord(mtdItId)
Expand Down
Loading

0 comments on commit ffb2629

Please sign in to comment.