From ff68d7b7e6c5bc04ec7672a8c076a6ad209668a6 Mon Sep 17 00:00:00 2001 From: hongwei Date: Fri, 22 Nov 2024 14:16:14 +0100 Subject: [PATCH] refactor/use AgentAccountLink method --- .../scala/code/api/util/ErrorMessages.scala | 2 + .../main/scala/code/api/util/NewStyle.scala | 19 +++++-- .../scala/code/api/v5_1_0/APIMethods510.scala | 52 +++++++++---------- .../scala/code/bankconnectors/Connector.scala | 10 ++++ .../bankconnectors/LocalMappedConnector.scala | 29 +++++++++++ .../customer/MappedCustomerProvider.scala | 1 + .../MappedCustomerAccountLink.scala | 9 +++- .../commons/model/CommonModel.scala | 9 ++++ .../commons/model/CommonModelTrait.scala | 7 +++ 9 files changed, 106 insertions(+), 32 deletions(-) diff --git a/obp-api/src/main/scala/code/api/util/ErrorMessages.scala b/obp-api/src/main/scala/code/api/util/ErrorMessages.scala index 41ca284f0..0b3cb7873 100644 --- a/obp-api/src/main/scala/code/api/util/ErrorMessages.scala +++ b/obp-api/src/main/scala/code/api/util/ErrorMessages.scala @@ -529,6 +529,8 @@ object ErrorMessages { val AgentAccountLinkNotFound = "OBP-30325: Agent Account Link not found." val AgentsNotFound = "OBP-30326: Agents not found." val CreateAgentAccountLinkError = "OBP-30327: Could not create the agent account link." + val AgentNumberAlreadyExists = "OBP-30328: Agent Number already exists. Please specify a different value for BANK_ID or AGENT_NUMBER." + val GetAgentAccountLinksError = "OBP-30226: Could not get the agent account links." // Branch related messages val BranchesNotFoundLicense = "OBP-32001: No branches available. License may not be set." 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 ae008ba89..3e727c382 100644 --- a/obp-api/src/main/scala/code/api/util/NewStyle.scala +++ b/obp-api/src/main/scala/code/api/util/NewStyle.scala @@ -77,7 +77,7 @@ import code.counterpartylimit.{CounterpartyLimit} import com.openbankproject.commons.model.CounterpartyLimitTrait import code.crm.CrmEvent import code.crm.CrmEvent.CrmEvent -import com.openbankproject.commons.model.CustomerAccountLinkTrait +import com.openbankproject.commons.model.{CustomerAccountLinkTrait, AgentAccountLinkTrait} import code.dynamicMessageDoc.{DynamicMessageDocProvider, JsonDynamicMessageDoc} import code.dynamicResourceDoc.{DynamicResourceDocProvider, JsonDynamicResourceDoc} import code.endpointMapping.{EndpointMappingProvider, EndpointMappingT} @@ -754,13 +754,19 @@ object NewStyle extends MdcLoggable{ i => (unboxFullOrFail(i._1, callContext, s"$InvalidConnectorResponse", 400), i._2) } } + + def checkAgentNumberAvailable(bankId: BankId, agentNumber: String, callContext: Option[CallContext]): OBPReturnType[Boolean] = { + Connector.connector.vend.checkAgentNumberAvailable(bankId: BankId, agentNumber: String, callContext: Option[CallContext]) map { + i => (unboxFullOrFail(i._1, callContext, s"$InvalidConnectorResponse", 400), i._2) + } + } + def getCustomerByCustomerNumber(customerNumber : String, bankId : BankId, callContext: Option[CallContext]): OBPReturnType[Customer] = { Connector.connector.vend.getCustomerByCustomerNumber(customerNumber, bankId, callContext) map { unboxFullOrFail(_, callContext, CustomerNotFound, 404) } } - def getCustomerAddress(customerId : String, callContext: Option[CallContext]): OBPReturnType[List[CustomerAddress]] = { Connector.connector.vend.getCustomerAddress(customerId, callContext) map { i => (connectorEmptyResponse(i._1, callContext), i._2) @@ -4128,8 +4134,8 @@ object NewStyle extends MdcLoggable{ i => (unboxFullOrFail(i._1, callContext, CreateCustomerAccountLinkError), i._2) } - def createAgentAccountLink(agentId: String, bankId: String, accountId: String, relationshipType: String, callContext: Option[CallContext]): OBPReturnType[CustomerAccountLinkTrait] = - Connector.connector.vend.createCustomerAccountLink(agentId: String, bankId, accountId: String, relationshipType: String, callContext: Option[CallContext]) map { + def createAgentAccountLink(agentId: String, bankId: String, accountId: String, callContext: Option[CallContext]): OBPReturnType[AgentAccountLinkTrait] = + Connector.connector.vend.createAgentAccountLink(agentId: String, bankId, accountId: String, callContext: Option[CallContext]) map { i => (unboxFullOrFail(i._1, callContext, CreateAgentAccountLinkError), i._2) } @@ -4138,6 +4144,11 @@ object NewStyle extends MdcLoggable{ i => (unboxFullOrFail(i._1, callContext, GetCustomerAccountLinksError), i._2) } + def getAgentAccountLinksByAgentId(agentId: String, callContext: Option[CallContext]): OBPReturnType[List[CustomerAccountLinkTrait]] = + Connector.connector.vend.getAgentAccountLinksByAgentId(agentId: String, callContext: Option[CallContext]) map { + i => (unboxFullOrFail(i._1, callContext, GetAgentAccountLinksError), i._2) + } + def getCustomerAccountLinksByBankIdAccountId(bankId: String, accountId: String, callContext: Option[CallContext]): OBPReturnType[List[CustomerAccountLinkTrait]] = Connector.connector.vend.getCustomerAccountLinksByBankIdAccountId(bankId, accountId: String, callContext: Option[CallContext]) map { i => (unboxFullOrFail(i._1, callContext, GetCustomerAccountLinksError), i._2) diff --git a/obp-api/src/main/scala/code/api/v5_1_0/APIMethods510.scala b/obp-api/src/main/scala/code/api/v5_1_0/APIMethods510.scala index ee0fff03a..46fa5d219 100644 --- a/obp-api/src/main/scala/code/api/v5_1_0/APIMethods510.scala +++ b/obp-api/src/main/scala/code/api/v5_1_0/APIMethods510.scala @@ -361,10 +361,8 @@ trait APIMethods510 { $UserNotLoggedIn, $BankNotFound, InvalidJsonFormat, - CustomerNumberAlreadyExists, - UserNotFoundById, - CustomerAlreadyExistsForUser, - CreateConsumerError, + AgentNumberAlreadyExists, + CreateAgentError, UnknownError ), List(apiTagCustomer, apiTagPerson) @@ -374,15 +372,16 @@ trait APIMethods510 { case "banks" :: BankId(bankId) :: "agents" :: Nil JsonPost json -> _ => { cc => implicit val ec = EndpointContext(Some(cc)) for { - postedData <- NewStyle.function.tryons(s"$InvalidJsonFormat The Json body should be the $PostAgentJsonV510 ", 400, cc.callContext) { + putData <- NewStyle.function.tryons(s"$InvalidJsonFormat The Json body should be the $PostAgentJsonV510 ", 400, cc.callContext) { json.extract[PostAgentJsonV510] } - (_, callContext) <- NewStyle.function.checkCustomerNumberAvailable(bankId, postedData.agent_number, cc.callContext) + (agentNumberIsAvailable, callContext) <- NewStyle.function.checkAgentNumberAvailable(bankId, putData.agent_number, cc.callContext) + _ <- Helper.booleanToFuture(failMsg= s"$AgentNumberAlreadyExists Current agent_number(${putData.agent_number}) and Current bank_id(${bankId.value})", cc=callContext) {agentNumberIsAvailable} (agent, callContext) <- NewStyle.function.createAgent( bankId = bankId.value, - legalName = postedData.legal_name, - mobileNumber = postedData.mobile_phone_number, - number = postedData.agent_number, + legalName = putData.legal_name, + mobileNumber = putData.mobile_phone_number, + number = putData.agent_number, callContext, ) (bankAccount, callContext) <- NewStyle.function.createBankAccount( @@ -390,14 +389,15 @@ trait APIMethods510 { AccountId(APIUtil.generateUUID()), "AGENT", "AGENT", - postedData.currency, + putData.currency, 0, - postedData.legal_name, + putData.legal_name, null, Nil, callContext ) - (_, callContext) <- NewStyle.function.createCustomerAccountLink(agent.agentId, bankAccount.bankId.value, bankAccount.accountId.value, "Owner", callContext) + (_, callContext) <- NewStyle.function.createAgentAccountLink(agent.agentId, bankAccount.bankId.value, bankAccount.accountId.value, callContext) + } yield { (JSONFactory510.createAgentJson(agent, bankAccount), HttpCode.`201`(callContext)) } @@ -420,10 +420,8 @@ trait APIMethods510 { $UserNotLoggedIn, $BankNotFound, InvalidJsonFormat, - CustomerNumberAlreadyExists, - UserNotFoundById, - CustomerAlreadyExistsForUser, - CreateConsumerError, + AgentNotFound, + AgentAccountLinkNotFound, UnknownError ), List(apiTagCustomer, apiTagPerson), @@ -438,11 +436,11 @@ trait APIMethods510 { json.extract[PutAgentJsonV510] } (agent, callContext) <- NewStyle.function.getAgentByAgentId(agentId, cc.callContext) - (customerAccountLinks, callContext) <- NewStyle.function.getCustomerAccountLinksByCustomerId(agentId, callContext) - customerAccountLink <- NewStyle.function.tryons(AgentAccountLinkNotFound, 400, callContext) { - customerAccountLinks.head + (agentAccountLinks, callContext) <- NewStyle.function.getAgentAccountLinksByAgentId(agentId, callContext) + agentAccountLink <- NewStyle.function.tryons(AgentAccountLinkNotFound, 400, callContext) { + agentAccountLinks.head } - (bankAccount, callContext) <- NewStyle.function.getBankAccount(BankId(customerAccountLink.bankId), AccountId(customerAccountLink.accountId), callContext) + (bankAccount, callContext) <- NewStyle.function.getBankAccount(BankId(agentAccountLink.bankId), AccountId(agentAccountLink.accountId), callContext) (agent, callContext) <- NewStyle.function.updateAgentStatus( agentId, postedData.is_pending_agent, @@ -470,6 +468,8 @@ trait APIMethods510 { List( $UserNotLoggedIn, $BankNotFound, + AgentNotFound, + AgentAccountLinkNotFound, UnknownError ), List(apiTagAccount) @@ -479,13 +479,12 @@ trait APIMethods510 { case "banks" :: BankId(bankId) :: "agents" :: agentId :: Nil JsonGet _ => { cc => implicit val ec = EndpointContext(Some(cc)) for { - (Full(u), callContext) <- SS.user - (agent, callContext) <- NewStyle.function.getAgentByAgentId(agentId, callContext) - (customerAccountLinks, callContext) <- NewStyle.function.getCustomerAccountLinksByCustomerId(agentId, callContext) - customerAccountLink <- NewStyle.function.tryons(AgentAccountLinkNotFound, 400, callContext) { - customerAccountLinks.head + (agent, callContext) <- NewStyle.function.getAgentByAgentId(agentId, cc.callContext) + (agentAccountLinks, callContext) <- NewStyle.function.getAgentAccountLinksByAgentId(agentId, callContext) + agentAccountLink <- NewStyle.function.tryons(AgentAccountLinkNotFound, 400, callContext) { + agentAccountLinks.head } - (bankAccount, callContext) <- NewStyle.function.getBankAccount(BankId(customerAccountLink.bankId), AccountId(customerAccountLink.accountId), callContext) + (bankAccount, callContext) <- NewStyle.function.getBankAccount(BankId(agentAccountLink.bankId), AccountId(agentAccountLink.accountId), callContext) } yield { (JSONFactory510.createAgentJson(agent, bankAccount), HttpCode.`200`(callContext)) } @@ -966,6 +965,7 @@ trait APIMethods510 { minimalAgentsJsonV510, List( $BankNotFound, + AgentsNotFound, UnknownError ), List(apiTagAccount) diff --git a/obp-api/src/main/scala/code/bankconnectors/Connector.scala b/obp-api/src/main/scala/code/bankconnectors/Connector.scala index 57a28978c..a0614e1d7 100644 --- a/obp-api/src/main/scala/code/bankconnectors/Connector.scala +++ b/obp-api/src/main/scala/code/bankconnectors/Connector.scala @@ -1059,6 +1059,12 @@ trait Connector extends MdcLoggable { callContext: Option[CallContext] ): OBPReturnType[Box[Boolean]] = Future{(Failure(setUnimplementedError(nameOf(checkCustomerNumberAvailable _))), callContext)} + def checkAgentNumberAvailable( + bankId: BankId, + agentNumber: String, + callContext: Option[CallContext] + ): OBPReturnType[Box[Boolean]] = Future{(Failure(setUnimplementedError(nameOf(checkAgentNumberAvailable _))), callContext)} + def createCustomer( bankId: BankId, legalName: String, @@ -1793,6 +1799,8 @@ trait Connector extends MdcLoggable { def getCustomerAccountLinksByCustomerId(customerId: String, callContext: Option[CallContext]): OBPReturnType[Box[List[CustomerAccountLinkTrait]]] = Future{(Failure(setUnimplementedError(nameOf(getCustomerAccountLinksByCustomerId _))), callContext)} + def getAgentAccountLinksByAgentId(agnetId: String, callContext: Option[CallContext]): OBPReturnType[Box[List[CustomerAccountLinkTrait]]] = Future{(Failure(setUnimplementedError(nameOf(getCustomerAccountLinksByCustomerId _))), callContext)} + def getCustomerAccountLinksByBankIdAccountId(bankId: String, accountId: String, callContext: Option[CallContext]): OBPReturnType[Box[List[CustomerAccountLinkTrait]]] = Future{(Failure(setUnimplementedError(nameOf(getCustomerAccountLinksByBankIdAccountId _))), callContext)} def getCustomerAccountLinkById(customerAccountLinkId: String, callContext: Option[CallContext]): OBPReturnType[Box[CustomerAccountLinkTrait]] = Future{(Failure(setUnimplementedError(nameOf(getCustomerAccountLinkById _))), callContext)} @@ -1801,6 +1809,8 @@ trait Connector extends MdcLoggable { def createCustomerAccountLink(customerId: String, bankId: String, accountId: String, relationshipType: String, callContext: Option[CallContext]): OBPReturnType[Box[CustomerAccountLinkTrait]] = Future{(Failure(setUnimplementedError(nameOf(createCustomerAccountLink _))), callContext)} + def createAgentAccountLink(agentId: String, bankId: String, accountId: String, callContext: Option[CallContext]): OBPReturnType[Box[AgentAccountLinkTrait]] = Future{(Failure(setUnimplementedError(nameOf(createAgentAccountLink _))), callContext)} + def updateCustomerAccountLinkById(customerAccountLinkId: String, relationshipType: String, callContext: Option[CallContext]): OBPReturnType[Box[CustomerAccountLinkTrait]] = Future{(Failure(setUnimplementedError(nameOf(updateCustomerAccountLinkById _))), callContext)} def getConsentImplicitSCA(user: User, callContext: Option[CallContext]): OBPReturnType[Box[ConsentImplicitSCAT]] = Future{(Failure(setUnimplementedError(nameOf(getConsentImplicitSCA _))), callContext)} diff --git a/obp-api/src/main/scala/code/bankconnectors/LocalMappedConnector.scala b/obp-api/src/main/scala/code/bankconnectors/LocalMappedConnector.scala index c1715d4a9..602086fef 100644 --- a/obp-api/src/main/scala/code/bankconnectors/LocalMappedConnector.scala +++ b/obp-api/src/main/scala/code/bankconnectors/LocalMappedConnector.scala @@ -3074,6 +3074,17 @@ object LocalMappedConnector extends Connector with MdcLoggable { CustomerX.customerProvider.vend.checkCustomerNumberAvailable(bankId, customerNumber) }, callContext) } + + override def checkAgentNumberAvailable( + bankId: BankId, + agentNumber: String, + callContext: Option[CallContext] + ): OBPReturnType[Box[Boolean]] = Future { + //in OBP, customer and agent share the same customer model. the CustomerAccountLink and AgentAccountLink also share the same model + (tryo { + CustomerX.customerProvider.vend.checkCustomerNumberAvailable(bankId, agentNumber) + }, callContext) + } override def createCustomer( @@ -5059,6 +5070,11 @@ object LocalMappedConnector extends Connector with MdcLoggable { (CustomerAccountLinkX.customerAccountLink.vend.getCustomerAccountLinksByCustomerId(customerId),callContext) } + override def getAgentAccountLinksByAgentId(agentId: String, callContext: Option[CallContext]) = Future{ + //in OBP, customer and agent share the same customer model. the CustomerAccountLink and AgentAccountLink also share the same model + (CustomerAccountLinkX.customerAccountLink.vend.getCustomerAccountLinksByCustomerId(agentId),callContext) + } + override def getCustomerAccountLinkById(customerAccountLinkId: String, callContext: Option[CallContext]) = Future{ (CustomerAccountLinkX.customerAccountLink.vend.getCustomerAccountLinkById(customerAccountLinkId),callContext) } @@ -5078,6 +5094,19 @@ object LocalMappedConnector extends Connector with MdcLoggable { CustomerAccountLinkX.customerAccountLink.vend.createCustomerAccountLink(customerId: String, bankId, accountId: String, relationshipType: String) map { ( _, callContext) } } + override def createAgentAccountLink(agentId: String, bankId: String, accountId: String, callContext: Option[CallContext]): OBPReturnType[Box[AgentAccountLinkTrait]] = Future{ + //in OBP, customer and agent share the same customer model. the CustomerAccountLink and AgentAccountLink also share the same model + CustomerAccountLinkX.customerAccountLink.vend.createCustomerAccountLink(agentId: String, bankId, accountId: String, "Owner") map { customer => ( + AgentAccountLinkTraitCommons( + agentAccountLinkId = customer.customerAccountLinkId, + agentId = customer.customerId, + bankId = customer.bankId, + accountId = customer.accountId, + ), + callContext) + } + } + override def getConsentImplicitSCA(user: User, callContext: Option[CallContext]): OBPReturnType[Box[ConsentImplicitSCAT]] = Future { //find the email from the user, and the OBP Implicit SCA is email (Full(ConsentImplicitSCA( diff --git a/obp-api/src/main/scala/code/customer/MappedCustomerProvider.scala b/obp-api/src/main/scala/code/customer/MappedCustomerProvider.scala index 5ce838796..9ef7a8994 100644 --- a/obp-api/src/main/scala/code/customer/MappedCustomerProvider.scala +++ b/obp-api/src/main/scala/code/customer/MappedCustomerProvider.scala @@ -333,6 +333,7 @@ object MappedCustomerProvider extends CustomerProvider with MdcLoggable { } +//in OBP, customer and agent share the same customer model. the CustomerAccountLink and AgentAccountLink also share the same model class MappedCustomer extends Customer with Agent with LongKeyedMapper[MappedCustomer] with IdPK with CreatedUpdated { def getSingleton = MappedCustomer diff --git a/obp-api/src/main/scala/code/customeraccountlinks/MappedCustomerAccountLink.scala b/obp-api/src/main/scala/code/customeraccountlinks/MappedCustomerAccountLink.scala index 9734b87a5..2a3d216a6 100644 --- a/obp-api/src/main/scala/code/customeraccountlinks/MappedCustomerAccountLink.scala +++ b/obp-api/src/main/scala/code/customeraccountlinks/MappedCustomerAccountLink.scala @@ -8,7 +8,7 @@ import net.liftweb.mapper._ import scala.concurrent.Future import com.openbankproject.commons.ExecutionContext.Implicits.global import net.liftweb.util.Helpers.tryo -import com.openbankproject.commons.model.CustomerAccountLinkTrait +import com.openbankproject.commons.model.{CustomerAccountLinkTrait,AgentAccountLinkTrait} object MappedCustomerAccountLinkProvider extends CustomerAccountLinkProvider { override def createCustomerAccountLink(customerId: String, bankId: String, accountId: String, relationshipType: String): Box[CustomerAccountLinkTrait] = { @@ -103,7 +103,8 @@ object MappedCustomerAccountLinkProvider extends CustomerAccountLinkProvider { } } -class CustomerAccountLink extends CustomerAccountLinkTrait with LongKeyedMapper[CustomerAccountLink] with IdPK with CreatedUpdated { +//in OBP, customer and agent share the same customer model. the CustomerAccountLink and AgentAccountLink also share the same model +class CustomerAccountLink extends CustomerAccountLinkTrait with AgentAccountLinkTrait with LongKeyedMapper[CustomerAccountLink] with IdPK with CreatedUpdated { def getSingleton = CustomerAccountLink @@ -118,6 +119,10 @@ class CustomerAccountLink extends CustomerAccountLinkTrait with LongKeyedMapper[ override def bankId: String = BankId.get // id.toString override def accountId: String = AccountId.get override def relationshipType: String = RelationshipType.get + + override def agentId: String = CustomerId.get + override def agentAccountLinkId: String = CustomerAccountLinkId.get + } object CustomerAccountLink extends CustomerAccountLink with LongKeyedMetaMapper[CustomerAccountLink] { diff --git a/obp-commons/src/main/scala/com/openbankproject/commons/model/CommonModel.scala b/obp-commons/src/main/scala/com/openbankproject/commons/model/CommonModel.scala index 14d785f23..4da9c0c0b 100644 --- a/obp-commons/src/main/scala/com/openbankproject/commons/model/CommonModel.scala +++ b/obp-commons/src/main/scala/com/openbankproject/commons/model/CommonModel.scala @@ -604,6 +604,15 @@ case class CustomerAccountLinkTraitCommons( object CustomerAccountLinkTraitCommons extends Converter[CustomerAccountLinkTrait, CustomerAccountLinkTraitCommons] +case class AgentAccountLinkTraitCommons( + agentAccountLinkId: String, + agentId: String, + bankId: String, + accountId: String +) extends AgentAccountLinkTrait + +object AgentAccountLinkTraitCommons extends Converter[AgentAccountLinkTrait, AgentAccountLinkTraitCommons] + case class CounterpartyLimitTraitCommons( counterpartyLimitId: String, diff --git a/obp-commons/src/main/scala/com/openbankproject/commons/model/CommonModelTrait.scala b/obp-commons/src/main/scala/com/openbankproject/commons/model/CommonModelTrait.scala index 3d07072cb..b1e939464 100644 --- a/obp-commons/src/main/scala/com/openbankproject/commons/model/CommonModelTrait.scala +++ b/obp-commons/src/main/scala/com/openbankproject/commons/model/CommonModelTrait.scala @@ -648,6 +648,13 @@ trait CustomerAccountLinkTrait { def relationshipType: String } +trait AgentAccountLinkTrait { + def agentAccountLinkId: String + def agentId: String + def bankId: String + def accountId: String +} + trait CounterpartyLimitTrait extends JsonAble{ def counterpartyLimitId: String def bankId: String