Skip to content

Commit 448aec2

Browse files
committed
refactor/update getOtherBankAccountByNumber method to improve handling of external bank accounts and in-memory account creation
1 parent 417fcd1 commit 448aec2

3 files changed

Lines changed: 53 additions & 46 deletions

File tree

‎obp-api/src/main/scala/code/api/util/NewStyle.scala‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,8 @@ object NewStyle extends MdcLoggable{
450450
(unboxFullOrFail(i._1, callContext, s"$BankAccountNotFound Current BankId is $bankId and Current AccountNumber is $accountNumber", 404), i._2)
451451
}
452452

453-
// this is for the bankAccount from external, maybe can not get any account, so we will create a in-memory account for payments.
453+
// This method handles external bank accounts that may not exist in our database.
454+
// If the account is not found, we create an in-memory account using counterparty information for payment processing.
454455
def getOtherBankAccountByNumber(bankId : Option[BankId], accountNumber : String, counterparty: Option[CounterpartyTrait], callContext: Option[CallContext]) : OBPReturnType[(BankAccount)] = {
455456
Connector.connector.vend.getOtherBankAccountByNumber(bankId, accountNumber, counterparty, callContext) } map { i =>
456457
(unboxFullOrFail(i._1, callContext, s"$BankAccountNotFound Current BankId is $bankId and Current AccountNumber is $accountNumber", 404), i._2)

‎obp-api/src/main/scala/code/bankconnectors/LocalMappedConnector.scala‎

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,61 +1035,66 @@ object LocalMappedConnector extends Connector with MdcLoggable {
10351035
}
10361036
}
10371037

