From 11c6b6714fe79fd046f8e8d504fc2d56efd8da8b Mon Sep 17 00:00:00 2001 From: Hongwei Date: Mon, 16 Nov 2026 15:01:25 +0100 Subject: [PATCH 01/11] feat: delete customer messages and dependants in the customer cascade The customer row is now the last task, with the customer-account links ahead of it, so a failure of the last step rolls back every earlier one. --- .../deletion/DeleteCustomerCascade.scala | 22 +++++++++- .../v4_0_0/DeleteCustomerCascadeTest.scala | 43 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala index abbe9455c4..c3f3a3b1f3 100644 --- a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala +++ b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala @@ -31,7 +31,8 @@ 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 @@ -50,6 +51,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 +62,13 @@ object DeleteCustomerCascade { deleteKycCheck(customerId) :: deleteKycDocument(customerId) :: deleteCustomerAddress(customerId) :: + deleteCustomerMessages(customerId) :: + deleteCustomerDependants(customerId) :: deleteCustomerIdMapping(customerId) :: deleteAccountApplication(customerId) :: deleteCustomerUserCustomerLinks(customerId) :: - deleteCustomer(customerId) :: deleteCustomerAccountLinks(customerId) :: + deleteCustomer(customerId) :: Nil doneTasks.forall(_ == true) } @@ -76,6 +82,18 @@ 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) + )) + } 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/DeleteCustomerCascadeTest.scala b/obp-api/src/test/scala/code/api/v4_0_0/DeleteCustomerCascadeTest.scala index 34a31abdc8..9cf19f587f 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,23 @@ 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.v4_0_0.Http4s400.Implementations4_0_0 +import code.customer.{MappedCustomer, MappedCustomerMessage} 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 +60,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 +116,29 @@ class DeleteCustomerCascadeTest extends V400ServerSetup { } } + 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) + MappedCustomerMessage.count(By(MappedCustomerMessage.customer, pk)) should equal(1) + MappedCustomerDependant.count(By(MappedCustomerDependant.mCustomer, pk)) should equal(1) + + 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(1) + } + } + } From d07e58e8964189dc472c406bc6aea75cc8c055be Mon Sep 17 00:00:00 2001 From: Hongwei Date: Mon, 16 Nov 2026 15:16:36 +0100 Subject: [PATCH 02/11] test: count dependants against the ones the customer already has --- .../scala/code/api/v4_0_0/DeleteCustomerCascadeTest.scala | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 9cf19f587f..530b5f4db7 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 @@ -129,15 +129,17 @@ class DeleteCustomerCascadeTest extends V400ServerSetup { } 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 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(1) + MappedCustomerDependant.count(By(MappedCustomerDependant.mCustomer, otherPk)) should equal(otherDependantsBefore) } } From d70b096ffe96911edcddbe4c9b20b678a6dc66a1 Mon Sep 17 00:00:00 2001 From: Hongwei Date: Mon, 16 Nov 2026 15:16:44 +0100 Subject: [PATCH 03/11] feat: delete links between customers in the customer cascade --- .../deletion/DeleteCustomerCascade.scala | 7 ++++++ .../v4_0_0/DeleteCustomerCascadeTest.scala | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala index c3f3a3b1f3..9da1f3224b 100644 --- a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala +++ b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala @@ -36,6 +36,7 @@ import code.customer.{MappedCustomer, MappedCustomerMessage} import code.customer.internalMapping.MappedCustomerIdMapping import code.customeraccountlinks.CustomerAccountLink import code.customeraddress.MappedCustomerAddress +import code.customerlinks.CustomerLink import code.customerattribute.MappedCustomerAttribute import code.kycchecks.MappedKycCheck import code.kycdocuments.MappedKycDocument @@ -64,6 +65,7 @@ object DeleteCustomerCascade { deleteCustomerAddress(customerId) :: deleteCustomerMessages(customerId) :: deleteCustomerDependants(customerId) :: + deleteCustomerLinks(customerId) :: deleteCustomerIdMapping(customerId) :: deleteAccountApplication(customerId) :: deleteCustomerUserCustomerLinks(customerId) :: @@ -94,6 +96,11 @@ object DeleteCustomerCascade { 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 deleteCustomerAccountLinks(customerId: CustomerId): Boolean = { CustomerAccountLink.bulkDelete_!!( By(CustomerAccountLink.CustomerId, customerId.value) 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 530b5f4db7..9aaee05936 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 @@ -35,6 +35,7 @@ import code.api.util.ApiRole.{CanDeleteCustomerCascade, CanDeleteTransactionCasc import code.api.util.ErrorMessages.{UserHasMissingRoles, AuthenticatedUserIsRequired} import code.api.v4_0_0.Http4s400.Implementations4_0_0 import code.customer.{MappedCustomer, MappedCustomerMessage} +import code.customerlinks.{CustomerLink, MappedCustomerLinkProvider} import code.entitlement.Entitlement import com.github.dwickern.macros.NameOf.nameOf import com.openbankproject.commons.model.ErrorMessage @@ -116,6 +117,28 @@ 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 - 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) From b7b4da20e410bf1a9648834d360f0eab9c81db53 Mon Sep 17 00:00:00 2001 From: Hongwei Date: Mon, 16 Nov 2026 15:18:12 +0100 Subject: [PATCH 04/11] feat: delete social media and CRM events in the customer cascade CRM events are matched on bank and customer number. Social media rows hold no bank, so they are only deleted when no customer of another bank has the same number. --- .../deletion/DeleteCustomerCascade.scala | 29 ++++++++++++- .../v4_0_0/DeleteCustomerCascadeTest.scala | 43 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala index 9da1f3224b..3c4df487fd 100644 --- a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala +++ b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala @@ -36,18 +36,20 @@ 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 import code.kycmedias.MappedKycMedia import code.kycstatuses.MappedKycStatus +import code.socialmedia.MappedSocialMedia import code.taxresidence.MappedTaxResidence import code.usercustomerlinks.MappedUserCustomerLink import com.openbankproject.commons.model.CustomerId import deletion.DeletionUtil.databaseAtomicTask import net.liftweb.common.{Box, Empty, Full} -import net.liftweb.mapper.By +import net.liftweb.mapper.{By, NotBy} object DeleteCustomerCascade { @@ -66,6 +68,8 @@ object DeleteCustomerCascade { deleteCustomerMessages(customerId) :: deleteCustomerDependants(customerId) :: deleteCustomerLinks(customerId) :: + deleteSocialMedia(customerId) :: + deleteCrmEvents(customerId) :: deleteCustomerIdMapping(customerId) :: deleteAccountApplication(customerId) :: deleteCustomerUserCustomerLinks(customerId) :: @@ -101,6 +105,29 @@ object DeleteCustomerCascade { CustomerLink.bulkDelete_!!(By(CustomerLink.CustomerId, customerId.value)) && CustomerLink.bulkDelete_!!(By(CustomerLink.OtherCustomerId, customerId.value)) } + /** + * Social media rows carry the customer number and no bank, and a customer number is only unique within a bank. + * They are deleted only when no customer of another bank has the same number, so one customer's cascade + * never removes another customer's handles. + */ + private def deleteSocialMedia(customerId: CustomerId): Boolean = { + MappedCustomer.find(By(MappedCustomer.mCustomerId, customerId.value)).forall { c => + val number = c.mNumber.get + val numberUsedByAnotherCustomer = + MappedCustomer.count(By(MappedCustomer.mNumber, number), NotBy(MappedCustomer.mCustomerId, customerId.value)) > 0 + number.isEmpty || numberUsedByAnotherCustomer || + MappedSocialMedia.bulkDelete_!!(By(MappedSocialMedia.mCustomerNumber, number)) + } + } + 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/DeleteCustomerCascadeTest.scala b/obp-api/src/test/scala/code/api/v4_0_0/DeleteCustomerCascadeTest.scala index 9aaee05936..ef79377785 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 @@ -35,7 +35,9 @@ import code.api.util.ApiRole.{CanDeleteCustomerCascade, CanDeleteTransactionCasc import code.api.util.ErrorMessages.{UserHasMissingRoles, AuthenticatedUserIsRequired} 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.socialmedia.MappedSocialMedia import code.entitlement.Entitlement import com.github.dwickern.macros.NameOf.nameOf import com.openbankproject.commons.model.ErrorMessage @@ -139,6 +141,47 @@ class DeleteCustomerCascadeTest extends V400ServerSetup { } } + feature(s"test $ApiEndpoint1 - rows that name the customer by its number") { + scenario("Social media and 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 addRows(number: String): Unit = { + MappedSocialMedia.create.mCustomerNumber(number).mType("twitter").mHandle("@" + number).saveMe() + MappedCrmEvent.create.mBankId(bankId).mCustomerNumber(number).mCustomerName("name").mCategory("call") + .mUserId(resourceUser1).saveMe() + } + def rows(number: String) = ( + MappedSocialMedia.count(By(MappedSocialMedia.mCustomerNumber, number)), + MappedCrmEvent.count(By(MappedCrmEvent.mBankId, bankId), By(MappedCrmEvent.mCustomerNumber, number))) + val number = numberOf(customerId) + val otherNumber = numberOf(otherCustomerId) + addRows(number) + addRows(otherNumber) + rows(number) should equal((1, 1)) + + cascadeDelete(customerId).code should equal(200) + + rows(number) should equal((0, 0)) + rows(otherNumber) should equal((1, 1)) + } + + scenario("Social media is kept when a customer of another bank has the same number", ApiEndpoint1, VersionOfApi) { + val customerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) + val number = MappedCustomer.find(By(MappedCustomer.mCustomerId, customerId)).openOrThrowException("expected the customer").mNumber.get + // The same number at another bank: social media does not record the bank, so the rows cannot be told apart. + MappedCustomer.create.mBank("another-bank").mNumber(number).mCustomerId(APIUtil.generateUUID()).saveMe() + MappedSocialMedia.create.mCustomerNumber(number).mType("twitter").mHandle("@shared").saveMe() + MappedCrmEvent.create.mBankId(bankId).mCustomerNumber(number).mCustomerName("name").mCategory("call") + .mUserId(resourceUser1).saveMe() + + cascadeDelete(customerId).code should equal(200) + + MappedSocialMedia.count(By(MappedSocialMedia.mCustomerNumber, number)) should equal(1) + MappedCrmEvent.count(By(MappedCrmEvent.mBankId, bankId), By(MappedCrmEvent.mCustomerNumber, number)) should equal(0) + } + } + 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) From bbf83ab0c46d467ede260fc8d624173882ee5d8e Mon Sep 17 00:00:00 2001 From: Hongwei Date: Mon, 16 Nov 2026 15:18:38 +0100 Subject: [PATCH 05/11] feat: delete standing orders and direct debits in the customer cascade --- .../deletion/DeleteCustomerCascade.scala | 10 ++++++++ .../v4_0_0/DeleteCustomerCascadeTest.scala | 24 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala index 3c4df487fd..aafcc843bb 100644 --- a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala +++ b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala @@ -39,11 +39,13 @@ import code.customeraddress.MappedCustomerAddress import code.crm.MappedCrmEvent import code.customerlinks.CustomerLink import code.customerattribute.MappedCustomerAttribute +import code.directdebit.DirectDebit import code.kycchecks.MappedKycCheck import code.kycdocuments.MappedKycDocument import code.kycmedias.MappedKycMedia import code.kycstatuses.MappedKycStatus import code.socialmedia.MappedSocialMedia +import code.standingorders.StandingOrder import code.taxresidence.MappedTaxResidence import code.usercustomerlinks.MappedUserCustomerLink import com.openbankproject.commons.model.CustomerId @@ -70,6 +72,8 @@ object DeleteCustomerCascade { deleteCustomerLinks(customerId) :: deleteSocialMedia(customerId) :: deleteCrmEvents(customerId) :: + deleteStandingOrders(customerId) :: + deleteDirectDebits(customerId) :: deleteCustomerIdMapping(customerId) :: deleteAccountApplication(customerId) :: deleteCustomerUserCustomerLinks(customerId) :: @@ -128,6 +132,12 @@ object DeleteCustomerCascade { ) } } + private def deleteStandingOrders(customerId: CustomerId): Boolean = { + StandingOrder.bulkDelete_!!(By(StandingOrder.CustomerId, customerId.value)) + } + private def deleteDirectDebits(customerId: CustomerId): Boolean = { + DirectDebit.bulkDelete_!!(By(DirectDebit.CustomerId, customerId.value)) + } 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/DeleteCustomerCascadeTest.scala b/obp-api/src/test/scala/code/api/v4_0_0/DeleteCustomerCascadeTest.scala index ef79377785..b6787e11e4 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 @@ -37,7 +37,9 @@ 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.directdebit.{DirectDebit, MappedDirectDebitProvider} import code.socialmedia.MappedSocialMedia +import code.standingorders.{MappedStandingOrderProvider, StandingOrder} import code.entitlement.Entitlement import com.github.dwickern.macros.NameOf.nameOf import com.openbankproject.commons.model.ErrorMessage @@ -141,6 +143,28 @@ class DeleteCustomerCascadeTest extends V400ServerSetup { } } + feature(s"test $ApiEndpoint1 - payment instructions signed by the customer") { + scenario("Standing orders and direct debits 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 addRows(id: String): Unit = { + MappedStandingOrderProvider.createStandingOrder(bankId, "account-" + id, id, resourceUser1.userId, "counterparty", + BigDecimal(10), "EUR", "MONTHLY", "1", new Date(), new Date(), None).openOrThrowException("expected the standing order") + MappedDirectDebitProvider.createDirectDebit(bankId, "account-" + id, id, resourceUser1.userId, "counterparty", + new Date(), new Date(), None).openOrThrowException("expected the direct debit") + } + def rows(id: String) = (StandingOrder.count(By(StandingOrder.CustomerId, id)), DirectDebit.count(By(DirectDebit.CustomerId, id))) + addRows(customerId) + addRows(otherCustomerId) + rows(customerId) should equal((1, 1)) + + cascadeDelete(customerId).code should equal(200) + + rows(customerId) should equal((0, 0)) + rows(otherCustomerId) should equal((1, 1)) + } + } + feature(s"test $ApiEndpoint1 - rows that name the customer by its number") { scenario("Social media and CRM events of the deleted customer go, those of another customer stay", ApiEndpoint1, VersionOfApi) { val customerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) From e3005e787ba57e2a62eca4cc14c6c9fb8c6905d6 Mon Sep 17 00:00:00 2001 From: Hongwei Date: Mon, 16 Nov 2026 15:20:33 +0100 Subject: [PATCH 06/11] feat: delete mandates with their provisions and signatory panels in the customer cascade --- .../deletion/DeleteCustomerCascade.scala | 13 +++++++- .../v4_0_0/DeleteCustomerCascadeTest.scala | 31 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala index aafcc843bb..459e7262f8 100644 --- a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala +++ b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala @@ -40,6 +40,7 @@ import code.crm.MappedCrmEvent import code.customerlinks.CustomerLink import code.customerattribute.MappedCustomerAttribute import code.directdebit.DirectDebit +import code.mandate.{Mandate, MandateProvision, SignatoryPanel} import code.kycchecks.MappedKycCheck import code.kycdocuments.MappedKycDocument import code.kycmedias.MappedKycMedia @@ -51,7 +52,7 @@ import code.usercustomerlinks.MappedUserCustomerLink import com.openbankproject.commons.model.CustomerId import deletion.DeletionUtil.databaseAtomicTask import net.liftweb.common.{Box, Empty, Full} -import net.liftweb.mapper.{By, NotBy} +import net.liftweb.mapper.{By, ByList, NotBy} object DeleteCustomerCascade { @@ -74,6 +75,7 @@ object DeleteCustomerCascade { deleteCrmEvents(customerId) :: deleteStandingOrders(customerId) :: deleteDirectDebits(customerId) :: + deleteMandates(customerId) :: deleteCustomerIdMapping(customerId) :: deleteAccountApplication(customerId) :: deleteCustomerUserCustomerLinks(customerId) :: @@ -138,6 +140,15 @@ object DeleteCustomerCascade { private def deleteDirectDebits(customerId: CustomerId): Boolean = { DirectDebit.bulkDelete_!!(By(DirectDebit.CustomerId, customerId.value)) } + /** A mandate's provisions and signatory panels are found through the mandate id, so they go before the mandate. */ + private def deleteMandates(customerId: CustomerId): Boolean = { + val mandateIds = Mandate.findAll(By(Mandate.CustomerId, customerId.value)).map(_.MandateId.get) + mandateIds.isEmpty || ( + MandateProvision.bulkDelete_!!(ByList(MandateProvision.MandateId, mandateIds)) && + SignatoryPanel.bulkDelete_!!(ByList(SignatoryPanel.MandateId, mandateIds)) && + Mandate.bulkDelete_!!(By(Mandate.CustomerId, customerId.value)) + ) + } 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/DeleteCustomerCascadeTest.scala b/obp-api/src/test/scala/code/api/v4_0_0/DeleteCustomerCascadeTest.scala index b6787e11e4..36bba7a9f7 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 @@ -32,12 +32,13 @@ 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.ErrorMessages.{AuthenticatedUserIsRequired, 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.directdebit.{DirectDebit, MappedDirectDebitProvider} +import code.mandate.{Mandate, MandateProvision, MappedMandateProvider, SignatoryPanel} import code.socialmedia.MappedSocialMedia import code.standingorders.{MappedStandingOrderProvider, StandingOrder} import code.entitlement.Entitlement @@ -165,6 +166,34 @@ class DeleteCustomerCascadeTest extends V400ServerSetup { } } + feature(s"test $ApiEndpoint1 - mandates of the customer") { + scenario("A mandate with its provisions and signatory panels goes, those of another customer stay", ApiEndpoint1, VersionOfApi) { + val customerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) + val otherCustomerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) + def addMandate(id: String): String = { + val mandate = MappedMandateProvider.createMandate(bankId, "account-" + id, id, "name", "reference-" + id, "legal", "description", + "ACTIVE", new Date(), new Date(), resourceUser1.userId).openOrThrowException("expected the mandate") + MappedMandateProvider.createMandateProvision(mandate.mandateId, "provision", "description", "legal", "TYPE", + "conditions", "requirements", "", "", "", true, 1).openOrThrowException("expected the provision") + MappedMandateProvider.createSignatoryPanel(mandate.mandateId, "panel", "description", resourceUser1.userId) + .openOrThrowException("expected the panel") + mandate.mandateId + } + val mandateId = addMandate(customerId) + val otherMandateId = addMandate(otherCustomerId) + def rows(mandate: String, customer: String) = ( + Mandate.count(By(Mandate.CustomerId, customer)), + MandateProvision.count(By(MandateProvision.MandateId, mandate)), + SignatoryPanel.count(By(SignatoryPanel.MandateId, mandate))) + rows(mandateId, customerId) should equal((1, 1, 1)) + + cascadeDelete(customerId).code should equal(200) + + rows(mandateId, customerId) should equal((0, 0, 0)) + rows(otherMandateId, otherCustomerId) should equal((1, 1, 1)) + } + } + feature(s"test $ApiEndpoint1 - rows that name the customer by its number") { scenario("Social media and CRM events of the deleted customer go, those of another customer stay", ApiEndpoint1, VersionOfApi) { val customerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) From de6a27cc205ca34037fe79cf77bca14c14ddf6fd Mon Sep 17 00:00:00 2001 From: Hongwei Date: Mon, 16 Nov 2026 15:20:34 +0100 Subject: [PATCH 07/11] feat: customer cascade delete only finds customers of the bank in the URL --- .../scala/code/api/v4_0_0/Http4s400.scala | 8 ++++++-- .../v4_0_0/DeleteCustomerCascadeTest.scala | 20 ++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) 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/test/scala/code/api/v4_0_0/DeleteCustomerCascadeTest.scala b/obp-api/src/test/scala/code/api/v4_0_0/DeleteCustomerCascadeTest.scala index 36bba7a9f7..5c925158aa 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 @@ -32,7 +32,8 @@ 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.{AuthenticatedUserIsRequired, UserHasMissingRoles} +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 @@ -144,6 +145,23 @@ class DeleteCustomerCascadeTest extends V400ServerSetup { } } + 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 - payment instructions signed by the customer") { scenario("Standing orders and direct debits of the deleted customer go, those of another customer stay", ApiEndpoint1, VersionOfApi) { val customerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) From 72f097e3d3c7af2f9d6c7551921ef2fd1308fb06 Mon Sep 17 00:00:00 2001 From: Hongwei Date: Mon, 16 Nov 2026 15:20:42 +0100 Subject: [PATCH 08/11] test: a customer cascade stopped by a database error keeps what it had deleted --- .../v4_0_0/CascadeDeleteAtomicityTest.scala | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) 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") + } } } From 8d2417fe2afe1ebab4d3b3510b2bbcdef67e82b5 Mon Sep 17 00:00:00 2001 From: Hongwei Date: Mon, 16 Nov 2026 16:24:41 +0100 Subject: [PATCH 09/11] revert: do not delete mandates in the customer cascade Mandates are tied to an account, and the account cascade keeps them, so the customer cascade leaves them too. --- .../deletion/DeleteCustomerCascade.scala | 13 +-------- .../v4_0_0/DeleteCustomerCascadeTest.scala | 29 ------------------- 2 files changed, 1 insertion(+), 41 deletions(-) diff --git a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala index 459e7262f8..aafcc843bb 100644 --- a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala +++ b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala @@ -40,7 +40,6 @@ import code.crm.MappedCrmEvent import code.customerlinks.CustomerLink import code.customerattribute.MappedCustomerAttribute import code.directdebit.DirectDebit -import code.mandate.{Mandate, MandateProvision, SignatoryPanel} import code.kycchecks.MappedKycCheck import code.kycdocuments.MappedKycDocument import code.kycmedias.MappedKycMedia @@ -52,7 +51,7 @@ import code.usercustomerlinks.MappedUserCustomerLink import com.openbankproject.commons.model.CustomerId import deletion.DeletionUtil.databaseAtomicTask import net.liftweb.common.{Box, Empty, Full} -import net.liftweb.mapper.{By, ByList, NotBy} +import net.liftweb.mapper.{By, NotBy} object DeleteCustomerCascade { @@ -75,7 +74,6 @@ object DeleteCustomerCascade { deleteCrmEvents(customerId) :: deleteStandingOrders(customerId) :: deleteDirectDebits(customerId) :: - deleteMandates(customerId) :: deleteCustomerIdMapping(customerId) :: deleteAccountApplication(customerId) :: deleteCustomerUserCustomerLinks(customerId) :: @@ -140,15 +138,6 @@ object DeleteCustomerCascade { private def deleteDirectDebits(customerId: CustomerId): Boolean = { DirectDebit.bulkDelete_!!(By(DirectDebit.CustomerId, customerId.value)) } - /** A mandate's provisions and signatory panels are found through the mandate id, so they go before the mandate. */ - private def deleteMandates(customerId: CustomerId): Boolean = { - val mandateIds = Mandate.findAll(By(Mandate.CustomerId, customerId.value)).map(_.MandateId.get) - mandateIds.isEmpty || ( - MandateProvision.bulkDelete_!!(ByList(MandateProvision.MandateId, mandateIds)) && - SignatoryPanel.bulkDelete_!!(ByList(SignatoryPanel.MandateId, mandateIds)) && - Mandate.bulkDelete_!!(By(Mandate.CustomerId, customerId.value)) - ) - } 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/DeleteCustomerCascadeTest.scala b/obp-api/src/test/scala/code/api/v4_0_0/DeleteCustomerCascadeTest.scala index 5c925158aa..715af82060 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 @@ -39,7 +39,6 @@ import code.customer.{MappedCustomer, MappedCustomerMessage} import code.crm.MappedCrmEvent import code.customerlinks.{CustomerLink, MappedCustomerLinkProvider} import code.directdebit.{DirectDebit, MappedDirectDebitProvider} -import code.mandate.{Mandate, MandateProvision, MappedMandateProvider, SignatoryPanel} import code.socialmedia.MappedSocialMedia import code.standingorders.{MappedStandingOrderProvider, StandingOrder} import code.entitlement.Entitlement @@ -184,34 +183,6 @@ class DeleteCustomerCascadeTest extends V400ServerSetup { } } - feature(s"test $ApiEndpoint1 - mandates of the customer") { - scenario("A mandate with its provisions and signatory panels goes, those of another customer stay", ApiEndpoint1, VersionOfApi) { - val customerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) - val otherCustomerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) - def addMandate(id: String): String = { - val mandate = MappedMandateProvider.createMandate(bankId, "account-" + id, id, "name", "reference-" + id, "legal", "description", - "ACTIVE", new Date(), new Date(), resourceUser1.userId).openOrThrowException("expected the mandate") - MappedMandateProvider.createMandateProvision(mandate.mandateId, "provision", "description", "legal", "TYPE", - "conditions", "requirements", "", "", "", true, 1).openOrThrowException("expected the provision") - MappedMandateProvider.createSignatoryPanel(mandate.mandateId, "panel", "description", resourceUser1.userId) - .openOrThrowException("expected the panel") - mandate.mandateId - } - val mandateId = addMandate(customerId) - val otherMandateId = addMandate(otherCustomerId) - def rows(mandate: String, customer: String) = ( - Mandate.count(By(Mandate.CustomerId, customer)), - MandateProvision.count(By(MandateProvision.MandateId, mandate)), - SignatoryPanel.count(By(SignatoryPanel.MandateId, mandate))) - rows(mandateId, customerId) should equal((1, 1, 1)) - - cascadeDelete(customerId).code should equal(200) - - rows(mandateId, customerId) should equal((0, 0, 0)) - rows(otherMandateId, otherCustomerId) should equal((1, 1, 1)) - } - } - feature(s"test $ApiEndpoint1 - rows that name the customer by its number") { scenario("Social media and CRM events of the deleted customer go, those of another customer stay", ApiEndpoint1, VersionOfApi) { val customerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) From 928e40726a2767ddd885c5299ff22c18026003e1 Mon Sep 17 00:00:00 2001 From: Hongwei Date: Mon, 16 Nov 2026 16:25:05 +0100 Subject: [PATCH 10/11] revert: do not delete standing orders and direct debits in the customer cascade They are tied to an account, and the account cascade keeps them, so the customer cascade leaves them too. --- .../deletion/DeleteCustomerCascade.scala | 10 -------- .../v4_0_0/DeleteCustomerCascadeTest.scala | 24 ------------------- 2 files changed, 34 deletions(-) diff --git a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala index aafcc843bb..3c4df487fd 100644 --- a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala +++ b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala @@ -39,13 +39,11 @@ import code.customeraddress.MappedCustomerAddress import code.crm.MappedCrmEvent import code.customerlinks.CustomerLink import code.customerattribute.MappedCustomerAttribute -import code.directdebit.DirectDebit import code.kycchecks.MappedKycCheck import code.kycdocuments.MappedKycDocument import code.kycmedias.MappedKycMedia import code.kycstatuses.MappedKycStatus import code.socialmedia.MappedSocialMedia -import code.standingorders.StandingOrder import code.taxresidence.MappedTaxResidence import code.usercustomerlinks.MappedUserCustomerLink import com.openbankproject.commons.model.CustomerId @@ -72,8 +70,6 @@ object DeleteCustomerCascade { deleteCustomerLinks(customerId) :: deleteSocialMedia(customerId) :: deleteCrmEvents(customerId) :: - deleteStandingOrders(customerId) :: - deleteDirectDebits(customerId) :: deleteCustomerIdMapping(customerId) :: deleteAccountApplication(customerId) :: deleteCustomerUserCustomerLinks(customerId) :: @@ -132,12 +128,6 @@ object DeleteCustomerCascade { ) } } - private def deleteStandingOrders(customerId: CustomerId): Boolean = { - StandingOrder.bulkDelete_!!(By(StandingOrder.CustomerId, customerId.value)) - } - private def deleteDirectDebits(customerId: CustomerId): Boolean = { - DirectDebit.bulkDelete_!!(By(DirectDebit.CustomerId, customerId.value)) - } 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/DeleteCustomerCascadeTest.scala b/obp-api/src/test/scala/code/api/v4_0_0/DeleteCustomerCascadeTest.scala index 715af82060..2f5399deb8 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 @@ -38,9 +38,7 @@ 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.directdebit.{DirectDebit, MappedDirectDebitProvider} import code.socialmedia.MappedSocialMedia -import code.standingorders.{MappedStandingOrderProvider, StandingOrder} import code.entitlement.Entitlement import com.github.dwickern.macros.NameOf.nameOf import com.openbankproject.commons.model.ErrorMessage @@ -161,28 +159,6 @@ class DeleteCustomerCascadeTest extends V400ServerSetup { } } - feature(s"test $ApiEndpoint1 - payment instructions signed by the customer") { - scenario("Standing orders and direct debits 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 addRows(id: String): Unit = { - MappedStandingOrderProvider.createStandingOrder(bankId, "account-" + id, id, resourceUser1.userId, "counterparty", - BigDecimal(10), "EUR", "MONTHLY", "1", new Date(), new Date(), None).openOrThrowException("expected the standing order") - MappedDirectDebitProvider.createDirectDebit(bankId, "account-" + id, id, resourceUser1.userId, "counterparty", - new Date(), new Date(), None).openOrThrowException("expected the direct debit") - } - def rows(id: String) = (StandingOrder.count(By(StandingOrder.CustomerId, id)), DirectDebit.count(By(DirectDebit.CustomerId, id))) - addRows(customerId) - addRows(otherCustomerId) - rows(customerId) should equal((1, 1)) - - cascadeDelete(customerId).code should equal(200) - - rows(customerId) should equal((0, 0)) - rows(otherCustomerId) should equal((1, 1)) - } - } - feature(s"test $ApiEndpoint1 - rows that name the customer by its number") { scenario("Social media and CRM events of the deleted customer go, those of another customer stay", ApiEndpoint1, VersionOfApi) { val customerId = createAndGetCustomerIdViaEndpoint(bankId, resourceUser1.userId) From d7c9fd936eb2bbc43dfdbb326eab8e32fe79ef1c Mon Sep 17 00:00:00 2001 From: Hongwei Date: Mon, 16 Nov 2026 16:25:34 +0100 Subject: [PATCH 11/11] revert: do not delete social media in the customer cascade Social media rows hold a customer number but no bank, so they cannot be told apart from those of a same-numbered customer at another bank. CRM events, which hold the bank, stay in the cascade. --- .../deletion/DeleteCustomerCascade.scala | 18 +--------- .../v4_0_0/DeleteCustomerCascadeTest.scala | 34 +++++++------------ 2 files changed, 14 insertions(+), 38 deletions(-) diff --git a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala index 3c4df487fd..e42ab397f8 100644 --- a/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala +++ b/obp-api/src/main/scala/deletion/DeleteCustomerCascade.scala @@ -43,13 +43,12 @@ import code.kycchecks.MappedKycCheck import code.kycdocuments.MappedKycDocument import code.kycmedias.MappedKycMedia import code.kycstatuses.MappedKycStatus -import code.socialmedia.MappedSocialMedia import code.taxresidence.MappedTaxResidence import code.usercustomerlinks.MappedUserCustomerLink import com.openbankproject.commons.model.CustomerId import deletion.DeletionUtil.databaseAtomicTask import net.liftweb.common.{Box, Empty, Full} -import net.liftweb.mapper.{By, NotBy} +import net.liftweb.mapper.By object DeleteCustomerCascade { @@ -68,7 +67,6 @@ object DeleteCustomerCascade { deleteCustomerMessages(customerId) :: deleteCustomerDependants(customerId) :: deleteCustomerLinks(customerId) :: - deleteSocialMedia(customerId) :: deleteCrmEvents(customerId) :: deleteCustomerIdMapping(customerId) :: deleteAccountApplication(customerId) :: @@ -105,20 +103,6 @@ object DeleteCustomerCascade { CustomerLink.bulkDelete_!!(By(CustomerLink.CustomerId, customerId.value)) && CustomerLink.bulkDelete_!!(By(CustomerLink.OtherCustomerId, customerId.value)) } - /** - * Social media rows carry the customer number and no bank, and a customer number is only unique within a bank. - * They are deleted only when no customer of another bank has the same number, so one customer's cascade - * never removes another customer's handles. - */ - private def deleteSocialMedia(customerId: CustomerId): Boolean = { - MappedCustomer.find(By(MappedCustomer.mCustomerId, customerId.value)).forall { c => - val number = c.mNumber.get - val numberUsedByAnotherCustomer = - MappedCustomer.count(By(MappedCustomer.mNumber, number), NotBy(MappedCustomer.mCustomerId, customerId.value)) > 0 - number.isEmpty || numberUsedByAnotherCustomer || - MappedSocialMedia.bulkDelete_!!(By(MappedSocialMedia.mCustomerNumber, number)) - } - } private def deleteCrmEvents(customerId: CustomerId): Boolean = { MappedCustomer.find(By(MappedCustomer.mCustomerId, customerId.value)).forall { c => val number = c.mNumber.get 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 2f5399deb8..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 @@ -38,7 +38,6 @@ 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.socialmedia.MappedSocialMedia import code.entitlement.Entitlement import com.github.dwickern.macros.NameOf.nameOf import com.openbankproject.commons.model.ErrorMessage @@ -160,43 +159,36 @@ class DeleteCustomerCascadeTest extends V400ServerSetup { } feature(s"test $ApiEndpoint1 - rows that name the customer by its number") { - scenario("Social media and CRM events of the deleted customer go, those of another customer stay", ApiEndpoint1, VersionOfApi) { + 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 addRows(number: String): Unit = { - MappedSocialMedia.create.mCustomerNumber(number).mType("twitter").mHandle("@" + number).saveMe() - MappedCrmEvent.create.mBankId(bankId).mCustomerNumber(number).mCustomerName("name").mCategory("call") + def addEvent(number: String, atBank: String): Unit = + MappedCrmEvent.create.mBankId(atBank).mCustomerNumber(number).mCustomerName("name").mCategory("call") .mUserId(resourceUser1).saveMe() - } - def rows(number: String) = ( - MappedSocialMedia.count(By(MappedSocialMedia.mCustomerNumber, number)), - MappedCrmEvent.count(By(MappedCrmEvent.mBankId, bankId), By(MappedCrmEvent.mCustomerNumber, number))) + def events(number: String, atBank: String) = + MappedCrmEvent.count(By(MappedCrmEvent.mBankId, atBank), By(MappedCrmEvent.mCustomerNumber, number)) val number = numberOf(customerId) val otherNumber = numberOf(otherCustomerId) - addRows(number) - addRows(otherNumber) - rows(number) should equal((1, 1)) + addEvent(number, bankId) + addEvent(otherNumber, bankId) + events(number, bankId) should equal(1) cascadeDelete(customerId).code should equal(200) - rows(number) should equal((0, 0)) - rows(otherNumber) should equal((1, 1)) + events(number, bankId) should equal(0) + events(otherNumber, bankId) should equal(1) } - scenario("Social media is kept when a customer of another bank has the same number", ApiEndpoint1, VersionOfApi) { + 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 - // The same number at another bank: social media does not record the bank, so the rows cannot be told apart. - MappedCustomer.create.mBank("another-bank").mNumber(number).mCustomerId(APIUtil.generateUUID()).saveMe() - MappedSocialMedia.create.mCustomerNumber(number).mType("twitter").mHandle("@shared").saveMe() - MappedCrmEvent.create.mBankId(bankId).mCustomerNumber(number).mCustomerName("name").mCategory("call") + MappedCrmEvent.create.mBankId("another-bank").mCustomerNumber(number).mCustomerName("name").mCategory("call") .mUserId(resourceUser1).saveMe() cascadeDelete(customerId).code should equal(200) - MappedSocialMedia.count(By(MappedSocialMedia.mCustomerNumber, number)) should equal(1) - MappedCrmEvent.count(By(MappedCrmEvent.mBankId, bankId), By(MappedCrmEvent.mCustomerNumber, number)) should equal(0) + MappedCrmEvent.count(By(MappedCrmEvent.mBankId, "another-bank"), By(MappedCrmEvent.mCustomerNumber, number)) should equal(1) } }