From 706a822e004b45cbed1c42af16d11a66f378d411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Mon, 11 Sep 2023 09:59:45 +0200 Subject: [PATCH 01/13] refactor/Enable request timeout at endpoint get call context v2.2.0 --- .../scala/code/api/v2_2_0/APIMethods220.scala | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/obp-api/src/main/scala/code/api/v2_2_0/APIMethods220.scala b/obp-api/src/main/scala/code/api/v2_2_0/APIMethods220.scala index f3edb060a0..3a53ada56f 100644 --- a/obp-api/src/main/scala/code/api/v2_2_0/APIMethods220.scala +++ b/obp-api/src/main/scala/code/api/v2_2_0/APIMethods220.scala @@ -1,11 +1,13 @@ package code.api.v2_2_0 import java.util.Date + import code.api.ResourceDocs1_4_0.SwaggerDefinitionsJSON._ import code.api.util.APIUtil._ import code.api.util.ApiRole.{canCreateBranch, _} import code.api.util.ApiTag._ import code.api.util.ErrorMessages.{BankAccountNotFound, _} +import code.api.util.FutureUtil.EndpointContext import code.api.util.NewStyle.HttpCode import code.api.util.{ErrorMessages, _} import code.api.v1_2_1.{CreateViewJsonV121, UpdateViewJsonV121} @@ -97,7 +99,7 @@ trait APIMethods220 { lazy val getViewsForBankAccount : OBPEndpoint = { //get the available views on an bank account case "banks" :: BankId(bankId) :: "accounts" :: AccountId(accountId) :: "views" :: Nil JsonGet _ => { - cc => + cc => implicit val ec = EndpointContext(Some(cc)) for { (Full(u), callContext) <- authenticatedAccess(cc) (account, callContext) <- NewStyle.function.checkBankAccountExists(bankId, accountId, callContext) @@ -278,7 +280,7 @@ trait APIMethods220 { lazy val getCurrentFxRate: OBPEndpoint = { case "banks" :: BankId(bankId) :: "fx" :: fromCurrencyCode :: toCurrencyCode :: Nil JsonGet _ => { - cc => + cc => implicit val ec = EndpointContext(Some(cc)) for { (_, callContext) <- getCurrentFxRateIsPublic match { case false => authenticatedAccess(cc) @@ -326,7 +328,7 @@ trait APIMethods220 { lazy val getExplictCounterpartiesForAccount : OBPEndpoint = { case "banks" :: BankId(bankId) :: "accounts" :: AccountId(accountId) :: ViewId(viewId) :: "counterparties" :: Nil JsonGet req => { - cc => + cc => implicit val ec = EndpointContext(Some(cc)) for { (Full(u), callContext) <- authenticatedAccess(cc) (account, callContext) <- NewStyle.function.checkBankAccountExists(bankId, accountId, callContext) @@ -376,7 +378,7 @@ trait APIMethods220 { lazy val getExplictCounterpartyById : OBPEndpoint = { case "banks" :: BankId(bankId) :: "accounts" :: AccountId(accountId) :: ViewId(viewId) :: "counterparties" :: CounterpartyId(counterpartyId) :: Nil JsonGet req => { - cc => + cc => implicit val ec = EndpointContext(Some(cc)) for { (Full(u), callContext) <- authenticatedAccess(cc) (account, callContext) <- NewStyle.function.checkBankAccountExists(bankId, accountId, callContext) @@ -416,6 +418,7 @@ trait APIMethods220 { lazy val getMessageDocs: OBPEndpoint = { case "message-docs" :: connector :: Nil JsonGet _ => { cc => { + implicit val ec = EndpointContext(Some(cc)) for { connectorObject <- Future(tryo{Connector.getConnectorInstance(connector)}) map { i => val msg = "$InvalidConnector Current Input is $connector. It should be eg: kafka_vSept2018..." @@ -774,6 +777,7 @@ trait APIMethods220 { // Create a new account case "banks" :: BankId(bankId) :: "accounts" :: AccountId(accountId) :: Nil JsonPut json -> _ => { cc =>{ + implicit val ec = EndpointContext(Some(cc)) for { (Full(u), callContext) <- authenticatedAccess(cc) failMsg = s"$InvalidJsonFormat The Json body should be the $CreateAccountJSONV220 " @@ -865,7 +869,7 @@ trait APIMethods220 { lazy val config: OBPEndpoint = { case "config" :: Nil JsonGet _ => - cc => + cc => implicit val ec = EndpointContext(Some(cc)) for { (Full(u), callContext) <- authenticatedAccess(cc) _ <- NewStyle.function.hasEntitlement("", u.userId, ApiRole.canGetConfig, callContext) @@ -923,7 +927,7 @@ trait APIMethods220 { lazy val getConnectorMetrics : OBPEndpoint = { case "management" :: "connector" :: "metrics" :: Nil JsonGet _ => { - cc => + cc => implicit val ec = EndpointContext(Some(cc)) for { (Full(u), callContext) <- authenticatedAccess(cc) _ <- NewStyle.function.hasEntitlement("", u.userId, ApiRole.canGetConnectorMetrics, callContext) @@ -1113,7 +1117,7 @@ trait APIMethods220 { lazy val createCounterparty: OBPEndpoint = { case "banks" :: BankId(bankId) :: "accounts" :: AccountId(accountId) :: ViewId(viewId) :: "counterparties" :: Nil JsonPost json -> _ => { - cc => + cc => implicit val ec = EndpointContext(Some(cc)) for { (Full(u), callContext) <- authenticatedAccess(cc) _ <- Helper.booleanToFuture(InvalidAccountIdFormat, cc=callContext) {isValidID(accountId.value)} From 5d97b8b3888bfd633f0ac6f493b37d044d45083b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Mon, 11 Sep 2023 11:26:10 +0200 Subject: [PATCH 02/13] feature/Set long_endpoint_timeout default value from 60s to 55s --- obp-api/src/main/resources/props/sample.props.template | 2 +- obp-api/src/main/scala/code/api/constant/constant.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/obp-api/src/main/resources/props/sample.props.template b/obp-api/src/main/resources/props/sample.props.template index 8cf7158c98..aa362e9f42 100644 --- a/obp-api/src/main/resources/props/sample.props.template +++ b/obp-api/src/main/resources/props/sample.props.template @@ -106,7 +106,7 @@ read_authentication_type_validation_requires_role=false ## Define endpoint timeouts in miliseconds short_endpoint_timeout = 1000 medium_endpoint_timeout = 7000 -long_endpoint_timeout = 60000 +long_endpoint_timeout = 55000 ##Added Props property_name_prefix, default is OBP_. This adds the prefix only for the system environment property name, eg: db.driver --> OBP_db.driver #system_environment_property_name_prefix=OBP_ diff --git a/obp-api/src/main/scala/code/api/constant/constant.scala b/obp-api/src/main/scala/code/api/constant/constant.scala index b0707099f5..388862de36 100644 --- a/obp-api/src/main/scala/code/api/constant/constant.scala +++ b/obp-api/src/main/scala/code/api/constant/constant.scala @@ -16,7 +16,7 @@ object Constant extends MdcLoggable { final val shortEndpointTimeoutInMillis = APIUtil.getPropsAsLongValue(nameOfProperty = "short_endpoint_timeout", 1L * 1000L) final val mediumEndpointTimeoutInMillis = APIUtil.getPropsAsLongValue(nameOfProperty = "medium_endpoint_timeout", 7L * 1000L) - final val longEndpointTimeoutInMillis = APIUtil.getPropsAsLongValue(nameOfProperty = "long_endpoint_timeout", 60L * 1000L) + final val longEndpointTimeoutInMillis = APIUtil.getPropsAsLongValue(nameOfProperty = "long_endpoint_timeout", 55L * 1000L) final val h2DatabaseDefaultUrlValue = "jdbc:h2:mem:OBPTest_H2_v2.1.214;NON_KEYWORDS=VALUE;DB_CLOSE_DELAY=10" From 78fcd7c3f54be350e2693318fceb2fe616938964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Mon, 11 Sep 2023 16:34:12 +0200 Subject: [PATCH 03/13] refactor/Rewrite createFx v2.2.0 as a new style endpoint --- .../scala/code/api/util/ErrorMessages.scala | 3 ++ .../main/scala/code/api/util/NewStyle.scala | 14 ++++++++ .../scala/code/api/v2_2_0/APIMethods220.scala | 35 ++++++++++++------- 3 files changed, 40 insertions(+), 12 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 1ffd3ce887..003c69d7bb 100644 --- a/obp-api/src/main/scala/code/api/util/ErrorMessages.scala +++ b/obp-api/src/main/scala/code/api/util/ErrorMessages.scala @@ -130,6 +130,9 @@ object ErrorMessages { val ScaMethodNotDefined = "OBP-10030: Strong customer authentication method is not defined at this instance." + val createFxCurrencyIssue = "OBP-10050: Cannot create FX currency. " + + // Authentication / Authorisation / User messages (OBP-20XXX) 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 4c2f5fff82..aca5152ef4 100644 --- a/obp-api/src/main/scala/code/api/util/NewStyle.scala +++ b/obp-api/src/main/scala/code/api/util/NewStyle.scala @@ -2222,6 +2222,20 @@ object NewStyle extends MdcLoggable{ unboxFullOrFail(_, callContext, FXCurrencyCodeCombinationsNotSupported) } + def createOrUpdateFXRate(bankId: String, + fromCurrencyCode: String, + toCurrencyCode: String, + conversionValue: Double, + inverseConversionValue: Double, + effectiveDate: Date, + callContext: Option[CallContext] + ): Future[FXRate] = + Future( + Connector.connector.vend.createOrUpdateFXRate(bankId, fromCurrencyCode, toCurrencyCode, conversionValue, inverseConversionValue, effectiveDate) + ) map { + unboxFullOrFail(_, callContext, createFxCurrencyIssue) + } + def createMeeting( bankId: BankId, staffUser: User, diff --git a/obp-api/src/main/scala/code/api/v2_2_0/APIMethods220.scala b/obp-api/src/main/scala/code/api/v2_2_0/APIMethods220.scala index 3a53ada56f..f1c1f6d57b 100644 --- a/obp-api/src/main/scala/code/api/v2_2_0/APIMethods220.scala +++ b/obp-api/src/main/scala/code/api/v2_2_0/APIMethods220.scala @@ -697,7 +697,7 @@ trait APIMethods220 { UserHasMissingRoles, UnknownError ), - List(apiTagFx, apiTagOldStyle), + List(apiTagFx), Some(List(canCreateFxRate, canCreateFxRateAtAnyBank)) ) @@ -705,25 +705,36 @@ trait APIMethods220 { lazy val createFx: OBPEndpoint = { case "banks" :: BankId(bankId) :: "fx" :: Nil JsonPut json -> _ => { - cc => + cc => implicit val ec = EndpointContext(Some(cc)) for { - u <- cc.user ?~!ErrorMessages.UserNotLoggedIn - (bank, callContext) <- BankX(bankId, Some(cc)) ?~! BankNotFound - _ <- NewStyle.function.hasAllEntitlements(bank.bankId.value, u.userId, createFxEntitlementsRequiredForSpecificBank, createFxEntitlementsRequiredForAnyBank, callContext) - fx <- tryo {json.extract[FXRateJsonV220]} ?~! ErrorMessages.InvalidJsonFormat - _ <- booleanToBox(APIUtil.isValidCurrencyISOCode(fx.from_currency_code),InvalidISOCurrencyCode+s"Current from_currency_code is ${fx.from_currency_code}") - _ <- booleanToBox(APIUtil.isValidCurrencyISOCode(fx.to_currency_code),InvalidISOCurrencyCode+s"Current to_currency_code is ${fx.to_currency_code}") - success <- Connector.connector.vend.createOrUpdateFXRate( + (Full(u), callContext) <- authenticatedAccess(cc) + (bank, callContext) <- NewStyle.function.getBank(bankId, callContext) + _ <- Future { + NewStyle.function.hasAllEntitlements( + bank.bankId.value, + u.userId, + createFxEntitlementsRequiredForSpecificBank, + createFxEntitlementsRequiredForAnyBank, + callContext + ) + } + fx <- NewStyle.function.tryons(ErrorMessages.InvalidJsonFormat, 400, callContext) { + json.extract[FXRateJsonV220] + } + _ <- NewStyle.function.isValidCurrencyISOCode(fx.from_currency_code, callContext) + _ <- NewStyle.function.isValidCurrencyISOCode(fx.to_currency_code, callContext) + fxRate <- NewStyle.function.createOrUpdateFXRate( bankId = fx.bank_id, fromCurrencyCode = fx.from_currency_code, toCurrencyCode = fx.to_currency_code, conversionValue = fx.conversion_value, inverseConversionValue = fx.inverse_conversion_value, - effectiveDate = fx.effective_date + effectiveDate = fx.effective_date, + callContext ) } yield { - val json = JSONFactory220.createFXRateJSON(success) - createdJsonResponse(Extraction.decompose(json)) + val viewJSON = JSONFactory220.createFXRateJSON(fxRate) + (viewJSON, HttpCode.`200`(callContext)) } } } From 4091c2741581a486e095cfe2388608960faa38e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Tue, 12 Sep 2023 09:04:18 +0200 Subject: [PATCH 04/13] test/Fix in case of createFx v2.2.0 --- obp-api/src/main/scala/code/api/v2_2_0/APIMethods220.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/obp-api/src/main/scala/code/api/v2_2_0/APIMethods220.scala b/obp-api/src/main/scala/code/api/v2_2_0/APIMethods220.scala index f1c1f6d57b..835027f6e2 100644 --- a/obp-api/src/main/scala/code/api/v2_2_0/APIMethods220.scala +++ b/obp-api/src/main/scala/code/api/v2_2_0/APIMethods220.scala @@ -734,7 +734,7 @@ trait APIMethods220 { ) } yield { val viewJSON = JSONFactory220.createFXRateJSON(fxRate) - (viewJSON, HttpCode.`200`(callContext)) + (viewJSON, HttpCode.`201`(callContext)) } } } From 49550f1e6fd2ca49f8caaa5b3b05598c98de3d34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Tue, 12 Sep 2023 10:26:21 +0200 Subject: [PATCH 05/13] refactor/Rewrite createUser v2.0.0 as a new style endpoint --- .../scala/code/api/v2_0_0/APIMethods200.scala | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/obp-api/src/main/scala/code/api/v2_0_0/APIMethods200.scala b/obp-api/src/main/scala/code/api/v2_0_0/APIMethods200.scala index dd4244e192..6998bf45b2 100644 --- a/obp-api/src/main/scala/code/api/v2_0_0/APIMethods200.scala +++ b/obp-api/src/main/scala/code/api/v2_0_0/APIMethods200.scala @@ -1532,41 +1532,43 @@ trait APIMethods200 { createUserJson, userJsonV200, List(UserNotLoggedIn, InvalidJsonFormat, InvalidStrongPasswordFormat ,"Error occurred during user creation.", "User with the same username already exists." , UnknownError), - List(apiTagUser, apiTagOnboarding, apiTagOldStyle)) + List(apiTagUser, apiTagOnboarding)) lazy val createUser: OBPEndpoint = { case "users" :: Nil JsonPost json -> _ => { - cc => + cc => implicit val ec = EndpointContext(Some(cc)) for { - postedData <- tryo {json.extract[CreateUserJson]} ?~! ErrorMessages.InvalidJsonFormat - _ <- tryo(assert(fullPasswordValidation(postedData.password))) ?~! ErrorMessages.InvalidStrongPasswordFormat - } yield { - if (AuthUser.find(By(AuthUser.username, postedData.username)).isEmpty) { - val userCreated = AuthUser.create + postedData <- NewStyle.function.tryons(ErrorMessages.InvalidJsonFormat, 400, cc.callContext) { + json.extract[CreateUserJson] + } + _ <- Helper.booleanToFuture(ErrorMessages.InvalidStrongPasswordFormat, 400, cc.callContext) { + fullPasswordValidation(postedData.password) + } + _ <- Helper.booleanToFuture("User with the same username already exists.", 409, cc.callContext) { + AuthUser.find(By(AuthUser.username, postedData.username)).isEmpty + } + userCreated <- Future { + AuthUser.create .firstName(postedData.first_name) .lastName(postedData.last_name) .username(postedData.username) .email(postedData.email) .password(postedData.password) .validated(APIUtil.getPropsAsBoolValue("user_account_validated", false)) - if(userCreated.validate.size > 0){ - Full(errorJsonResponse(userCreated.validate.map(_.msg).mkString(";"))) - } - else - { - userCreated.saveMe() - if (userCreated.saved_?) { - AuthUser.grantDefaultEntitlementsToAuthUser(userCreated) - val json = JSONFactory200.createUserJSONfromAuthUser(userCreated) - successJsonResponse(Extraction.decompose(json), 201) - } - else - Full(errorJsonResponse("Error occurred during user creation.")) - } } - else { - Full(errorJsonResponse("User with the same username already exists.", 409)) + _ <- Helper.booleanToFuture(userCreated.validate.map(_.msg).mkString(";"), 400, cc.callContext) { + userCreated.validate.size == 0 + } + savedUser <- NewStyle.function.tryons(ErrorMessages.InvalidJsonFormat, 400, cc.callContext) { + userCreated.saveMe() } + _ <- Helper.booleanToFuture("Error occurred during user creation.", 400, cc.callContext) { + userCreated.saved_? + } + } yield { + AuthUser.grantDefaultEntitlementsToAuthUser(savedUser) + val json = JSONFactory200.createUserJSONfromAuthUser(userCreated) + (json, HttpCode.`201`(cc.callContext)) } } } From 5155ace7e4173d488e1eab17599c87360151a17c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Tue, 12 Sep 2023 10:29:07 +0200 Subject: [PATCH 06/13] docfix/Remove tag old style at endpoint getTransactionTypes v2.0.0 --- obp-api/src/main/scala/code/api/v2_0_0/APIMethods200.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/obp-api/src/main/scala/code/api/v2_0_0/APIMethods200.scala b/obp-api/src/main/scala/code/api/v2_0_0/APIMethods200.scala index 6998bf45b2..a18932b71d 100644 --- a/obp-api/src/main/scala/code/api/v2_0_0/APIMethods200.scala +++ b/obp-api/src/main/scala/code/api/v2_0_0/APIMethods200.scala @@ -1232,7 +1232,7 @@ trait APIMethods200 { emptyObjectJson, transactionTypesJsonV200, List(BankNotFound, UnknownError), - List(apiTagBank, apiTagPSD2AIS, apiTagPsd2, apiTagOldStyle) + List(apiTagBank, apiTagPSD2AIS, apiTagPsd2) ) lazy val getTransactionTypes : OBPEndpoint = { From 532dbc4042a1ffd93f55489c8c23859bb9bbe041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Tue, 12 Sep 2023 11:09:35 +0200 Subject: [PATCH 07/13] refactor/Rewrite getCustomersMessages v1.4.0 as a new style endpoint --- .../main/scala/code/api/v1_4_0/APIMethods140.scala | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/obp-api/src/main/scala/code/api/v1_4_0/APIMethods140.scala b/obp-api/src/main/scala/code/api/v1_4_0/APIMethods140.scala index 179aa159f4..2e238f3442 100644 --- a/obp-api/src/main/scala/code/api/v1_4_0/APIMethods140.scala +++ b/obp-api/src/main/scala/code/api/v1_4_0/APIMethods140.scala @@ -110,20 +110,21 @@ trait APIMethods140 extends MdcLoggable with APIMethods130 with APIMethods121{ emptyObjectJson, customerMessagesJson, List(UserNotLoggedIn, UnknownError), - List(apiTagMessage, apiTagCustomer, apiTagOldStyle)) + List(apiTagMessage, apiTagCustomer)) lazy val getCustomersMessages : OBPEndpoint = { case "banks" :: BankId(bankId) :: "customer" :: "messages" :: Nil JsonGet _ => { - cc =>{ + cc => { + implicit val ec = EndpointContext(Some(cc)) for { - u <- cc.user ?~! ErrorMessages.UserNotLoggedIn - (bank, callContext ) <- BankX(bankId, Some(cc)) ?~! {ErrorMessages.BankNotFound} + (Full(u), callContext) <- authenticatedAccess(cc) + (_, callContext) <- NewStyle.function.getBank(bankId, callContext) //au <- ResourceUser.find(By(ResourceUser.id, u.apiId)) //role <- au.isCustomerMessageAdmin ~> APIFailure("User does not have sufficient permissions", 401) } yield { val messages = CustomerMessages.customerMessageProvider.vend.getMessages(u, bankId) val json = JSONFactory1_4_0.createCustomerMessagesJson(messages) - successJsonResponse(Extraction.decompose(json)) + (json, HttpCode.`200`(callContext)) } } } From 8c8169ac410a8c79fba7d8fc56a9873e1d614cd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Tue, 12 Sep 2023 17:02:53 +0200 Subject: [PATCH 08/13] refactor/Rewrite updateCounterpartyCorporateLocation v1.2.1 as a new style endpoint --- .../scala/code/api/v1_2_1/APIMethods121.scala | 36 ++++++++++++------- .../scala/code/api/v1_2_1/API1_2_1Test.scala | 4 +-- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala b/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala index 22c2d30cbf..a81cecaa76 100644 --- a/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala +++ b/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala @@ -2151,26 +2151,36 @@ trait APIMethods121 { "Coordinates not possible", "Corporate Location cannot be updated", UnknownError), - List(apiTagCounterpartyMetaData, apiTagCounterparty, apiTagOldStyle)) + List(apiTagCounterpartyMetaData, apiTagCounterparty)) lazy val updateCounterpartyCorporateLocation : OBPEndpoint = { //update corporate location of other bank account case "banks" :: BankId(bankId) :: "accounts" :: AccountId(accountId) :: ViewId(viewId) :: "other_accounts":: other_account_id :: "metadata" :: "corporate_location" :: Nil JsonPut json -> _ => { - cc => + cc => implicit val ec = EndpointContext(Some(cc)) for { - u <- cc.user ?~ UserNotLoggedIn - account <- BankAccountX(bankId, accountId) ?~! BankAccountNotFound - view <- APIUtil.checkViewAccessAndReturnView(viewId, BankIdAccountId(account.bankId, account.accountId), cc.user, None) - otherBankAccount <- account.moderatedOtherBankAccount(other_account_id, view, BankIdAccountId(account.bankId, account.accountId), cc.user, Some(cc)) - metadata <- Box(otherBankAccount.metadata) ?~ { s"$NoViewPermission can_see_other_account_metadata. Current ViewId($viewId)" } - addCorpLocation <- Box(metadata.addCorporateLocation) ?~ {"the view " + viewId + "does not allow updating a corporate location"} - corpLocationJson <- tryo{(json.extract[CorporateLocationJSON])} ?~ {InvalidJsonFormat} - correctCoordinates <- checkIfLocationPossible(corpLocationJson.corporate_location.latitude, corpLocationJson.corporate_location.longitude) - updated <- Counterparties.counterparties.vend.addCorporateLocation(other_account_id, u.userPrimaryKey, (now:TimeSpan), corpLocationJson.corporate_location.longitude, corpLocationJson.corporate_location.latitude) ?~ {"Corporate Location cannot be updated"} + (Full(u), callContext) <- authenticatedAccess(cc) + (_, callContext) <- NewStyle.function.getBank(bankId, callContext) + (account, callContext) <- NewStyle.function.checkBankAccountExists(bankId, accountId, callContext) + view <- NewStyle.function.checkViewAccessAndReturnView(viewId, BankIdAccountId(bankId, accountId), Some(u), callContext) + otherBankAccount <- NewStyle.function.moderatedOtherBankAccount(account, other_account_id, view, Full(u), callContext) + _ <- Helper.booleanToFuture(failMsg = s"$NoViewPermission can_see_other_account_metadata. Current ViewId($viewId)", cc=callContext) { + otherBankAccount.metadata.isDefined + } + _ <- Helper.booleanToFuture(failMsg = "the view " + viewId + "does not allow updating a corporate location", cc=callContext) { + otherBankAccount.metadata.get.addCorporateLocation.isDefined + } + corpLocationJson <- NewStyle.function.tryons(failMsg = InvalidJsonFormat, 400, callContext) { + json.extract[CorporateLocationJSON] + } + _ <- Helper.booleanToFuture(failMsg = "Coordinates not possible", 400, callContext) { + checkIfLocationPossible(corpLocationJson.corporate_location.latitude, corpLocationJson.corporate_location.longitude).isDefined + } + (updated, _) <- Future(Counterparties.counterparties.vend.addCorporateLocation(other_account_id, u.userPrimaryKey, (now:TimeSpan), corpLocationJson.corporate_location.longitude, corpLocationJson.corporate_location.latitude)) map { i => + (unboxFullOrFail(i, callContext, "Corporate Location cannot be updated", 400), i) + } if(updated) } yield { - val successJson = SuccessMessage("corporate location updated") - successJsonResponse(Extraction.decompose(successJson)) + (SuccessMessage("corporate location updated"), HttpCode.`200`(callContext)) } } } diff --git a/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala b/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala index 17359a7d69..a226e1226e 100644 --- a/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala +++ b/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala @@ -3985,8 +3985,8 @@ class API1_2_1Test extends ServerSetupWithTestData with DefaultUsers with Privat val randomLoc = randomLocation When("the request is sent") val putReply = updateCorporateLocationForOneCounterparty(bankId, bankAccount.id, view, otherBankAccount.id, randomLoc, user3) - Then("we should get a 400 code") - putReply.code should equal (400) + Then("we should get a 403 code") + putReply.code should equal (403) And("we should get an error message") putReply.body.extract[ErrorMessage].message.nonEmpty should equal (true) } From 780bc90195b211e03744e9cef787d846cf0030c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Wed, 13 Sep 2023 09:24:18 +0200 Subject: [PATCH 09/13] refactor/Rewrite updateCounterpartyCorporatePhysicalLocation v1.2.1 as a new style endpoint --- .../scala/code/api/v1_2_1/APIMethods121.scala | 37 ++++++++++++------- .../scala/code/api/v1_2_1/API1_2_1Test.scala | 4 +- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala b/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala index a81cecaa76..f4f0c8fe6c 100644 --- a/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala +++ b/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala @@ -2297,27 +2297,36 @@ trait APIMethods121 { "Coordinates not possible", "Physical Location cannot be updated", UnknownError), - List(apiTagCounterpartyMetaData, apiTagCounterparty, apiTagOldStyle)) + List(apiTagCounterpartyMetaData, apiTagCounterparty)) lazy val updateCounterpartyPhysicalLocation : OBPEndpoint = { //update physical location to other bank account case "banks" :: BankId(bankId) :: "accounts" :: AccountId(accountId) :: ViewId(viewId) :: "other_accounts":: other_account_id :: "metadata" :: "physical_location" :: Nil JsonPut json -> _ => { - cc => + cc => implicit val ec = EndpointContext(Some(cc)) for { - u <- cc.user ?~ UserNotLoggedIn - account <- BankAccountX(bankId, accountId) ?~! BankAccountNotFound - view <- APIUtil.checkViewAccessAndReturnView(viewId, BankIdAccountId(account.bankId, account.accountId), cc.user, None) - otherBankAccount <- account.moderatedOtherBankAccount(other_account_id, view, BankIdAccountId(account.bankId, account.accountId), cc.user, Some(cc)) - metadata <- Box(otherBankAccount.metadata) ?~ { s"$NoViewPermission can_see_other_account_metadata. Current ViewId($viewId)" } - addPhysicalLocation <- Box(metadata.addPhysicalLocation) ?~ {"the view " + viewId + "does not allow updating a physical location"} - physicalLocationJson <- tryo{(json.extract[PhysicalLocationJSON])} ?~ {InvalidJsonFormat} - correctCoordinates <- checkIfLocationPossible(physicalLocationJson.physical_location.latitude, physicalLocationJson.physical_location.longitude) - correctCoordinates <- checkIfLocationPossible(physicalLocationJson.physical_location.latitude, physicalLocationJson.physical_location.longitude) - updated <- Counterparties.counterparties.vend.addPhysicalLocation(other_account_id, u.userPrimaryKey, (now:TimeSpan), physicalLocationJson.physical_location.longitude, physicalLocationJson.physical_location.latitude) ?~ {"Physical Location cannot be updated"} + (Full(u), callContext) <- authenticatedAccess(cc) + (_, callContext) <- NewStyle.function.getBank(bankId, callContext) + (account, callContext) <- NewStyle.function.checkBankAccountExists(bankId, accountId, callContext) + view <- NewStyle.function.checkViewAccessAndReturnView(viewId, BankIdAccountId(bankId, accountId), Some(u), callContext) + otherBankAccount <- NewStyle.function.moderatedOtherBankAccount(account, other_account_id, view, Full(u), callContext) + _ <- Helper.booleanToFuture(failMsg = s"$NoViewPermission can_see_other_account_metadata. Current ViewId($viewId)", cc=callContext) { + otherBankAccount.metadata.isDefined + } + _ <- Helper.booleanToFuture(failMsg = "the view " + viewId + "does not allow updating a physical location", cc=callContext) { + otherBankAccount.metadata.get.addPhysicalLocation.isDefined + } + physicalLocationJson <- NewStyle.function.tryons(failMsg = InvalidJsonFormat, 400, callContext) { + json.extract[PhysicalLocationJSON] + } + _ <- Helper.booleanToFuture(failMsg = "Coordinates not possible", 400, callContext) { + checkIfLocationPossible(physicalLocationJson.physical_location.latitude, physicalLocationJson.physical_location.longitude).isDefined + } + (updated, _) <- Future(Counterparties.counterparties.vend.addPhysicalLocation(other_account_id, u.userPrimaryKey, (now:TimeSpan), physicalLocationJson.physical_location.longitude, physicalLocationJson.physical_location.latitude)) map { i => + (unboxFullOrFail(i, callContext, "Physical Location cannot be updated", 400), i) + } if(updated) } yield { - val successJson = SuccessMessage("physical location updated") - successJsonResponse(Extraction.decompose(successJson)) + (SuccessMessage("physical location updated"), HttpCode.`200`(callContext)) } } } diff --git a/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala b/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala index a226e1226e..d8cc4e08b5 100644 --- a/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala +++ b/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala @@ -4223,8 +4223,8 @@ class API1_2_1Test extends ServerSetupWithTestData with DefaultUsers with Privat val randomLoc = randomLocation When("the request is sent") val putReply = updatePhysicalLocationForOneCounterparty(bankId, bankAccount.id, view, otherBankAccount.id, randomLoc, user3) - Then("we should get a 400 code") - putReply.code should equal (400) + Then("we should get a 403 code") + putReply.code should equal (403) And("we should get an error message") putReply.body.extract[ErrorMessage].message.nonEmpty should equal (true) } From 14fd125a0488e4032088f2734d8bf3ffde8516b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Wed, 13 Sep 2023 10:05:36 +0200 Subject: [PATCH 10/13] refactor/Rewrite addCounterpartyCorporateLocation v1.2.1 as a new style endpoint --- .../scala/code/api/v1_2_1/APIMethods121.scala | 38 ++++++++++++------- .../scala/code/api/v1_2_1/API1_2_1Test.scala | 8 ++-- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala b/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala index f4f0c8fe6c..a32622c03c 100644 --- a/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala +++ b/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala @@ -2108,26 +2108,38 @@ trait APIMethods121 { "Coordinates not possible", "Corporate Location cannot be deleted", UnknownError), - List(apiTagCounterpartyMetaData, apiTagCounterparty, apiTagOldStyle)) + List(apiTagCounterpartyMetaData, apiTagCounterparty)) lazy val addCounterpartyCorporateLocation : OBPEndpoint = { //add corporate location to other bank account case "banks" :: BankId(bankId) :: "accounts" :: AccountId(accountId) :: ViewId(viewId) :: "other_accounts" :: other_account_id :: "metadata" :: "corporate_location" :: Nil JsonPost json -> _ => { - cc => + cc => implicit val ec = EndpointContext(Some(cc)) for { - u <- cc.user ?~ UserNotLoggedIn - account <- BankAccountX(bankId, accountId) ?~! BankAccountNotFound - view <- APIUtil.checkViewAccessAndReturnView(viewId, BankIdAccountId(account.bankId, account.accountId), cc.user, None) - otherBankAccount <- account.moderatedOtherBankAccount(other_account_id, view, BankIdAccountId(account.bankId, account.accountId), cc.user, Some(cc)) - metadata <- Box(otherBankAccount.metadata) ?~ { s"$NoViewPermission can_see_other_account_metadata. Current ViewId($viewId)" } - addCorpLocation <- Box(metadata.addCorporateLocation) ?~ {"the view " + viewId + "does not allow adding a corporate location"} - corpLocationJson <- tryo{(json.extract[CorporateLocationJSON])} ?~ {InvalidJsonFormat} - correctCoordinates <- checkIfLocationPossible(corpLocationJson.corporate_location.latitude, corpLocationJson.corporate_location.longitude) - added <- Counterparties.counterparties.vend.addCorporateLocation(other_account_id, u.userPrimaryKey, (now:TimeSpan), corpLocationJson.corporate_location.longitude, corpLocationJson.corporate_location.latitude) ?~ {"Corporate Location cannot be deleted"} + (Full(u), callContext) <- authenticatedAccess(cc) + (_, callContext) <- NewStyle.function.getBank(bankId, callContext) + (account, callContext) <- NewStyle.function.checkBankAccountExists(bankId, accountId, callContext) + view <- NewStyle.function.checkViewAccessAndReturnView(viewId, BankIdAccountId(bankId, accountId), Some(u), callContext) + otherBankAccount <- NewStyle.function.moderatedOtherBankAccount(account, other_account_id, view, Full(u), callContext) + _ <- Helper.booleanToFuture(failMsg = s"$NoViewPermission can_see_other_account_metadata. Current ViewId($viewId)", cc=callContext) { + otherBankAccount.metadata.isDefined + } + _ <- Helper.booleanToFuture(failMsg = "the view " + viewId + "does not allow adding a corporate location", cc=callContext) { + otherBankAccount.metadata.get.addCorporateLocation.isDefined + } + corpLocationJson <- NewStyle.function.tryons(failMsg = InvalidJsonFormat, 400, callContext) { + json.extract[CorporateLocationJSON] + } + _ <- Helper.booleanToFuture(failMsg = "Coordinates not possible", 400, callContext) { + checkIfLocationPossible(corpLocationJson.corporate_location.latitude, corpLocationJson.corporate_location.longitude).isDefined + } + (added, _) <- Future( + Counterparties.counterparties.vend.addCorporateLocation(other_account_id, u.userPrimaryKey, (now:TimeSpan), corpLocationJson.corporate_location.longitude, corpLocationJson.corporate_location.latitude) + ) map { i => + (unboxFullOrFail(i, callContext, "Corporate Location cannot be added", 400), i) + } if(added) } yield { - val successJson = SuccessMessage("corporate location added") - successJsonResponse(Extraction.decompose(successJson), 201) + (SuccessMessage("corporate location added"), HttpCode.`201`(callContext)) } } } diff --git a/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala b/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala index d8cc4e08b5..564a43f69b 100644 --- a/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala +++ b/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala @@ -3891,8 +3891,8 @@ class API1_2_1Test extends ServerSetupWithTestData with DefaultUsers with Privat val randomLoc = randomLocation When("the request is sent") val postReply = postCorporateLocationForOneCounterparty(bankId, bankAccount.id, view, otherBankAccount.id, randomLoc, user3) - Then("we should get a 400 code") - postReply.code should equal (400) + Then("we should get a 403 code") + postReply.code should equal (403) And("we should get an error message") postReply.body.extract[ErrorMessage].message.nonEmpty should equal (true) } @@ -3906,8 +3906,8 @@ class API1_2_1Test extends ServerSetupWithTestData with DefaultUsers with Privat val randomLoc = randomLocation When("the request is sent") val postReply = postCorporateLocationForOneCounterparty(bankId, bankAccount.id, randomString(5), otherBankAccount.id, randomLoc, user1) - Then("we should get a 400 code") - postReply.code should equal (400) + Then("we should get a 403 code") + postReply.code should equal (403) And("we should get an error message") postReply.body.extract[ErrorMessage].message.nonEmpty should equal (true) } From 54a098d03c4f02ce02230b448d289b71fc92a08e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Wed, 13 Sep 2023 10:15:51 +0200 Subject: [PATCH 11/13] refactor/Rewrite addCounterpartyPhysicalLocation v1.2.1 as a new style endpoint --- .../scala/code/api/v1_2_1/APIMethods121.scala | 39 ++++++++++++------- .../scala/code/api/v1_2_1/API1_2_1Test.scala | 8 ++-- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala b/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala index a32622c03c..e2319db2cd 100644 --- a/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala +++ b/obp-api/src/main/scala/code/api/v1_2_1/APIMethods121.scala @@ -2265,27 +2265,38 @@ trait APIMethods121 { "Coordinates not possible", "Physical Location cannot be added", UnknownError), - List(apiTagCounterpartyMetaData, apiTagCounterparty, apiTagOldStyle)) + List(apiTagCounterpartyMetaData, apiTagCounterparty)) lazy val addCounterpartyPhysicalLocation : OBPEndpoint = { //add physical location to other bank account case "banks" :: BankId(bankId) :: "accounts" :: AccountId(accountId) :: ViewId(viewId) :: "other_accounts" :: other_account_id :: "metadata" :: "physical_location" :: Nil JsonPost json -> _ => { - cc => + cc => implicit val ec = EndpointContext(Some(cc)) for { - u <- cc.user ?~ UserNotLoggedIn - account <- BankAccountX(bankId, accountId) ?~! BankAccountNotFound - view <- APIUtil.checkViewAccessAndReturnView(viewId, BankIdAccountId(account.bankId, account.accountId), cc.user, None) - otherBankAccount <- account.moderatedOtherBankAccount(other_account_id, view, BankIdAccountId(account.bankId, account.accountId), cc.user, Some(cc)) - metadata <- Box(otherBankAccount.metadata) ?~ { s"$NoViewPermission can_see_other_account_metadata. Current ViewId($viewId)" } - addPhysicalLocation <- Box(metadata.addPhysicalLocation) ?~ {"the view " + viewId + "does not allow adding a physical location"} - physicalLocationJson <- tryo{(json.extract[PhysicalLocationJSON])} ?~ {InvalidJsonFormat} - correctCoordinates <- checkIfLocationPossible(physicalLocationJson.physical_location.latitude, physicalLocationJson.physical_location.longitude) - correctCoordinates <- checkIfLocationPossible(physicalLocationJson.physical_location.latitude, physicalLocationJson.physical_location.longitude) - added <- Counterparties.counterparties.vend.addPhysicalLocation(other_account_id, u.userPrimaryKey, (now:TimeSpan), physicalLocationJson.physical_location.longitude, physicalLocationJson.physical_location.latitude) ?~ {"Physical Location cannot be added"} + (Full(u), callContext) <- authenticatedAccess(cc) + (_, callContext) <- NewStyle.function.getBank(bankId, callContext) + (account, callContext) <- NewStyle.function.checkBankAccountExists(bankId, accountId, callContext) + view <- NewStyle.function.checkViewAccessAndReturnView(viewId, BankIdAccountId(bankId, accountId), Some(u), callContext) + otherBankAccount <- NewStyle.function.moderatedOtherBankAccount(account, other_account_id, view, Full(u), callContext) + _ <- Helper.booleanToFuture(failMsg = s"$NoViewPermission can_see_other_account_metadata. Current ViewId($viewId)", cc=callContext) { + otherBankAccount.metadata.isDefined + } + _ <- Helper.booleanToFuture(failMsg = "the view " + viewId + "does not allow adding a physical location", cc=callContext) { + otherBankAccount.metadata.get.addPhysicalLocation.isDefined + } + physicalLocationJson <- NewStyle.function.tryons(failMsg = InvalidJsonFormat, 400, callContext) { + json.extract[PhysicalLocationJSON] + } + _ <- Helper.booleanToFuture(failMsg = "Coordinates not possible", 400, callContext) { + checkIfLocationPossible(physicalLocationJson.physical_location.latitude, physicalLocationJson.physical_location.longitude).isDefined + } + (added, _) <- Future( + Counterparties.counterparties.vend.addPhysicalLocation(other_account_id, u.userPrimaryKey, (now:TimeSpan), physicalLocationJson.physical_location.longitude, physicalLocationJson.physical_location.latitude) + ) map { i => + (unboxFullOrFail(i, callContext, "Physical Location cannot be added", 400), i) + } if(added) } yield { - val successJson = SuccessMessage("physical location added") - successJsonResponse(Extraction.decompose(successJson), 201) + (SuccessMessage("physical location added"), HttpCode.`201`(callContext)) } } } diff --git a/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala b/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala index 564a43f69b..b8c5f72011 100644 --- a/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala +++ b/obp-api/src/test/scala/code/api/v1_2_1/API1_2_1Test.scala @@ -4129,8 +4129,8 @@ class API1_2_1Test extends ServerSetupWithTestData with DefaultUsers with Privat val randomLoc = randomLocation When("the request is sent") val postReply = postPhysicalLocationForOneCounterparty(bankId, bankAccount.id, view, otherBankAccount.id, randomLoc, user3) - Then("we should get a 400 code") - postReply.code should equal (400) + Then("we should get a 403 code") + postReply.code should equal (403) And("we should get an error message") postReply.body.extract[ErrorMessage].message.nonEmpty should equal (true) } @@ -4144,8 +4144,8 @@ class API1_2_1Test extends ServerSetupWithTestData with DefaultUsers with Privat val randomLoc = randomLocation When("the request is sent") val postReply = postPhysicalLocationForOneCounterparty(bankId, bankAccount.id, randomString(5), otherBankAccount.id, randomLoc, user1) - Then("we should get a 400 code") - postReply.code should equal (400) + Then("we should get a 403 code") + postReply.code should equal (403) And("we should get an error message") postReply.body.extract[ErrorMessage].message.nonEmpty should equal (true) } From 0e69fdcca1460f638864d6d2f83508c41bd95111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Wed, 13 Sep 2023 11:33:17 +0200 Subject: [PATCH 12/13] refactor/Rewrite createBranch v3.0.0 as a new style endpoint --- .../scala/code/api/v3_0_0/APIMethods300.scala | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/obp-api/src/main/scala/code/api/v3_0_0/APIMethods300.scala b/obp-api/src/main/scala/code/api/v3_0_0/APIMethods300.scala index f0269ccdab..8e3f7ace5d 100644 --- a/obp-api/src/main/scala/code/api/v3_0_0/APIMethods300.scala +++ b/obp-api/src/main/scala/code/api/v3_0_0/APIMethods300.scala @@ -1084,24 +1084,32 @@ trait APIMethods300 { InsufficientAuthorisationToCreateBranch, UnknownError ), - List(apiTagBranch, apiTagOldStyle), + List(apiTagBranch), Some(List(canCreateBranch, canCreateBranchAtAnyBank)) ) lazy val createBranch: OBPEndpoint = { case "banks" :: BankId(bankId) :: "branches" :: Nil JsonPost json -> _ => { - cc => + cc => implicit val ec = EndpointContext(Some(cc)) for { - u <- cc.user ?~!ErrorMessages.UserNotLoggedIn - (bank, _) <- BankX(bankId, Some(cc)) ?~! BankNotFound - _ <- NewStyle.function.hasAllEntitlements(bank.bankId.value, u.userId, canCreateBranch::Nil, canCreateBranchAtAnyBank::Nil, cc.callContext) - branchJsonV300 <- tryo {json.extract[BranchJsonV300]} ?~! {ErrorMessages.InvalidJsonFormat + " BranchJsonV300"} - _ <- booleanToBox(branchJsonV300.bank_id == bank.bankId.value, "BANK_ID has to be the same in the URL and Body") - branch <- transformToBranchFromV300(branchJsonV300) ?~! {ErrorMessages.CouldNotTransformJsonToInternalModel + " Branch"} - success: BranchT <- Connector.connector.vend.createOrUpdateBranch(branch) ?~! {ErrorMessages.CountNotSaveOrUpdateResource + " Branch"} + (Full(u), callContext) <- authenticatedAccess(cc) + (bank, callContext) <- NewStyle.function.getBank(bankId, callContext) + _ <- Future( + NewStyle.function.hasAllEntitlements(bank.bankId.value, u.userId, canCreateBranch::Nil, canCreateBranchAtAnyBank::Nil, cc.callContext) + ) + branchJsonV300 <- NewStyle.function.tryons(failMsg = InvalidJsonFormat + " BranchJsonV300", 400, callContext) { + json.extract[BranchJsonV300] + } + _ <- Helper.booleanToFuture(failMsg = "BANK_ID has to be the same in the URL and Body", 400, callContext) { + branchJsonV300.bank_id == bank.bankId.value + } + branch <- NewStyle.function.tryons(CouldNotTransformJsonToInternalModel + " Branch", 400, cc.callContext) { + transformToBranch(branchJsonV300) + } + success: BranchT <- NewStyle.function.createOrUpdateBranch(branch, callContext) } yield { val json = JSONFactory300.createBranchJsonV300(success) - createdJsonResponse(Extraction.decompose(json), 201) + (json, HttpCode.`201`(callContext)) } } } From 09298193f91903f0b8bd2f9e4ed0687106ec215f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Wed, 13 Sep 2023 13:51:55 +0200 Subject: [PATCH 13/13] refactor/Rewrite createBranch v3.0.0 as a new style endpoint - 2 --- .../main/scala/code/api/util/NewStyle.scala | 8 ++++++ .../code/api/v3_0_0/JSONFactory3.0.0.scala | 25 ++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) 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 aca5152ef4..a0724db145 100644 --- a/obp-api/src/main/scala/code/api/util/NewStyle.scala +++ b/obp-api/src/main/scala/code/api/util/NewStyle.scala @@ -134,6 +134,14 @@ object NewStyle extends MdcLoggable{ x => fullBoxOrException(x ~> APIFailureNewStyle(BranchNotFoundByBranchId, 400, callContext.map(_.toLight))) } map { unboxFull(_) } } + + def createOrUpdateBranch(branch: BranchT, callContext: Option[CallContext]): Future[BranchT] = { + Future { + Connector.connector.vend.createOrUpdateBranch(branch) + } map { + unboxFullOrFail(_, callContext, ErrorMessages.CountNotSaveOrUpdateResource + " Branch", 400) + } + } /** * delete a branch, just set isDeleted field to true, marks it is deleted diff --git a/obp-api/src/main/scala/code/api/v3_0_0/JSONFactory3.0.0.scala b/obp-api/src/main/scala/code/api/v3_0_0/JSONFactory3.0.0.scala index 330543c9fa..20cefeceb8 100644 --- a/obp-api/src/main/scala/code/api/v3_0_0/JSONFactory3.0.0.scala +++ b/obp-api/src/main/scala/code/api/v3_0_0/JSONFactory3.0.0.scala @@ -1134,11 +1134,23 @@ object JSONFactory300{ // This goes FROM JSON TO internal representation of a Branch def transformToBranchFromV300(branchJsonV300: BranchJsonV300): Box[Branch] = { + + val branch: Branch = transforToBranchCommon(branchJsonV300) + Full(branch) + } + // This goes FROM JSON TO internal representation of a Branch + def transformToBranch(branchJsonV300: BranchJsonV300): Branch = { + + val branch: Branch = transforToBranchCommon(branchJsonV300) - val address : Address = transformToAddressFromV300(branchJsonV300.address) // Note the address in V220 is V140 - val location: Location = transformToLocationFromV140(branchJsonV300.location) // Note the location is V140 - val meta: Meta = transformToMetaFromV140(branchJsonV300.meta) // Note the meta is V140 + branch + } + + private def transforToBranchCommon(branchJsonV300: BranchJsonV300) = { + val address: Address = transformToAddressFromV300(branchJsonV300.address) // Note the address in V220 is V140 + val location: Location = transformToLocationFromV140(branchJsonV300.location) // Note the location is V140 + val meta: Meta = transformToMetaFromV140(branchJsonV300.meta) // Note the meta is V140 val lobby: Lobby = Lobby( @@ -1177,13 +1189,9 @@ object JSONFactory300{ ) - - val branchRouting = Some(Routing(branchJsonV300.branch_routing.scheme, branchJsonV300.branch_routing.address)) - - val isAccessible: Boolean = Try(branchJsonV300.is_accessible.toBoolean).getOrElse(false) @@ -1207,8 +1215,7 @@ object JSONFactory300{ phoneNumber = Some(branchJsonV300.phone_number), isDeleted = Some(false) ) - - Full(branch) + branch } def createUserJSON(user : User, entitlements: List[Entitlement]) : UserJsonV200 = {