1038-
// this is for the bankAccount from external, maybe can not get any account, so we will create a in-memory account for payments.
1039-
override def getOtherBankAccountByNumber(bankId : Option[BankId], accountNumber : String, counterparty: Option[CounterpartyTrait], callContext: Option[CallContext]) : OBPReturnType[Box[(BankAccount)]] =
1040-
Future {
1041-
// First try to get the account using existing method
1042-
val existingAccount = getBankAccountByNumber(bankId, accountNumber, callContext)
1043-
1044-
Await.result(existingAccount, 5.seconds)._1 match {
1038+
// This method handles external bank accounts that may not exist in our database.
1039+
// If the account is not found, we create an in-memory account using counterparty information for payment processing.
1040+
override def getOtherBankAccountByNumber(bankId : Option[BankId], accountNumber : String, counterparty: Option[CounterpartyTrait], callContext: Option[CallContext]) : OBPReturnType[Box[(BankAccount)]] = {
1041+
1042+
for {
1043+
(existingAccountBox, updatedCallContext) <- getBankAccountByNumber(bankId, accountNumber, callContext)
1044+
(finalAccountBox, finalCallContext) <- existingAccountBox match {
10451045
case Full(account) =>
1046-
// If account found, return it
1047-
(Full(account), callContext)
1046+
// If account found in database, return it
1047+
Future.successful((Full(account), updatedCallContext))
10481048
case _ =>
1049-
// If account not found, create in-memory account from counterparty if available
1049+
// If account not found, check if we can create in-memory account
10501050
counterparty match {
10511051
case Some(cp) =>
1052-
// Create in-memory account similar to BankingData.getBankAccountFromCounterparty
1053-
val accountRouting1 =
1054-
if (cp.otherAccountRoutingScheme.isEmpty) Nil
1055-
else List(AccountRouting(cp.otherAccountRoutingScheme, cp.otherAccountRoutingAddress))
1056-
val accountRouting2 =
1057-
if (cp.otherAccountSecondaryRoutingScheme.isEmpty) Nil
1058-
else List(AccountRouting(cp.otherAccountSecondaryRoutingScheme, cp.otherAccountSecondaryRoutingAddress))
1059-
1060-
// Due to the new field in the database, old counterparty have void currency, so by default, we set it to EUR
1061-
val counterpartyCurrency = if (cp.currency.nonEmpty) cp.currency else "EUR"
1062-
1063-
val inMemoryAccount = BankAccountCommons(
1064-
AccountId(if (cp.otherAccountSecondaryRoutingAddress.nonEmpty) cp.otherAccountSecondaryRoutingAddress else accountNumber),
1065-
"", 0,
1066-
currency = counterpartyCurrency,
1067-
name = cp.name,
1068-
"", accountNumber,
1069-
BankId(cp.otherBankRoutingAddress),
1070-
new Date(), "",
1071-
accountRoutings = accountRouting1 ++ accountRouting2,
1072-
List.empty,
1073-
accountHolder = cp.name,
1074-
Some(List(Attribute(
1075-
name = "BANK_ROUTING_SCHEME",
1076-
`type` = "STRING",
1077-
value = cp.otherBankRoutingScheme
1078-
),
1079-
Attribute(
1080-
name = "BANK_ROUTING_ADDRESS",
1052+
// Create in-memory account using counterparty information
1053+
Future {
1054+
val accountRouting1 =
1055+
if (cp.otherAccountRoutingScheme.isEmpty) Nil
1056+
else List(AccountRouting(cp.otherAccountRoutingScheme, cp.otherAccountRoutingAddress))
1057+
val accountRouting2 =
1058+
if (cp.otherAccountSecondaryRoutingScheme.isEmpty) Nil
1059+
else List(AccountRouting(cp.otherAccountSecondaryRoutingScheme, cp.otherAccountSecondaryRoutingAddress))
1060+
1061+
// Due to the new field in the database, old counterparty have void currency, so by default, we set it to EUR
1062+
val counterpartyCurrency = if (cp.currency.nonEmpty) cp.currency else "EUR"
1063+
1064+
val inMemoryAccount = BankAccountCommons(
1065+
AccountId(if (cp.otherAccountSecondaryRoutingAddress.nonEmpty) cp.otherAccountSecondaryRoutingAddress else accountNumber),
1066+
"", 0,
1067+
currency = counterpartyCurrency,
1068+
name = cp.name,
1069+
"", accountNumber,
1070+
BankId(cp.otherBankRoutingAddress),
1071+
new Date(), "",
1072+
accountRoutings = accountRouting1 ++ accountRouting2,
1073+
List.empty,
1074+
accountHolder = cp.name,
1075+
Some(List(Attribute(
1076+
name = "BANK_ROUTING_SCHEME",
10811077
`type` = "STRING",
1082-
value = cp.otherBankRoutingAddress
1078+
value = cp.otherBankRoutingScheme
10831079
),
1084-
))
1085-
)
1086-
(Full(inMemoryAccount), callContext)
1080+
Attribute(
1081+
name = "BANK_ROUTING_ADDRESS",
1082+
`type` = "STRING",
1083+
value = cp.otherBankRoutingAddress
1084+
),
1085+
))
1086+
)
1087+
(Full(inMemoryAccount), updatedCallContext)
1088+
}
10871089
case None =>
10881090
// No counterparty provided, return failure
1089-
(Failure(s"$InvalidAccountNumber, current AccountNumber is $accountNumber and no counterparty provided for creating in-memory account"), callContext)
1091+
Future.successful((Failure(s"$InvalidAccountNumber, current AccountNumber is $accountNumber and no counterparty provided for creating in-memory account"), updatedCallContext))
10901092
}
10911093
}
1094+
} yield {
1095+
(finalAccountBox, finalCallContext)
10921096
}
1097+
}
10931098

10941099
override def getBankAccountByRoutings(
10951100
bankAccountRoutings: BankAccountRoutings,

‎obp-api/src/main/scala/code/model/BankingData.scala‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,10 @@ object BankAccountX {
476476
else if (counterparty.otherAccountRoutingScheme.equalsIgnoreCase("ACCOUNT_NUMBER")|| counterparty.otherAccountRoutingScheme.equalsIgnoreCase("ACCOUNT_NO")){
477477
for{
478478
bankIdOption <- Future.successful(if(counterparty.otherBankRoutingAddress.isEmpty) None else Some(counterparty.otherBankRoutingAddress))
479-
(account, callContext) <- NewStyle.function.getBankAccountByNumber(
479+
(account, callContext) <- NewStyle.function.getOtherBankAccountByNumber(
480480
bankIdOption.map(BankId(_)),
481481
counterparty.otherAccountRoutingAddress,
482+
Some(counterparty),
482483
callContext)
483484
} yield {
484485
(Full(account), callContext)

0 commit comments

Comments
 (0)