-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
APIS-7339: Gatekeeper User Can Set Deleted Restricted (#538)
* APIS-7339: Gatekeeper User Can Set Deleted Restricted * APIS-7339: Gatekeeper User Can Set Deleted Restricted -Update events * APIS-7339: Gatekeeper User Can Set Deleted Restricted -Update events - bump api-platform-application-domain and api-platform-application-events versions
- Loading branch information
1 parent
b7c3e3b
commit 09e56b0
Showing
12 changed files
with
363 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...thirdpartyapplication/services/commands/delete/AllowApplicationDeleteCommandHandler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2023 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 uk.gov.hmrc.thirdpartyapplication.services.commands.delete | ||
|
||
import javax.inject.{Inject, Singleton} | ||
import scala.concurrent.ExecutionContext | ||
|
||
import cats._ | ||
import cats.data._ | ||
import cats.implicits._ | ||
|
||
import uk.gov.hmrc.apiplatform.modules.common.domain.models.Actors | ||
import uk.gov.hmrc.apiplatform.modules.applications.core.domain.models.DeleteRestriction.NoRestriction | ||
import uk.gov.hmrc.apiplatform.modules.applications.core.domain.models.DeleteRestrictionType.DO_NOT_DELETE | ||
import uk.gov.hmrc.apiplatform.modules.commands.applications.domain.models.ApplicationCommands.AllowApplicationDelete | ||
import uk.gov.hmrc.apiplatform.modules.commands.applications.domain.models.CommandFailures | ||
import uk.gov.hmrc.apiplatform.modules.events.applications.domain.models._ | ||
import uk.gov.hmrc.thirdpartyapplication.models.db.StoredApplication | ||
import uk.gov.hmrc.thirdpartyapplication.repository._ | ||
import uk.gov.hmrc.thirdpartyapplication.services.commands.CommandHandler | ||
|
||
@Singleton | ||
class AllowApplicationDeleteCommandHandler @Inject() ( | ||
applicationRepository: ApplicationRepository | ||
)(implicit val ec: ExecutionContext | ||
) extends CommandHandler { | ||
|
||
import CommandHandler._ | ||
|
||
private def validate(app: StoredApplication): Validated[Failures, Unit] = { | ||
Apply[Validated[Failures, *]].map( | ||
cond((app.deleteRestriction.deleteRestrictionType == DO_NOT_DELETE), CommandFailures.GenericFailure(s"Delete is already allowed")) | ||
) { case _ => () } | ||
} | ||
|
||
private def asEvents(app: StoredApplication, cmd: AllowApplicationDelete): NonEmptyList[ApplicationEvent] = { | ||
NonEmptyList.of( | ||
ApplicationEvents.AllowApplicationDelete( | ||
id = EventId.random, | ||
applicationId = app.id, | ||
reasons = cmd.reasons, | ||
eventDateTime = cmd.timestamp, | ||
actor = Actors.GatekeeperUser(cmd.gatekeeperUser) | ||
) | ||
) | ||
} | ||
|
||
def process(app: StoredApplication, cmd: AllowApplicationDelete): AppCmdResultT = { | ||
|
||
for { | ||
valid <- E.fromEither(validate(app).toEither) | ||
savedApp <- E.liftF(applicationRepository.updateDeleteRestriction(app.id, NoRestriction)) | ||
events = asEvents(app, cmd) | ||
} yield (savedApp, events) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...rdpartyapplication/services/commands/delete/RestrictApplicationDeleteCommandHandler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2023 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 uk.gov.hmrc.thirdpartyapplication.services.commands.delete | ||
|
||
import javax.inject.{Inject, Singleton} | ||
import scala.concurrent.ExecutionContext | ||
|
||
import cats._ | ||
import cats.data._ | ||
import cats.implicits._ | ||
|
||
import uk.gov.hmrc.apiplatform.modules.common.domain.models.Actors | ||
import uk.gov.hmrc.apiplatform.modules.applications.core.domain.models.DeleteRestriction.DoNotDelete | ||
import uk.gov.hmrc.apiplatform.modules.applications.core.domain.models.DeleteRestrictionType.NO_RESTRICTION | ||
import uk.gov.hmrc.apiplatform.modules.commands.applications.domain.models.ApplicationCommands.RestrictApplicationDelete | ||
import uk.gov.hmrc.apiplatform.modules.commands.applications.domain.models.CommandFailures | ||
import uk.gov.hmrc.apiplatform.modules.events.applications.domain.models._ | ||
import uk.gov.hmrc.thirdpartyapplication.models.db.StoredApplication | ||
import uk.gov.hmrc.thirdpartyapplication.repository._ | ||
import uk.gov.hmrc.thirdpartyapplication.services.commands.CommandHandler | ||
|
||
@Singleton | ||
class RestrictApplicationDeleteCommandHandler @Inject() ( | ||
applicationRepository: ApplicationRepository | ||
)(implicit val ec: ExecutionContext | ||
) extends CommandHandler { | ||
|
||
import CommandHandler._ | ||
|
||
private def validate(app: StoredApplication): Validated[Failures, Unit] = { | ||
Apply[Validated[Failures, *]].map( | ||
cond((app.deleteRestriction.deleteRestrictionType == NO_RESTRICTION), CommandFailures.GenericFailure(s"Delete is already restricted")) | ||
) { case _ => () } | ||
} | ||
|
||
private def asEvents(app: StoredApplication, cmd: RestrictApplicationDelete): NonEmptyList[ApplicationEvent] = { | ||
NonEmptyList.of( | ||
ApplicationEvents.RestrictApplicationDelete( | ||
id = EventId.random, | ||
applicationId = app.id, | ||
reasons = cmd.reasons, | ||
eventDateTime = cmd.timestamp, | ||
actor = Actors.GatekeeperUser(cmd.gatekeeperUser) | ||
) | ||
) | ||
} | ||
|
||
def process(app: StoredApplication, cmd: RestrictApplicationDelete): AppCmdResultT = { | ||
|
||
for { | ||
valid <- E.fromEither(validate(app).toEither) | ||
savedApp <- E.liftF(applicationRepository.updateDeleteRestriction(app.id, DoNotDelete(cmd.reasons, Actors.GatekeeperUser(cmd.gatekeeperUser), cmd.timestamp))) | ||
events = asEvents(app, cmd) | ||
} yield (savedApp, events) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...dpartyapplication/services/commands/delete/AllowApplicationDeleteCommandHandlerSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2023 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 uk.gov.hmrc.thirdpartyapplication.services.commands.delete | ||
|
||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
import uk.gov.hmrc.http.HeaderCarrier | ||
|
||
import uk.gov.hmrc.apiplatform.modules.common.domain.models.Actors | ||
import uk.gov.hmrc.apiplatform.modules.common.utils.FixedClock | ||
import uk.gov.hmrc.apiplatform.modules.applications.core.domain.models.DeleteRestriction.{DoNotDelete, NoRestriction} | ||
import uk.gov.hmrc.apiplatform.modules.commands.applications.domain.models.ApplicationCommands | ||
import uk.gov.hmrc.apiplatform.modules.events.applications.domain.models._ | ||
import uk.gov.hmrc.thirdpartyapplication.mocks.repository.ApplicationRepositoryMockModule | ||
import uk.gov.hmrc.thirdpartyapplication.services.commands.{CommandHandler, CommandHandlerBaseSpec} | ||
|
||
class AllowApplicationDeleteCommandHandlerSpec extends CommandHandlerBaseSpec { | ||
|
||
val reasons = "Some reasons" | ||
|
||
trait Setup extends ApplicationRepositoryMockModule { | ||
|
||
implicit val hc: HeaderCarrier = HeaderCarrier() | ||
|
||
val appWithDeleteAllowed = storedApp.inSandbox() | ||
val timestamp = FixedClock.instant | ||
val deleteRestrictionDoNotDelete = DoNotDelete(reasons, Actors.GatekeeperUser(gatekeeperUser), timestamp) | ||
val appWithDeleteBlocked = appWithDeleteAllowed.copy(deleteRestriction = deleteRestrictionDoNotDelete) | ||
|
||
val underTest = new AllowApplicationDeleteCommandHandler(ApplicationRepoMock.aMock) | ||
|
||
def checkSuccessResult(expectedActor: Actors.GatekeeperUser)(fn: => CommandHandler.AppCmdResultT) = { | ||
val testMe = await(fn.value).value | ||
|
||
inside(testMe) { case (app, events) => | ||
events should have size 1 | ||
val event = events.head | ||
|
||
inside(event) { | ||
case ApplicationEvents.AllowApplicationDelete(_, appId, eventDateTime, anActor, reason) => | ||
appId shouldBe app.id | ||
anActor shouldBe expectedActor | ||
eventDateTime shouldBe timestamp | ||
reason shouldBe reasons | ||
} | ||
} | ||
} | ||
} | ||
|
||
"AllowApplicationDelete" should { | ||
val cmd = ApplicationCommands.AllowApplicationDelete(gatekeeperUser, reasons, instant) | ||
|
||
"create correct event for a valid request with app" in new Setup { | ||
ApplicationRepoMock.UpdateDeleteRestriction.thenReturnWhen(NoRestriction)(appWithDeleteAllowed) | ||
|
||
checkSuccessResult(Actors.GatekeeperUser(gatekeeperUser)) { | ||
underTest.process(appWithDeleteBlocked, cmd) | ||
} | ||
} | ||
|
||
"return an error if delete is already allowed" in new Setup { | ||
|
||
checkFailsWith("Delete is already allowed") { | ||
underTest.process(appWithDeleteAllowed, cmd) | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.