Skip to content

Commit

Permalink
SAR-4526: Refactored subscription service
Browse files Browse the repository at this point in the history
  • Loading branch information
r-melvin committed Sep 17, 2019
1 parent b011c5d commit 476a9f1
Show file tree
Hide file tree
Showing 16 changed files with 385 additions and 79 deletions.
24 changes: 11 additions & 13 deletions app/models/frontend/FERequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@
package models.frontend

import models.DateModel
import play.api.data.validation.ValidationError
import models.subscription.business.AccountingMethod
import play.api.libs.functional.syntax._
import play.api.libs.json._

case class FERequest
(
nino: String,
incomeSource: IncomeSourceType,
isAgent: Boolean = false,
arn: Option[String] = None,
accountingPeriodStart: Option[DateModel] = None,
accountingPeriodEnd: Option[DateModel] = None,
tradingName: Option[String] = None,
cashOrAccruals: Option[String] = None
)
case class FERequest(nino: String,
incomeSource: IncomeSourceType,
isAgent: Boolean = false,
arn: Option[String] = None,
accountingPeriodStart: Option[DateModel] = None,
accountingPeriodEnd: Option[DateModel] = None,
tradingName: Option[String] = None,
cashOrAccruals: Option[AccountingMethod] = None
)

object FERequest {

Expand All @@ -44,7 +42,7 @@ object FERequest {
(JsPath \ "accountingPeriodStart").readNullable[DateModel] and
(JsPath \ "accountingPeriodEnd").readNullable[DateModel] and
(JsPath \ "tradingName").readNullable[String] and
(JsPath \ "cashOrAccruals").readNullable[String]
(JsPath \ "cashOrAccruals").readNullable[AccountingMethod]
) (FERequest.apply _)

val writes: OWrites[FERequest] = Json.writes[FERequest]
Expand Down
2 changes: 1 addition & 1 deletion app/models/monitoring/rosmAndEnrol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object rosmAndEnrol {
"acccountingPeriodStartDate" -> fERequest.accountingPeriodStart.fold("-")(x => x.toDesDateFormat),
"acccountingPeriodEndDate" -> fERequest.accountingPeriodEnd.fold("-")(x => x.toDesDateFormat),
"tradingName" -> fERequest.tradingName.fold("-")(identity),
"cashOrAccruals" -> fERequest.cashOrAccruals.fold("-")(x => x.toLowerCase),
"cashOrAccruals" -> fERequest.cashOrAccruals.fold("-")(x => x.stringValue.toLowerCase),
"Authorization" -> urlHeaderAuthorization
)

Expand Down
45 changes: 18 additions & 27 deletions app/models/subscription/business/BusinessDetailsModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,37 @@ package models.subscription.business
import play.api.libs.json._


sealed trait CashOrAccruals {
def cashOrAccruals: String
sealed trait AccountingMethod {
val stringValue: String
}

case object Cash extends CashOrAccruals {
override val cashOrAccruals = "cash"
case object Cash extends AccountingMethod {
override val stringValue = "cash"
}

case object Accruals extends CashOrAccruals {
override val cashOrAccruals = "accruals"
case object Accruals extends AccountingMethod {
override val stringValue = "accruals"
}

object CashOrAccruals {
val feCash = "Cash"
val feAccruals = "Accruals"
object AccountingMethod {

private val reader: Reads[CashOrAccruals] = __.read[String].map {
case `feCash` | Cash.cashOrAccruals => Cash
case `feAccruals` | Accruals.cashOrAccruals => Accruals
private val reader: Reads[AccountingMethod] = __.read[String].map {
case Cash.stringValue => Cash
case Accruals.stringValue => Accruals
}

private val writer: Writes[CashOrAccruals] = Writes[CashOrAccruals](cashOrAccruals =>
JsString(cashOrAccruals.cashOrAccruals)
private val writer: Writes[AccountingMethod] = Writes[AccountingMethod](cashOrAccruals =>
JsString(cashOrAccruals.stringValue)
)

implicit val format: Format[CashOrAccruals] = Format(reader, writer)

implicit def convert(str: String): CashOrAccruals = str match {
case `feCash` | Cash.cashOrAccruals => Cash
case `feAccruals` | Accruals.cashOrAccruals => Accruals
}
implicit val format: Format[AccountingMethod] = Format(reader, writer)
}

case class BusinessDetailsModel
(
accountingPeriodStartDate: String,
accountingPeriodEndDate: String,
tradingName: String,
cashOrAccruals: CashOrAccruals
)
case class BusinessDetailsModel(accountingPeriodStartDate: String,
accountingPeriodEndDate: String,
tradingName: String,
cashOrAccruals: AccountingMethod
)

object BusinessDetailsModel {
implicit val format: Format[BusinessDetailsModel] = Json.format[BusinessDetailsModel]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,29 @@
package models.subscription.incomesource

import models.DateModel
import models.subscription.business.CashOrAccruals
import models.subscription.business.AccountingMethod
import play.api.libs.json.{Json, OFormat}

case class IncomeSource(nino: String,
arn: Option[String],
businessIncome: Option[BusinessIncomeModel],
propertyIncome: Option[PropertyIncomeModel]) {
case class SignUpRequest(nino: String,
arn: Option[String],
businessIncome: Option[BusinessIncomeModel],
propertyIncome: Option[PropertyIncomeModel]
) {

val isAgent: Boolean = arn.isDefined
}

case class BusinessIncomeModel(tradingName: Option[String],
accountingPeriod: AccountingPeriod,
accountingMethod: CashOrAccruals)
accountingMethod: AccountingMethod)

case class PropertyIncomeModel(cashOrAccruals: Option[CashOrAccruals]) //TODO change to non option when cash and accruals has been added to property
case class PropertyIncomeModel(accountingMethod: Option[AccountingMethod]) //TODO change to non option when cash and accruals has been added to property


case class AccountingPeriod(startDate: DateModel, endDate: DateModel)

object IncomeSource {
implicit val format: OFormat[IncomeSource] = Json.format[IncomeSource]
object SignUpRequest {
implicit val format: OFormat[SignUpRequest] = Json.format[SignUpRequest]
}

object BusinessIncomeModel {
Expand Down
54 changes: 54 additions & 0 deletions app/services/SubmissionOrchestrationService.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2019 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 services

import connectors.{BusinessConnector, PropertyConnector, RegistrationConnector}
import javax.inject.{Inject, Singleton}
import models.subscription.incomesource.SignUpRequest
import services.SubmissionOrchestrationService._
import uk.gov.hmrc.http.HeaderCarrier

import scala.concurrent.{ExecutionContext, Future}

@Singleton
class SubmissionOrchestrationService @Inject()(registrationConnector: RegistrationConnector,
businessConnector: BusinessConnector,
propertyConnector: PropertyConnector
)(implicit ec: ExecutionContext) {

def submit(signUpRequest: SignUpRequest)(implicit hc: HeaderCarrier): Future[SuccessfulSubmission.type] =
for {
_ <- registrationConnector.register(signUpRequest.nino, signUpRequest.isAgent)
_ <- signUpRequest.businessIncome match {
case Some(business) => businessConnector.businessSubscribe(signUpRequest.nino, business)
case None => Future.successful(NoSubmissionNeeded)
}
_ <- signUpRequest.propertyIncome match {
case Some(_) => propertyConnector.propertySubscribe(signUpRequest.nino)
case None => Future.successful(NoSubmissionNeeded)
}
} yield SuccessfulSubmission

}

object SubmissionOrchestrationService {

case object NoSubmissionNeeded

case object SuccessfulSubmission

}
10 changes: 5 additions & 5 deletions it/helpers/IntegrationTestConstants.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import models.digitalcontact.PaperlessPreferenceKey
import models.frontend.{Both, Business, FERequest, Property}
import models.lockout.LockoutRequest
import models.registration.RegistrationRequestModel
import models.subscription.business.{BusinessDetailsModel, BusinessSubscriptionRequestModel, Cash, CashOrAccruals}
import models.subscription.business.{BusinessDetailsModel, BusinessSubscriptionRequestModel, Cash}
import models.subscription.incomesource.{AccountingPeriod, BusinessIncomeModel, PropertyIncomeModel}
import models.{DateModel, ErrorModel}
import play.api.http.Status._
Expand Down Expand Up @@ -72,7 +72,7 @@ object IntegrationTestConstants {
)

val testPropertyIncomeModel = PropertyIncomeModel(
cashOrAccruals = Cash
accountingMethod = Cash
)

val lockoutRequest = LockoutRequest(
Expand All @@ -92,7 +92,7 @@ object IntegrationTestConstants {
accountingPeriodStart = DateModel("01", "05", "2017"),
accountingPeriodEnd = DateModel("30", "04", "2018"),
tradingName = "Test Business",
cashOrAccruals = "cash"
cashOrAccruals = Cash
)

val feBothRequest = FERequest(
Expand All @@ -102,15 +102,15 @@ object IntegrationTestConstants {
accountingPeriodStart = DateModel("01", "05", "2017"),
accountingPeriodEnd = DateModel("30", "04", "2018"),
tradingName = "Test Business",
cashOrAccruals = "cash"
cashOrAccruals = Cash
)

val businessSubscriptionRequestPayload = BusinessSubscriptionRequestModel(
List(BusinessDetailsModel(
accountingPeriodStartDate = "2017-05-01",
accountingPeriodEndDate = "2018-04-30",
tradingName = "Test Business",
cashOrAccruals = "cash"
cashOrAccruals = Cash
))
)

Expand Down
39 changes: 39 additions & 0 deletions test/connectors/mocks/subscription/MockBusinessConnector.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2019 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 connectors.mocks.subscription

import connectors.{BusinessConnector, BusinessIncomeSubscriptionSuccess}
import models.subscription.incomesource.BusinessIncomeModel
import org.mockito.ArgumentMatchers
import org.mockito.Mockito.when
import org.scalatest.mockito.MockitoSugar
import uk.gov.hmrc.http.HeaderCarrier

import scala.concurrent.Future

trait MockBusinessConnector extends MockitoSugar {

val mockBusinessConnector: BusinessConnector = mock[BusinessConnector]

def mockBusinessSubscribe(nino: String, businessIncomeModel: BusinessIncomeModel)
(response: Future[BusinessIncomeSubscriptionSuccess.type])
(implicit hc: HeaderCarrier): Unit = {
when(mockBusinessConnector.businessSubscribe(ArgumentMatchers.eq(nino), ArgumentMatchers.eq(businessIncomeModel))(ArgumentMatchers.any[HeaderCarrier]))
.thenReturn(response)
}

}
38 changes: 38 additions & 0 deletions test/connectors/mocks/subscription/MockPropertyConnector.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2019 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 connectors.mocks.subscription

import connectors.{PropertyConnector, PropertyIncomeSubscriptionSuccess}
import org.mockito.ArgumentMatchers
import org.mockito.Mockito.when
import org.scalatest.mockito.MockitoSugar
import uk.gov.hmrc.http.HeaderCarrier

import scala.concurrent.Future

trait MockPropertyConnector extends MockitoSugar {

val mockPropertyConnector: PropertyConnector = mock[PropertyConnector]

def mockPropertySubscribe(nino: String)
(response: Future[PropertyIncomeSubscriptionSuccess.type])
(implicit hc: HeaderCarrier): Unit = {
when(mockPropertyConnector.propertySubscribe(ArgumentMatchers.eq(nino))(ArgumentMatchers.any[HeaderCarrier]))
.thenReturn(response)
}

}
38 changes: 38 additions & 0 deletions test/connectors/mocks/subscription/MockRegistrationConnector.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2019 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 connectors.mocks.subscription

import connectors.RegistrationSuccess
import org.mockito.ArgumentMatchers
import org.mockito.Mockito.when
import org.scalatest.mockito.MockitoSugar
import uk.gov.hmrc.http.HeaderCarrier

import scala.concurrent.Future

trait MockRegistrationConnector extends MockitoSugar {

val mockRegistrationConnector: connectors.RegistrationConnector = mock[connectors.RegistrationConnector]

def mockRegister(nino: String, isAnAgent: Boolean)
(response: Future[RegistrationSuccess.type])
(implicit hc: HeaderCarrier): Unit = {
when(mockRegistrationConnector.register(ArgumentMatchers.eq(nino), ArgumentMatchers.eq(isAnAgent))(ArgumentMatchers.any[HeaderCarrier]))
.thenReturn(response)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import models.frontend.{Both, Business, IncomeSourceType, Property}
import play.api.libs.json.{JsString, JsValue, Json}
import uk.gov.hmrc.play.test.UnitSpec

class IncomeSourceSpec extends UnitSpec {
class SignUpRequestSpec extends UnitSpec {

"IncomeSourceType" should {

Expand Down
4 changes: 1 addition & 3 deletions test/models/frontend/FERequestSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

package models.frontend

import models.frontend
import models.frontend.{FERequest, _}
import play.api.libs.json.{JsError, JsValue, Json}
import play.api.libs.json.{JsValue, Json}
import uk.gov.hmrc.play.test.UnitSpec
import utils.JsonUtils._
import utils.TestConstants
Expand Down
Loading

0 comments on commit 476a9f1

Please sign in to comment.