-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynthetic_accounts.go
290 lines (243 loc) · 10.4 KB
/
synthetic_accounts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package rize
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/google/go-querystring/query"
)
// Handles all Synthetic Account operations
type syntheticAccountService service
// SyntheticAccount data type
type SyntheticAccount struct {
UID string `json:"uid,omitempty"`
ExternalUID string `json:"external_uid,omitempty"`
Name string `json:"name,omitempty"`
PoolUID string `json:"pool_uid,omitempty"`
CustomerUID string `json:"customer_uid,omitempty"`
SyntheticAccountTypeUID string `json:"synthetic_account_type_uid,omitempty"`
SyntheticAccountCategory string `json:"synthetic_account_category,omitempty"`
Status string `json:"status,omitempty"`
Liability bool `json:"liability,omitempty"`
NetUSDBalance string `json:"net_usd_balance,omitempty"`
NetUSDPendingBalance string `json:"net_usd_pending_balance,omitempty"`
NetUSDAvailableBalance string `json:"net_usd_available_balance,omitempty"`
AssetBalances []*SyntheticAccountAssetBalance `json:"asset_balances,omitempty"`
MasterAccount bool `json:"master_account,omitempty"`
AccountNumber string `json:"account_number,omitempty"`
AccountNumberLastFour string `json:"account_number_last_four,omitempty"`
RoutingNumber string `json:"routing_number,omitempty"`
OpenedAt time.Time `json:"opened_at,omitempty"`
ClosedAt time.Time `json:"closed_at,omitempty"`
ClosedToSyntheticAccountUID string `json:"closed_to_synthetic_account_uid,omitempty"`
}
// SyntheticAccountAssetBalance provides a list of balances for the various asset types
type SyntheticAccountAssetBalance struct {
AssetQuantity string `json:"asset_quantity,omitempty"`
AssetType string `json:"asset_type,omitempty"`
CurrentUSDValue string `json:"current_usd_value,omitempty"`
CustodialAccountUID string `json:"custodial_account_uid,omitempty"`
CustodialAccountName string `json:"custodial_account_name,omitempty"`
Debit bool `json:"debit,omitempty"`
}
// SyntheticAccountType data type
type SyntheticAccountType struct {
UID string `json:"uid,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
ProgramUID string `json:"program_uid,omitempty"`
SyntheticAccountCategory string `json:"synthetic_account_category,omitempty"`
TargetAnnualYieldPercent float64 `json:"target_annual_yield_percent,omitempty"`
}
// SyntheticAccountListParams builds the query parameters used in querying Synthetic Accounts
type SyntheticAccountListParams struct {
CustomerUID string `url:"customer_uid,omitempty" json:"customer_uid,omitempty"`
ExternalUID string `url:"external_uid,omitempty" json:"external_uid,omitempty"`
PoolUID string `url:"pool_uid,omitempty" json:"pool_uid,omitempty"`
Limit int `url:"limit,omitempty" json:"limit,omitempty"`
Offset int `url:"offset,omitempty" json:"offset,omitempty"`
SyntheticAccountTypeUID string `url:"synthetic_account_type_uid,omitempty" json:"synthetic_account_type_uid,omitempty"`
SyntheticAccountCategory string `url:"synthetic_account_category,omitempty" json:"synthetic_account_category,omitempty"`
Liability bool `url:"liability,omitempty" json:"liability,omitempty"`
Status string `url:"status,omitempty" json:"status,omitempty"`
Sort string `url:"sort,omitempty" json:"sort,omitempty"`
}
// SyntheticAccountCreateParams are the body params used when creating a new Synthetic Account
type SyntheticAccountCreateParams struct {
ExternalUID string `json:"external_uid,omitempty"`
Name string `json:"name"`
PoolUID string `json:"pool_uid"`
SyntheticAccountTypeUID string `json:"synthetic_account_type_uid"`
AccountNumber string `json:"account_number,omitempty"`
RoutingNumber string `json:"routing_number,omitempty"`
PlaidProcessorToken string `json:"plaid_processor_token,omitempty"` // Deprecated
ExternalProcessorToken string `json:"external_processor_token,omitempty"`
}
// SyntheticAccountUpdateParams are the body params used when updating a Synthetic Account
type SyntheticAccountUpdateParams struct {
Name string `json:"name,omitempty"`
Note string `json:"note,omitempty"`
}
// SyntheticAccountTypeListParams builds the query parameters used in querying Synthetic Account Types
type SyntheticAccountTypeListParams struct {
ProgramUID string `url:"program_uid,omitempty" json:"program_uid,omitempty"`
Limit int `url:"limit,omitempty" json:"limit,omitempty"`
Offset int `url:"offset,omitempty" json:"offset,omitempty"`
}
// SyntheticAccountListResponse is an API response containing a list of Synthetic Accounts
type SyntheticAccountListResponse struct {
ListResponse
Data []*SyntheticAccount `json:"data"`
}
// SyntheticAccountTypeListResponse is an API response containing a list of Synthetic Account Types
type SyntheticAccountTypeListResponse struct {
ListResponse
Data []*SyntheticAccountType `json:"data"`
}
// List retrieves a list of Synthetic Account filtered by the given parameters
func (sa *syntheticAccountService) List(ctx context.Context, params *SyntheticAccountListParams) (*SyntheticAccountListResponse, error) {
// Build SyntheticAccountListParams into query string params
v, err := query.Values(params)
if err != nil {
return nil, err
}
res, err := sa.client.doRequest(ctx, http.MethodGet, "synthetic_accounts", v, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &SyntheticAccountListResponse{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Create a new Synthetic Account in the Pool with the provided specification
func (sa *syntheticAccountService) Create(ctx context.Context, params *SyntheticAccountCreateParams) (*SyntheticAccount, error) {
if params.Name == "" || params.PoolUID == "" || params.SyntheticAccountTypeUID == "" {
return nil, fmt.Errorf("properties Name, PoolUID and SyntheticAccountTypeUID are required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := sa.client.doRequest(ctx, http.MethodPost, "synthetic_accounts", nil, bytes.NewBuffer(bytesMessage))
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &SyntheticAccount{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Get returns a single Synthetic Account resource along with supporting details and account balances
func (sa *syntheticAccountService) Get(ctx context.Context, uid string) (*SyntheticAccount, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
res, err := sa.client.doRequest(ctx, http.MethodGet, fmt.Sprintf("synthetic_accounts/%s", uid), nil, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &SyntheticAccount{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Update the Synthetic Account metadata
func (sa *syntheticAccountService) Update(ctx context.Context, uid string, params *SyntheticAccountUpdateParams) (*SyntheticAccount, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := sa.client.doRequest(ctx, http.MethodPut, fmt.Sprintf("synthetic_accounts/%s", uid), nil, bytes.NewBuffer(bytesMessage))
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &SyntheticAccount{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Delete will archive a Synthetic Account
func (sa *syntheticAccountService) Delete(ctx context.Context, uid string) (*http.Response, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
res, err := sa.client.doRequest(ctx, http.MethodDelete, fmt.Sprintf("synthetic_accounts/%s", uid), nil, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
return res, nil
}
// ListAccountTypes retrieves a list of Synthetic Account Types filtered by the given parameters
func (sa *syntheticAccountService) ListAccountTypes(ctx context.Context, params *SyntheticAccountTypeListParams) (*SyntheticAccountTypeListResponse, error) {
// Build SyntheticAccountTypeListParams into query string params
v, err := query.Values(params)
if err != nil {
return nil, err
}
res, err := sa.client.doRequest(ctx, http.MethodGet, "synthetic_account_types", v, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &SyntheticAccountTypeListResponse{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// GetAccountType returns a single Synthetic Account Type resource along with supporting details
func (sa *syntheticAccountService) GetAccountType(ctx context.Context, uid string) (*SyntheticAccountType, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
res, err := sa.client.doRequest(ctx, http.MethodGet, fmt.Sprintf("synthetic_account_types/%s", uid), nil, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &SyntheticAccountType{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}