|
1 | 1 | package com.bunq.sdk.context;
|
2 | 2 |
|
3 | 3 | import com.bunq.sdk.exception.BunqException;
|
4 |
| -import com.bunq.sdk.model.core.BunqModel; |
| 4 | +import com.bunq.sdk.model.core.UserContextHelper; |
5 | 5 | import com.bunq.sdk.model.generated.endpoint.MonetaryAccountBank;
|
6 | 6 | import com.bunq.sdk.model.generated.endpoint.User;
|
7 | 7 | import com.bunq.sdk.model.generated.endpoint.UserCompany;
|
8 | 8 | import com.bunq.sdk.model.generated.endpoint.UserPerson;
|
9 | 9 |
|
10 |
| -import java.util.List; |
11 |
| - |
12 | 10 | public class UserContext {
|
13 | 11 |
|
14 | 12 | /**
|
15 | 13 | * Error constants.
|
16 | 14 | */
|
17 | 15 | private static final String ERROR_UNEXPECTED_USER_INSTANCE = "\"%s\" is unexpected user instance.";
|
18 |
| - private static final String ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND = "No active monetary account found."; |
19 |
| - private static final String ERROR_PRIMARY_MONETARY_ACCOUNT_IS_NOT_SET = "Primary monetaryAccount is not set."; |
20 |
| - |
21 |
| - private static final String MONETARY_ACCOUNT_STATUS_ACTIVE = "ACTIVE"; |
22 |
| - private static final int INDEX_FIRST = 0; |
| 16 | + private static final String ERROR_PRIMARY_MONETARY_ACCOUNT_IS_NOT_SET = "Primary monetaryAccount is not set"; |
23 | 17 |
|
| 18 | + private final ApiContext apiContext; |
24 | 19 | private UserCompany userCompany;
|
25 | 20 | private UserPerson userPerson;
|
26 | 21 | private MonetaryAccountBank primaryMonetaryAccountBank;
|
27 |
| - private Integer userId; |
28 | 22 |
|
29 |
| - public UserContext(Integer userId) { |
30 |
| - this.setUser(this.getUserObject()); |
31 |
| - this.userId = userId; |
| 23 | + public UserContext(ApiContext apiContext) { |
| 24 | + this.apiContext = apiContext; |
| 25 | + refreshContext(); |
32 | 26 | }
|
33 | 27 |
|
34 |
| - private BunqModel getUserObject() { |
35 |
| - return User.list().getValue().get(INDEX_FIRST).getReferencedObject(); |
36 |
| - } |
37 |
| - |
38 |
| - private void setUser(BunqModel user) { |
39 |
| - if (user instanceof UserPerson) { |
40 |
| - this.userPerson = (UserPerson) user; |
41 |
| - } else if (user instanceof UserCompany) { |
42 |
| - this.userCompany = (UserCompany) user; |
| 28 | + private void initUser(User user) { |
| 29 | + if (user.getUserPerson() != null) { |
| 30 | + this.userPerson = user.getUserPerson(); |
| 31 | + } else if (user.getUserCompany() != null) { |
| 32 | + this.userCompany = user.getUserCompany(); |
43 | 33 | } else {
|
44 | 34 | throw new BunqException(ERROR_UNEXPECTED_USER_INSTANCE);
|
45 | 35 | }
|
46 | 36 | }
|
47 | 37 |
|
48 |
| - public void initMainMonetaryAccount() { |
49 |
| - List<MonetaryAccountBank> allMonetaryAccount = MonetaryAccountBank.list().getValue(); |
50 |
| - |
51 |
| - for (MonetaryAccountBank monetaryAccountBank : allMonetaryAccount) { |
52 |
| - if (monetaryAccountBank.getStatus().equals(MONETARY_ACCOUNT_STATUS_ACTIVE)) { |
53 |
| - this.primaryMonetaryAccountBank = monetaryAccountBank; |
54 |
| - |
55 |
| - return; |
56 |
| - } |
57 |
| - } |
58 |
| - |
59 |
| - throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND); |
| 38 | + private void initMainMonetaryAccount(MonetaryAccountBank monetaryAccountBank) { |
| 39 | + this.primaryMonetaryAccountBank = monetaryAccountBank; |
60 | 40 | }
|
61 | 41 |
|
62 | 42 | public void refreshContext() {
|
63 |
| - this.setUser(this.getUserObject()); |
64 |
| - this.initMainMonetaryAccount(); |
| 43 | + UserContextHelper helper = new UserContextHelper(this.apiContext); |
| 44 | + this.initUser(helper.getFirstUser()); |
| 45 | + this.initMainMonetaryAccount(helper.getFirstActiveMonetaryAccountBankByUserId(getUserId())); |
65 | 46 | }
|
66 | 47 |
|
67 | 48 | public Integer getUserId() {
|
68 |
| - return this.userId; |
| 49 | + return this.apiContext.getSessionContext().getUserId(); |
69 | 50 | }
|
70 | 51 |
|
71 | 52 | public boolean isOnlyUserPersonSet() {
|
@@ -99,4 +80,5 @@ public UserCompany getUserCompany() {
|
99 | 80 | public MonetaryAccountBank getPrimaryMonetaryAccountBank() {
|
100 | 81 | return this.primaryMonetaryAccountBank;
|
101 | 82 | }
|
| 83 | + |
102 | 84 | }
|
0 commit comments