Skip to content

Commit

Permalink
ADR-1360: startedTime added to UserAnswers, explicit audit events rem…
Browse files Browse the repository at this point in the history
…oved from the service
  • Loading branch information
msatta committed Oct 3, 2024
1 parent a236465 commit 7466191
Show file tree
Hide file tree
Showing 13 changed files with 16 additions and 332 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ import play.api.http.HttpEntity
import play.api.libs.json._
import play.api.mvc._
import uk.gov.hmrc.alcoholdutyreturns.controllers.actions.AuthorisedAction
import uk.gov.hmrc.alcoholdutyreturns.models.audit.AuditReturnStarted
import uk.gov.hmrc.alcoholdutyreturns.models.{ErrorResponse, ObligationData, ReturnAndUserDetails, ReturnId, UserAnswers}
import uk.gov.hmrc.alcoholdutyreturns.models.{ErrorResponse, ReturnAndUserDetails, ReturnId, UserAnswers}
import uk.gov.hmrc.alcoholdutyreturns.repositories.{CacheRepository, UpdateFailure, UpdateSuccess}
import uk.gov.hmrc.alcoholdutyreturns.service.{AccountService, AuditService, LockingService}
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.alcoholdutyreturns.service.{AccountService, LockingService}
import uk.gov.hmrc.play.bootstrap.backend.controller.BackendController

import javax.inject.Inject
Expand All @@ -37,7 +35,6 @@ class CacheController @Inject() (
cacheRepository: CacheRepository,
lockingService: LockingService,
accountService: AccountService,
auditService: AuditService,
override val controllerComponents: ControllerComponents
)(implicit executionContext: ExecutionContext)
extends BackendController(controllerComponents)
Expand Down Expand Up @@ -99,7 +96,6 @@ class CacheController @Inject() (
val userAnswers =
UserAnswers.createUserAnswers(returnAndUserDetails, subscriptionSummary, obligationData)
cacheRepository.add(userAnswers).map { userAnswers =>
auditReturnStarted(userAnswers, obligationData)
Created(Json.toJson(userAnswers))
}
}
Expand All @@ -126,23 +122,6 @@ class CacheController @Inject() (
.map(_ => Ok("Lock refreshed"))
}

private def auditReturnStarted(userAnswers: UserAnswers, obligationData: ObligationData)(implicit
hc: HeaderCarrier
): Unit = {
val eventDetail = AuditReturnStarted(
appaId = userAnswers.returnId.appaId,
periodKey = userAnswers.returnId.periodKey,
credentialId = userAnswers.internalId,
groupId = userAnswers.groupId,
obligationData = obligationData,
alcoholRegimes = userAnswers.regimes.regimes,
returnStartedTime = userAnswers.lastUpdated,
returnValidUntilTime = userAnswers.validUntil
)

auditService.audit(eventDetail)
}

def error(errorResponse: ErrorResponse): Result = Result(
header = ResponseHeader(errorResponse.status),
body = HttpEntity.Strict(ByteString(Json.toBytes(Json.toJson(errorResponse))), Some("application/json"))
Expand Down
3 changes: 3 additions & 0 deletions app/uk/gov/hmrc/alcoholdutyreturns/models/UserAnswers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ case class UserAnswers(
internalId: String,
regimes: AlcoholRegimes,
data: JsObject = Json.obj(),
startedTime: Instant = Instant.now,
lastUpdated: Instant = Instant.now,
validUntil: Option[Instant] = None
)
Expand Down Expand Up @@ -79,6 +80,7 @@ object UserAnswers {
(__ \ "internalId").read[String] and
AlcoholRegimes.alcoholRegimesFormat and
(__ \ "data").read[JsObject] and
(__ \ "startedTime").read(MongoJavatimeFormats.instantFormat) and
(__ \ "lastUpdated").read(MongoJavatimeFormats.instantFormat) and
(__ \ "validUntil").readNullable(MongoJavatimeFormats.instantFormat)
)(UserAnswers.apply _)
Expand All @@ -90,6 +92,7 @@ object UserAnswers {
(__ \ "internalId").write[String] and
AlcoholRegimes.alcoholRegimesFormat and
(__ \ "data").write[JsObject] and
(__ \ "startedTime").write(MongoJavatimeFormats.instantFormat) and
(__ \ "lastUpdated").write(MongoJavatimeFormats.instantFormat) and
(__ \ "validUntil").writeNullable(MongoJavatimeFormats.instantFormat)
)(unlift(UserAnswers.unapply))
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

28 changes: 0 additions & 28 deletions app/uk/gov/hmrc/alcoholdutyreturns/models/audit/AuditType.scala

This file was deleted.

32 changes: 0 additions & 32 deletions app/uk/gov/hmrc/alcoholdutyreturns/service/AuditService.scala

This file was deleted.

32 changes: 2 additions & 30 deletions app/uk/gov/hmrc/alcoholdutyreturns/service/ReturnsService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import com.google.inject.{Inject, Singleton}
import play.api.Logging
import uk.gov.hmrc.alcoholdutyreturns.connector.{CalculatorConnector, ReturnsConnector}
import uk.gov.hmrc.alcoholdutyreturns.models.ErrorResponse.BadRequest
import uk.gov.hmrc.alcoholdutyreturns.models.audit.AuditReturnSubmitted
import uk.gov.hmrc.alcoholdutyreturns.models.calculation.CalculateDutyDueByTaxTypeRequest
import uk.gov.hmrc.alcoholdutyreturns.models.{ErrorResponse, ReturnId, UserAnswers}
import uk.gov.hmrc.alcoholdutyreturns.models.returns.{AdrReturnCreatedDetails, AdrReturnSubmission, ReturnCreate, ReturnCreatedDetails, TotalDutyDuebyTaxType}
import uk.gov.hmrc.alcoholdutyreturns.models.{ErrorResponse, ReturnId}
import uk.gov.hmrc.alcoholdutyreturns.models.returns.{AdrReturnSubmission, ReturnCreate, ReturnCreatedDetails, TotalDutyDuebyTaxType}
import uk.gov.hmrc.alcoholdutyreturns.repositories.CacheRepository
import uk.gov.hmrc.http.HeaderCarrier

Expand All @@ -36,7 +35,6 @@ class ReturnsService @Inject() (
returnsConnector: ReturnsConnector,
calculatorConnector: CalculatorConnector,
cacheRepository: CacheRepository,
auditService: AuditService,
schemaValidationService: SchemaValidationService
)(implicit
ec: ExecutionContext
Expand Down Expand Up @@ -81,37 +79,11 @@ class ReturnsService @Inject() (
None
}
)
_ = auditReturnSubmitted(
userAnswers,
returnToSubmit,
AdrReturnCreatedDetails.fromReturnCreatedDetails(returnCreatedDetails),
returnId
)
_ <- EitherT.right[ErrorResponse](
cacheRepository
.clearUserAnswersById(returnId)
)
} yield returnCreatedDetails
}

