diff --git a/src/main/java/com/bunq/sdk/context/BunqContext.java b/src/main/java/com/bunq/sdk/context/BunqContext.java index 2f758c42..b6ddbffe 100644 --- a/src/main/java/com/bunq/sdk/context/BunqContext.java +++ b/src/main/java/com/bunq/sdk/context/BunqContext.java @@ -15,8 +15,7 @@ public final class BunqContext { public static void loadApiContext(ApiContext apiContext) { BunqContext.apiContext = apiContext; - BunqContext.userContext = new UserContext(apiContext.getSessionContext().getUserId()); - BunqContext.userContext.initMainMonetaryAccount(); + BunqContext.userContext = new UserContext(apiContext); } public static ApiContext getApiContext() { diff --git a/src/main/java/com/bunq/sdk/context/UserContext.java b/src/main/java/com/bunq/sdk/context/UserContext.java index a5c18777..3f67b0de 100644 --- a/src/main/java/com/bunq/sdk/context/UserContext.java +++ b/src/main/java/com/bunq/sdk/context/UserContext.java @@ -1,71 +1,52 @@ package com.bunq.sdk.context; import com.bunq.sdk.exception.BunqException; -import com.bunq.sdk.model.core.BunqModel; +import com.bunq.sdk.model.core.UserContextHelper; import com.bunq.sdk.model.generated.endpoint.MonetaryAccountBank; import com.bunq.sdk.model.generated.endpoint.User; import com.bunq.sdk.model.generated.endpoint.UserCompany; import com.bunq.sdk.model.generated.endpoint.UserPerson; -import java.util.List; - public class UserContext { /** * Error constants. */ private static final String ERROR_UNEXPECTED_USER_INSTANCE = "\"%s\" is unexpected user instance."; - private static final String ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND = "No active monetary account found."; - private static final String ERROR_PRIMARY_MONETARY_ACCOUNT_IS_NOT_SET = "Primary monetaryAccount is not set."; - - private static final String MONETARY_ACCOUNT_STATUS_ACTIVE = "ACTIVE"; - private static final int INDEX_FIRST = 0; + private static final String ERROR_PRIMARY_MONETARY_ACCOUNT_IS_NOT_SET = "Primary monetaryAccount is not set"; + private final ApiContext apiContext; private UserCompany userCompany; private UserPerson userPerson; private MonetaryAccountBank primaryMonetaryAccountBank; - private Integer userId; - public UserContext(Integer userId) { - this.setUser(this.getUserObject()); - this.userId = userId; + public UserContext(ApiContext apiContext) { + this.apiContext = apiContext; + refreshContext(); } - private BunqModel getUserObject() { - return User.list().getValue().get(INDEX_FIRST).getReferencedObject(); - } - - private void setUser(BunqModel user) { - if (user instanceof UserPerson) { - this.userPerson = (UserPerson) user; - } else if (user instanceof UserCompany) { - this.userCompany = (UserCompany) user; + private void initUser(User user) { + if (user.getUserPerson() != null) { + this.userPerson = user.getUserPerson(); + } else if (user.getUserCompany() != null) { + this.userCompany = user.getUserCompany(); } else { throw new BunqException(ERROR_UNEXPECTED_USER_INSTANCE); } } - public void initMainMonetaryAccount() { - List allMonetaryAccount = MonetaryAccountBank.list().getValue(); - - for (MonetaryAccountBank monetaryAccountBank : allMonetaryAccount) { - if (monetaryAccountBank.getStatus().equals(MONETARY_ACCOUNT_STATUS_ACTIVE)) { - this.primaryMonetaryAccountBank = monetaryAccountBank; - - return; - } - } - - throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND); + private void initMainMonetaryAccount(MonetaryAccountBank monetaryAccountBank) { + this.primaryMonetaryAccountBank = monetaryAccountBank; } public void refreshContext() { - this.setUser(this.getUserObject()); - this.initMainMonetaryAccount(); + UserContextHelper helper = new UserContextHelper(this.apiContext); + this.initUser(helper.getFirstUser()); + this.initMainMonetaryAccount(helper.getFirstActiveMonetaryAccountBankByUserId(getUserId())); } public Integer getUserId() { - return this.userId; + return this.apiContext.getSessionContext().getUserId(); } public boolean isOnlyUserPersonSet() { @@ -99,4 +80,5 @@ public UserCompany getUserCompany() { public MonetaryAccountBank getPrimaryMonetaryAccountBank() { return this.primaryMonetaryAccountBank; } + } diff --git a/src/main/java/com/bunq/sdk/model/core/UserContextHelper.java b/src/main/java/com/bunq/sdk/model/core/UserContextHelper.java new file mode 100644 index 00000000..27360782 --- /dev/null +++ b/src/main/java/com/bunq/sdk/model/core/UserContextHelper.java @@ -0,0 +1,79 @@ +package com.bunq.sdk.model.core; + +import com.bunq.sdk.context.ApiContext; +import com.bunq.sdk.exception.BunqException; +import com.bunq.sdk.http.ApiClient; +import com.bunq.sdk.http.BunqResponse; +import com.bunq.sdk.http.BunqResponseRaw; +import com.bunq.sdk.model.generated.endpoint.MonetaryAccountBank; +import com.bunq.sdk.model.generated.endpoint.User; + +import java.util.List; + +public class UserContextHelper extends BunqModel { + + /** + * Error constants. + */ + private static final String ERROR_NO_ACTIVE_MONETARY_ACCOUNT_BANK = + "No active monetary account found."; + + /** + * Endpoint constants. + */ + private static final String ENDPOINT_USER = "user"; + private static final String ENDPOINT_MONETARY_ACCOUNT_BANK = "user/%s/monetary-account-bank"; + + /** + * The index of the first item in an array. + */ + private static final Integer INDEX_FIRST = 0; + + /** + * Status constants. + */ + private static final String STATUS_ACTIVE = "ACTIVE"; + + private final ApiClient apiClient; + + public UserContextHelper(ApiContext apiContext) { + this.apiClient = new ApiClient(apiContext); + } + + private BunqResponseRaw getRawResponse(String url) { + return this.apiClient.get(url, null, null); + } + + public User getFirstUser() { + BunqResponseRaw responseRaw = getRawResponse(ENDPOINT_USER); + BunqResponse> response = fromJsonList(User.class, responseRaw); + + return response.getValue().get(INDEX_FIRST); + } + + public MonetaryAccountBank getFirstActiveMonetaryAccountBankByUserId(Integer userId) { + BunqResponseRaw responseRaw = getRawResponse( + String.format(ENDPOINT_MONETARY_ACCOUNT_BANK, userId) + ); + String wrapper = MonetaryAccountBank.class.getSimpleName(); + BunqResponse> response = fromJsonList( + MonetaryAccountBank.class, + responseRaw, + wrapper + ); + + for (MonetaryAccountBank monetaryAccountBank : response.getValue()) { + if (STATUS_ACTIVE.equals(monetaryAccountBank.getStatus())) { + return monetaryAccountBank; + } + } + + throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_BANK); + } + + @Override + public boolean isAllFieldNull() { + return true; + } + +} diff --git a/src/test/java/com/bunq/sdk/context/UserContextTest.java b/src/test/java/com/bunq/sdk/context/UserContextTest.java new file mode 100644 index 00000000..9312b79f --- /dev/null +++ b/src/test/java/com/bunq/sdk/context/UserContextTest.java @@ -0,0 +1,20 @@ +package com.bunq.sdk.context; + +import com.bunq.sdk.BunqSdkTestBase; +import org.junit.Test; + +import static org.junit.Assert.assertNotNull; + +public class UserContextTest extends BunqSdkTestBase { + + @Test + public void buildUserContext() { + ApiContext context = getApiContext(); + + UserContext userContext = new UserContext(context); + + assertNotNull(userContext.getUserId()); + assertNotNull(userContext.getMainMonetaryAccountId()); + } + +}