Skip to content

Commit 417fcd1

Browse files
committed
feature/add getOtherBankAccountByNumber method to handle external bank accounts and create in-memory accounts for payments
1 parent 192603d commit 417fcd1

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,12 @@ 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.
454+
def getOtherBankAccountByNumber(bankId : Option[BankId], accountNumber : String, counterparty: Option[CounterpartyTrait], callContext: Option[CallContext]) : OBPReturnType[(BankAccount)] = {
455+
Connector.connector.vend.getOtherBankAccountByNumber(bankId, accountNumber, counterparty, callContext) } map { i =>
456+
(unboxFullOrFail(i._1, callContext, s"$BankAccountNotFound Current BankId is $bankId and Current AccountNumber is $accountNumber", 404), i._2)
457+
}
458+
453459
def getBankSettlementAccounts(bankId: BankId, callContext: Option[CallContext]): OBPReturnType[List[BankAccount]] = {
454460
Connector.connector.vend.getBankSettlementAccounts(bankId: BankId, callContext: Option[CallContext]) map { i =>
455461
(unboxFullOrFail(i._1, callContext,s"$BankNotFound Current BankId is $bankId", 404 ), i._2)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,10 @@ trait Connector extends MdcLoggable {
530530

531531
def getBankAccountByNumber(bankId : Option[BankId], accountNumber : String, callContext: Option[CallContext]) : OBPReturnType[Box[(BankAccount)]] = Future {(Failure(setUnimplementedError(nameOf(getBankAccountByNumber _))), callContext)}
532532

533+
// This method handles external bank accounts that may not exist in our database.
534+
// If the account is not found, we create an in-memory account using counterparty information for payment processing.
535+
def getOtherBankAccountByNumber(bankId : Option[BankId], accountNumber : String, counterparty: Option[CounterpartyTrait], callContext: Option[CallContext]) : OBPReturnType[Box[(BankAccount)]] = Future {(Failure(setUnimplementedError(nameOf(getOtherBankAccountByNumber _))), callContext)}
536+
533537
def getBankAccountByRoutings(bankAccountRoutings: BankAccountRoutings, callContext: Option[CallContext]) : OBPReturnType[Box[(BankAccount)]] = Future {(Failure(setUnimplementedError(nameOf(getBankAccountByRoutings _))), callContext)}
534538

535539
def getCounterpartyFromTransaction(bankId: BankId, accountId: AccountId, counterpartyId: String, callContext: Option[CallContext]): OBPReturnType[Box[Counterparty]] = Future {(Failure(setUnimplementedError(nameOf(checkBankAccountExists _))), callContext)}

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,62 @@ 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 {
1045+
case Full(account) =>
1046+
// If account found, return it
1047+
(Full(account), callContext)
1048+
case _ =>
1049+
// If account not found, create in-memory account from counterparty if available
1050+
counterparty match {
1051+
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",
1081+
`type` = "STRING",
1082+
value = cp.otherBankRoutingAddress
1083+
),
1084+
))
1085+
)
1086+
(Full(inMemoryAccount), callContext)
1087+
case None =>
1088+
// No counterparty provided, return failure
1089+
(Failure(s"$InvalidAccountNumber, current AccountNumber is $accountNumber and no counterparty provided for creating in-memory account"), callContext)
1090+
}
1091+
}
1092+
}
1093+
10381094
override def getBankAccountByRoutings(
10391095
bankAccountRoutings: BankAccountRoutings,
10401096
callContext: Option[CallContext]

0 commit comments

Comments
 (0)