diff --git a/obp-api/src/main/scala/code/api/util/NewStyle.scala b/obp-api/src/main/scala/code/api/util/NewStyle.scala index a9825b53f..24c9bc80e 100644 --- a/obp-api/src/main/scala/code/api/util/NewStyle.scala +++ b/obp-api/src/main/scala/code/api/util/NewStyle.scala @@ -450,7 +450,8 @@ object NewStyle extends MdcLoggable{ (unboxFullOrFail(i._1, callContext, s"$BankAccountNotFound Current BankId is $bankId and Current AccountNumber is $accountNumber", 404), i._2) } - // this is for the bankAccount from external, maybe can not get any account, so we will create a in-memory account for payments. + // This method handles external bank accounts that may not exist in our database. + // If the account is not found, we create an in-memory account using counterparty information for payment processing. def getOtherBankAccountByNumber(bankId : Option[BankId], accountNumber : String, counterparty: Option[CounterpartyTrait], callContext: Option[CallContext]) : OBPReturnType[(BankAccount)] = { Connector.connector.vend.getOtherBankAccountByNumber(bankId, accountNumber, counterparty, callContext) } map { i => (unboxFullOrFail(i._1, callContext, s"$BankAccountNotFound Current BankId is $bankId and Current AccountNumber is $accountNumber", 404), i._2) diff --git a/obp-api/src/main/scala/code/bankconnectors/LocalMappedConnector.scala b/obp-api/src/main/scala/code/bankconnectors/LocalMappedConnector.scala index 05ed44ead..0bfe96e38 100644 --- a/obp-api/src/main/scala/code/bankconnectors/LocalMappedConnector.scala +++ b/obp-api/src/main/scala/code/bankconnectors/LocalMappedConnector.scala @@ -1035,61 +1035,66 @@ object LocalMappedConnector extends Connector with MdcLoggable { } } - // this is for the bankAccount from external, maybe can not get any account, so we will create a in-memory account for payments. - override def getOtherBankAccountByNumber(bankId : Option[BankId], accountNumber : String, counterparty: Option[CounterpartyTrait], callContext: Option[CallContext]) : OBPReturnType[Box[(BankAccount)]] = - Future { - // First try to get the account using existing method - val existingAccount = getBankAccountByNumber(bankId, accountNumber, callContext) - - Await.result(existingAccount, 5.seconds)._1 match { + // This method handles external bank accounts that may not exist in our database. + // If the account is not found, we create an in-memory account using counterparty information for payment processing. + override def getOtherBankAccountByNumber(bankId : Option[BankId], accountNumber : String, counterparty: Option[CounterpartyTrait], callContext: Option[CallContext]) : OBPReturnType[Box[(BankAccount)]] = { + + for { + (existingAccountBox, updatedCallContext) <- getBankAccountByNumber(bankId, accountNumber, callContext) + (finalAccountBox, finalCallContext) <- existingAccountBox match { case Full(account) => - // If account found, return it - (Full(account), callContext) + // If account found in database, return it + Future.successful((Full(account), updatedCallContext)) case _ => - // If account not found, create in-memory account from counterparty if available + // If account not found, check if we can create in-memory account counterparty match { case Some(cp) => - // Create in-memory account similar to BankingData.getBankAccountFromCounterparty - val accountRouting1 = - if (cp.otherAccountRoutingScheme.isEmpty) Nil - else List(AccountRouting(cp.otherAccountRoutingScheme, cp.otherAccountRoutingAddress)) - val accountRouting2 = - if (cp.otherAccountSecondaryRoutingScheme.isEmpty) Nil - else List(AccountRouting(cp.otherAccountSecondaryRoutingScheme, cp.otherAccountSecondaryRoutingAddress)) + // Create in-memory account using counterparty information + Future { + val accountRouting1 = + if (cp.otherAccountRoutingScheme.isEmpty) Nil + else List(AccountRouting(cp.otherAccountRoutingScheme, cp.otherAccountRoutingAddress)) + val accountRouting2 = + if (cp.otherAccountSecondaryRoutingScheme.isEmpty) Nil + else List(AccountRouting(cp.otherAccountSecondaryRoutingScheme, cp.otherAccountSecondaryRoutingAddress)) - // Due to the new field in the database, old counterparty have void currency, so by default, we set it to EUR - val counterpartyCurrency = if (cp.currency.nonEmpty) cp.currency else "EUR" + // Due to the new field in the database, old counterparty have void currency, so by default, we set it to EUR + val counterpartyCurrency = if (cp.currency.nonEmpty) cp.currency else "EUR" - val inMemoryAccount = BankAccountCommons( - AccountId(if (cp.otherAccountSecondaryRoutingAddress.nonEmpty) cp.otherAccountSecondaryRoutingAddress else accountNumber), - "", 0, - currency = counterpartyCurrency, - name = cp.name, - "", accountNumber, - BankId(cp.otherBankRoutingAddress), - new Date(), "", - accountRoutings = accountRouting1 ++ accountRouting2, - List.empty, - accountHolder = cp.name, - Some(List(Attribute( - name = "BANK_ROUTING_SCHEME", - `type` = "STRING", - value = cp.otherBankRoutingScheme - ), - Attribute( - name = "BANK_ROUTING_ADDRESS", + val inMemoryAccount = BankAccountCommons( + AccountId(if (cp.otherAccountSecondaryRoutingAddress.nonEmpty) cp.otherAccountSecondaryRoutingAddress else accountNumber), + "", 0, + currency = counterpartyCurrency, + name = cp.name, + "", accountNumber, + BankId(cp.otherBankRoutingAddress), + new Date(), "", + accountRoutings = accountRouting1 ++ accountRouting2, + List.empty, + accountHolder = cp.name, + Some(List(Attribute( + name = "BANK_ROUTING_SCHEME", `type` = "STRING", - value = cp.otherBankRoutingAddress + value = cp.otherBankRoutingScheme ), - )) - ) - (Full(inMemoryAccount), callContext) + Attribute( + name = "BANK_ROUTING_ADDRESS", + `type` = "STRING", + value = cp.otherBankRoutingAddress + ), + )) + ) + (Full(inMemoryAccount), updatedCallContext) + } case None => // No counterparty provided, return failure - (Failure(s"$InvalidAccountNumber, current AccountNumber is $accountNumber and no counterparty provided for creating in-memory account"), callContext) + Future.successful((Failure(s"$InvalidAccountNumber, current AccountNumber is $accountNumber and no counterparty provided for creating in-memory account"), updatedCallContext)) } } + } yield { + (finalAccountBox, finalCallContext) } + } override def getBankAccountByRoutings( bankAccountRoutings: BankAccountRoutings, diff --git a/obp-api/src/main/scala/code/model/BankingData.scala b/obp-api/src/main/scala/code/model/BankingData.scala index 89d770d97..1976fb38a 100644 --- a/obp-api/src/main/scala/code/model/BankingData.scala +++ b/obp-api/src/main/scala/code/model/BankingData.scala @@ -476,9 +476,10 @@ object BankAccountX { else if (counterparty.otherAccountRoutingScheme.equalsIgnoreCase("ACCOUNT_NUMBER")|| counterparty.otherAccountRoutingScheme.equalsIgnoreCase("ACCOUNT_NO")){ for{ bankIdOption <- Future.successful(if(counterparty.otherBankRoutingAddress.isEmpty) None else Some(counterparty.otherBankRoutingAddress)) - (account, callContext) <- NewStyle.function.getBankAccountByNumber( + (account, callContext) <- NewStyle.function.getOtherBankAccountByNumber( bankIdOption.map(BankId(_)), counterparty.otherAccountRoutingAddress, + Some(counterparty), callContext) } yield { (Full(account), callContext)