diff --git a/obp-api/src/main/scala/code/api/v4_0_0/Http4s400.scala b/obp-api/src/main/scala/code/api/v4_0_0/Http4s400.scala index c2de505302..c29495677e 100644 --- a/obp-api/src/main/scala/code/api/v4_0_0/Http4s400.scala +++ b/obp-api/src/main/scala/code/api/v4_0_0/Http4s400.scala @@ -7680,9 +7680,13 @@ object Http4s400 { lazy val deleteCustomerCascade: Http4sRoute = Http4sRoute { case req @ DELETE -> `prefixPath` / "management" / "cascading" / "banks" / _ / "customers" / customerIdStr => - EndpointHelpers.withUserAndBank(req) { (_, _, cc) => + EndpointHelpers.withUserAndBank(req) { (_, bank, cc) => for { - (_, _) <- NewStyle.function.getCustomerByCustomerId(customerIdStr, Some(cc)) + (customer, _) <- NewStyle.function.getCustomerByCustomerId(customerIdStr, Some(cc)) + // The delete is addressed through a bank, so a customer of another bank is not found here. + _ <- code.util.Helper.booleanToFuture(CustomerNotFoundByCustomerId, 404, cc = Some(cc)) { + customer.bankId == bank.bankId.value + } _ <- Future(deletion.DeleteCustomerCascade.atomicDelete(CustomerId(customerIdStr))) } yield Full(true) } diff --git a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala index abbe9455c4..e42ab397f8 100644 --- a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala +++ b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala @@ -31,10 +31,13 @@ import code.accountapplication.MappedAccountApplication import code.api.APIFailureNewStyle import code.api.util.APIUtil.fullBoxOrException import code.api.util.ErrorMessages.CouldNotDeleteCascade -import code.customer.MappedCustomer +import code.CustomerDependants.MappedCustomerDependant +import code.customer.{MappedCustomer, MappedCustomerMessage} import code.customer.internalMapping.MappedCustomerIdMapping import code.customeraccountlinks.CustomerAccountLink import code.customeraddress.MappedCustomerAddress +import code.crm.MappedCrmEvent +import code.customerlinks.CustomerLink import code.customerattribute.MappedCustomerAttribute import code.kycchecks.MappedKycCheck import code.kycdocuments.MappedKycDocument @@ -50,6 +53,9 @@ import net.liftweb.mapper.By object DeleteCustomerCascade { def delete(customerId: CustomerId): Boolean = { + // The tasks run in list order. Rows that point at the customer go first and the customer row goes last, + // as the account cascade does with the account: some tasks read the customer row (its primary key, its + // bank and number) to find their rows, and a failure of the last task rolls back all the others. val doneTasks = deleteCustomerAttributes(customerId) :: deleteTaxResidence(customerId) :: @@ -58,11 +64,15 @@ object DeleteCustomerCascade { deleteKycCheck(customerId) :: deleteKycDocument(customerId) :: deleteCustomerAddress(customerId) :: + deleteCustomerMessages(customerId) :: + deleteCustomerDependants(customerId) :: + deleteCustomerLinks(customerId) :: + deleteCrmEvents(customerId) :: deleteCustomerIdMapping(customerId) :: deleteAccountApplication(customerId) :: deleteCustomerUserCustomerLinks(customerId) :: - deleteCustomer(customerId) :: deleteCustomerAccountLinks(customerId) :: + deleteCustomer(customerId) :: Nil doneTasks.forall(_ == true) } @@ -76,6 +86,32 @@ object DeleteCustomerCascade { fullBoxOrException(Empty ~> APIFailureNewStyle(CouldNotDeleteCascade, 400)) } } + private def deleteCustomerMessages(customerId: CustomerId): Boolean = { + MappedCustomer.find(By(MappedCustomer.mCustomerId, customerId.value)).forall(c => + MappedCustomerMessage.bulkDelete_!!( + By(MappedCustomerMessage.customer, c.id.get) + )) + } + private def deleteCustomerDependants(customerId: CustomerId): Boolean = { + MappedCustomer.find(By(MappedCustomer.mCustomerId, customerId.value)).forall(c => + MappedCustomerDependant.bulkDelete_!!( + By(MappedCustomerDependant.mCustomer, c.id.get) + )) + } + /** A link between two customers is held once, so the deleted customer can be on either side of it. */ + private def deleteCustomerLinks(customerId: CustomerId): Boolean = { + CustomerLink.bulkDelete_!!(By(CustomerLink.CustomerId, customerId.value)) && + CustomerLink.bulkDelete_!!(By(CustomerLink.OtherCustomerId, customerId.value)) + } + private def deleteCrmEvents(customerId: CustomerId): Boolean = { + MappedCustomer.find(By(MappedCustomer.mCustomerId, customerId.value)).forall { c => + val number = c.mNumber.get + number.isEmpty || MappedCrmEvent.bulkDelete_!!( + By(MappedCrmEvent.mBankId, c.mBank.get), + By(MappedCrmEvent.mCustomerNumber, number) + ) + } + } private def deleteCustomerAccountLinks(customerId: CustomerId): Boolean = { CustomerAccountLink.bulkDelete_!!( By(CustomerAccountLink.CustomerId, customerId.value) diff --git a/obp-api/src/test/scala/code/api/v4_0_0/CascadeDeleteAtomicityTest.scala b/obp-api/src/test/scala/code/api/v4_0_0/CascadeDeleteAtomicityTest.scala index 074c99927a..12516c20bd 100644 --- a/obp-api/src/test/scala/code/api/v4_0_0/CascadeDeleteAtomicityTest.scala +++ b/obp-api/src/test/scala/code/api/v4_0_0/CascadeDeleteAtomicityTest.scala @@ -34,6 +34,8 @@ import code.api.util.ApiRole import code.api.util.ErrorMessages.CouldNotDeleteCascade import code.api.v3_1_0.CreateAccountResponseJsonV310 import code.api.v4_0_0.Http4s400.Implementations4_0_0 +import code.customer.MappedCustomer +import code.customerattribute.MappedCustomerAttribute import code.entitlement.Entitlement import code.methodrouting.{MethodRouting, MethodRoutingCommons, MethodRoutingProvider} import code.model.dataAccess.{MappedBank, MappedBankAccount} @@ -56,6 +58,7 @@ class CascadeDeleteAtomicityTest extends V400ServerSetup { object VersionOfApi extends Tag(ApiVersion.v4_0_0.toString) object DeleteAccountCascade extends Tag(nameOf(Implementations4_0_0.deleteAccountCascade)) object DeleteBankCascade extends Tag(nameOf(Implementations4_0_0.deleteBankCascade)) + object DeleteCustomerCascade extends Tag(nameOf(Implementations4_0_0.deleteCustomerCascade)) private def createAccountWithSecondViewer(bankId: String): String = { Entitlement.entitlement.vend.addEntitlement(bankId, resourceUser1.userId, ApiRole.canCreateAccount.toString) @@ -144,5 +147,30 @@ class CascadeDeleteAtomicityTest extends V400ServerSetup { accessRows(bankId, accountId) should equal(accessBefore) } finally runSql("DROP TABLE cascade_atomicity_blocker") } + + scenario("A customer cascade that a database error stops keeps everything it had deleted", DeleteCustomerCascade, VersionOfApi) { + val bankId = randomBankId + val customerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) + createAndGetCustomerAttributeIdViaEndpoint(bankId, customerId, user1) + def attributes = MappedCustomerAttribute.count(By(MappedCustomerAttribute.mCustomerId, customerId)) + val attributesBefore = attributes + attributesBefore should be >= 1L + val customerPk = MappedCustomer.find(By(MappedCustomer.mCustomerId, customerId)).openOrThrowException("expected the customer").id.get + Entitlement.entitlement.vend.addEntitlement(bankId, resourceUser1.userId, ApiRole.canDeleteCustomerCascade.toString) + // A row that still refers to the customer, so deleting the customer itself, the last step, fails. The attributes + // are deleted by an earlier step: they must be back after the failure. + val table = MappedCustomer.dbTableName + runSql(s"CREATE TABLE cascade_customer_atomicity_blocker (customer_pk BIGINT REFERENCES $table(id))") + try { + runSql(s"INSERT INTO cascade_customer_atomicity_blocker (customer_pk) VALUES ($customerPk)") + + val response = makeDeleteRequest( + (v4_0_0_Request / "management" / "cascading" / "banks" / bankId / "customers" / customerId).DELETE <@ (user1)) + + response.code should not equal 200 + MappedCustomer.find(By(MappedCustomer.mCustomerId, customerId)).isDefined should equal(true) + attributes should equal(attributesBefore) + } finally runSql("DROP TABLE cascade_customer_atomicity_blocker") + } } } diff --git a/obp-api/src/test/scala/code/api/v4_0_0/DeleteCustomerCascadeTest.scala b/obp-api/src/test/scala/code/api/v4_0_0/DeleteCustomerCascadeTest.scala index 34a31abdc8..fe8ee1a494 100644 --- a/obp-api/src/test/scala/code/api/v4_0_0/DeleteCustomerCascadeTest.scala +++ b/obp-api/src/test/scala/code/api/v4_0_0/DeleteCustomerCascadeTest.scala @@ -27,17 +27,26 @@ TESOBE (http://www.tesobe.com/) package code.api.v4_0_0 +import code.CustomerDependants.MappedCustomerDependant +import code.api.util.APIUtil import code.api.util.APIUtil.OAuth._ import code.api.util.ApiRole import code.api.util.ApiRole.{CanDeleteCustomerCascade, CanDeleteTransactionCascade} -import code.api.util.ErrorMessages.{UserHasMissingRoles, AuthenticatedUserIsRequired} +import code.api.util.APIUtil.extractErrorMessageCode +import code.api.util.ErrorMessages.{AuthenticatedUserIsRequired, CustomerNotFoundByCustomerId, UserHasMissingRoles} import code.api.v4_0_0.Http4s400.Implementations4_0_0 +import code.customer.{MappedCustomer, MappedCustomerMessage} +import code.crm.MappedCrmEvent +import code.customerlinks.{CustomerLink, MappedCustomerLinkProvider} import code.entitlement.Entitlement import com.github.dwickern.macros.NameOf.nameOf import com.openbankproject.commons.model.ErrorMessage import com.openbankproject.commons.util.ApiVersion +import net.liftweb.mapper.By import org.scalatest.Tag +import java.util.Date + class DeleteCustomerCascadeTest extends V400ServerSetup { /** @@ -54,6 +63,18 @@ class DeleteCustomerCascadeTest extends V400ServerSetup { lazy val bankId = randomBankId lazy val bankAccount = randomPrivateAccountViaEndpoint(bankId) + private def customerPrimaryKey(customerId: String): Long = + MappedCustomer.find(By(MappedCustomer.mCustomerId, customerId)).openOrThrowException("expected the customer").id.get + + private def grantCascadeRole(): Unit = + if (!APIUtil.hasEntitlement(bankId, resourceUser1.userId, ApiRole.canDeleteCustomerCascade)) + Entitlement.entitlement.vend.addEntitlement(bankId, resourceUser1.userId, ApiRole.canDeleteCustomerCascade.toString) + + private def cascadeDelete(customerId: String) = { + grantCascadeRole() + makeDeleteRequest((v4_0_0_Request / "management" / "cascading" / "banks" / bankId / "customers" / customerId).DELETE <@(user1)) + } + feature(s"test $ApiEndpoint1 version $VersionOfApi - Unauthorized access") { scenario("We will call the endpoint without user credentials", ApiEndpoint1, VersionOfApi) { When("We make a request v4.0.0") @@ -98,4 +119,104 @@ class DeleteCustomerCascadeTest extends V400ServerSetup { } } + feature(s"test $ApiEndpoint1 - rows that name the customer by its id") { + scenario("A link between customers goes when either customer is deleted, links of others stay", ApiEndpoint1, VersionOfApi) { + val first = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) + val second = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) + val third = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) + def link(from: String, to: String) = + MappedCustomerLinkProvider.createCustomerLink(bankId, from, bankId, to, "RELATED").openOrThrowException("expected the link") + link(first, second) // the deleted customer is the first side + link(third, first) // the deleted customer is the other side + link(second, third) // unrelated to the deleted customer + def linksOf(id: String) = + CustomerLink.count(By(CustomerLink.CustomerId, id)) + CustomerLink.count(By(CustomerLink.OtherCustomerId, id)) + + linksOf(first) should equal(2) + + cascadeDelete(first).code should equal(200) + + linksOf(first) should equal(0) + CustomerLink.count(By(CustomerLink.CustomerId, second), By(CustomerLink.OtherCustomerId, third)) should equal(1) + } + } + + feature(s"test $ApiEndpoint1 - the bank in the URL") { + scenario("A customer of another bank is not found and is kept", ApiEndpoint1, VersionOfApi) { + val customerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) + val otherBankId = createBank(APIUtil.generateUUID()).bankId.value + Entitlement.entitlement.vend.addEntitlement(otherBankId, resourceUser1.userId, ApiRole.canDeleteCustomerCascade.toString) + + val wrongBank = makeDeleteRequest((v4_0_0_Request / "management" / "cascading" / "banks" / otherBankId / "customers" / customerId).DELETE <@(user1)) + + wrongBank.code should equal(404) + wrongBank.body.extract[ErrorMessage].message should include(extractErrorMessageCode(CustomerNotFoundByCustomerId)) + MappedCustomer.find(By(MappedCustomer.mCustomerId, customerId)).isDefined should equal(true) + + cascadeDelete(customerId).code should equal(200) + MappedCustomer.find(By(MappedCustomer.mCustomerId, customerId)).isDefined should equal(false) + } + } + + feature(s"test $ApiEndpoint1 - rows that name the customer by its number") { + scenario("CRM events of the deleted customer go, those of another customer stay", ApiEndpoint1, VersionOfApi) { + val customerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) + val otherCustomerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) + def numberOf(id: String) = MappedCustomer.find(By(MappedCustomer.mCustomerId, id)).openOrThrowException("expected the customer").mNumber.get + def addEvent(number: String, atBank: String): Unit = + MappedCrmEvent.create.mBankId(atBank).mCustomerNumber(number).mCustomerName("name").mCategory("call") + .mUserId(resourceUser1).saveMe() + def events(number: String, atBank: String) = + MappedCrmEvent.count(By(MappedCrmEvent.mBankId, atBank), By(MappedCrmEvent.mCustomerNumber, number)) + val number = numberOf(customerId) + val otherNumber = numberOf(otherCustomerId) + addEvent(number, bankId) + addEvent(otherNumber, bankId) + events(number, bankId) should equal(1) + + cascadeDelete(customerId).code should equal(200) + + events(number, bankId) should equal(0) + events(otherNumber, bankId) should equal(1) + } + + scenario("A CRM event with the same customer number at another bank stays", ApiEndpoint1, VersionOfApi) { + val customerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) + val number = MappedCustomer.find(By(MappedCustomer.mCustomerId, customerId)).openOrThrowException("expected the customer").mNumber.get + MappedCrmEvent.create.mBankId("another-bank").mCustomerNumber(number).mCustomerName("name").mCategory("call") + .mUserId(resourceUser1).saveMe() + + cascadeDelete(customerId).code should equal(200) + + MappedCrmEvent.count(By(MappedCrmEvent.mBankId, "another-bank"), By(MappedCrmEvent.mCustomerNumber, number)) should equal(1) + } + } + + feature(s"test $ApiEndpoint1 - rows that point at the customer") { + scenario("Messages and dependants of the deleted customer go, those of another customer stay", ApiEndpoint1, VersionOfApi) { + val customerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) + val otherCustomerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) + val pk = customerPrimaryKey(customerId) + val otherPk = customerPrimaryKey(otherCustomerId) + def addRows(customerPk: Long): Unit = { + MappedCustomerMessage.create.customer(customerPk).bank(bankId).mMessage("hello") + .mFromPerson("person").mFromDepartment("department").mTransport("sms").saveMe() + MappedCustomerDependant.create.mCustomer(customerPk).mDateOfBirth(new Date()).saveMe() + } + addRows(pk) + addRows(otherPk) + // Creating a customer through the endpoint already adds dependants, so count against what was there. + val otherDependantsBefore = MappedCustomerDependant.count(By(MappedCustomerDependant.mCustomer, otherPk)) + MappedCustomerMessage.count(By(MappedCustomerMessage.customer, pk)) should equal(1) + MappedCustomerDependant.count(By(MappedCustomerDependant.mCustomer, pk)) should be >= 1L + + cascadeDelete(customerId).code should equal(200) + + MappedCustomerMessage.count(By(MappedCustomerMessage.customer, pk)) should equal(0) + MappedCustomerDependant.count(By(MappedCustomerDependant.mCustomer, pk)) should equal(0) + MappedCustomerMessage.count(By(MappedCustomerMessage.customer, otherPk)) should equal(1) + MappedCustomerDependant.count(By(MappedCustomerDependant.mCustomer, otherPk)) should equal(otherDependantsBefore) + } + } + }