private def auditReturnSubmitted(
userAnswers: Option[UserAnswers],
returnToSubmit: ReturnCreate,
returnCreatedDetails: AdrReturnCreatedDetails,
returnId: ReturnId
)(implicit
hc: HeaderCarrier
): Unit = {
val eventDetail = AuditReturnSubmitted(
appaId = returnId.appaId,
periodKey = returnId.periodKey,
credentialId = userAnswers.map(_.internalId),
groupId = userAnswers.map(_.groupId),
returnSubmittedTime = returnCreatedDetails.processingDate,
alcoholRegimes = userAnswers.map(_.regimes.regimes),
requestPayload = returnToSubmit,
responsePayload = returnCreatedDetails
)
auditService.audit(eventDetail)
}
}
2 changes: 2 additions & 0 deletions test-utils/helpers/TestData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ trait TestData extends ModelGenerators {
groupId,
internalId,
allAlcoholRegimes,
startedTime = Instant.now(clock),
lastUpdated = Instant.now(clock)
)

Expand All @@ -61,6 +62,7 @@ trait TestData extends ModelGenerators {
internalId,
allAlcoholRegimes,
JsObject(Seq(ObligationData.toString -> Json.toJson(getObligationData(LocalDate.now(clock))))),
startedTime = Instant.now(clock),
lastUpdated = Instant.now(clock)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,21 @@ import play.api.mvc.Result
import uk.gov.hmrc.alcoholdutyreturns.base.SpecBase
import uk.gov.hmrc.alcoholdutyreturns.models.{ApprovalStatus, ErrorResponse}
import uk.gov.hmrc.alcoholdutyreturns.repositories.{CacheRepository, UpdateFailure, UpdateSuccess}
import uk.gov.hmrc.alcoholdutyreturns.service.{AccountService, AuditService, FakeLockingService, LockingService}
import uk.gov.hmrc.alcoholdutyreturns.service.{AccountService, FakeLockingService, LockingService}

import java.time.LocalDate
import scala.concurrent.Future

class CacheControllerSpec extends SpecBase {
val mockCacheRepository: CacheRepository = mock[CacheRepository]
val mockAccountService: AccountService = mock[AccountService]
val mockAuditService: AuditService = mock[AuditService]
val mockLockingService: LockingService = new FakeLockingService

val controller = new CacheController(
fakeAuthorisedAction,
mockCacheRepository,
mockLockingService,
mockAccountService,
mockAuditService,
cc
)

Expand Down Expand Up @@ -76,7 +74,6 @@ class CacheControllerSpec extends SpecBase {
mockCacheRepository,
mockLockingService,
mockAccountService,
mockAuditService,
cc
)

Expand Down Expand Up @@ -124,7 +121,6 @@ class CacheControllerSpec extends SpecBase {
mockCacheRepository,
lockingService,
mockAccountService,
mockAuditService,
cc
)

Expand Down Expand Up @@ -165,7 +161,6 @@ class CacheControllerSpec extends SpecBase {
mockCacheRepository,
mockLockingService,
mockAccountService,
mockAuditService,
cc
)

Expand Down Expand Up @@ -239,7 +234,6 @@ class CacheControllerSpec extends SpecBase {
mockCacheRepository,
mockLockingService,
mockAccountService,
mockAuditService,
cc
)

Expand All @@ -260,7 +254,6 @@ class CacheControllerSpec extends SpecBase {
mockCacheRepository,
mockLockingService,
mockAccountService,
mockAuditService,
cc
)

Expand Down
Loading

0 comments on commit 7466191

Please sign in to comment.