This repository was archived by the owner on May 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathaccounts.js
More file actions
166 lines (145 loc) · 5.11 KB
/
accounts.js
File metadata and controls
166 lines (145 loc) · 5.11 KB
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
import endpoints from './api/endpoints';
// The maximumum number of activities we can pull in one go.
const ACTIVITIES_MAX_DRAW = 99;
// Supported activity types
const ACTIVITIES_TYPES_ALL = [
'sell',
'deposit',
'withdrawal',
'dividend',
'institutional_transfer',
'internal_transfer',
'refund',
'referral_bonus',
'affiliate',
'buy',
];
class Accounts {
/**
* Creates a new Account object associated with an Https worker state.
*
* @param {*} httpsWorker
*/
constructor(httpsWorker) {
this.worker = httpsWorker;
}
/**
* Retrieves all account ids open under this Wealthsimple Trade account.
*/
async all() {
const accounts = await this.worker.handleRequest(endpoints.ACCOUNT_IDS, {});
return {
tfsa: accounts.find((account) => account.startsWith('tfsa')),
rrsp: accounts.find((account) => account.startsWith('rrsp')),
crypto: accounts.find((account) => account.startsWith('non-registered-crypto')),
personal: accounts.find((account) => account.startsWith('non-registered') && !account.startsWith('non-registered-crypto')),
};
}
/**
* Returns a list of details about your open accounts, like account type, buying power,
* current balance, and more.
*/
async data() {
return this.worker.handleRequest(endpoints.LIST_ACCOUNT, {});
}
/**
* Retrieves some surface information about you like your name and email, account
* signatures, and other metadata.
*/
async me() {
return this.worker.handleRequest(endpoints.ME, {});
}
/**
* Detailed information about you that you provided on signup, like residential and
* mailing addresses, employment, phone numbers, and so on.
*/
async person() {
return this.worker.handleRequest(endpoints.PERSON, {});
}
/**
* Query the history of the account within a certain time interval.
*
* @param {*} interval The time interval for the history query
* @param {*} accountId The account to query
*/
async history(interval, accountId) {
return this.worker.handleRequest(endpoints.HISTORY_ACCOUNT, { interval, accountId });
}
/**
* Fetches activities on your Wealthsimple Trade account. You can limit number of activities
* to fetch or refine what activities are fetched based on activity type (e.g., buy, sell),
* account (e.g., tfsa, rrsp).
*/
async activities(filters = {}) {
// The maximum draw per API call is 99. If it's higher, we must abort.
if (filters?.limit && filters.limit > ACTIVITIES_MAX_DRAW) {
throw new Error('filters.limit can not exceed 99! Leave filters.limit undefined if you want to retrieve all.');
}
// Tell the user that filter.accounts must be an error
if (filters?.accounts && !Array.isArray(filters.accounts)) {
throw new Error('filters.accounts must be an array!');
}
// Tell the user that filter.type must be an error
if (filters?.type && !Array.isArray(filters.type)) {
throw new Error('filters.type must be an array!');
}
const results = [];
// Draw the first set of activities
let response = await this.worker.handleRequest(endpoints.ACTIVITIES, {
limit: filters?.limit ?? ACTIVITIES_MAX_DRAW,
accountIds: filters?.accounts?.join() ?? '',
bookmark: '',
type: filters?.type ?? ACTIVITIES_TYPES_ALL,
});
results.push(...response.results);
/*
* To get all activities, we will have to draw the maximum number
* of activities per API call until we are done (i.e., when we
* receive a number of activities less than the maximum per draw)
*/
if (!filters?.limit) {
/*
* The no-await-in-loop rule is being disabled for this case because
* every loop feeds back into the next one with the bookmark (i.e., they
* are not independent and must run sequentially).
*/
/* eslint-disable no-await-in-loop */
while (results.length % ACTIVITIES_MAX_DRAW === 0) {
response = await this.worker.handleRequest(endpoints.ACTIVITIES, {
limit: ACTIVITIES_MAX_DRAW,
accountIds: filters?.accounts?.join() ?? '',
bookmark: response.bookmark,
type: filters?.type ?? ACTIVITIES_TYPES_ALL,
});
// Escape the loop if the returned results is empty. This means we have reached the end.
if (response.results.length === 0) {
break;
}
results.push(...response.results);
}
/* eslint-enable no-await-in-loop */
}
return results;
}
/**
* Retrieves all bank accounts linked to the Wealthsimple Trade account.
*/
async getBankAccounts() {
return this.worker.handleRequest(endpoints.BANK_ACCOUNTS, {});
}
/**
* Grab all deposit records on the Wealthsimple Trade account.
*/
async deposits() {
return this.worker.handleRequest(endpoints.DEPOSITS, {});
}
/**
* Lists all positions in the specified trading account under the Wealthsimple Trade Account.
*
* @param {*} accountId The specific account in the Wealthsimple Trade account
*/
async positions(accountId) {
return this.worker.handleRequest(endpoints.POSITIONS, { accountId });
}
}
export default Accounts;