@@ -1026,15 +1026,76 @@ object LocalMappedConnector extends Connector with MdcLoggable {
10261026 else
10271027 s " $AccountNumberNotUniqueError, current BankId is ${bankId.head.value}, AccountNumber is $accountNumber"
10281028
1029- if (bankAccounts.length > 1 ){
1029+ if (bankAccounts.length > 1 ){ // If the account number is not unique, return the error message
10301030 (Failure (errorMessage), callContext)
1031- }else if (bankAccounts.length == 1 ){
1031+ }else if (bankAccounts.length == 1 ){ // If the account number is unique, return the account
10321032 (Full (bankAccounts.head), callContext)
1033- }else {
1034- (Failure (errorMessage ), callContext)
1033+ }else { // If the account number is not found, return the error message
1034+ (Failure (s " $InvalidAccountNumber , current AccountNumber is $accountNumber " ), callContext)
10351035 }
10361036 }
10371037
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 {
1045+ case Full (account) =>
1046+ // If account found in database, return it
1047+ Future .successful((Full (account), updatedCallContext))
1048+ case _ =>
1049+ // If account not found, check if we can create in-memory account
1050+ counterparty match {
1051+ case Some (cp) =>
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" ,
1077+ `type` = " STRING" ,
1078+ value = cp.otherBankRoutingScheme
1079+ ),
1080+ Attribute (
1081+ name = " BANK_ROUTING_ADDRESS" ,
1082+ `type` = " STRING" ,
1083+ value = cp.otherBankRoutingAddress
1084+ ),
1085+ ))
1086+ )
1087+ (Full (inMemoryAccount), updatedCallContext)
1088+ }
1089+ case None =>
1090+ // No counterparty provided, return failure
1091+ Future .successful((Failure (s " $InvalidAccountNumber, current AccountNumber is $accountNumber and no counterparty provided for creating in-memory account " ), updatedCallContext))
1092+ }
1093+ }
1094+ } yield {
1095+ (finalAccountBox, finalCallContext)
1096+ }
1097+ }
1098+
10381099 override def getBankAccountByRoutings (
10391100 bankAccountRoutings : BankAccountRoutings ,
10401101 callContext : Option [CallContext ]
0 commit comments