@finatic/client 0.0.131

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +489 -0
  2. package/dist/index.d.ts +2037 -0
  3. package/dist/index.js +4815 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/index.mjs +4787 -0
  6. package/dist/index.mjs.map +1 -0
  7. package/dist/types/client/ApiClient.d.ts +234 -0
  8. package/dist/types/client/FinaticConnect.d.ts +307 -0
  9. package/dist/types/index.d.ts +17 -0
  10. package/dist/types/mocks/MockApiClient.d.ts +228 -0
  11. package/dist/types/mocks/MockDataProvider.d.ts +132 -0
  12. package/dist/types/mocks/MockFactory.d.ts +53 -0
  13. package/dist/types/mocks/index.d.ts +5 -0
  14. package/dist/types/mocks/utils.d.ts +29 -0
  15. package/dist/types/portal/PortalUI.d.ts +38 -0
  16. package/dist/types/security/ApiSecurity.d.ts +24 -0
  17. package/dist/types/security/RuntimeSecurity.d.ts +28 -0
  18. package/dist/types/security/SecurityUtils.d.ts +21 -0
  19. package/dist/types/security/index.d.ts +2 -0
  20. package/dist/types/services/AnalyticsService.d.ts +18 -0
  21. package/dist/types/services/ApiClient.d.ts +121 -0
  22. package/dist/types/services/PortalService.d.ts +24 -0
  23. package/dist/types/services/TradingService.d.ts +55 -0
  24. package/dist/types/services/api.d.ts +23 -0
  25. package/dist/types/services/auth.d.ts +9 -0
  26. package/dist/types/services/index.d.ts +4 -0
  27. package/dist/types/services/portfolio.d.ts +10 -0
  28. package/dist/types/services/trading.d.ts +10 -0
  29. package/dist/types/shared/index.d.ts +2 -0
  30. package/dist/types/shared/themes/index.d.ts +2 -0
  31. package/dist/types/shared/themes/portalPresets.d.ts +8 -0
  32. package/dist/types/shared/themes/presets.d.ts +3 -0
  33. package/dist/types/shared/themes/system.d.ts +2 -0
  34. package/dist/types/shared/types/index.d.ts +110 -0
  35. package/dist/types/types/api.d.ts +486 -0
  36. package/dist/types/types/config.d.ts +12 -0
  37. package/dist/types/types/connect.d.ts +51 -0
  38. package/dist/types/types/errors.d.ts +47 -0
  39. package/dist/types/types/portal.d.ts +75 -0
  40. package/dist/types/types/security.d.ts +35 -0
  41. package/dist/types/types/shared.d.ts +50 -0
  42. package/dist/types/types/theme.d.ts +101 -0
  43. package/dist/types/types.d.ts +157 -0
  44. package/dist/types/utils/errors.d.ts +42 -0
  45. package/dist/types/utils/events.d.ts +12 -0
  46. package/dist/types/utils/themeUtils.d.ts +34 -0
  47. package/package.json +56 -0
package/dist/index.mjs ADDED
@@ -0,0 +1,4787 @@
1
+ import { v4 } from 'uuid';
2
+
3
+ /**
4
+ * API-related types for Finatic Connect SDK
5
+ */
6
+ var SessionState;
7
+ (function (SessionState) {
8
+ SessionState["PENDING"] = "PENDING";
9
+ SessionState["AUTHENTICATING"] = "AUTHENTICATING";
10
+ SessionState["ACTIVE"] = "ACTIVE";
11
+ SessionState["COMPLETED"] = "COMPLETED";
12
+ SessionState["EXPIRED"] = "EXPIRED";
13
+ })(SessionState || (SessionState = {}));
14
+
15
+ /**
16
+ * Core types for Finatic Connect SDK
17
+ */
18
+ class PaginatedResult {
19
+ constructor(data, paginationInfo, navigationCallback) {
20
+ this.data = data;
21
+ this.metadata = {
22
+ hasMore: paginationInfo.has_more,
23
+ nextOffset: paginationInfo.next_offset,
24
+ currentOffset: paginationInfo.current_offset,
25
+ limit: paginationInfo.limit,
26
+ currentPage: Math.floor(paginationInfo.current_offset / paginationInfo.limit) + 1,
27
+ hasNext: paginationInfo.has_more,
28
+ hasPrevious: paginationInfo.current_offset > 0,
29
+ };
30
+ this.navigationCallback = navigationCallback;
31
+ }
32
+ get hasNext() {
33
+ return this.metadata.hasNext;
34
+ }
35
+ get hasPrevious() {
36
+ return this.metadata.hasPrevious;
37
+ }
38
+ get currentPage() {
39
+ return this.metadata.currentPage;
40
+ }
41
+ /**
42
+ * Navigate to the next page
43
+ * @returns Promise<PaginatedResult<T> | null> - Next page result or null if no next page
44
+ */
45
+ async nextPage() {
46
+ if (!this.hasNext || !this.navigationCallback) {
47
+ return null;
48
+ }
49
+ try {
50
+ return await this.navigationCallback(this.metadata.nextOffset, this.metadata.limit);
51
+ }
52
+ catch (error) {
53
+ console.error('Error navigating to next page:', error);
54
+ return null;
55
+ }
56
+ }
57
+ /**
58
+ * Navigate to the previous page
59
+ * @returns Promise<PaginatedResult<T> | null> - Previous page result or null if no previous page
60
+ */
61
+ async previousPage() {
62
+ if (!this.hasPrevious || !this.navigationCallback) {
63
+ return null;
64
+ }
65
+ const previousOffset = Math.max(0, this.metadata.currentOffset - this.metadata.limit);
66
+ try {
67
+ return await this.navigationCallback(previousOffset, this.metadata.limit);
68
+ }
69
+ catch (error) {
70
+ console.error('Error navigating to previous page:', error);
71
+ return null;
72
+ }
73
+ }
74
+ /**
75
+ * Navigate to a specific page
76
+ * @param pageNumber - The page number to navigate to (1-based)
77
+ * @returns Promise<PaginatedResult<T> | null> - Page result or null if page doesn't exist
78
+ */
79
+ async goToPage(pageNumber) {
80
+ if (pageNumber < 1 || !this.navigationCallback) {
81
+ return null;
82
+ }
83
+ const targetOffset = (pageNumber - 1) * this.metadata.limit;
84
+ try {
85
+ return await this.navigationCallback(targetOffset, this.metadata.limit);
86
+ }
87
+ catch (error) {
88
+ console.error('Error navigating to page:', pageNumber, error);
89
+ return null;
90
+ }
91
+ }
92
+ /**
93
+ * Get the first page
94
+ * @returns Promise<PaginatedResult<T> | null> - First page result or null if error
95
+ */
96
+ async firstPage() {
97
+ return this.goToPage(1);
98
+ }
99
+ /**
100
+ * Get the last page (this is a best effort - we don't know the total count)
101
+ * @returns Promise<PaginatedResult<T> | null> - Last page result or null if error
102
+ */
103
+ async lastPage() {
104
+ if (!this.navigationCallback) {
105
+ return null;
106
+ }
107
+ const findLast = async (page) => {
108
+ const next = await page.nextPage();
109
+ if (next) {
110
+ return findLast(next);
111
+ }
112
+ return page;
113
+ };
114
+ return findLast(this);
115
+ }
116
+ /**
117
+ * Get pagination info as a string
118
+ * @returns string - Human readable pagination info
119
+ */
120
+ getPaginationInfo() {
121
+ return `Page ${this.currentPage} (${this.metadata.currentOffset + 1}-${this.metadata.currentOffset + this.metadata.limit}) - ${this.hasNext ? 'Has more' : 'Last page'}`;
122
+ }
123
+ }
124
+
125
+ class BaseError extends Error {
126
+ constructor(message, code = 'UNKNOWN_ERROR') {
127
+ super(message);
128
+ this.code = code;
129
+ this.name = this.constructor.name;
130
+ }
131
+ }
132
+ class ApiError extends BaseError {
133
+ constructor(status, message, details) {
134
+ super(message, `API_ERROR_${status}`);
135
+ this.status = status;
136
+ this.details = details;
137
+ this.name = 'ApiError';
138
+ }
139
+ }
140
+ class SessionError extends ApiError {
141
+ constructor(message, details) {
142
+ super(400, message, details);
143
+ this.name = 'SessionError';
144
+ }
145
+ }
146
+ class AuthenticationError extends ApiError {
147
+ constructor(message, details) {
148
+ super(401, message, details);
149
+ this.name = 'AuthenticationError';
150
+ }
151
+ }
152
+ class AuthorizationError extends ApiError {
153
+ constructor(message, details) {
154
+ super(403, message, details);
155
+ this.name = 'AuthorizationError';
156
+ }
157
+ }
158
+ class RateLimitError extends ApiError {
159
+ constructor(message, details) {
160
+ super(429, message, details);
161
+ this.name = 'RateLimitError';
162
+ }
163
+ }
164
+ class TokenError extends BaseError {
165
+ constructor(message) {
166
+ super(message, 'TOKEN_ERROR');
167
+ this.name = 'TokenError';
168
+ }
169
+ }
170
+ class ValidationError extends BaseError {
171
+ constructor(message) {
172
+ super(message, 'VALIDATION_ERROR');
173
+ this.name = 'ValidationError';
174
+ }
175
+ }
176
+ class NetworkError extends BaseError {
177
+ constructor(message) {
178
+ super(message, 'NETWORK_ERROR');
179
+ this.name = 'NetworkError';
180
+ }
181
+ }
182
+ class SecurityError extends BaseError {
183
+ constructor(message) {
184
+ super(message, 'SECURITY_ERROR');
185
+ this.name = 'SecurityError';
186
+ }
187
+ }
188
+ class CompanyAccessError extends ApiError {
189
+ constructor(message, details) {
190
+ super(403, message, details);
191
+ this.name = 'CompanyAccessError';
192
+ }
193
+ }
194
+ class OrderError extends ApiError {
195
+ constructor(message, details) {
196
+ super(500, message, details);
197
+ this.name = 'OrderError';
198
+ }
199
+ }
200
+ class OrderValidationError extends ApiError {
201
+ constructor(message, details) {
202
+ super(400, message, details);
203
+ this.name = 'OrderValidationError';
204
+ }
205
+ }
206
+
207
+ class ApiClient {
208
+ constructor(baseUrl, deviceInfo) {
209
+ this.currentSessionState = null;
210
+ this.currentSessionId = null;
211
+ this.tradingContext = {};
212
+ // Token management
213
+ this.tokenInfo = null;
214
+ this.refreshPromise = null;
215
+ this.REFRESH_BUFFER_MINUTES = 5; // Refresh token 5 minutes before expiry
216
+ // Session and company context
217
+ this.companyId = null;
218
+ this.csrfToken = null;
219
+ this.baseUrl = baseUrl;
220
+ this.deviceInfo = deviceInfo;
221
+ // Ensure baseUrl doesn't end with a slash
222
+ this.baseUrl = baseUrl.replace(/\/$/, '');
223
+ // Append /api/v1 if not already present
224
+ if (!this.baseUrl.includes('/api/v1')) {
225
+ this.baseUrl = `${this.baseUrl}/api/v1`;
226
+ }
227
+ }
228
+ /**
229
+ * Set session context (session ID, company ID, CSRF token)
230
+ */
231
+ setSessionContext(sessionId, companyId, csrfToken) {
232
+ this.currentSessionId = sessionId;
233
+ this.companyId = companyId;
234
+ this.csrfToken = csrfToken || null;
235
+ }
236
+ /**
237
+ * Get the current session ID
238
+ */
239
+ getCurrentSessionId() {
240
+ return this.currentSessionId;
241
+ }
242
+ /**
243
+ * Get the current company ID
244
+ */
245
+ getCurrentCompanyId() {
246
+ return this.companyId;
247
+ }
248
+ /**
249
+ * Get the current CSRF token
250
+ */
251
+ getCurrentCsrfToken() {
252
+ return this.csrfToken;
253
+ }
254
+ /**
255
+ * Store tokens after successful authentication
256
+ */
257
+ setTokens(accessToken, refreshToken, expiresAt, userId) {
258
+ this.tokenInfo = {
259
+ accessToken,
260
+ refreshToken,
261
+ expiresAt,
262
+ userId,
263
+ };
264
+ }
265
+ /**
266
+ * Get the current access token, refreshing if necessary
267
+ */
268
+ async getValidAccessToken() {
269
+ if (!this.tokenInfo) {
270
+ throw new AuthenticationError('No tokens available. Please authenticate first.');
271
+ }
272
+ // Check if token is expired or about to expire
273
+ if (this.isTokenExpired()) {
274
+ await this.refreshTokens();
275
+ }
276
+ return this.tokenInfo.accessToken;
277
+ }
278
+ /**
279
+ * Check if the current token is expired or about to expire
280
+ */
281
+ isTokenExpired() {
282
+ if (!this.tokenInfo)
283
+ return true;
284
+ const expiryTime = new Date(this.tokenInfo.expiresAt).getTime();
285
+ const currentTime = Date.now();
286
+ const bufferTime = this.REFRESH_BUFFER_MINUTES * 60 * 1000; // 5 minutes in milliseconds
287
+ return currentTime >= expiryTime - bufferTime;
288
+ }
289
+ /**
290
+ * Refresh the access token using the refresh token
291
+ */
292
+ async refreshTokens() {
293
+ if (!this.tokenInfo) {
294
+ throw new AuthenticationError('No refresh token available.');
295
+ }
296
+ // If a refresh is already in progress, wait for it
297
+ if (this.refreshPromise) {
298
+ await this.refreshPromise;
299
+ return;
300
+ }
301
+ // Start a new refresh
302
+ this.refreshPromise = this.performTokenRefresh();
303
+ try {
304
+ await this.refreshPromise;
305
+ }
306
+ finally {
307
+ this.refreshPromise = null;
308
+ }
309
+ }
310
+ /**
311
+ * Perform the actual token refresh request
312
+ */
313
+ async performTokenRefresh() {
314
+ if (!this.tokenInfo) {
315
+ throw new AuthenticationError('No refresh token available.');
316
+ }
317
+ try {
318
+ const response = await this.request('/company/auth/refresh', {
319
+ method: 'POST',
320
+ headers: {
321
+ 'Content-Type': 'application/json',
322
+ },
323
+ body: {
324
+ refresh_token: this.tokenInfo.refreshToken,
325
+ },
326
+ });
327
+ // Update stored tokens
328
+ this.tokenInfo = {
329
+ accessToken: response.response_data.access_token,
330
+ refreshToken: response.response_data.refresh_token,
331
+ expiresAt: response.response_data.expires_at,
332
+ userId: this.tokenInfo.userId,
333
+ };
334
+ return this.tokenInfo;
335
+ }
336
+ catch (error) {
337
+ // Clear tokens on refresh failure
338
+ this.tokenInfo = null;
339
+ throw new AuthenticationError('Token refresh failed. Please re-authenticate.', error);
340
+ }
341
+ }
342
+ /**
343
+ * Clear stored tokens (useful for logout)
344
+ */
345
+ clearTokens() {
346
+ this.tokenInfo = null;
347
+ this.refreshPromise = null;
348
+ }
349
+ /**
350
+ * Get current token info (for debugging/testing)
351
+ */
352
+ getTokenInfo() {
353
+ return this.tokenInfo ? { ...this.tokenInfo } : null;
354
+ }
355
+ /**
356
+ * Make a request to the API.
357
+ */
358
+ async request(path, options) {
359
+ // Ensure path starts with a slash
360
+ const normalizedPath = path.startsWith('/') ? path : `/${path}`;
361
+ const url = new URL(`${this.baseUrl}${normalizedPath}`);
362
+ if (options.params) {
363
+ Object.entries(options.params).forEach(([key, value]) => {
364
+ url.searchParams.append(key, value);
365
+ });
366
+ }
367
+ const response = await fetch(url.toString(), {
368
+ method: options.method,
369
+ headers: options.headers,
370
+ body: options.body ? JSON.stringify(options.body) : undefined,
371
+ });
372
+ if (!response.ok) {
373
+ const error = await response.json();
374
+ throw this.handleError(response.status, error);
375
+ }
376
+ const data = await response.json();
377
+ // Check if the response has a success field and it's false
378
+ if (data && typeof data === 'object' && 'success' in data && data.success === false) {
379
+ // For order endpoints, provide more context
380
+ const isOrderEndpoint = path.includes('/brokers/orders');
381
+ if (isOrderEndpoint) {
382
+ // Add context that this is an order-related error
383
+ data._isOrderError = true;
384
+ }
385
+ throw this.handleError(data.status_code || 500, data);
386
+ }
387
+ // Check if the response has a status_code field indicating an error (4xx or 5xx)
388
+ if (data && typeof data === 'object' && 'status_code' in data && data.status_code >= 400) {
389
+ throw this.handleError(data.status_code, data);
390
+ }
391
+ // Check if the response has errors field with content
392
+ if (data &&
393
+ typeof data === 'object' &&
394
+ 'errors' in data &&
395
+ data.errors &&
396
+ Array.isArray(data.errors) &&
397
+ data.errors.length > 0) {
398
+ throw this.handleError(data.status_code || 500, data);
399
+ }
400
+ return data;
401
+ }
402
+ /**
403
+ * Handle API errors. This method can be overridden by language-specific implementations.
404
+ */
405
+ handleError(status, error) {
406
+ // Extract message from the error object with multiple fallback options
407
+ let message = 'API request failed';
408
+ if (error && typeof error === 'object') {
409
+ // Try different possible message fields
410
+ message =
411
+ error.message ||
412
+ error.detail?.message ||
413
+ error.error?.message ||
414
+ error.errors?.[0]?.message ||
415
+ (typeof error.errors === 'string' ? error.errors : null) ||
416
+ 'API request failed';
417
+ }
418
+ // Check if this is an order-related error (either from order endpoints or order validation)
419
+ const isOrderError = error._isOrderError ||
420
+ message.includes('ORDER_FAILED') ||
421
+ message.includes('AUTH_ERROR') ||
422
+ message.includes('not found or not available for trading') ||
423
+ message.includes('Symbol') ||
424
+ (error.errors &&
425
+ Array.isArray(error.errors) &&
426
+ error.errors.some((e) => e.category === 'INVALID_ORDER' ||
427
+ e.message?.includes('Symbol') ||
428
+ e.message?.includes('not available for trading')));
429
+ if (isOrderError) {
430
+ // Check if this is a validation error (400 status with specific validation messages)
431
+ const isValidationError = status === 400 &&
432
+ (message.includes('not found or not available for trading') ||
433
+ message.includes('Symbol') ||
434
+ (error.errors &&
435
+ Array.isArray(error.errors) &&
436
+ error.errors.some((e) => e.category === 'INVALID_ORDER' ||
437
+ e.message?.includes('Symbol') ||
438
+ e.message?.includes('not available for trading'))));
439
+ if (isValidationError) {
440
+ return new OrderValidationError(message, error);
441
+ }
442
+ // For order placement errors, provide more specific error messages
443
+ if (message.includes('ORDER_FAILED') || message.includes('AUTH_ERROR')) {
444
+ // Extract the specific error from the nested structure
445
+ const orderErrorMatch = message.match(/\[([^\]]+)\]/g);
446
+ if (orderErrorMatch && orderErrorMatch.length > 0) {
447
+ // Take the last error in the chain (most specific)
448
+ const specificError = orderErrorMatch[orderErrorMatch.length - 1].replace(/[[\]]/g, '');
449
+ message = `Order failed: ${specificError}`;
450
+ }
451
+ }
452
+ // Use OrderError for order-related failures
453
+ return new OrderError(message, error);
454
+ }
455
+ switch (status) {
456
+ case 400:
457
+ return new SessionError(message, error);
458
+ case 401:
459
+ return new AuthenticationError(message || 'Unauthorized: Invalid or missing session token', error);
460
+ case 403:
461
+ if (error.detail?.code === 'NO_COMPANY_ACCESS') {
462
+ return new CompanyAccessError(error.detail.message || 'No broker connections found for this company', error.detail);
463
+ }
464
+ return new AuthorizationError(message || 'Forbidden: No access to the requested data', error);
465
+ case 404:
466
+ return new ApiError(status, message || 'Not found: The requested data does not exist', error);
467
+ case 429:
468
+ return new RateLimitError(message || 'Rate limit exceeded', error);
469
+ case 500:
470
+ return new ApiError(status, message || 'Internal server error', error);
471
+ default:
472
+ return new ApiError(status, message || 'API request failed', error);
473
+ }
474
+ }
475
+ // Session Management
476
+ async startSession(token, userId) {
477
+ const response = await this.request('/auth/session/start', {
478
+ method: 'POST',
479
+ headers: {
480
+ 'Content-Type': 'application/json',
481
+ 'One-Time-Token': token,
482
+ 'X-Device-Info': JSON.stringify({
483
+ ip_address: this.deviceInfo?.ip_address || '',
484
+ user_agent: this.deviceInfo?.user_agent || '',
485
+ fingerprint: this.deviceInfo?.fingerprint || '',
486
+ }),
487
+ },
488
+ body: {
489
+ user_id: userId,
490
+ },
491
+ });
492
+ // Store session ID and set state to ACTIVE
493
+ this.currentSessionId = response.data.session_id;
494
+ this.currentSessionState = SessionState.ACTIVE;
495
+ return response;
496
+ }
497
+ // OTP Flow
498
+ async requestOtp(sessionId, email) {
499
+ return this.request('/auth/otp/request', {
500
+ method: 'POST',
501
+ headers: {
502
+ 'Content-Type': 'application/json',
503
+ 'X-Session-ID': sessionId,
504
+ 'X-Device-Info': JSON.stringify({
505
+ ip_address: this.deviceInfo?.ip_address || '',
506
+ user_agent: this.deviceInfo?.user_agent || '',
507
+ fingerprint: this.deviceInfo?.fingerprint || '',
508
+ }),
509
+ },
510
+ body: {
511
+ email,
512
+ },
513
+ });
514
+ }
515
+ async verifyOtp(sessionId, otp) {
516
+ const response = await this.request('/auth/otp/verify', {
517
+ method: 'POST',
518
+ headers: {
519
+ 'Content-Type': 'application/json',
520
+ 'X-Session-ID': sessionId,
521
+ 'X-Device-Info': JSON.stringify({
522
+ ip_address: this.deviceInfo?.ip_address || '',
523
+ user_agent: this.deviceInfo?.user_agent || '',
524
+ fingerprint: this.deviceInfo?.fingerprint || '',
525
+ }),
526
+ },
527
+ body: {
528
+ otp,
529
+ },
530
+ });
531
+ // Store tokens after successful OTP verification
532
+ if (response.success && response.data) {
533
+ const expiresAt = new Date(Date.now() + response.data.expires_in * 1000).toISOString();
534
+ this.setTokens(response.data.access_token, response.data.refresh_token, expiresAt, response.data.user_id);
535
+ }
536
+ return response;
537
+ }
538
+ // Direct Authentication
539
+ async authenticateDirectly(sessionId, userId) {
540
+ // Ensure session is active before authenticating
541
+ if (this.currentSessionState !== SessionState.ACTIVE) {
542
+ throw new SessionError('Session must be in ACTIVE state to authenticate');
543
+ }
544
+ const response = await this.request('/auth/session/authenticate', {
545
+ method: 'POST',
546
+ headers: {
547
+ 'Content-Type': 'application/json',
548
+ 'Session-ID': sessionId,
549
+ 'X-Session-ID': sessionId,
550
+ 'X-Device-Info': JSON.stringify({
551
+ ip_address: this.deviceInfo?.ip_address || '',
552
+ user_agent: this.deviceInfo?.user_agent || '',
553
+ fingerprint: this.deviceInfo?.fingerprint || '',
554
+ }),
555
+ },
556
+ body: {
557
+ session_id: sessionId,
558
+ user_id: userId,
559
+ },
560
+ });
561
+ // Store tokens after successful direct authentication
562
+ if (response.success && response.data) {
563
+ // For direct auth, we don't get expires_in, so we'll set a default 1-hour expiry
564
+ const expiresAt = new Date(Date.now() + 60 * 60 * 1000).toISOString(); // 1 hour
565
+ this.setTokens(response.data.access_token, response.data.refresh_token, expiresAt, userId);
566
+ }
567
+ return response;
568
+ }
569
+ // Portal Management
570
+ /**
571
+ * Get the portal URL for an active session
572
+ * @param sessionId The session identifier
573
+ * @returns Portal URL response
574
+ * @throws SessionError if session is not in ACTIVE state
575
+ */
576
+ async getPortalUrl(sessionId) {
577
+ if (this.currentSessionState !== SessionState.ACTIVE) {
578
+ throw new SessionError('Session must be in ACTIVE state to get portal URL');
579
+ }
580
+ return this.request('/auth/session/portal', {
581
+ method: 'GET',
582
+ headers: {
583
+ 'Content-Type': 'application/json',
584
+ 'Session-ID': sessionId,
585
+ 'X-Session-ID': sessionId,
586
+ 'X-Device-Info': JSON.stringify({
587
+ ip_address: this.deviceInfo?.ip_address || '',
588
+ user_agent: this.deviceInfo?.user_agent || '',
589
+ fingerprint: this.deviceInfo?.fingerprint || '',
590
+ }),
591
+ },
592
+ });
593
+ }
594
+ async validatePortalSession(sessionId, signature) {
595
+ return this.request('/portal/validate', {
596
+ method: 'GET',
597
+ headers: {
598
+ 'Content-Type': 'application/json',
599
+ 'X-Session-ID': sessionId,
600
+ 'X-Device-Info': JSON.stringify({
601
+ ip_address: this.deviceInfo?.ip_address || '',
602
+ user_agent: this.deviceInfo?.user_agent || '',
603
+ fingerprint: this.deviceInfo?.fingerprint || '',
604
+ }),
605
+ },
606
+ params: {
607
+ signature,
608
+ },
609
+ });
610
+ }
611
+ async completePortalSession(sessionId) {
612
+ return this.request(`/portal/${sessionId}/complete`, {
613
+ method: 'POST',
614
+ headers: {
615
+ 'Content-Type': 'application/json',
616
+ },
617
+ });
618
+ }
619
+ // Portfolio Management
620
+ async getHoldings(accessToken) {
621
+ return this.request('/portfolio/holdings', {
622
+ method: 'GET',
623
+ headers: {
624
+ 'Content-Type': 'application/json',
625
+ Authorization: `Bearer ${accessToken}`,
626
+ },
627
+ });
628
+ }
629
+ async getOrders(accessToken) {
630
+ return this.request('/orders/', {
631
+ method: 'GET',
632
+ headers: {
633
+ 'Content-Type': 'application/json',
634
+ Authorization: `Bearer ${accessToken}`,
635
+ },
636
+ });
637
+ }
638
+ async getPortfolio(accessToken) {
639
+ const response = await this.request('/portfolio/', {
640
+ method: 'GET',
641
+ headers: {
642
+ Authorization: `Bearer ${accessToken}`,
643
+ 'Content-Type': 'application/json',
644
+ },
645
+ });
646
+ return response;
647
+ }
648
+ async placeOrder(accessToken, order) {
649
+ await this.request('/orders/', {
650
+ method: 'POST',
651
+ headers: {
652
+ Authorization: `Bearer ${accessToken}`,
653
+ 'Content-Type': 'application/json',
654
+ },
655
+ body: order,
656
+ });
657
+ }
658
+ // New methods with automatic token management
659
+ async getHoldingsAuto() {
660
+ const accessToken = await this.getValidAccessToken();
661
+ return this.getHoldings(accessToken);
662
+ }
663
+ async getOrdersAuto() {
664
+ const accessToken = await this.getValidAccessToken();
665
+ return this.getOrders(accessToken);
666
+ }
667
+ async getPortfolioAuto() {
668
+ const accessToken = await this.getValidAccessToken();
669
+ return this.getPortfolio(accessToken);
670
+ }
671
+ async placeOrderAuto(order) {
672
+ const accessToken = await this.getValidAccessToken();
673
+ return this.placeOrder(accessToken, order);
674
+ }
675
+ // Enhanced Trading Methods with Session Management
676
+ async placeBrokerOrder(accessToken, params, extras = {}) {
677
+ // Merge context with provided parameters
678
+ const fullParams = {
679
+ broker: (params.broker || this.tradingContext.broker) ||
680
+ (() => {
681
+ throw new Error('Broker not set. Call setBroker() or pass broker parameter.');
682
+ })(),
683
+ accountNumber: params.accountNumber ||
684
+ this.tradingContext.accountNumber ||
685
+ (() => {
686
+ throw new Error('Account not set. Call setAccount() or pass accountNumber parameter.');
687
+ })(),
688
+ symbol: params.symbol,
689
+ orderQty: params.orderQty,
690
+ action: params.action,
691
+ orderType: params.orderType,
692
+ assetType: params.assetType,
693
+ timeInForce: params.timeInForce || 'day',
694
+ price: params.price,
695
+ stopPrice: params.stopPrice,
696
+ };
697
+ // Build request body with snake_case parameter names
698
+ const requestBody = this.buildOrderRequestBody(fullParams, extras);
699
+ return this.request('/brokers/orders', {
700
+ method: 'POST',
701
+ headers: {
702
+ 'Content-Type': 'application/json',
703
+ Authorization: `Bearer ${accessToken}`,
704
+ 'Session-ID': this.currentSessionId || '',
705
+ 'X-Session-ID': this.currentSessionId || '',
706
+ 'X-Device-Info': JSON.stringify(this.deviceInfo),
707
+ },
708
+ body: requestBody,
709
+ });
710
+ }
711
+ async cancelBrokerOrder(orderId, broker, extras = {}) {
712
+ const targetBroker = broker ||
713
+ this.tradingContext.broker ||
714
+ (() => {
715
+ throw new Error('Broker not set. Call setBroker() or pass broker parameter.');
716
+ })();
717
+ // Build optional body for extras if provided
718
+ const body = Object.keys(extras).length > 0 ? { order: extras } : undefined;
719
+ return this.request(`/brokers/orders/${targetBroker}/${orderId}`, {
720
+ method: 'DELETE',
721
+ headers: {
722
+ 'Content-Type': 'application/json',
723
+ 'Session-ID': this.currentSessionId || '',
724
+ 'X-Session-ID': this.currentSessionId || '',
725
+ 'X-Device-Info': JSON.stringify(this.deviceInfo),
726
+ },
727
+ body,
728
+ });
729
+ }
730
+ async modifyBrokerOrder(orderId, params, broker, extras = {}) {
731
+ const targetBroker = broker ||
732
+ this.tradingContext.broker ||
733
+ (() => {
734
+ throw new Error('Broker not set. Call setBroker() or pass broker parameter.');
735
+ })();
736
+ // Build request body with snake_case parameter names and include broker
737
+ const requestBody = this.buildModifyRequestBody(params, extras, targetBroker);
738
+ return this.request(`/brokers/orders/${orderId}`, {
739
+ method: 'PATCH',
740
+ headers: {
741
+ 'Content-Type': 'application/json',
742
+ 'Session-ID': this.currentSessionId || '',
743
+ 'X-Session-ID': this.currentSessionId || '',
744
+ 'X-Device-Info': JSON.stringify(this.deviceInfo),
745
+ },
746
+ body: requestBody,
747
+ });
748
+ }
749
+ // Context management methods
750
+ setBroker(broker) {
751
+ this.tradingContext.broker = broker;
752
+ // Clear account when broker changes
753
+ this.tradingContext.accountNumber = undefined;
754
+ this.tradingContext.accountId = undefined;
755
+ }
756
+ setAccount(accountNumber, accountId) {
757
+ this.tradingContext.accountNumber = accountNumber;
758
+ this.tradingContext.accountId = accountId;
759
+ }
760
+ getTradingContext() {
761
+ return { ...this.tradingContext };
762
+ }
763
+ clearTradingContext() {
764
+ this.tradingContext = {};
765
+ }
766
+ // Stock convenience methods
767
+ async placeStockMarketOrder(accessToken, symbol, orderQty, action, broker, accountNumber, extras = {}) {
768
+ return this.placeBrokerOrder(accessToken, {
769
+ broker,
770
+ accountNumber,
771
+ symbol,
772
+ orderQty,
773
+ action,
774
+ orderType: 'Market',
775
+ assetType: 'Stock',
776
+ timeInForce: 'day',
777
+ }, extras);
778
+ }
779
+ async placeStockLimitOrder(accessToken, symbol, orderQty, action, price, timeInForce = 'gtc', broker, accountNumber, extras = {}) {
780
+ return this.placeBrokerOrder(accessToken, {
781
+ broker,
782
+ accountNumber,
783
+ symbol,
784
+ orderQty,
785
+ action,
786
+ orderType: 'Limit',
787
+ assetType: 'Stock',
788
+ timeInForce,
789
+ price,
790
+ }, extras);
791
+ }
792
+ async placeStockStopOrder(accessToken, symbol, orderQty, action, stopPrice, timeInForce = 'day', broker, accountNumber, extras = {}) {
793
+ return this.placeBrokerOrder(accessToken, {
794
+ broker,
795
+ accountNumber,
796
+ symbol,
797
+ orderQty,
798
+ action,
799
+ orderType: 'Stop',
800
+ assetType: 'Stock',
801
+ timeInForce,
802
+ stopPrice,
803
+ }, extras);
804
+ }
805
+ // Crypto convenience methods
806
+ async placeCryptoMarketOrder(accessToken, symbol, orderQty, action, options = {}, broker, accountNumber, extras = {}) {
807
+ const orderParams = {
808
+ broker,
809
+ accountNumber,
810
+ symbol,
811
+ orderQty: options.quantity || orderQty,
812
+ action,
813
+ orderType: 'Market',
814
+ assetType: 'Crypto',
815
+ timeInForce: 'gtc', // Crypto typically uses GTC
816
+ };
817
+ // Add notional if provided
818
+ if (options.notional) {
819
+ orderParams.notional = options.notional;
820
+ }
821
+ return this.placeBrokerOrder(accessToken, orderParams, extras);
822
+ }
823
+ async placeCryptoLimitOrder(accessToken, symbol, orderQty, action, price, timeInForce = 'gtc', options = {}, broker, accountNumber, extras = {}) {
824
+ const orderParams = {
825
+ broker,
826
+ accountNumber,
827
+ symbol,
828
+ orderQty: options.quantity || orderQty,
829
+ action,
830
+ orderType: 'Limit',
831
+ assetType: 'Crypto',
832
+ timeInForce,
833
+ price,
834
+ };
835
+ // Add notional if provided
836
+ if (options.notional) {
837
+ orderParams.notional = options.notional;
838
+ }
839
+ return this.placeBrokerOrder(accessToken, orderParams, extras);
840
+ }
841
+ // Options convenience methods
842
+ async placeOptionsMarketOrder(accessToken, symbol, orderQty, action, options, broker, accountNumber, extras = {}) {
843
+ const orderParams = {
844
+ broker,
845
+ accountNumber,
846
+ symbol,
847
+ orderQty,
848
+ action,
849
+ orderType: 'Market',
850
+ assetType: 'Option',
851
+ timeInForce: 'day',
852
+ };
853
+ // Add options-specific parameters to extras
854
+ const targetBroker = broker || this.tradingContext.broker;
855
+ const optionsExtras = {
856
+ ...extras,
857
+ ...(targetBroker && {
858
+ [targetBroker]: {
859
+ ...extras[targetBroker],
860
+ strikePrice: options.strikePrice,
861
+ expirationDate: options.expirationDate,
862
+ optionType: options.optionType,
863
+ contractSize: options.contractSize || 100,
864
+ },
865
+ }),
866
+ };
867
+ return this.placeBrokerOrder(accessToken, orderParams, optionsExtras);
868
+ }
869
+ async placeOptionsLimitOrder(accessToken, symbol, orderQty, action, price, options, timeInForce = 'gtc', broker, accountNumber, extras = {}) {
870
+ const orderParams = {
871
+ broker,
872
+ accountNumber,
873
+ symbol,
874
+ orderQty,
875
+ action,
876
+ orderType: 'Limit',
877
+ assetType: 'Option',
878
+ timeInForce,
879
+ price,
880
+ };
881
+ // Add options-specific parameters to extras
882
+ const targetBroker = broker || this.tradingContext.broker;
883
+ const optionsExtras = {
884
+ ...extras,
885
+ ...(targetBroker && {
886
+ [targetBroker]: {
887
+ ...extras[targetBroker],
888
+ strikePrice: options.strikePrice,
889
+ expirationDate: options.expirationDate,
890
+ optionType: options.optionType,
891
+ contractSize: options.contractSize || 100,
892
+ },
893
+ }),
894
+ };
895
+ return this.placeBrokerOrder(accessToken, orderParams, optionsExtras);
896
+ }
897
+ // Futures convenience methods
898
+ async placeFuturesMarketOrder(accessToken, symbol, orderQty, action, broker, accountNumber, extras = {}) {
899
+ return this.placeBrokerOrder(accessToken, {
900
+ broker,
901
+ accountNumber,
902
+ symbol,
903
+ orderQty,
904
+ action,
905
+ orderType: 'Market',
906
+ assetType: 'Futures',
907
+ timeInForce: 'day',
908
+ }, extras);
909
+ }
910
+ async placeFuturesLimitOrder(accessToken, symbol, orderQty, action, price, timeInForce = 'gtc', broker, accountNumber, extras = {}) {
911
+ return this.placeBrokerOrder(accessToken, {
912
+ broker,
913
+ accountNumber,
914
+ symbol,
915
+ orderQty,
916
+ action,
917
+ orderType: 'Limit',
918
+ assetType: 'Futures',
919
+ timeInForce,
920
+ price,
921
+ }, extras);
922
+ }
923
+ buildOrderRequestBody(params, extras = {}) {
924
+ const baseOrder = {
925
+ order_type: params.orderType,
926
+ asset_type: params.assetType,
927
+ action: params.action,
928
+ time_in_force: params.timeInForce.toLowerCase(),
929
+ account_number: params.accountNumber,
930
+ symbol: params.symbol,
931
+ order_qty: params.orderQty,
932
+ };
933
+ if (params.price !== undefined)
934
+ baseOrder.price = params.price;
935
+ if (params.stopPrice !== undefined)
936
+ baseOrder.stop_price = params.stopPrice;
937
+ // Apply broker-specific defaults
938
+ const brokerExtras = this.applyBrokerDefaults(params.broker, extras[params.broker] || {});
939
+ return {
940
+ broker: params.broker,
941
+ ...baseOrder,
942
+ ...brokerExtras,
943
+ };
944
+ }
945
+ buildModifyRequestBody(params, extras, broker) {
946
+ const requestBody = {
947
+ broker: broker,
948
+ };
949
+ if (params.orderType !== undefined)
950
+ requestBody.order_type = params.orderType;
951
+ if (params.assetType !== undefined)
952
+ requestBody.asset_type = params.assetType;
953
+ if (params.action !== undefined)
954
+ requestBody.action = params.action;
955
+ if (params.timeInForce !== undefined)
956
+ requestBody.time_in_force = params.timeInForce.toLowerCase();
957
+ if (params.accountNumber !== undefined)
958
+ requestBody.account_number = params.accountNumber;
959
+ if (params.symbol !== undefined)
960
+ requestBody.symbol = params.symbol;
961
+ if (params.orderQty !== undefined)
962
+ requestBody.order_qty = params.orderQty;
963
+ if (params.price !== undefined)
964
+ requestBody.price = params.price;
965
+ if (params.stopPrice !== undefined)
966
+ requestBody.stop_price = params.stopPrice;
967
+ // Apply broker-specific defaults
968
+ const brokerExtras = this.applyBrokerDefaults(broker, extras[broker] || {});
969
+ return {
970
+ ...requestBody,
971
+ ...brokerExtras,
972
+ };
973
+ }
974
+ applyBrokerDefaults(broker, extras) {
975
+ switch (broker) {
976
+ case 'robinhood':
977
+ return {
978
+ extendedHours: extras.extendedHours ?? true,
979
+ marketHours: extras.marketHours ?? 'regular_hours',
980
+ trailType: extras.trailType ?? 'percentage',
981
+ ...extras,
982
+ };
983
+ case 'ninja_trader':
984
+ return {
985
+ accountSpec: extras.accountSpec ?? '',
986
+ isAutomated: extras.isAutomated ?? true,
987
+ ...extras,
988
+ };
989
+ case 'tasty_trade':
990
+ return {
991
+ automatedSource: extras.automatedSource ?? true,
992
+ ...extras,
993
+ };
994
+ default:
995
+ return extras;
996
+ }
997
+ }
998
+ async revokeToken(accessToken) {
999
+ return this.request('/auth/token/revoke', {
1000
+ method: 'POST',
1001
+ headers: {
1002
+ 'Content-Type': 'application/json',
1003
+ Authorization: `Bearer ${accessToken}`,
1004
+ },
1005
+ });
1006
+ }
1007
+ async getUserToken(userId) {
1008
+ return this.request('/auth/token', {
1009
+ method: 'POST',
1010
+ headers: {
1011
+ 'Content-Type': 'application/json',
1012
+ },
1013
+ body: { userId },
1014
+ });
1015
+ }
1016
+ getCurrentSessionState() {
1017
+ return this.currentSessionState;
1018
+ }
1019
+ // Broker Data Management
1020
+ async getBrokerList(accessToken) {
1021
+ return this.request('/brokers/', {
1022
+ method: 'GET',
1023
+ headers: {
1024
+ 'Content-Type': 'application/json',
1025
+ Authorization: `Bearer ${accessToken}`,
1026
+ 'Session-ID': this.currentSessionId || '',
1027
+ 'X-Session-ID': this.currentSessionId || '',
1028
+ 'X-Device-Info': JSON.stringify({
1029
+ ip_address: this.deviceInfo?.ip_address || '',
1030
+ user_agent: this.deviceInfo?.user_agent || '',
1031
+ fingerprint: this.deviceInfo?.fingerprint || '',
1032
+ }),
1033
+ },
1034
+ });
1035
+ }
1036
+ async getBrokerAccounts(accessToken, options) {
1037
+ const params = {};
1038
+ if (options?.broker_name)
1039
+ params.broker_name = options.broker_name;
1040
+ if (options?.account_id)
1041
+ params.account_id = options.account_id;
1042
+ return this.request('/brokers/data/accounts', {
1043
+ method: 'GET',
1044
+ headers: {
1045
+ 'Content-Type': 'application/json',
1046
+ Authorization: `Bearer ${accessToken}`,
1047
+ 'Session-ID': this.currentSessionId || '',
1048
+ 'X-Session-ID': this.currentSessionId || '',
1049
+ 'X-Device-Info': JSON.stringify({
1050
+ ip_address: this.deviceInfo?.ip_address || '',
1051
+ user_agent: this.deviceInfo?.user_agent || '',
1052
+ fingerprint: this.deviceInfo?.fingerprint || '',
1053
+ }),
1054
+ },
1055
+ params,
1056
+ });
1057
+ }
1058
+ async getBrokerOrders(accessToken, options) {
1059
+ const params = {};
1060
+ if (options?.broker_name)
1061
+ params.broker_name = options.broker_name;
1062
+ if (options?.account_id)
1063
+ params.account_id = options.account_id;
1064
+ if (options?.symbol)
1065
+ params.symbol = options.symbol;
1066
+ return this.request('/brokers/data/orders', {
1067
+ method: 'GET',
1068
+ headers: {
1069
+ 'Content-Type': 'application/json',
1070
+ Authorization: `Bearer ${accessToken}`,
1071
+ 'Session-ID': this.currentSessionId || '',
1072
+ 'X-Session-ID': this.currentSessionId || '',
1073
+ 'X-Device-Info': JSON.stringify({
1074
+ ip_address: this.deviceInfo?.ip_address || '',
1075
+ user_agent: this.deviceInfo?.user_agent || '',
1076
+ fingerprint: this.deviceInfo?.fingerprint || '',
1077
+ }),
1078
+ },
1079
+ params,
1080
+ });
1081
+ }
1082
+ async getBrokerPositions(accessToken, options) {
1083
+ const queryParams = {};
1084
+ if (options?.broker_name)
1085
+ queryParams.broker_name = options.broker_name;
1086
+ if (options?.account_id)
1087
+ queryParams.account_id = options.account_id;
1088
+ if (options?.symbol)
1089
+ queryParams.symbol = options.symbol;
1090
+ return this.request('/brokers/data/positions', {
1091
+ method: 'GET',
1092
+ headers: {
1093
+ Authorization: `Bearer ${accessToken}`,
1094
+ 'Content-Type': 'application/json',
1095
+ 'Session-ID': this.currentSessionId || '',
1096
+ 'X-Session-ID': this.currentSessionId || '',
1097
+ 'X-Device-Info': JSON.stringify({
1098
+ ip_address: this.deviceInfo?.ip_address || '',
1099
+ user_agent: this.deviceInfo?.user_agent || '',
1100
+ fingerprint: this.deviceInfo?.fingerprint || '',
1101
+ }),
1102
+ },
1103
+ params: queryParams,
1104
+ });
1105
+ }
1106
+ async getBrokerConnections(accessToken) {
1107
+ return this.request('/brokers/connections', {
1108
+ method: 'GET',
1109
+ headers: {
1110
+ Authorization: `Bearer ${accessToken}`,
1111
+ 'Content-Type': 'application/json',
1112
+ },
1113
+ });
1114
+ }
1115
+ // Automatic token management versions of broker methods
1116
+ async getBrokerListAuto() {
1117
+ const accessToken = await this.getValidAccessToken();
1118
+ return this.getBrokerList(accessToken);
1119
+ }
1120
+ async getBrokerAccountsAuto(options) {
1121
+ const accessToken = await this.getValidAccessToken();
1122
+ return this.getBrokerAccounts(accessToken, options);
1123
+ }
1124
+ async getBrokerOrdersAuto(options) {
1125
+ const accessToken = await this.getValidAccessToken();
1126
+ return this.getBrokerOrders(accessToken, options);
1127
+ }
1128
+ async getBrokerPositionsAuto(options) {
1129
+ const accessToken = await this.getValidAccessToken();
1130
+ return this.getBrokerPositions(accessToken, options);
1131
+ }
1132
+ async getBrokerConnectionsAuto() {
1133
+ const accessToken = await this.getValidAccessToken();
1134
+ return this.getBrokerConnections(accessToken);
1135
+ }
1136
+ // Automatic token management versions of trading methods
1137
+ async placeBrokerOrderAuto(params, extras = {}) {
1138
+ const accessToken = await this.getValidAccessToken();
1139
+ return this.placeBrokerOrder(accessToken, params, extras);
1140
+ }
1141
+ async placeStockMarketOrderAuto(symbol, orderQty, action, broker, accountNumber, extras = {}) {
1142
+ const accessToken = await this.getValidAccessToken();
1143
+ return this.placeStockMarketOrder(accessToken, symbol, orderQty, action, broker, accountNumber, extras);
1144
+ }
1145
+ async placeStockLimitOrderAuto(symbol, orderQty, action, price, timeInForce = 'gtc', broker, accountNumber, extras = {}) {
1146
+ const accessToken = await this.getValidAccessToken();
1147
+ return this.placeStockLimitOrder(accessToken, symbol, orderQty, action, price, timeInForce, broker, accountNumber, extras);
1148
+ }
1149
+ async placeStockStopOrderAuto(symbol, orderQty, action, stopPrice, timeInForce = 'day', broker, accountNumber, extras = {}) {
1150
+ const accessToken = await this.getValidAccessToken();
1151
+ return this.placeStockStopOrder(accessToken, symbol, orderQty, action, stopPrice, timeInForce, broker, accountNumber, extras);
1152
+ }
1153
+ // Page-based pagination methods
1154
+ async getBrokerOrdersPage(page = 1, perPage = 100, filters) {
1155
+ const accessToken = await this.getValidAccessToken();
1156
+ const offset = (page - 1) * perPage;
1157
+ const params = {
1158
+ limit: perPage.toString(),
1159
+ offset: offset.toString(),
1160
+ };
1161
+ // Add filter parameters
1162
+ if (filters) {
1163
+ if (filters.broker_id)
1164
+ params.broker_id = filters.broker_id;
1165
+ if (filters.connection_id)
1166
+ params.connection_id = filters.connection_id;
1167
+ if (filters.account_id)
1168
+ params.account_id = filters.account_id;
1169
+ if (filters.symbol)
1170
+ params.symbol = filters.symbol;
1171
+ if (filters.status)
1172
+ params.status = filters.status;
1173
+ if (filters.side)
1174
+ params.side = filters.side;
1175
+ if (filters.asset_type)
1176
+ params.asset_type = filters.asset_type;
1177
+ if (filters.created_after)
1178
+ params.created_after = filters.created_after;
1179
+ if (filters.created_before)
1180
+ params.created_before = filters.created_before;
1181
+ }
1182
+ const response = await this.request('/brokers/data/orders', {
1183
+ method: 'GET',
1184
+ headers: {
1185
+ 'Content-Type': 'application/json',
1186
+ Authorization: `Bearer ${accessToken}`,
1187
+ 'X-Company-ID': this.companyId || '',
1188
+ 'X-Session-ID': this.currentSessionId || '',
1189
+ 'X-CSRF-Token': this.csrfToken || '',
1190
+ 'X-Device-Info': JSON.stringify({
1191
+ ip_address: this.deviceInfo?.ip_address || '',
1192
+ user_agent: this.deviceInfo?.user_agent || '',
1193
+ fingerprint: this.deviceInfo?.fingerprint || '',
1194
+ }),
1195
+ },
1196
+ params,
1197
+ });
1198
+ return new PaginatedResult(response.response_data, response.pagination || {
1199
+ has_more: false,
1200
+ next_offset: offset,
1201
+ current_offset: offset,
1202
+ limit: perPage,
1203
+ });
1204
+ }
1205
+ async getBrokerAccountsPage(page = 1, perPage = 100, filters) {
1206
+ const accessToken = await this.getValidAccessToken();
1207
+ const offset = (page - 1) * perPage;
1208
+ const params = {
1209
+ limit: perPage.toString(),
1210
+ offset: offset.toString(),
1211
+ };
1212
+ // Add filter parameters
1213
+ if (filters) {
1214
+ if (filters.broker_id)
1215
+ params.broker_id = filters.broker_id;
1216
+ if (filters.connection_id)
1217
+ params.connection_id = filters.connection_id;
1218
+ if (filters.account_type)
1219
+ params.account_type = filters.account_type;
1220
+ if (filters.status)
1221
+ params.status = filters.status;
1222
+ if (filters.currency)
1223
+ params.currency = filters.currency;
1224
+ }
1225
+ const response = await this.request('/brokers/data/accounts', {
1226
+ method: 'GET',
1227
+ headers: {
1228
+ 'Content-Type': 'application/json',
1229
+ Authorization: `Bearer ${accessToken}`,
1230
+ 'X-Company-ID': this.companyId || '',
1231
+ 'X-Session-ID': this.currentSessionId || '',
1232
+ 'X-CSRF-Token': this.csrfToken || '',
1233
+ 'X-Device-Info': JSON.stringify({
1234
+ ip_address: this.deviceInfo?.ip_address || '',
1235
+ user_agent: this.deviceInfo?.user_agent || '',
1236
+ fingerprint: this.deviceInfo?.fingerprint || '',
1237
+ }),
1238
+ },
1239
+ params,
1240
+ });
1241
+ return new PaginatedResult(response.response_data, response.pagination || {
1242
+ has_more: false,
1243
+ next_offset: offset,
1244
+ current_offset: offset,
1245
+ limit: perPage,
1246
+ });
1247
+ }
1248
+ async getBrokerPositionsPage(page = 1, perPage = 100, filters) {
1249
+ const accessToken = await this.getValidAccessToken();
1250
+ const offset = (page - 1) * perPage;
1251
+ const params = {
1252
+ limit: perPage.toString(),
1253
+ offset: offset.toString(),
1254
+ };
1255
+ // Add filter parameters
1256
+ if (filters) {
1257
+ if (filters.broker_id)
1258
+ params.broker_id = filters.broker_id;
1259
+ if (filters.account_id)
1260
+ params.account_id = filters.account_id;
1261
+ if (filters.symbol)
1262
+ params.symbol = filters.symbol;
1263
+ if (filters.position_status)
1264
+ params.position_status = filters.position_status;
1265
+ if (filters.side)
1266
+ params.side = filters.side;
1267
+ }
1268
+ const response = await this.request('/brokers/data/positions', {
1269
+ method: 'GET',
1270
+ headers: {
1271
+ 'Content-Type': 'application/json',
1272
+ Authorization: `Bearer ${accessToken}`,
1273
+ 'X-Company-ID': this.companyId || '',
1274
+ 'X-Session-ID': this.currentSessionId || '',
1275
+ 'X-CSRF-Token': this.csrfToken || '',
1276
+ 'X-Device-Info': JSON.stringify({
1277
+ ip_address: this.deviceInfo?.ip_address || '',
1278
+ user_agent: this.deviceInfo?.user_agent || '',
1279
+ fingerprint: this.deviceInfo?.fingerprint || '',
1280
+ }),
1281
+ },
1282
+ params,
1283
+ });
1284
+ return new PaginatedResult(response.response_data, response.pagination || {
1285
+ has_more: false,
1286
+ next_offset: offset,
1287
+ current_offset: offset,
1288
+ limit: perPage,
1289
+ });
1290
+ }
1291
+ // Navigation methods
1292
+ async getNextPage(previousResult, fetchFunction) {
1293
+ if (!previousResult.hasNext) {
1294
+ return null;
1295
+ }
1296
+ return fetchFunction(previousResult.metadata.nextOffset, previousResult.metadata.limit);
1297
+ }
1298
+ /**
1299
+ * Check if this is a mock client
1300
+ * @returns false for real API client
1301
+ */
1302
+ isMockClient() {
1303
+ return false;
1304
+ }
1305
+ }
1306
+
1307
+ class EventEmitter {
1308
+ constructor() {
1309
+ this.events = new Map();
1310
+ }
1311
+ on(event, callback) {
1312
+ if (!this.events.has(event)) {
1313
+ this.events.set(event, new Set());
1314
+ }
1315
+ this.events.get(event).add(callback);
1316
+ }
1317
+ off(event, callback) {
1318
+ if (this.events.has(event)) {
1319
+ this.events.get(event).delete(callback);
1320
+ }
1321
+ }
1322
+ once(event, callback) {
1323
+ const onceCallback = (...args) => {
1324
+ callback(...args);
1325
+ this.off(event, onceCallback);
1326
+ };
1327
+ this.on(event, onceCallback);
1328
+ }
1329
+ emit(event, ...args) {
1330
+ if (this.events.has(event)) {
1331
+ this.events.get(event).forEach(callback => {
1332
+ try {
1333
+ callback(...args);
1334
+ }
1335
+ catch (error) {
1336
+ console.error(`Error in event handler for ${event}:`, error);
1337
+ }
1338
+ });
1339
+ }
1340
+ }
1341
+ removeAllListeners(event) {
1342
+ if (event) {
1343
+ this.events.delete(event);
1344
+ }
1345
+ else {
1346
+ this.events.clear();
1347
+ }
1348
+ }
1349
+ listenerCount(event) {
1350
+ return this.events.get(event)?.size || 0;
1351
+ }
1352
+ listeners(event) {
1353
+ return Array.from(this.events.get(event) || []);
1354
+ }
1355
+ }
1356
+
1357
+ class PortalUI {
1358
+ constructor(portalUrl) {
1359
+ this.iframe = null;
1360
+ this.container = null;
1361
+ this.messageHandler = null;
1362
+ this.sessionId = null;
1363
+ this.portalOrigin = null;
1364
+ this.userToken = null;
1365
+ this.originalBodyStyle = null;
1366
+ this.createContainer();
1367
+ }
1368
+ createContainer() {
1369
+ console.debug('[PortalUI] Creating container and iframe');
1370
+ this.container = document.createElement('div');
1371
+ this.container.style.cssText = `
1372
+ position: fixed;
1373
+ top: 0;
1374
+ left: 0;
1375
+ width: 100%;
1376
+ height: 100%;
1377
+ background: rgba(0, 0, 0, 0.5);
1378
+ display: none;
1379
+ z-index: 9999;
1380
+ `;
1381
+ this.iframe = document.createElement('iframe');
1382
+ // Let the portal handle its own styling - only set essential attributes
1383
+ this.iframe.style.cssText = `
1384
+ position: absolute;
1385
+ top: 50%;
1386
+ left: 50%;
1387
+ transform: translate(-50%, -50%);
1388
+ width: 90%;
1389
+ max-width: 500px;
1390
+ height: 90%;
1391
+ max-height: 600px;
1392
+ border: none;
1393
+ border-radius: 24px;
1394
+ `;
1395
+ // Set security headers
1396
+ this.iframe.setAttribute('sandbox', 'allow-scripts allow-forms allow-popups allow-same-origin');
1397
+ this.iframe.setAttribute('referrerpolicy', 'strict-origin-when-cross-origin');
1398
+ this.iframe.setAttribute('allow', 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture');
1399
+ // Add CSP meta tag to allow Google Fonts and other required resources
1400
+ const meta = document.createElement('meta');
1401
+ meta.setAttribute('http-equiv', 'Content-Security-Policy');
1402
+ meta.setAttribute('content', `
1403
+ default-src 'self' https:;
1404
+ style-src 'self' 'unsafe-inline' https: https://fonts.googleapis.com https://fonts.gstatic.com https://cdn.jsdelivr.net;
1405
+ font-src 'self' https://fonts.gstatic.com;
1406
+ img-src 'self' data: https:;
1407
+ script-src 'self' 'unsafe-inline' 'unsafe-eval' https:;
1408
+ connect-src 'self' https:;
1409
+ frame-src 'self' https:;
1410
+ `
1411
+ .replace(/\s+/g, ' ')
1412
+ .trim());
1413
+ this.iframe.contentDocument?.head.appendChild(meta);
1414
+ this.container.appendChild(this.iframe);
1415
+ document.body.appendChild(this.container);
1416
+ console.debug('[PortalUI] Container and iframe created successfully');
1417
+ }
1418
+ /**
1419
+ * Lock background scrolling by setting overflow: hidden on body
1420
+ */
1421
+ lockScroll() {
1422
+ if (typeof document !== 'undefined' && document.body) {
1423
+ // Store original body style to restore later
1424
+ this.originalBodyStyle = document.body.style.cssText;
1425
+ // Add overflow: hidden to prevent scrolling
1426
+ document.body.style.overflow = 'hidden';
1427
+ document.body.style.position = 'fixed';
1428
+ document.body.style.width = '100%';
1429
+ document.body.style.top = `-${window.scrollY}px`;
1430
+ console.debug('[PortalUI] Background scroll locked');
1431
+ }
1432
+ }
1433
+ /**
1434
+ * Unlock background scrolling by restoring original body style
1435
+ */
1436
+ unlockScroll() {
1437
+ if (typeof document !== 'undefined' && document.body && this.originalBodyStyle !== null) {
1438
+ // Restore original body style
1439
+ document.body.style.cssText = this.originalBodyStyle;
1440
+ // Restore scroll position
1441
+ const scrollY = document.body.style.top;
1442
+ document.body.style.position = '';
1443
+ document.body.style.top = '';
1444
+ window.scrollTo(0, parseInt(scrollY || '0') * -1);
1445
+ this.originalBodyStyle = null;
1446
+ console.debug('[PortalUI] Background scroll unlocked');
1447
+ }
1448
+ }
1449
+ show(url, sessionId, options = {}) {
1450
+ if (!this.iframe || !this.container) {
1451
+ this.createContainer();
1452
+ }
1453
+ // Set portalOrigin to the actual portal URL's origin
1454
+ try {
1455
+ this.portalOrigin = new URL(url).origin;
1456
+ }
1457
+ catch (e) {
1458
+ this.portalOrigin = null;
1459
+ }
1460
+ this.sessionId = sessionId;
1461
+ this.options = options;
1462
+ this.container.style.display = 'block';
1463
+ this.iframe.src = url;
1464
+ // Lock background scrolling
1465
+ this.lockScroll();
1466
+ // Set up message handler
1467
+ this.messageHandler = this.handleMessage.bind(this);
1468
+ window.addEventListener('message', this.messageHandler);
1469
+ }
1470
+ hide() {
1471
+ if (this.container) {
1472
+ this.container.style.display = 'none';
1473
+ }
1474
+ if (this.iframe) {
1475
+ this.iframe.src = '';
1476
+ }
1477
+ if (this.messageHandler) {
1478
+ window.removeEventListener('message', this.messageHandler);
1479
+ this.messageHandler = null;
1480
+ }
1481
+ this.sessionId = null;
1482
+ // Unlock background scrolling
1483
+ this.unlockScroll();
1484
+ }
1485
+ handleMessage(event) {
1486
+ // Verify origin matches the portal URL
1487
+ if (!this.portalOrigin || event.origin !== this.portalOrigin) {
1488
+ console.warn('[PortalUI] Received message from unauthorized origin:', event.origin, 'Expected:', this.portalOrigin);
1489
+ return;
1490
+ }
1491
+ const { type, userId, access_token, refresh_token, error, height, data } = event.data;
1492
+ console.log('[PortalUI] Received message:', event.data);
1493
+ switch (type) {
1494
+ case 'portal-success': {
1495
+ // Handle both direct userId and data.userId formats
1496
+ const successUserId = userId || (data && data.userId);
1497
+ const successAccessToken = access_token || (data && data.access_token);
1498
+ const successRefreshToken = refresh_token || (data && data.refresh_token);
1499
+ this.handlePortalSuccess(successUserId, successAccessToken, successRefreshToken);
1500
+ break;
1501
+ }
1502
+ case 'portal-error': {
1503
+ // Handle both direct error and data.message formats
1504
+ const errorMessage = error || (data && data.message);
1505
+ this.handlePortalError(errorMessage);
1506
+ break;
1507
+ }
1508
+ case 'portal-close':
1509
+ this.handlePortalClose();
1510
+ break;
1511
+ case 'event':
1512
+ this.handleGenericEvent(data);
1513
+ break;
1514
+ case 'resize':
1515
+ this.handleResize(height);
1516
+ break;
1517
+ // Legacy support for old message types
1518
+ case 'success':
1519
+ this.handleSuccess(userId, access_token, refresh_token);
1520
+ break;
1521
+ case 'error':
1522
+ this.handleError(error);
1523
+ break;
1524
+ case 'close':
1525
+ this.handleClose();
1526
+ break;
1527
+ default:
1528
+ console.warn('[PortalUI] Received unhandled message type:', type);
1529
+ }
1530
+ }
1531
+ handlePortalSuccess(userId, accessToken, refreshToken) {
1532
+ if (!userId) {
1533
+ console.error('[PortalUI] Missing userId in portal-success message');
1534
+ return;
1535
+ }
1536
+ console.log('[PortalUI] Portal success - User connected:', userId);
1537
+ // If tokens are provided, store them internally
1538
+ if (accessToken && refreshToken) {
1539
+ const userToken = {
1540
+ accessToken: accessToken,
1541
+ refreshToken: refreshToken,
1542
+ expiresIn: 3600, // Default to 1 hour
1543
+ user_id: userId,
1544
+ tokenType: 'Bearer',
1545
+ scope: 'api:access',
1546
+ };
1547
+ this.userToken = userToken;
1548
+ console.log('[PortalUI] Portal authentication successful');
1549
+ }
1550
+ else {
1551
+ console.warn('[PortalUI] No tokens received from portal');
1552
+ }
1553
+ // Pass userId to parent (SDK will handle tokens internally)
1554
+ this.options?.onSuccess?.(userId);
1555
+ }
1556
+ handlePortalError(error) {
1557
+ console.error('[PortalUI] Portal error:', error);
1558
+ this.options?.onError?.(new Error(error || 'Unknown portal error'));
1559
+ }
1560
+ handlePortalClose() {
1561
+ console.log('[PortalUI] Portal closed by user');
1562
+ this.options?.onClose?.();
1563
+ this.hide();
1564
+ }
1565
+ handleGenericEvent(data) {
1566
+ if (!data || !data.type) {
1567
+ console.warn('[PortalUI] Invalid event data:', data);
1568
+ return;
1569
+ }
1570
+ console.log('[PortalUI] Generic event received:', data.type, data.data);
1571
+ // Emit the event to be handled by the SDK
1572
+ // This will be implemented in FinaticConnect
1573
+ if (this.options?.onEvent) {
1574
+ this.options.onEvent(data.type, data.data);
1575
+ }
1576
+ }
1577
+ handleSuccess(userId, access_token, refresh_token) {
1578
+ if (!userId || !access_token || !refresh_token) {
1579
+ console.error('[PortalUI] Missing required fields in success message');
1580
+ return;
1581
+ }
1582
+ // Convert portal tokens to UserToken format
1583
+ const userToken = {
1584
+ accessToken: access_token,
1585
+ refreshToken: refresh_token,
1586
+ expiresIn: 3600, // Default to 1 hour
1587
+ user_id: userId,
1588
+ tokenType: 'Bearer',
1589
+ scope: 'api:access',
1590
+ };
1591
+ // Store tokens internally
1592
+ this.userToken = userToken;
1593
+ // Pass userId to parent
1594
+ this.options?.onSuccess?.(userId);
1595
+ }
1596
+ handleError(error) {
1597
+ console.error('[PortalUI] Received error:', error);
1598
+ this.options?.onError?.(new Error(error || 'Unknown error'));
1599
+ }
1600
+ handleClose() {
1601
+ console.log('[PortalUI] Received close message');
1602
+ this.options?.onClose?.();
1603
+ this.hide();
1604
+ }
1605
+ handleResize(height) {
1606
+ if (height && this.iframe) {
1607
+ console.log('[PortalUI] Received resize message:', height);
1608
+ this.iframe.style.height = `${height}px`;
1609
+ }
1610
+ }
1611
+ getTokens() {
1612
+ return this.userToken;
1613
+ }
1614
+ }
1615
+
1616
+ /**
1617
+ * Mock data provider for Finatic API endpoints
1618
+ */
1619
+ class MockDataProvider {
1620
+ constructor(config = {}) {
1621
+ this.sessionData = new Map();
1622
+ this.userTokens = new Map();
1623
+ this.config = {
1624
+ delay: config.delay || this.getRandomDelay(50, 200),
1625
+ scenario: config.scenario || 'success',
1626
+ customData: config.customData || {},
1627
+ };
1628
+ }
1629
+ /**
1630
+ * Get a random delay between min and max milliseconds
1631
+ */
1632
+ getRandomDelay(min, max) {
1633
+ return Math.floor(Math.random() * (max - min + 1)) + min;
1634
+ }
1635
+ /**
1636
+ * Simulate network delay
1637
+ */
1638
+ async simulateDelay() {
1639
+ const delay = this.config.delay || this.getRandomDelay(50, 200);
1640
+ await new Promise(resolve => setTimeout(resolve, delay));
1641
+ }
1642
+ /**
1643
+ * Generate a realistic session ID
1644
+ */
1645
+ generateSessionId() {
1646
+ return `session_${v4().replace(/-/g, '')}`;
1647
+ }
1648
+ /**
1649
+ * Generate a realistic user ID
1650
+ */
1651
+ generateUserId() {
1652
+ return `user_${v4().replace(/-/g, '').substring(0, 8)}`;
1653
+ }
1654
+ /**
1655
+ * Generate a realistic company ID
1656
+ */
1657
+ generateCompanyId() {
1658
+ return `company_${v4().replace(/-/g, '').substring(0, 8)}`;
1659
+ }
1660
+ /**
1661
+ * Generate mock tokens
1662
+ */
1663
+ generateTokens(userId) {
1664
+ const accessToken = `mock_access_${v4().replace(/-/g, '')}`;
1665
+ const refreshToken = `mock_refresh_${v4().replace(/-/g, '')}`;
1666
+ return {
1667
+ accessToken,
1668
+ refreshToken,
1669
+ expiresIn: 3600, // 1 hour
1670
+ user_id: userId,
1671
+ tokenType: 'Bearer',
1672
+ scope: 'read write',
1673
+ };
1674
+ }
1675
+ // Authentication & Session Management Mocks
1676
+ async mockStartSession(token, userId) {
1677
+ await this.simulateDelay();
1678
+ const sessionId = this.generateSessionId();
1679
+ const companyId = this.generateCompanyId();
1680
+ const actualUserId = userId || this.generateUserId();
1681
+ const sessionData = {
1682
+ session_id: sessionId,
1683
+ state: SessionState.ACTIVE,
1684
+ company_id: companyId,
1685
+ status: 'active',
1686
+ expires_at: new Date(Date.now() + 30 * 60 * 1000).toISOString(), // 30 minutes
1687
+ user_id: actualUserId,
1688
+ auto_login: false,
1689
+ };
1690
+ this.sessionData.set(sessionId, sessionData);
1691
+ return {
1692
+ data: sessionData,
1693
+ message: 'Session started successfully',
1694
+ };
1695
+ }
1696
+ async mockRequestOtp(sessionId, email) {
1697
+ await this.simulateDelay();
1698
+ return {
1699
+ success: true,
1700
+ message: 'OTP sent successfully',
1701
+ data: true,
1702
+ };
1703
+ }
1704
+ async mockVerifyOtp(sessionId, otp) {
1705
+ await this.simulateDelay();
1706
+ const userId = this.generateUserId();
1707
+ const tokens = this.generateTokens(userId);
1708
+ this.userTokens.set(userId, tokens);
1709
+ return {
1710
+ success: true,
1711
+ message: 'OTP verified successfully',
1712
+ data: {
1713
+ access_token: tokens.accessToken,
1714
+ refresh_token: tokens.refreshToken,
1715
+ user_id: userId,
1716
+ expires_in: tokens.expiresIn,
1717
+ scope: tokens.scope,
1718
+ token_type: tokens.tokenType,
1719
+ },
1720
+ };
1721
+ }
1722
+ async mockAuthenticateDirectly(sessionId, userId) {
1723
+ await this.simulateDelay();
1724
+ const tokens = this.generateTokens(userId);
1725
+ this.userTokens.set(userId, tokens);
1726
+ return {
1727
+ success: true,
1728
+ message: 'Authentication successful',
1729
+ data: {
1730
+ access_token: tokens.accessToken,
1731
+ refresh_token: tokens.refreshToken,
1732
+ },
1733
+ };
1734
+ }
1735
+ async mockGetPortalUrl(sessionId) {
1736
+ await this.simulateDelay();
1737
+ const scenario = this.getScenario();
1738
+ if (scenario === 'error') {
1739
+ return {
1740
+ success: false,
1741
+ message: 'Failed to retrieve portal URL (mock error)',
1742
+ data: { portal_url: '' },
1743
+ };
1744
+ }
1745
+ if (scenario === 'network_error') {
1746
+ throw new Error('Network error (mock)');
1747
+ }
1748
+ if (scenario === 'rate_limit') {
1749
+ return {
1750
+ success: false,
1751
+ message: 'Rate limit exceeded (mock)',
1752
+ data: { portal_url: '' },
1753
+ };
1754
+ }
1755
+ if (scenario === 'auth_failure') {
1756
+ return {
1757
+ success: false,
1758
+ message: 'Authentication failed (mock)',
1759
+ data: { portal_url: '' },
1760
+ };
1761
+ }
1762
+ // Default: success
1763
+ return {
1764
+ success: true,
1765
+ message: 'Portal URL retrieved successfully',
1766
+ data: {
1767
+ portal_url: 'http://localhost:3000/mock-portal',
1768
+ },
1769
+ };
1770
+ }
1771
+ async mockValidatePortalSession(sessionId, signature) {
1772
+ await this.simulateDelay();
1773
+ return {
1774
+ valid: true,
1775
+ company_id: this.generateCompanyId(),
1776
+ status: 'active',
1777
+ };
1778
+ }
1779
+ async mockCompletePortalSession(sessionId) {
1780
+ await this.simulateDelay();
1781
+ return {
1782
+ success: true,
1783
+ message: 'Portal session completed successfully',
1784
+ data: {
1785
+ portal_url: `https://portal.finatic.dev/complete/${sessionId}`,
1786
+ },
1787
+ };
1788
+ }
1789
+ async mockRefreshToken(refreshToken) {
1790
+ await this.simulateDelay();
1791
+ const newAccessToken = `mock_access_${v4().replace(/-/g, '')}`;
1792
+ const newRefreshToken = `mock_refresh_${v4().replace(/-/g, '')}`;
1793
+ return {
1794
+ success: true,
1795
+ response_data: {
1796
+ access_token: newAccessToken,
1797
+ refresh_token: newRefreshToken,
1798
+ expires_at: new Date(Date.now() + 3600 * 1000).toISOString(),
1799
+ company_id: this.generateCompanyId(),
1800
+ company_name: 'Mock Company',
1801
+ email_verified: true,
1802
+ },
1803
+ message: 'Token refreshed successfully',
1804
+ };
1805
+ }
1806
+ // Broker Management Mocks
1807
+ async mockGetBrokerList() {
1808
+ await this.simulateDelay();
1809
+ const brokers = [
1810
+ {
1811
+ id: 'robinhood',
1812
+ name: 'robinhood',
1813
+ display_name: 'Robinhood',
1814
+ description: 'Commission-free stock and options trading',
1815
+ website: 'https://robinhood.com',
1816
+ features: ['stocks', 'options', 'crypto', 'fractional_shares'],
1817
+ auth_type: 'oauth',
1818
+ logo_path: '/logos/robinhood.png',
1819
+ is_active: true,
1820
+ },
1821
+ {
1822
+ id: 'tasty_trade',
1823
+ name: 'tasty_trade',
1824
+ display_name: 'TastyTrade',
1825
+ description: 'Options and futures trading platform',
1826
+ website: 'https://tastytrade.com',
1827
+ features: ['options', 'futures', 'stocks'],
1828
+ auth_type: 'oauth',
1829
+ logo_path: '/logos/tastytrade.png',
1830
+ is_active: true,
1831
+ },
1832
+ {
1833
+ id: 'ninja_trader',
1834
+ name: 'ninja_trader',
1835
+ display_name: 'NinjaTrader',
1836
+ description: 'Advanced futures and forex trading platform',
1837
+ website: 'https://ninjatrader.com',
1838
+ features: ['futures', 'forex', 'options'],
1839
+ auth_type: 'api_key',
1840
+ logo_path: '/logos/ninjatrader.png',
1841
+ is_active: true,
1842
+ },
1843
+ ];
1844
+ return {
1845
+ _id: v4(),
1846
+ response_data: brokers,
1847
+ message: 'Broker list retrieved successfully',
1848
+ status_code: 200,
1849
+ warnings: null,
1850
+ errors: null,
1851
+ };
1852
+ }
1853
+ async mockGetBrokerAccounts() {
1854
+ await this.simulateDelay();
1855
+ const accounts = [
1856
+ {
1857
+ id: v4(),
1858
+ user_broker_connection_id: v4(),
1859
+ broker_provided_account_id: '123456789',
1860
+ account_name: 'Individual Account',
1861
+ account_type: 'individual',
1862
+ currency: 'USD',
1863
+ cash_balance: 15000.5,
1864
+ buying_power: 45000.0,
1865
+ status: 'active',
1866
+ created_at: new Date().toISOString(),
1867
+ updated_at: new Date().toISOString(),
1868
+ last_synced_at: new Date().toISOString(),
1869
+ },
1870
+ {
1871
+ id: v4(),
1872
+ user_broker_connection_id: v4(),
1873
+ broker_provided_account_id: '987654321',
1874
+ account_name: 'IRA Account',
1875
+ account_type: 'ira',
1876
+ currency: 'USD',
1877
+ cash_balance: 25000.75,
1878
+ buying_power: 75000.0,
1879
+ status: 'active',
1880
+ created_at: new Date().toISOString(),
1881
+ updated_at: new Date().toISOString(),
1882
+ last_synced_at: new Date().toISOString(),
1883
+ },
1884
+ ];
1885
+ return {
1886
+ _id: v4(),
1887
+ response_data: accounts,
1888
+ message: 'Broker accounts retrieved successfully',
1889
+ status_code: 200,
1890
+ warnings: null,
1891
+ errors: null,
1892
+ };
1893
+ }
1894
+ async mockGetBrokerConnections() {
1895
+ await this.simulateDelay();
1896
+ const connections = [
1897
+ {
1898
+ id: v4(),
1899
+ broker_id: 'robinhood',
1900
+ user_id: this.generateUserId(),
1901
+ company_id: this.generateCompanyId(),
1902
+ status: 'connected',
1903
+ connected_at: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString(), // 7 days ago
1904
+ last_synced_at: new Date().toISOString(),
1905
+ permissions: {
1906
+ read: true,
1907
+ write: true,
1908
+ },
1909
+ metadata: {
1910
+ nickname: 'My Robinhood',
1911
+ },
1912
+ needs_reauth: false,
1913
+ },
1914
+ {
1915
+ id: v4(),
1916
+ broker_id: 'tasty_trade',
1917
+ user_id: this.generateUserId(),
1918
+ company_id: this.generateCompanyId(),
1919
+ status: 'connected',
1920
+ connected_at: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000).toISOString(), // 3 days ago
1921
+ last_synced_at: new Date().toISOString(),
1922
+ permissions: {
1923
+ read: true,
1924
+ write: false,
1925
+ },
1926
+ metadata: {
1927
+ nickname: 'Tasty Options',
1928
+ },
1929
+ needs_reauth: false,
1930
+ },
1931
+ ];
1932
+ return {
1933
+ _id: v4(),
1934
+ response_data: connections,
1935
+ message: 'Broker connections retrieved successfully',
1936
+ status_code: 200,
1937
+ warnings: null,
1938
+ errors: null,
1939
+ };
1940
+ }
1941
+ // Portfolio & Trading Mocks
1942
+ async mockGetHoldings() {
1943
+ await this.simulateDelay();
1944
+ const holdings = [
1945
+ {
1946
+ symbol: 'AAPL',
1947
+ quantity: 100,
1948
+ averagePrice: 150.25,
1949
+ currentPrice: 175.5,
1950
+ marketValue: 17550.0,
1951
+ unrealizedPnL: 2525.0,
1952
+ realizedPnL: 0,
1953
+ costBasis: 15025.0,
1954
+ currency: 'USD',
1955
+ },
1956
+ {
1957
+ symbol: 'TSLA',
1958
+ quantity: 50,
1959
+ averagePrice: 200.0,
1960
+ currentPrice: 220.75,
1961
+ marketValue: 11037.5,
1962
+ unrealizedPnL: 1037.5,
1963
+ realizedPnL: 0,
1964
+ costBasis: 10000.0,
1965
+ currency: 'USD',
1966
+ },
1967
+ {
1968
+ symbol: 'MSFT',
1969
+ quantity: 75,
1970
+ averagePrice: 300.0,
1971
+ currentPrice: 325.25,
1972
+ marketValue: 24393.75,
1973
+ unrealizedPnL: 1893.75,
1974
+ realizedPnL: 0,
1975
+ costBasis: 22500.0,
1976
+ currency: 'USD',
1977
+ },
1978
+ ];
1979
+ return { data: holdings };
1980
+ }
1981
+ async mockGetPortfolio() {
1982
+ await this.simulateDelay();
1983
+ const portfolio = {
1984
+ id: v4(),
1985
+ name: 'Main Portfolio',
1986
+ type: 'individual',
1987
+ status: 'active',
1988
+ cash: 15000.5,
1989
+ buyingPower: 45000.0,
1990
+ equity: 52981.25,
1991
+ longMarketValue: 37981.25,
1992
+ shortMarketValue: 0,
1993
+ initialMargin: 0,
1994
+ maintenanceMargin: 0,
1995
+ lastEquity: 52000.0,
1996
+ positions: [
1997
+ {
1998
+ symbol: 'AAPL',
1999
+ quantity: 100,
2000
+ averagePrice: 150.25,
2001
+ currentPrice: 175.5,
2002
+ marketValue: 17550.0,
2003
+ unrealizedPnL: 2525.0,
2004
+ realizedPnL: 0,
2005
+ costBasis: 15025.0,
2006
+ currency: 'USD',
2007
+ },
2008
+ {
2009
+ symbol: 'TSLA',
2010
+ quantity: 50,
2011
+ averagePrice: 200.0,
2012
+ currentPrice: 220.75,
2013
+ marketValue: 11037.5,
2014
+ unrealizedPnL: 1037.5,
2015
+ realizedPnL: 0,
2016
+ costBasis: 10000.0,
2017
+ currency: 'USD',
2018
+ },
2019
+ {
2020
+ symbol: 'MSFT',
2021
+ quantity: 75,
2022
+ averagePrice: 300.0,
2023
+ currentPrice: 325.25,
2024
+ marketValue: 24393.75,
2025
+ unrealizedPnL: 1893.75,
2026
+ realizedPnL: 0,
2027
+ costBasis: 22500.0,
2028
+ currency: 'USD',
2029
+ },
2030
+ ],
2031
+ performance: {
2032
+ totalReturn: 0.089,
2033
+ dailyReturn: 0.002,
2034
+ weeklyReturn: 0.015,
2035
+ monthlyReturn: 0.045,
2036
+ yearlyReturn: 0.089,
2037
+ maxDrawdown: -0.05,
2038
+ sharpeRatio: 1.2,
2039
+ beta: 0.95,
2040
+ alpha: 0.02,
2041
+ },
2042
+ };
2043
+ return { data: portfolio };
2044
+ }
2045
+ async mockGetOrders(filter) {
2046
+ await this.simulateDelay();
2047
+ const mockOrders = [
2048
+ {
2049
+ symbol: 'AAPL',
2050
+ side: 'buy',
2051
+ quantity: 100,
2052
+ type_: 'market',
2053
+ timeInForce: 'day',
2054
+ },
2055
+ {
2056
+ symbol: 'TSLA',
2057
+ side: 'sell',
2058
+ quantity: 50,
2059
+ type_: 'limit',
2060
+ price: 250.0,
2061
+ timeInForce: 'day',
2062
+ },
2063
+ ];
2064
+ // Apply filters if provided
2065
+ let filteredOrders = mockOrders;
2066
+ if (filter) {
2067
+ filteredOrders = this.applyOrderFilters(mockOrders, filter);
2068
+ }
2069
+ return { data: filteredOrders };
2070
+ }
2071
+ async mockGetBrokerOrders(filter) {
2072
+ await this.simulateDelay();
2073
+ // Determine how many orders to generate based on limit parameter
2074
+ const limit = filter?.limit || 100;
2075
+ const maxLimit = Math.min(limit, 1000); // Cap at 1000
2076
+ const count = Math.max(1, maxLimit); // At least 1
2077
+ // Generate diverse mock orders based on requested count
2078
+ const mockOrders = this.generateMockOrders(count);
2079
+ // Apply filters if provided
2080
+ let filteredOrders = mockOrders;
2081
+ if (filter) {
2082
+ filteredOrders = this.applyBrokerOrderFilters(mockOrders, filter);
2083
+ }
2084
+ return {
2085
+ data: filteredOrders,
2086
+ };
2087
+ }
2088
+ async mockGetBrokerPositions(filter) {
2089
+ await this.simulateDelay();
2090
+ // Determine how many positions to generate based on limit parameter
2091
+ const limit = filter?.limit || 100;
2092
+ const maxLimit = Math.min(limit, 1000); // Cap at 1000
2093
+ const count = Math.max(1, maxLimit); // At least 1
2094
+ // Generate diverse mock positions based on requested count
2095
+ const mockPositions = this.generateMockPositions(count);
2096
+ // Apply filters if provided
2097
+ let filteredPositions = mockPositions;
2098
+ if (filter) {
2099
+ filteredPositions = this.applyBrokerPositionFilters(mockPositions, filter);
2100
+ }
2101
+ return {
2102
+ data: filteredPositions,
2103
+ };
2104
+ }
2105
+ async mockGetBrokerDataAccounts(filter) {
2106
+ await this.simulateDelay();
2107
+ // Determine how many accounts to generate based on limit parameter
2108
+ const limit = filter?.limit || 100;
2109
+ const maxLimit = Math.min(limit, 1000); // Cap at 1000
2110
+ const count = Math.max(1, maxLimit); // At least 1
2111
+ // Generate diverse mock accounts based on requested count
2112
+ const mockAccounts = this.generateMockAccounts(count);
2113
+ // Apply filters if provided
2114
+ let filteredAccounts = mockAccounts;
2115
+ if (filter) {
2116
+ filteredAccounts = this.applyBrokerAccountFilters(mockAccounts, filter);
2117
+ }
2118
+ return {
2119
+ data: filteredAccounts,
2120
+ };
2121
+ }
2122
+ async mockPlaceOrder(order) {
2123
+ await this.simulateDelay();
2124
+ return {
2125
+ success: true,
2126
+ response_data: {
2127
+ orderId: v4(),
2128
+ status: 'submitted',
2129
+ broker: 'robinhood',
2130
+ accountNumber: '123456789',
2131
+ },
2132
+ message: 'Order placed successfully',
2133
+ status_code: 200,
2134
+ };
2135
+ }
2136
+ // Utility methods
2137
+ /**
2138
+ * Get stored session data
2139
+ */
2140
+ getSessionData(sessionId) {
2141
+ return this.sessionData.get(sessionId);
2142
+ }
2143
+ /**
2144
+ * Get stored user token
2145
+ */
2146
+ getUserToken(userId) {
2147
+ return this.userTokens.get(userId);
2148
+ }
2149
+ /**
2150
+ * Clear all stored data
2151
+ */
2152
+ clearData() {
2153
+ this.sessionData.clear();
2154
+ this.userTokens.clear();
2155
+ }
2156
+ /**
2157
+ * Update configuration
2158
+ */
2159
+ updateConfig(config) {
2160
+ this.config = { ...this.config, ...config };
2161
+ }
2162
+ setScenario(scenario) {
2163
+ this.config.scenario = scenario;
2164
+ }
2165
+ getScenario() {
2166
+ return this.config.scenario || 'success';
2167
+ }
2168
+ // Helper methods to apply filters
2169
+ applyOrderFilters(orders, filter) {
2170
+ return orders.filter(order => {
2171
+ if (filter.symbol && order.symbol !== filter.symbol)
2172
+ return false;
2173
+ if (filter.side && order.side !== filter.side)
2174
+ return false;
2175
+ return true;
2176
+ });
2177
+ }
2178
+ applyBrokerOrderFilters(orders, filter) {
2179
+ return orders.filter(order => {
2180
+ if (filter.broker_id && order.broker_id !== filter.broker_id)
2181
+ return false;
2182
+ if (filter.connection_id && order.connection_id !== filter.connection_id)
2183
+ return false;
2184
+ if (filter.account_id && order.account_id !== filter.account_id)
2185
+ return false;
2186
+ if (filter.symbol && order.symbol !== filter.symbol)
2187
+ return false;
2188
+ if (filter.status && order.status !== filter.status)
2189
+ return false;
2190
+ if (filter.side && order.side !== filter.side)
2191
+ return false;
2192
+ if (filter.asset_type && order.asset_type !== filter.asset_type)
2193
+ return false;
2194
+ if (filter.created_after && new Date(order.created_at) < new Date(filter.created_after))
2195
+ return false;
2196
+ if (filter.created_before && new Date(order.created_at) > new Date(filter.created_before))
2197
+ return false;
2198
+ return true;
2199
+ });
2200
+ }
2201
+ applyBrokerPositionFilters(positions, filter) {
2202
+ return positions.filter(position => {
2203
+ if (filter.broker_id && position.broker_id !== filter.broker_id)
2204
+ return false;
2205
+ if (filter.connection_id && position.connection_id !== filter.connection_id)
2206
+ return false;
2207
+ if (filter.account_id && position.account_id !== filter.account_id)
2208
+ return false;
2209
+ if (filter.symbol && position.symbol !== filter.symbol)
2210
+ return false;
2211
+ if (filter.side && position.side !== filter.side)
2212
+ return false;
2213
+ if (filter.asset_type && position.asset_type !== filter.asset_type)
2214
+ return false;
2215
+ if (filter.position_status && position.position_status !== filter.position_status)
2216
+ return false;
2217
+ if (filter.updated_after && new Date(position.updated_at) < new Date(filter.updated_after))
2218
+ return false;
2219
+ if (filter.updated_before && new Date(position.updated_at) > new Date(filter.updated_before))
2220
+ return false;
2221
+ return true;
2222
+ });
2223
+ }
2224
+ applyBrokerAccountFilters(accounts, filter) {
2225
+ return accounts.filter(account => {
2226
+ if (filter.broker_id && account.broker_id !== filter.broker_id)
2227
+ return false;
2228
+ if (filter.connection_id && account.connection_id !== filter.connection_id)
2229
+ return false;
2230
+ if (filter.account_type && account.account_type !== filter.account_type)
2231
+ return false;
2232
+ if (filter.status && account.status !== filter.status)
2233
+ return false;
2234
+ if (filter.currency && account.currency !== filter.currency)
2235
+ return false;
2236
+ return true;
2237
+ });
2238
+ }
2239
+ /**
2240
+ * Generate mock orders with diverse data
2241
+ */
2242
+ generateMockOrders(count) {
2243
+ const orders = [];
2244
+ const symbols = [
2245
+ 'AAPL',
2246
+ 'TSLA',
2247
+ 'MSFT',
2248
+ 'GOOGL',
2249
+ 'AMZN',
2250
+ 'META',
2251
+ 'NVDA',
2252
+ 'NFLX',
2253
+ 'SPY',
2254
+ 'QQQ',
2255
+ 'IWM',
2256
+ 'VTI',
2257
+ 'BTC',
2258
+ 'ETH',
2259
+ 'ADA',
2260
+ ];
2261
+ const brokers = ['robinhood', 'alpaca', 'tasty_trade', 'ninja_trader'];
2262
+ const orderTypes = ['market', 'limit', 'stop', 'stop_limit'];
2263
+ const sides = ['buy', 'sell'];
2264
+ const statuses = ['filled', 'pending', 'cancelled', 'rejected', 'partially_filled'];
2265
+ const assetTypes = ['stock', 'option', 'crypto', 'future'];
2266
+ for (let i = 0; i < count; i++) {
2267
+ const symbol = symbols[Math.floor(Math.random() * symbols.length)];
2268
+ const broker = brokers[Math.floor(Math.random() * brokers.length)];
2269
+ const orderType = orderTypes[Math.floor(Math.random() * orderTypes.length)];
2270
+ const side = sides[Math.floor(Math.random() * sides.length)];
2271
+ const status = statuses[Math.floor(Math.random() * statuses.length)];
2272
+ const assetType = assetTypes[Math.floor(Math.random() * assetTypes.length)];
2273
+ const quantity = Math.floor(Math.random() * 1000) + 1;
2274
+ const price = Math.random() * 500 + 10;
2275
+ const isFilled = status === 'filled' || status === 'partially_filled';
2276
+ const filledQuantity = isFilled ? Math.floor(quantity * (0.5 + Math.random() * 0.5)) : 0;
2277
+ const filledAvgPrice = isFilled ? price * (0.95 + Math.random() * 0.1) : 0;
2278
+ orders.push({
2279
+ id: v4(),
2280
+ broker_id: broker,
2281
+ connection_id: v4(),
2282
+ account_id: `account_${Math.floor(Math.random() * 1000000)}`,
2283
+ order_id: `order_${String(i + 1).padStart(6, '0')}`,
2284
+ symbol,
2285
+ order_type: orderType,
2286
+ side,
2287
+ quantity,
2288
+ price,
2289
+ status,
2290
+ asset_type: assetType,
2291
+ created_at: new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000).toISOString(), // Random date within last 30 days
2292
+ updated_at: new Date().toISOString(),
2293
+ filled_at: isFilled ? new Date().toISOString() : undefined,
2294
+ filled_quantity: filledQuantity,
2295
+ filled_avg_price: filledAvgPrice,
2296
+ });
2297
+ }
2298
+ return orders;
2299
+ }
2300
+ /**
2301
+ * Generate mock positions with diverse data
2302
+ */
2303
+ generateMockPositions(count) {
2304
+ const positions = [];
2305
+ const symbols = [
2306
+ 'AAPL',
2307
+ 'TSLA',
2308
+ 'MSFT',
2309
+ 'GOOGL',
2310
+ 'AMZN',
2311
+ 'META',
2312
+ 'NVDA',
2313
+ 'NFLX',
2314
+ 'SPY',
2315
+ 'QQQ',
2316
+ 'IWM',
2317
+ 'VTI',
2318
+ 'BTC',
2319
+ 'ETH',
2320
+ 'ADA',
2321
+ ];
2322
+ const brokers = ['robinhood', 'alpaca', 'tasty_trade', 'ninja_trader'];
2323
+ const sides = ['long', 'short'];
2324
+ const assetTypes = ['stock', 'option', 'crypto', 'future'];
2325
+ const positionStatuses = ['open', 'closed'];
2326
+ for (let i = 0; i < count; i++) {
2327
+ const symbol = symbols[Math.floor(Math.random() * symbols.length)];
2328
+ const broker = brokers[Math.floor(Math.random() * brokers.length)];
2329
+ const side = sides[Math.floor(Math.random() * sides.length)];
2330
+ const assetType = assetTypes[Math.floor(Math.random() * assetTypes.length)];
2331
+ const positionStatus = positionStatuses[Math.floor(Math.random() * positionStatuses.length)];
2332
+ const quantity = Math.floor(Math.random() * 1000) + 1;
2333
+ const averagePrice = Math.random() * 500 + 10;
2334
+ const currentPrice = averagePrice * (0.8 + Math.random() * 0.4); // ±20% variation
2335
+ const marketValue = quantity * currentPrice;
2336
+ const costBasis = quantity * averagePrice;
2337
+ const unrealizedGainLoss = marketValue - costBasis;
2338
+ const unrealizedGainLossPercent = (unrealizedGainLoss / costBasis) * 100;
2339
+ positions.push({
2340
+ id: v4(),
2341
+ broker_id: broker,
2342
+ connection_id: v4(),
2343
+ account_id: `account_${Math.floor(Math.random() * 1000000)}`,
2344
+ symbol,
2345
+ asset_type: assetType,
2346
+ side,
2347
+ quantity,
2348
+ average_price: averagePrice,
2349
+ market_value: marketValue,
2350
+ cost_basis: costBasis,
2351
+ unrealized_gain_loss: unrealizedGainLoss,
2352
+ unrealized_gain_loss_percent: unrealizedGainLossPercent,
2353
+ current_price: currentPrice,
2354
+ last_price: currentPrice,
2355
+ last_price_updated_at: new Date().toISOString(),
2356
+ position_status: positionStatus,
2357
+ created_at: new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000).toISOString(), // Random date within last 30 days
2358
+ updated_at: new Date().toISOString(),
2359
+ });
2360
+ }
2361
+ return positions;
2362
+ }
2363
+ /**
2364
+ * Generate mock accounts with diverse data
2365
+ */
2366
+ generateMockAccounts(count) {
2367
+ const accounts = [];
2368
+ const brokers = ['robinhood', 'alpaca', 'tasty_trade', 'ninja_trader'];
2369
+ const accountTypes = ['margin', 'cash', 'crypto_wallet', 'live', 'sim'];
2370
+ const statuses = ['active', 'inactive'];
2371
+ const currencies = ['USD', 'EUR', 'GBP', 'CAD'];
2372
+ const accountNames = [
2373
+ 'Individual Account',
2374
+ 'Paper Trading',
2375
+ 'Retirement Account',
2376
+ 'Crypto Wallet',
2377
+ 'Margin Account',
2378
+ 'Cash Account',
2379
+ 'Trading Account',
2380
+ 'Investment Account',
2381
+ ];
2382
+ for (let i = 0; i < count; i++) {
2383
+ const broker = brokers[Math.floor(Math.random() * brokers.length)];
2384
+ const accountType = accountTypes[Math.floor(Math.random() * accountTypes.length)];
2385
+ const status = statuses[Math.floor(Math.random() * statuses.length)];
2386
+ const currency = currencies[Math.floor(Math.random() * currencies.length)];
2387
+ const accountName = accountNames[Math.floor(Math.random() * accountNames.length)];
2388
+ const cashBalance = Math.random() * 100000 + 1000;
2389
+ const buyingPower = cashBalance * (1 + Math.random() * 2); // 1-3x cash balance
2390
+ const equity = buyingPower * (0.8 + Math.random() * 0.4); // ±20% variation
2391
+ accounts.push({
2392
+ id: v4(),
2393
+ broker_id: broker,
2394
+ connection_id: v4(),
2395
+ account_id: `account_${Math.floor(Math.random() * 1000000)}`,
2396
+ account_name: `${accountName} ${i + 1}`,
2397
+ account_type: accountType,
2398
+ status,
2399
+ currency,
2400
+ cash_balance: cashBalance,
2401
+ buying_power: buyingPower,
2402
+ equity,
2403
+ created_at: new Date(Date.now() - Math.random() * 365 * 24 * 60 * 60 * 1000).toISOString(), // Random date within last year
2404
+ updated_at: new Date().toISOString(),
2405
+ last_synced_at: new Date().toISOString(),
2406
+ });
2407
+ }
2408
+ return accounts;
2409
+ }
2410
+ }
2411
+
2412
+ /**
2413
+ * Mock API Client that implements the same interface as the real ApiClient
2414
+ * but returns mock data instead of making HTTP requests
2415
+ */
2416
+ class MockApiClient {
2417
+ constructor(baseUrl, deviceInfo, mockConfig) {
2418
+ this.currentSessionState = null;
2419
+ this.currentSessionId = null;
2420
+ this.tradingContext = {};
2421
+ // Token management
2422
+ this.tokenInfo = null;
2423
+ this.refreshPromise = null;
2424
+ this.REFRESH_BUFFER_MINUTES = 5;
2425
+ // Session and company context
2426
+ this.companyId = null;
2427
+ this.csrfToken = null;
2428
+ this.baseUrl = baseUrl;
2429
+ this.deviceInfo = deviceInfo;
2430
+ this.mockApiOnly = mockConfig?.mockApiOnly || false;
2431
+ this.mockDataProvider = new MockDataProvider(mockConfig);
2432
+ // Log that mocks are being used
2433
+ if (this.mockApiOnly) {
2434
+ console.log('🔧 Finatic SDK: Using MOCK API Client (API only - real portal)');
2435
+ }
2436
+ else {
2437
+ console.log('🔧 Finatic SDK: Using MOCK API Client');
2438
+ }
2439
+ }
2440
+ /**
2441
+ * Store tokens after successful authentication
2442
+ */
2443
+ setTokens(accessToken, refreshToken, expiresAt, userId) {
2444
+ this.tokenInfo = {
2445
+ accessToken,
2446
+ refreshToken,
2447
+ expiresAt,
2448
+ userId,
2449
+ };
2450
+ }
2451
+ /**
2452
+ * Get the current access token, refreshing if necessary
2453
+ */
2454
+ async getValidAccessToken() {
2455
+ if (!this.tokenInfo) {
2456
+ throw new AuthenticationError('No tokens available. Please authenticate first.');
2457
+ }
2458
+ // Check if token is expired or about to expire
2459
+ if (this.isTokenExpired()) {
2460
+ await this.refreshTokens();
2461
+ }
2462
+ return this.tokenInfo.accessToken;
2463
+ }
2464
+ /**
2465
+ * Check if the current token is expired or about to expire
2466
+ */
2467
+ isTokenExpired() {
2468
+ if (!this.tokenInfo)
2469
+ return true;
2470
+ const expiryTime = new Date(this.tokenInfo.expiresAt).getTime();
2471
+ const currentTime = Date.now();
2472
+ const bufferTime = this.REFRESH_BUFFER_MINUTES * 60 * 1000;
2473
+ return currentTime >= expiryTime - bufferTime;
2474
+ }
2475
+ /**
2476
+ * Refresh the access token using the refresh token
2477
+ */
2478
+ async refreshTokens() {
2479
+ if (!this.tokenInfo) {
2480
+ throw new AuthenticationError('No refresh token available.');
2481
+ }
2482
+ // If a refresh is already in progress, wait for it
2483
+ if (this.refreshPromise) {
2484
+ await this.refreshPromise;
2485
+ return;
2486
+ }
2487
+ // Start a new refresh
2488
+ this.refreshPromise = this.performTokenRefresh();
2489
+ try {
2490
+ await this.refreshPromise;
2491
+ }
2492
+ finally {
2493
+ this.refreshPromise = null;
2494
+ }
2495
+ }
2496
+ /**
2497
+ * Perform the actual token refresh request
2498
+ */
2499
+ async performTokenRefresh() {
2500
+ if (!this.tokenInfo) {
2501
+ throw new AuthenticationError('No refresh token available.');
2502
+ }
2503
+ try {
2504
+ const response = await this.mockDataProvider.mockRefreshToken(this.tokenInfo.refreshToken);
2505
+ // Update stored tokens
2506
+ this.tokenInfo = {
2507
+ accessToken: response.response_data.access_token,
2508
+ refreshToken: response.response_data.refresh_token,
2509
+ expiresAt: response.response_data.expires_at,
2510
+ userId: this.tokenInfo.userId,
2511
+ };
2512
+ return this.tokenInfo;
2513
+ }
2514
+ catch (error) {
2515
+ // Clear tokens on refresh failure
2516
+ this.tokenInfo = null;
2517
+ throw new AuthenticationError('Token refresh failed. Please re-authenticate.', error);
2518
+ }
2519
+ }
2520
+ /**
2521
+ * Clear stored tokens (useful for logout)
2522
+ */
2523
+ clearTokens() {
2524
+ this.tokenInfo = null;
2525
+ this.refreshPromise = null;
2526
+ }
2527
+ /**
2528
+ * Get current token info (for debugging/testing)
2529
+ */
2530
+ getTokenInfo() {
2531
+ return this.tokenInfo ? { ...this.tokenInfo } : null;
2532
+ }
2533
+ /**
2534
+ * Set session context (session ID, company ID, CSRF token)
2535
+ */
2536
+ setSessionContext(sessionId, companyId, csrfToken) {
2537
+ this.currentSessionId = sessionId;
2538
+ this.companyId = companyId;
2539
+ this.csrfToken = csrfToken || null;
2540
+ }
2541
+ /**
2542
+ * Get the current session ID
2543
+ */
2544
+ getCurrentSessionId() {
2545
+ return this.currentSessionId;
2546
+ }
2547
+ /**
2548
+ * Get the current company ID
2549
+ */
2550
+ getCurrentCompanyId() {
2551
+ return this.companyId;
2552
+ }
2553
+ /**
2554
+ * Get the current CSRF token
2555
+ */
2556
+ getCurrentCsrfToken() {
2557
+ return this.csrfToken;
2558
+ }
2559
+ // Session Management
2560
+ async startSession(token, userId) {
2561
+ const response = await this.mockDataProvider.mockStartSession(token, userId);
2562
+ // Store session ID and set state to ACTIVE
2563
+ this.currentSessionId = response.data.session_id;
2564
+ this.currentSessionState = SessionState.ACTIVE;
2565
+ return response;
2566
+ }
2567
+ // OTP Flow
2568
+ async requestOtp(sessionId, email) {
2569
+ return this.mockDataProvider.mockRequestOtp(sessionId, email);
2570
+ }
2571
+ async verifyOtp(sessionId, otp) {
2572
+ const response = await this.mockDataProvider.mockVerifyOtp(sessionId, otp);
2573
+ // Store tokens after successful OTP verification
2574
+ if (response.success && response.data) {
2575
+ const expiresAt = new Date(Date.now() + response.data.expires_in * 1000).toISOString();
2576
+ this.setTokens(response.data.access_token, response.data.refresh_token, expiresAt, response.data.user_id);
2577
+ }
2578
+ return response;
2579
+ }
2580
+ // Direct Authentication
2581
+ async authenticateDirectly(sessionId, userId) {
2582
+ // Ensure session is active before authenticating
2583
+ if (this.currentSessionState !== SessionState.ACTIVE) {
2584
+ throw new SessionError('Session must be in ACTIVE state to authenticate');
2585
+ }
2586
+ const response = await this.mockDataProvider.mockAuthenticateDirectly(sessionId, userId);
2587
+ // Store tokens after successful direct authentication
2588
+ if (response.success && response.data) {
2589
+ // For direct auth, we don't get expires_in, so we'll set a default 1-hour expiry
2590
+ const expiresAt = new Date(Date.now() + 60 * 60 * 1000).toISOString(); // 1 hour
2591
+ this.setTokens(response.data.access_token, response.data.refresh_token, expiresAt, userId);
2592
+ }
2593
+ return response;
2594
+ }
2595
+ // Portal Management
2596
+ async getPortalUrl(sessionId) {
2597
+ if (this.currentSessionState !== SessionState.ACTIVE) {
2598
+ throw new SessionError('Session must be in ACTIVE state to get portal URL');
2599
+ }
2600
+ // If in mockApiOnly mode, return the real portal URL
2601
+ if (this.mockApiOnly) {
2602
+ return {
2603
+ success: true,
2604
+ message: 'Portal URL retrieved successfully',
2605
+ data: {
2606
+ portal_url: 'http://localhost:5173/companies',
2607
+ },
2608
+ };
2609
+ }
2610
+ return this.mockDataProvider.mockGetPortalUrl(sessionId);
2611
+ }
2612
+ async validatePortalSession(sessionId, signature) {
2613
+ return this.mockDataProvider.mockValidatePortalSession(sessionId, signature);
2614
+ }
2615
+ async completePortalSession(sessionId) {
2616
+ return this.mockDataProvider.mockCompletePortalSession(sessionId);
2617
+ }
2618
+ // Portfolio Management
2619
+ async getHoldings(accessToken) {
2620
+ return this.mockDataProvider.mockGetHoldings();
2621
+ }
2622
+ async getOrders(accessToken, filter) {
2623
+ return this.mockDataProvider.mockGetOrders(filter);
2624
+ }
2625
+ async getPortfolio(accessToken) {
2626
+ return this.mockDataProvider.mockGetPortfolio();
2627
+ }
2628
+ async placeOrder(accessToken, order) {
2629
+ await this.mockDataProvider.mockPlaceOrder(order);
2630
+ }
2631
+ // New methods with automatic token management
2632
+ async getHoldingsAuto() {
2633
+ const accessToken = await this.getValidAccessToken();
2634
+ return this.getHoldings(accessToken);
2635
+ }
2636
+ async getOrdersAuto() {
2637
+ const accessToken = await this.getValidAccessToken();
2638
+ return this.getOrders(accessToken);
2639
+ }
2640
+ async getPortfolioAuto() {
2641
+ const accessToken = await this.getValidAccessToken();
2642
+ return this.getPortfolio(accessToken);
2643
+ }
2644
+ async placeOrderAuto(order) {
2645
+ const accessToken = await this.getValidAccessToken();
2646
+ return this.placeOrder(accessToken, order);
2647
+ }
2648
+ // Enhanced Trading Methods with Session Management
2649
+ async placeBrokerOrder(accessToken, params, extras = {}) {
2650
+ // Merge context with provided parameters
2651
+ const fullParams = {
2652
+ broker: (params.broker || this.tradingContext.broker) ||
2653
+ (() => {
2654
+ throw new Error('Broker not set. Call setBroker() or pass broker parameter.');
2655
+ })(),
2656
+ accountNumber: params.accountNumber ||
2657
+ this.tradingContext.accountNumber ||
2658
+ (() => {
2659
+ throw new Error('Account not set. Call setAccount() or pass accountNumber parameter.');
2660
+ })(),
2661
+ symbol: params.symbol,
2662
+ orderQty: params.orderQty,
2663
+ action: params.action,
2664
+ orderType: params.orderType,
2665
+ assetType: params.assetType,
2666
+ timeInForce: params.timeInForce || 'day',
2667
+ price: params.price,
2668
+ stopPrice: params.stopPrice,
2669
+ };
2670
+ // Convert timeInForce to the Order interface format
2671
+ const timeInForce = fullParams.timeInForce === 'gtd' ? 'gtc' : fullParams.timeInForce;
2672
+ return this.mockDataProvider.mockPlaceOrder({
2673
+ symbol: fullParams.symbol,
2674
+ side: fullParams.action.toLowerCase(),
2675
+ quantity: fullParams.orderQty,
2676
+ type_: fullParams.orderType.toLowerCase(),
2677
+ timeInForce: timeInForce,
2678
+ price: fullParams.price,
2679
+ stopPrice: fullParams.stopPrice,
2680
+ });
2681
+ }
2682
+ async cancelBrokerOrder(orderId, broker, extras = {}) {
2683
+ return {
2684
+ success: true,
2685
+ response_data: {
2686
+ orderId,
2687
+ status: 'cancelled',
2688
+ broker: broker || this.tradingContext.broker || 'robinhood',
2689
+ accountNumber: this.tradingContext.accountNumber || '123456789',
2690
+ },
2691
+ message: 'Order cancelled successfully',
2692
+ status_code: 200,
2693
+ };
2694
+ }
2695
+ async modifyBrokerOrder(orderId, params, broker, extras = {}) {
2696
+ return {
2697
+ success: true,
2698
+ response_data: {
2699
+ orderId,
2700
+ status: 'modified',
2701
+ broker: broker || this.tradingContext.broker || 'robinhood',
2702
+ accountNumber: this.tradingContext.accountNumber || '123456789',
2703
+ },
2704
+ message: 'Order modified successfully',
2705
+ status_code: 200,
2706
+ };
2707
+ }
2708
+ // Context management methods
2709
+ setBroker(broker) {
2710
+ this.tradingContext.broker = broker;
2711
+ // Clear account when broker changes
2712
+ this.tradingContext.accountNumber = undefined;
2713
+ this.tradingContext.accountId = undefined;
2714
+ }
2715
+ setAccount(accountNumber, accountId) {
2716
+ this.tradingContext.accountNumber = accountNumber;
2717
+ this.tradingContext.accountId = accountId;
2718
+ }
2719
+ getTradingContext() {
2720
+ return { ...this.tradingContext };
2721
+ }
2722
+ clearTradingContext() {
2723
+ this.tradingContext = {};
2724
+ }
2725
+ // Stock convenience methods
2726
+ async placeStockMarketOrder(accessToken, symbol, orderQty, action, broker, accountNumber, extras = {}) {
2727
+ return this.placeBrokerOrder(accessToken, {
2728
+ broker,
2729
+ accountNumber,
2730
+ symbol,
2731
+ orderQty,
2732
+ action,
2733
+ orderType: 'Market',
2734
+ assetType: 'Stock',
2735
+ timeInForce: 'day',
2736
+ }, extras);
2737
+ }
2738
+ async placeStockLimitOrder(accessToken, symbol, orderQty, action, price, timeInForce = 'gtc', broker, accountNumber, extras = {}) {
2739
+ return this.placeBrokerOrder(accessToken, {
2740
+ broker,
2741
+ accountNumber,
2742
+ symbol,
2743
+ orderQty,
2744
+ action,
2745
+ orderType: 'Limit',
2746
+ assetType: 'Stock',
2747
+ timeInForce,
2748
+ price,
2749
+ }, extras);
2750
+ }
2751
+ async placeStockStopOrder(accessToken, symbol, orderQty, action, stopPrice, timeInForce = 'day', broker, accountNumber, extras = {}) {
2752
+ return this.placeBrokerOrder(accessToken, {
2753
+ broker,
2754
+ accountNumber,
2755
+ symbol,
2756
+ orderQty,
2757
+ action,
2758
+ orderType: 'Stop',
2759
+ assetType: 'Stock',
2760
+ timeInForce,
2761
+ stopPrice,
2762
+ }, extras);
2763
+ }
2764
+ // Crypto convenience methods
2765
+ async placeCryptoMarketOrder(accessToken, symbol, orderQty, action, options = {}, broker, accountNumber, extras = {}) {
2766
+ const orderParams = {
2767
+ broker,
2768
+ accountNumber,
2769
+ symbol,
2770
+ orderQty: options.quantity || orderQty,
2771
+ action,
2772
+ orderType: 'Market',
2773
+ assetType: 'Crypto',
2774
+ timeInForce: 'gtc', // Crypto typically uses GTC
2775
+ };
2776
+ return this.placeBrokerOrder(accessToken, orderParams, extras);
2777
+ }
2778
+ async placeCryptoLimitOrder(accessToken, symbol, orderQty, action, price, timeInForce = 'gtc', options = {}, broker, accountNumber, extras = {}) {
2779
+ const orderParams = {
2780
+ broker,
2781
+ accountNumber,
2782
+ symbol,
2783
+ orderQty: options.quantity || orderQty,
2784
+ action,
2785
+ orderType: 'Limit',
2786
+ assetType: 'Crypto',
2787
+ timeInForce,
2788
+ price,
2789
+ };
2790
+ return this.placeBrokerOrder(accessToken, orderParams, extras);
2791
+ }
2792
+ // Options convenience methods
2793
+ async placeOptionsMarketOrder(accessToken, symbol, orderQty, action, options, broker, accountNumber, extras = {}) {
2794
+ const orderParams = {
2795
+ broker,
2796
+ accountNumber,
2797
+ symbol,
2798
+ orderQty,
2799
+ action,
2800
+ orderType: 'Market',
2801
+ assetType: 'Option',
2802
+ timeInForce: 'day',
2803
+ };
2804
+ return this.placeBrokerOrder(accessToken, orderParams, extras);
2805
+ }
2806
+ async placeOptionsLimitOrder(accessToken, symbol, orderQty, action, price, options, timeInForce = 'gtc', broker, accountNumber, extras = {}) {
2807
+ const orderParams = {
2808
+ broker,
2809
+ accountNumber,
2810
+ symbol,
2811
+ orderQty,
2812
+ action,
2813
+ orderType: 'Limit',
2814
+ assetType: 'Option',
2815
+ timeInForce,
2816
+ price,
2817
+ };
2818
+ return this.placeBrokerOrder(accessToken, orderParams, extras);
2819
+ }
2820
+ // Futures convenience methods
2821
+ async placeFuturesMarketOrder(accessToken, symbol, orderQty, action, broker, accountNumber, extras = {}) {
2822
+ return this.placeBrokerOrder(accessToken, {
2823
+ broker,
2824
+ accountNumber,
2825
+ symbol,
2826
+ orderQty,
2827
+ action,
2828
+ orderType: 'Market',
2829
+ assetType: 'Futures',
2830
+ timeInForce: 'day',
2831
+ }, extras);
2832
+ }
2833
+ async placeFuturesLimitOrder(accessToken, symbol, orderQty, action, price, timeInForce = 'gtc', broker, accountNumber, extras = {}) {
2834
+ return this.placeBrokerOrder(accessToken, {
2835
+ broker,
2836
+ accountNumber,
2837
+ symbol,
2838
+ orderQty,
2839
+ action,
2840
+ orderType: 'Limit',
2841
+ assetType: 'Futures',
2842
+ timeInForce,
2843
+ price,
2844
+ }, extras);
2845
+ }
2846
+ async revokeToken(accessToken) {
2847
+ // Clear tokens on revoke
2848
+ this.clearTokens();
2849
+ }
2850
+ async getUserToken(userId) {
2851
+ const token = this.mockDataProvider.getUserToken(userId);
2852
+ if (!token) {
2853
+ throw new AuthenticationError('User token not found');
2854
+ }
2855
+ return token;
2856
+ }
2857
+ getCurrentSessionState() {
2858
+ return this.currentSessionState;
2859
+ }
2860
+ // Broker Data Management
2861
+ async getBrokerList(accessToken) {
2862
+ return this.mockDataProvider.mockGetBrokerList();
2863
+ }
2864
+ async getBrokerAccounts(accessToken, options) {
2865
+ return this.mockDataProvider.mockGetBrokerAccounts();
2866
+ }
2867
+ async getBrokerOrders(accessToken, options) {
2868
+ // Return empty orders for now - keeping original interface
2869
+ return {
2870
+ _id: v4(),
2871
+ response_data: [],
2872
+ message: 'Broker orders retrieved successfully',
2873
+ status_code: 200,
2874
+ warnings: null,
2875
+ errors: null,
2876
+ };
2877
+ }
2878
+ async getBrokerPositions(accessToken, options) {
2879
+ // Return empty positions for now - keeping original interface
2880
+ return {
2881
+ _id: v4(),
2882
+ response_data: [],
2883
+ message: 'Broker positions retrieved successfully',
2884
+ status_code: 200,
2885
+ warnings: null,
2886
+ errors: null,
2887
+ };
2888
+ }
2889
+ // New broker data methods with filtering support
2890
+ async getBrokerOrdersWithFilter(filter) {
2891
+ return this.mockDataProvider.mockGetBrokerOrders(filter);
2892
+ }
2893
+ async getBrokerPositionsWithFilter(filter) {
2894
+ return this.mockDataProvider.mockGetBrokerPositions(filter);
2895
+ }
2896
+ async getBrokerDataAccountsWithFilter(filter) {
2897
+ return this.mockDataProvider.mockGetBrokerDataAccounts(filter);
2898
+ }
2899
+ // Page-based pagination methods
2900
+ async getBrokerOrdersPage(page = 1, perPage = 100, filters) {
2901
+ const mockOrders = await this.mockDataProvider.mockGetBrokerOrders(filters);
2902
+ const orders = mockOrders.data;
2903
+ // Simulate pagination
2904
+ const startIndex = (page - 1) * perPage;
2905
+ const endIndex = startIndex + perPage;
2906
+ const paginatedOrders = orders.slice(startIndex, endIndex);
2907
+ const hasMore = endIndex < orders.length;
2908
+ const nextOffset = hasMore ? endIndex : startIndex;
2909
+ // Create navigation callback for mock pagination
2910
+ const navigationCallback = async (newOffset, newLimit) => {
2911
+ const newStartIndex = newOffset;
2912
+ const newEndIndex = newStartIndex + newLimit;
2913
+ const newPaginatedOrders = orders.slice(newStartIndex, newEndIndex);
2914
+ const newHasMore = newEndIndex < orders.length;
2915
+ const newNextOffset = newHasMore ? newEndIndex : newStartIndex;
2916
+ return new PaginatedResult(newPaginatedOrders, {
2917
+ has_more: newHasMore,
2918
+ next_offset: newNextOffset,
2919
+ current_offset: newStartIndex,
2920
+ limit: newLimit,
2921
+ }, navigationCallback);
2922
+ };
2923
+ return new PaginatedResult(paginatedOrders, {
2924
+ has_more: hasMore,
2925
+ next_offset: nextOffset,
2926
+ current_offset: startIndex,
2927
+ limit: perPage,
2928
+ }, navigationCallback);
2929
+ }
2930
+ async getBrokerAccountsPage(page = 1, perPage = 100, filters) {
2931
+ const mockAccounts = await this.mockDataProvider.mockGetBrokerDataAccounts(filters);
2932
+ const accounts = mockAccounts.data;
2933
+ // Simulate pagination
2934
+ const startIndex = (page - 1) * perPage;
2935
+ const endIndex = startIndex + perPage;
2936
+ const paginatedAccounts = accounts.slice(startIndex, endIndex);
2937
+ const hasMore = endIndex < accounts.length;
2938
+ const nextOffset = hasMore ? endIndex : startIndex;
2939
+ // Create navigation callback for mock pagination
2940
+ const navigationCallback = async (newOffset, newLimit) => {
2941
+ const newStartIndex = newOffset;
2942
+ const newEndIndex = newStartIndex + newLimit;
2943
+ const newPaginatedAccounts = accounts.slice(newStartIndex, newEndIndex);
2944
+ const newHasMore = newEndIndex < accounts.length;
2945
+ const newNextOffset = newHasMore ? newEndIndex : newStartIndex;
2946
+ return new PaginatedResult(newPaginatedAccounts, {
2947
+ has_more: newHasMore,
2948
+ next_offset: newNextOffset,
2949
+ current_offset: newStartIndex,
2950
+ limit: newLimit,
2951
+ }, navigationCallback);
2952
+ };
2953
+ return new PaginatedResult(paginatedAccounts, {
2954
+ has_more: hasMore,
2955
+ next_offset: nextOffset,
2956
+ current_offset: startIndex,
2957
+ limit: perPage,
2958
+ }, navigationCallback);
2959
+ }
2960
+ async getBrokerPositionsPage(page = 1, perPage = 100, filters) {
2961
+ const mockPositions = await this.mockDataProvider.mockGetBrokerPositions(filters);
2962
+ const positions = mockPositions.data;
2963
+ // Simulate pagination
2964
+ const startIndex = (page - 1) * perPage;
2965
+ const endIndex = startIndex + perPage;
2966
+ const paginatedPositions = positions.slice(startIndex, endIndex);
2967
+ const hasMore = endIndex < positions.length;
2968
+ const nextOffset = hasMore ? endIndex : startIndex;
2969
+ // Create navigation callback for mock pagination
2970
+ const navigationCallback = async (newOffset, newLimit) => {
2971
+ const newStartIndex = newOffset;
2972
+ const newEndIndex = newStartIndex + newLimit;
2973
+ const newPaginatedPositions = positions.slice(newStartIndex, newEndIndex);
2974
+ const newHasMore = newEndIndex < positions.length;
2975
+ const newNextOffset = newHasMore ? newEndIndex : newStartIndex;
2976
+ return new PaginatedResult(newPaginatedPositions, {
2977
+ has_more: newHasMore,
2978
+ next_offset: newNextOffset,
2979
+ current_offset: newStartIndex,
2980
+ limit: newLimit,
2981
+ }, navigationCallback);
2982
+ };
2983
+ return new PaginatedResult(paginatedPositions, {
2984
+ has_more: hasMore,
2985
+ next_offset: nextOffset,
2986
+ current_offset: startIndex,
2987
+ limit: perPage,
2988
+ }, navigationCallback);
2989
+ }
2990
+ async getBrokerConnections(accessToken) {
2991
+ return this.mockDataProvider.mockGetBrokerConnections();
2992
+ }
2993
+ // Automatic token management versions of broker methods
2994
+ async getBrokerListAuto() {
2995
+ const accessToken = await this.getValidAccessToken();
2996
+ return this.getBrokerList(accessToken);
2997
+ }
2998
+ async getBrokerAccountsAuto(options) {
2999
+ const accessToken = await this.getValidAccessToken();
3000
+ return this.getBrokerAccounts(accessToken, options);
3001
+ }
3002
+ async getBrokerOrdersAuto(options) {
3003
+ const accessToken = await this.getValidAccessToken();
3004
+ return this.getBrokerOrders(accessToken, options);
3005
+ }
3006
+ async getBrokerPositionsAuto(options) {
3007
+ const accessToken = await this.getValidAccessToken();
3008
+ return this.getBrokerPositions(accessToken, options);
3009
+ }
3010
+ async getBrokerConnectionsAuto() {
3011
+ const accessToken = await this.getValidAccessToken();
3012
+ return this.getBrokerConnections(accessToken);
3013
+ }
3014
+ // Automatic token management versions of trading methods
3015
+ async placeBrokerOrderAuto(params, extras = {}) {
3016
+ const accessToken = await this.getValidAccessToken();
3017
+ return this.placeBrokerOrder(accessToken, params, extras);
3018
+ }
3019
+ async placeStockMarketOrderAuto(symbol, orderQty, action, broker, accountNumber, extras = {}) {
3020
+ const accessToken = await this.getValidAccessToken();
3021
+ return this.placeStockMarketOrder(accessToken, symbol, orderQty, action, broker, accountNumber, extras);
3022
+ }
3023
+ async placeStockLimitOrderAuto(symbol, orderQty, action, price, timeInForce = 'gtc', broker, accountNumber, extras = {}) {
3024
+ const accessToken = await this.getValidAccessToken();
3025
+ return this.placeStockLimitOrder(accessToken, symbol, orderQty, action, price, timeInForce, broker, accountNumber, extras);
3026
+ }
3027
+ async placeStockStopOrderAuto(symbol, orderQty, action, stopPrice, timeInForce = 'day', broker, accountNumber, extras = {}) {
3028
+ const accessToken = await this.getValidAccessToken();
3029
+ return this.placeStockStopOrder(accessToken, symbol, orderQty, action, stopPrice, timeInForce, broker, accountNumber, extras);
3030
+ }
3031
+ // Utility methods for mock system
3032
+ getMockDataProvider() {
3033
+ return this.mockDataProvider;
3034
+ }
3035
+ clearMockData() {
3036
+ this.mockDataProvider.clearData();
3037
+ }
3038
+ /**
3039
+ * Check if this is a mock client
3040
+ * @returns true if this is a mock client
3041
+ */
3042
+ isMockClient() {
3043
+ return true;
3044
+ }
3045
+ }
3046
+
3047
+ /**
3048
+ * Utility functions for mock system environment detection
3049
+ */
3050
+ /**
3051
+ * Check if mocks should be used based on environment variables
3052
+ * Supports both browser and Node.js environments
3053
+ */
3054
+ function shouldUseMocks() {
3055
+ // Check Node.js environment
3056
+ if (typeof process !== 'undefined' && process?.env) {
3057
+ return (process.env.FINATIC_USE_MOCKS === 'true' ||
3058
+ process.env.NEXT_PUBLIC_FINATIC_USE_MOCKS === 'true');
3059
+ }
3060
+ // Check browser environment
3061
+ if (typeof window !== 'undefined') {
3062
+ // Check global variable
3063
+ if (window.FINATIC_USE_MOCKS === 'true') {
3064
+ return true;
3065
+ }
3066
+ // Check Next.js public environment variables
3067
+ if (window.NEXT_PUBLIC_FINATIC_USE_MOCKS === 'true') {
3068
+ return true;
3069
+ }
3070
+ // Check localStorage
3071
+ try {
3072
+ return localStorage.getItem('FINATIC_USE_MOCKS') === 'true';
3073
+ }
3074
+ catch (error) {
3075
+ // localStorage might not be available
3076
+ return false;
3077
+ }
3078
+ }
3079
+ return false;
3080
+ }
3081
+ /**
3082
+ * Check if only API should be mocked (but portal should use real URL)
3083
+ */
3084
+ function shouldMockApiOnly() {
3085
+ // Check Node.js environment
3086
+ if (typeof process !== 'undefined' && process?.env) {
3087
+ return (process.env.FINATIC_MOCK_API_ONLY === 'true' ||
3088
+ process.env.NEXT_PUBLIC_FINATIC_MOCK_API_ONLY === 'true');
3089
+ }
3090
+ // Check browser environment
3091
+ if (typeof window !== 'undefined') {
3092
+ // Check global variable
3093
+ if (window.FINATIC_MOCK_API_ONLY === 'true') {
3094
+ return true;
3095
+ }
3096
+ // Check Next.js public environment variables
3097
+ if (window.NEXT_PUBLIC_FINATIC_MOCK_API_ONLY === 'true') {
3098
+ return true;
3099
+ }
3100
+ // Check localStorage
3101
+ try {
3102
+ return localStorage.getItem('FINATIC_MOCK_API_ONLY') === 'true';
3103
+ }
3104
+ catch (error) {
3105
+ // localStorage might not be available
3106
+ return false;
3107
+ }
3108
+ }
3109
+ return false;
3110
+ }
3111
+ /**
3112
+ * Get mock configuration from environment
3113
+ */
3114
+ function getMockConfig() {
3115
+ const enabled = shouldUseMocks();
3116
+ const mockApiOnly = shouldMockApiOnly();
3117
+ let delay;
3118
+ // Check for custom delay in Node.js
3119
+ if (typeof process !== 'undefined' && process?.env?.FINATIC_MOCK_DELAY) {
3120
+ delay = parseInt(process.env.FINATIC_MOCK_DELAY, 10);
3121
+ }
3122
+ // Check for custom delay in browser
3123
+ if (typeof window !== 'undefined') {
3124
+ try {
3125
+ const storedDelay = localStorage.getItem('FINATIC_MOCK_DELAY');
3126
+ if (storedDelay) {
3127
+ delay = parseInt(storedDelay, 10);
3128
+ }
3129
+ }
3130
+ catch (error) {
3131
+ // localStorage might not be available
3132
+ }
3133
+ }
3134
+ return { enabled, delay, mockApiOnly };
3135
+ }
3136
+
3137
+ /**
3138
+ * Factory class for creating API clients (real or mock)
3139
+ */
3140
+ class MockFactory {
3141
+ /**
3142
+ * Create an API client based on environment configuration
3143
+ * @param baseUrl - The base URL for the API
3144
+ * @param deviceInfo - Optional device information
3145
+ * @param mockConfig - Optional mock configuration (only used if mocks are enabled)
3146
+ * @returns ApiClient or MockApiClient instance
3147
+ */
3148
+ static createApiClient(baseUrl, deviceInfo, mockConfig) {
3149
+ const useMocks = shouldUseMocks();
3150
+ const mockApiOnly = shouldMockApiOnly();
3151
+ if (useMocks || mockApiOnly) {
3152
+ // Merge environment config with provided config
3153
+ const envConfig = getMockConfig();
3154
+ const finalConfig = {
3155
+ delay: mockConfig?.delay || envConfig.delay,
3156
+ scenario: mockConfig?.scenario || 'success',
3157
+ customData: mockConfig?.customData || {},
3158
+ mockApiOnly: mockApiOnly, // Pass this flag to the mock client
3159
+ };
3160
+ return new MockApiClient(baseUrl, deviceInfo, finalConfig);
3161
+ }
3162
+ else {
3163
+ return new ApiClient(baseUrl, deviceInfo);
3164
+ }
3165
+ }
3166
+ /**
3167
+ * Force create a mock API client regardless of environment settings
3168
+ * @param baseUrl - The base URL for the API
3169
+ * @param deviceInfo - Optional device information
3170
+ * @param mockConfig - Optional mock configuration
3171
+ * @returns MockApiClient instance
3172
+ */
3173
+ static createMockApiClient(baseUrl, deviceInfo, mockConfig) {
3174
+ return new MockApiClient(baseUrl, deviceInfo, mockConfig);
3175
+ }
3176
+ /**
3177
+ * Force create a real API client regardless of environment settings
3178
+ * @param baseUrl - The base URL for the API
3179
+ * @param deviceInfo - Optional device information
3180
+ * @returns ApiClient instance
3181
+ */
3182
+ static createRealApiClient(baseUrl, deviceInfo) {
3183
+ return new ApiClient(baseUrl, deviceInfo);
3184
+ }
3185
+ /**
3186
+ * Check if mocks are currently enabled
3187
+ * @returns boolean indicating if mocks are enabled
3188
+ */
3189
+ static isMockMode() {
3190
+ return shouldUseMocks();
3191
+ }
3192
+ /**
3193
+ * Get current mock configuration
3194
+ * @returns Mock configuration object
3195
+ */
3196
+ static getMockConfig() {
3197
+ return getMockConfig();
3198
+ }
3199
+ }
3200
+
3201
+ // Dark theme (default Finatic theme)
3202
+ const darkTheme = {
3203
+ mode: 'dark',
3204
+ colors: {
3205
+ background: {
3206
+ primary: '#000000',
3207
+ secondary: '#1a1a1a',
3208
+ tertiary: '#2a2a2a',
3209
+ accent: 'rgba(0, 255, 255, 0.1)',
3210
+ glass: 'rgba(255, 255, 255, 0.05)',
3211
+ },
3212
+ status: {
3213
+ connected: '#00FFFF',
3214
+ disconnected: '#EF4444',
3215
+ warning: '#F59E0B',
3216
+ pending: '#8B5CF6',
3217
+ error: '#EF4444',
3218
+ success: '#00FFFF',
3219
+ },
3220
+ text: {
3221
+ primary: '#FFFFFF',
3222
+ secondary: '#CBD5E1',
3223
+ muted: '#94A3B8',
3224
+ inverse: '#000000',
3225
+ },
3226
+ border: {
3227
+ primary: 'rgba(0, 255, 255, 0.2)',
3228
+ secondary: 'rgba(255, 255, 255, 0.1)',
3229
+ hover: 'rgba(0, 255, 255, 0.4)',
3230
+ focus: 'rgba(0, 255, 255, 0.6)',
3231
+ accent: '#00FFFF',
3232
+ },
3233
+ input: {
3234
+ background: '#1a1a1a',
3235
+ border: 'rgba(0, 255, 255, 0.2)',
3236
+ borderFocus: '#00FFFF',
3237
+ text: '#FFFFFF',
3238
+ placeholder: '#94A3B8',
3239
+ },
3240
+ button: {
3241
+ primary: {
3242
+ background: '#00FFFF',
3243
+ text: '#000000',
3244
+ hover: '#00E6E6',
3245
+ active: '#00CCCC',
3246
+ },
3247
+ secondary: {
3248
+ background: 'transparent',
3249
+ text: '#00FFFF',
3250
+ border: '#00FFFF',
3251
+ hover: 'rgba(0, 255, 255, 0.1)',
3252
+ active: 'rgba(0, 255, 255, 0.2)',
3253
+ },
3254
+ },
3255
+ },
3256
+ branding: {
3257
+ primaryColor: '#00FFFF',
3258
+ },
3259
+ };
3260
+ // Light theme with cyan accents
3261
+ const lightTheme = {
3262
+ mode: 'light',
3263
+ colors: {
3264
+ background: {
3265
+ primary: '#FFFFFF',
3266
+ secondary: '#F8FAFC',
3267
+ tertiary: '#F1F5F9',
3268
+ accent: 'rgba(0, 255, 255, 0.1)',
3269
+ glass: 'rgba(0, 0, 0, 0.05)',
3270
+ },
3271
+ status: {
3272
+ connected: '#00FFFF',
3273
+ disconnected: '#EF4444',
3274
+ warning: '#F59E0B',
3275
+ pending: '#8B5CF6',
3276
+ error: '#EF4444',
3277
+ success: '#00FFFF',
3278
+ },
3279
+ text: {
3280
+ primary: '#1E293B',
3281
+ secondary: '#64748B',
3282
+ muted: '#94A3B8',
3283
+ inverse: '#FFFFFF',
3284
+ },
3285
+ border: {
3286
+ primary: 'rgba(0, 255, 255, 0.2)',
3287
+ secondary: 'rgba(0, 0, 0, 0.1)',
3288
+ hover: 'rgba(0, 255, 255, 0.4)',
3289
+ focus: 'rgba(0, 255, 255, 0.6)',
3290
+ accent: '#00FFFF',
3291
+ },
3292
+ input: {
3293
+ background: '#FFFFFF',
3294
+ border: 'rgba(0, 255, 255, 0.2)',
3295
+ borderFocus: '#00FFFF',
3296
+ text: '#1E293B',
3297
+ placeholder: '#94A3B8',
3298
+ },
3299
+ button: {
3300
+ primary: {
3301
+ background: '#00FFFF',
3302
+ text: '#000000',
3303
+ hover: '#00E6E6',
3304
+ active: '#00CCCC',
3305
+ },
3306
+ secondary: {
3307
+ background: 'transparent',
3308
+ text: '#00FFFF',
3309
+ border: '#00FFFF',
3310
+ hover: 'rgba(0, 255, 255, 0.1)',
3311
+ active: 'rgba(0, 255, 255, 0.2)',
3312
+ },
3313
+ },
3314
+ },
3315
+ branding: {
3316
+ primaryColor: '#00FFFF',
3317
+ },
3318
+ };
3319
+ // Corporate blue theme
3320
+ const corporateBlueTheme = {
3321
+ mode: 'dark',
3322
+ colors: {
3323
+ background: {
3324
+ primary: '#1E293B',
3325
+ secondary: '#334155',
3326
+ tertiary: '#475569',
3327
+ accent: 'rgba(59, 130, 246, 0.1)',
3328
+ glass: 'rgba(255, 255, 255, 0.05)',
3329
+ },
3330
+ status: {
3331
+ connected: '#3B82F6',
3332
+ disconnected: '#EF4444',
3333
+ warning: '#F59E0B',
3334
+ pending: '#8B5CF6',
3335
+ error: '#EF4444',
3336
+ success: '#10B981',
3337
+ },
3338
+ text: {
3339
+ primary: '#F8FAFC',
3340
+ secondary: '#CBD5E1',
3341
+ muted: '#94A3B8',
3342
+ inverse: '#1E293B',
3343
+ },
3344
+ border: {
3345
+ primary: 'rgba(59, 130, 246, 0.2)',
3346
+ secondary: 'rgba(255, 255, 255, 0.1)',
3347
+ hover: 'rgba(59, 130, 246, 0.4)',
3348
+ focus: 'rgba(59, 130, 246, 0.6)',
3349
+ accent: '#3B82F6',
3350
+ },
3351
+ input: {
3352
+ background: '#334155',
3353
+ border: 'rgba(59, 130, 246, 0.2)',
3354
+ borderFocus: '#3B82F6',
3355
+ text: '#F8FAFC',
3356
+ placeholder: '#94A3B8',
3357
+ },
3358
+ button: {
3359
+ primary: {
3360
+ background: '#3B82F6',
3361
+ text: '#FFFFFF',
3362
+ hover: '#2563EB',
3363
+ active: '#1D4ED8',
3364
+ },
3365
+ secondary: {
3366
+ background: 'transparent',
3367
+ text: '#3B82F6',
3368
+ border: '#3B82F6',
3369
+ hover: 'rgba(59, 130, 246, 0.1)',
3370
+ active: 'rgba(59, 130, 246, 0.2)',
3371
+ },
3372
+ },
3373
+ },
3374
+ branding: {
3375
+ primaryColor: '#3B82F6',
3376
+ },
3377
+ };
3378
+ // Purple theme
3379
+ const purpleTheme = {
3380
+ mode: 'dark',
3381
+ colors: {
3382
+ background: {
3383
+ primary: '#1a1a1a',
3384
+ secondary: '#2a2a2a',
3385
+ tertiary: '#3a3a3a',
3386
+ accent: 'rgba(168, 85, 247, 0.1)',
3387
+ glass: 'rgba(255, 255, 255, 0.05)',
3388
+ },
3389
+ status: {
3390
+ connected: '#A855F7',
3391
+ disconnected: '#EF4444',
3392
+ warning: '#F59E0B',
3393
+ pending: '#8B5CF6',
3394
+ error: '#EF4444',
3395
+ success: '#10B981',
3396
+ },
3397
+ text: {
3398
+ primary: '#F8FAFC',
3399
+ secondary: '#CBD5E1',
3400
+ muted: '#94A3B8',
3401
+ inverse: '#1a1a1a',
3402
+ },
3403
+ border: {
3404
+ primary: 'rgba(168, 85, 247, 0.2)',
3405
+ secondary: 'rgba(255, 255, 255, 0.1)',
3406
+ hover: 'rgba(168, 85, 247, 0.4)',
3407
+ focus: 'rgba(168, 85, 247, 0.6)',
3408
+ accent: '#A855F7',
3409
+ },
3410
+ input: {
3411
+ background: '#334155',
3412
+ border: 'rgba(168, 85, 247, 0.2)',
3413
+ borderFocus: '#A855F7',
3414
+ text: '#F8FAFC',
3415
+ placeholder: '#94A3B8',
3416
+ },
3417
+ button: {
3418
+ primary: {
3419
+ background: '#A855F7',
3420
+ text: '#FFFFFF',
3421
+ hover: '#9333EA',
3422
+ active: '#7C3AED',
3423
+ },
3424
+ secondary: {
3425
+ background: 'transparent',
3426
+ text: '#A855F7',
3427
+ border: '#A855F7',
3428
+ hover: 'rgba(168, 85, 247, 0.1)',
3429
+ active: 'rgba(168, 85, 247, 0.2)',
3430
+ },
3431
+ },
3432
+ },
3433
+ branding: {
3434
+ primaryColor: '#A855F7',
3435
+ },
3436
+ };
3437
+ // Green theme
3438
+ const greenTheme = {
3439
+ mode: 'dark',
3440
+ colors: {
3441
+ background: {
3442
+ primary: '#1a1a1a',
3443
+ secondary: '#2a2a2a',
3444
+ tertiary: '#3a3a3a',
3445
+ accent: 'rgba(34, 197, 94, 0.1)',
3446
+ glass: 'rgba(255, 255, 255, 0.05)',
3447
+ },
3448
+ status: {
3449
+ connected: '#22C55E',
3450
+ disconnected: '#EF4444',
3451
+ warning: '#F59E0B',
3452
+ pending: '#8B5CF6',
3453
+ error: '#EF4444',
3454
+ success: '#22C55E',
3455
+ },
3456
+ text: {
3457
+ primary: '#F8FAFC',
3458
+ secondary: '#CBD5E1',
3459
+ muted: '#94A3B8',
3460
+ inverse: '#1a1a1a',
3461
+ },
3462
+ border: {
3463
+ primary: 'rgba(34, 197, 94, 0.2)',
3464
+ secondary: 'rgba(255, 255, 255, 0.1)',
3465
+ hover: 'rgba(34, 197, 94, 0.4)',
3466
+ focus: 'rgba(34, 197, 94, 0.6)',
3467
+ accent: '#22C55E',
3468
+ },
3469
+ input: {
3470
+ background: '#334155',
3471
+ border: 'rgba(34, 197, 94, 0.2)',
3472
+ borderFocus: '#22C55E',
3473
+ text: '#F8FAFC',
3474
+ placeholder: '#94A3B8',
3475
+ },
3476
+ button: {
3477
+ primary: {
3478
+ background: '#22C55E',
3479
+ text: '#FFFFFF',
3480
+ hover: '#16A34A',
3481
+ active: '#15803D',
3482
+ },
3483
+ secondary: {
3484
+ background: 'transparent',
3485
+ text: '#22C55E',
3486
+ border: '#22C55E',
3487
+ hover: 'rgba(34, 197, 94, 0.1)',
3488
+ active: 'rgba(34, 197, 94, 0.2)',
3489
+ },
3490
+ },
3491
+ },
3492
+ branding: {
3493
+ primaryColor: '#22C55E',
3494
+ },
3495
+ };
3496
+ // Orange theme
3497
+ const orangeTheme = {
3498
+ mode: 'dark',
3499
+ colors: {
3500
+ background: {
3501
+ primary: '#1a1a1a',
3502
+ secondary: '#2a2a2a',
3503
+ tertiary: '#3a3a3a',
3504
+ accent: 'rgba(249, 115, 22, 0.1)',
3505
+ glass: 'rgba(255, 255, 255, 0.05)',
3506
+ },
3507
+ status: {
3508
+ connected: '#F97316',
3509
+ disconnected: '#EF4444',
3510
+ warning: '#F59E0B',
3511
+ pending: '#8B5CF6',
3512
+ error: '#EF4444',
3513
+ success: '#10B981',
3514
+ },
3515
+ text: {
3516
+ primary: '#F8FAFC',
3517
+ secondary: '#CBD5E1',
3518
+ muted: '#94A3B8',
3519
+ inverse: '#1a1a1a',
3520
+ },
3521
+ border: {
3522
+ primary: 'rgba(249, 115, 22, 0.2)',
3523
+ secondary: 'rgba(255, 255, 255, 0.1)',
3524
+ hover: 'rgba(249, 115, 22, 0.4)',
3525
+ focus: 'rgba(249, 115, 22, 0.6)',
3526
+ accent: '#F97316',
3527
+ },
3528
+ input: {
3529
+ background: '#334155',
3530
+ border: 'rgba(249, 115, 22, 0.2)',
3531
+ borderFocus: '#F97316',
3532
+ text: '#F8FAFC',
3533
+ placeholder: '#94A3B8',
3534
+ },
3535
+ button: {
3536
+ primary: {
3537
+ background: '#F97316',
3538
+ text: '#FFFFFF',
3539
+ hover: '#EA580C',
3540
+ active: '#DC2626',
3541
+ },
3542
+ secondary: {
3543
+ background: 'transparent',
3544
+ text: '#F97316',
3545
+ border: '#F97316',
3546
+ hover: 'rgba(249, 115, 22, 0.1)',
3547
+ active: 'rgba(249, 115, 22, 0.2)',
3548
+ },
3549
+ },
3550
+ },
3551
+ branding: {
3552
+ primaryColor: '#F97316',
3553
+ },
3554
+ };
3555
+ // Theme preset mapping
3556
+ const portalThemePresets = {
3557
+ dark: darkTheme,
3558
+ light: lightTheme,
3559
+ corporateBlue: corporateBlueTheme,
3560
+ purple: purpleTheme,
3561
+ green: greenTheme,
3562
+ orange: orangeTheme,
3563
+ };
3564
+
3565
+ /**
3566
+ * Generate a portal URL with theme parameters
3567
+ * @param baseUrl The base portal URL
3568
+ * @param theme The theme configuration
3569
+ * @returns The portal URL with theme parameters
3570
+ */
3571
+ function generatePortalThemeURL(baseUrl, theme) {
3572
+ if (!theme) {
3573
+ return baseUrl;
3574
+ }
3575
+ try {
3576
+ const url = new URL(baseUrl);
3577
+ if (theme.preset) {
3578
+ // Use preset theme
3579
+ url.searchParams.set('theme', theme.preset);
3580
+ }
3581
+ else if (theme.custom) {
3582
+ // Use custom theme
3583
+ const encodedTheme = btoa(JSON.stringify(theme.custom));
3584
+ url.searchParams.set('theme', 'custom');
3585
+ url.searchParams.set('themeObject', encodedTheme);
3586
+ }
3587
+ return url.toString();
3588
+ }
3589
+ catch (error) {
3590
+ console.error('Failed to generate theme URL:', error);
3591
+ return baseUrl;
3592
+ }
3593
+ }
3594
+ /**
3595
+ * Generate a portal URL with theme parameters, appending to existing query params
3596
+ * @param baseUrl The base portal URL (may already have query parameters)
3597
+ * @param theme The theme configuration
3598
+ * @returns The portal URL with theme parameters appended
3599
+ */
3600
+ function appendThemeToURL(baseUrl, theme) {
3601
+ if (!theme) {
3602
+ return baseUrl;
3603
+ }
3604
+ try {
3605
+ const url = new URL(baseUrl);
3606
+ if (theme.preset) {
3607
+ // Use preset theme
3608
+ url.searchParams.set('theme', theme.preset);
3609
+ }
3610
+ else if (theme.custom) {
3611
+ // Use custom theme
3612
+ const encodedTheme = btoa(JSON.stringify(theme.custom));
3613
+ url.searchParams.set('theme', 'custom');
3614
+ url.searchParams.set('themeObject', encodedTheme);
3615
+ }
3616
+ return url.toString();
3617
+ }
3618
+ catch (error) {
3619
+ console.error('Failed to append theme to URL:', error);
3620
+ return baseUrl;
3621
+ }
3622
+ }
3623
+ /**
3624
+ * Get a theme configuration by preset name
3625
+ * @param preset The preset theme name
3626
+ * @returns The theme configuration or undefined if not found
3627
+ */
3628
+ function getThemePreset(preset) {
3629
+ return portalThemePresets[preset];
3630
+ }
3631
+ /**
3632
+ * Validate a custom theme configuration
3633
+ * @param theme The theme configuration to validate
3634
+ * @returns True if valid, false otherwise
3635
+ */
3636
+ function validateCustomTheme(theme) {
3637
+ try {
3638
+ // Check required properties
3639
+ if (!theme.mode || !['dark', 'light', 'auto'].includes(theme.mode)) {
3640
+ return false;
3641
+ }
3642
+ if (!theme.colors) {
3643
+ return false;
3644
+ }
3645
+ // Check required color sections
3646
+ const requiredSections = ['background', 'status', 'text', 'border', 'input', 'button'];
3647
+ for (const section of requiredSections) {
3648
+ if (!theme.colors[section]) {
3649
+ return false;
3650
+ }
3651
+ }
3652
+ return true;
3653
+ }
3654
+ catch (error) {
3655
+ console.error('Theme validation error:', error);
3656
+ return false;
3657
+ }
3658
+ }
3659
+ /**
3660
+ * Create a custom theme from a preset with modifications
3661
+ * @param preset The base preset theme
3662
+ * @param modifications Partial theme modifications
3663
+ * @returns The modified theme configuration
3664
+ */
3665
+ function createCustomThemeFromPreset(preset, modifications) {
3666
+ const baseTheme = getThemePreset(preset);
3667
+ if (!baseTheme) {
3668
+ console.error(`Preset theme '${preset}' not found`);
3669
+ return null;
3670
+ }
3671
+ return {
3672
+ ...baseTheme,
3673
+ ...modifications,
3674
+ };
3675
+ }
3676
+
3677
+ class FinaticConnect extends EventEmitter {
3678
+ constructor(options, deviceInfo) {
3679
+ super();
3680
+ this.userToken = null;
3681
+ this.sessionId = null;
3682
+ this.BROKER_LIST_CACHE_KEY = 'finatic_broker_list_cache';
3683
+ this.BROKER_LIST_CACHE_VERSION = '1.0';
3684
+ this.BROKER_LIST_CACHE_DURATION = 1000 * 60 * 60 * 24; // 24 hours in milliseconds
3685
+ this.currentSessionState = null;
3686
+ this.options = options;
3687
+ this.baseUrl = options.baseUrl || 'http://localhost:8000';
3688
+ this.apiClient = MockFactory.createApiClient(this.baseUrl, deviceInfo);
3689
+ this.portalUI = new PortalUI(this.baseUrl);
3690
+ this.deviceInfo = deviceInfo;
3691
+ // Extract company ID from token
3692
+ try {
3693
+ // Validate token exists
3694
+ if (!options.token) {
3695
+ throw new Error('Token is required but not provided');
3696
+ }
3697
+ // Check if token is in JWT format (contains dots)
3698
+ if (options.token.includes('.')) {
3699
+ const tokenParts = options.token.split('.');
3700
+ if (tokenParts.length === 3) {
3701
+ const payload = JSON.parse(atob(tokenParts[1]));
3702
+ this.companyId = payload.company_id;
3703
+ }
3704
+ else {
3705
+ throw new Error('Invalid JWT token format');
3706
+ }
3707
+ }
3708
+ else {
3709
+ // Handle UUID format token
3710
+ // For UUID tokens, we'll get the company_id from the session start response
3711
+ this.companyId = ''; // Will be set after session start
3712
+ }
3713
+ }
3714
+ catch (error) {
3715
+ if (error instanceof Error) {
3716
+ throw new Error('Failed to parse token: ' + error.message);
3717
+ }
3718
+ else {
3719
+ throw new Error('Failed to parse token: Unknown error');
3720
+ }
3721
+ }
3722
+ // Set up event listeners for callbacks
3723
+ if (this.options.onSuccess) {
3724
+ this.on('success', this.options.onSuccess);
3725
+ }
3726
+ if (this.options.onError) {
3727
+ this.on('error', this.options.onError);
3728
+ }
3729
+ if (this.options.onClose) {
3730
+ this.on('close', this.options.onClose);
3731
+ }
3732
+ // Register automatic session cleanup
3733
+ this.registerSessionCleanup();
3734
+ }
3735
+ handleTokens(tokens) {
3736
+ if (!tokens.access_token || !tokens.refresh_token) {
3737
+ return;
3738
+ }
3739
+ // Keep existing user_id or use empty string as fallback
3740
+ const userId = this.userToken?.user_id || '';
3741
+ this.userToken = {
3742
+ accessToken: tokens.access_token,
3743
+ refreshToken: tokens.refresh_token,
3744
+ expiresIn: 3600, // Default to 1 hour if not provided
3745
+ user_id: userId,
3746
+ tokenType: 'Bearer',
3747
+ scope: 'api:access',
3748
+ };
3749
+ // Store tokens in ApiClient for automatic refresh
3750
+ const expiresAt = new Date(Date.now() + 3600 * 1000).toISOString(); // 1 hour from now
3751
+ this.apiClient.setTokens(tokens.access_token, tokens.refresh_token, expiresAt, userId);
3752
+ }
3753
+ /**
3754
+ * Check if the user is authenticated
3755
+ * @returns True if the user has a valid access token
3756
+ */
3757
+ isAuthenticated() {
3758
+ return this.isAuthed();
3759
+ }
3760
+ /**
3761
+ * Check if the user is fully authenticated (has userId, access token, and refresh token)
3762
+ * @returns True if the user is fully authenticated and ready for API calls
3763
+ */
3764
+ isAuthed() {
3765
+ return !!(this.userToken?.accessToken &&
3766
+ this.userToken?.refreshToken &&
3767
+ this.userToken?.user_id);
3768
+ }
3769
+ /**
3770
+ * Get user's orders with pagination and optional filtering
3771
+ * @param params - Query parameters including page, perPage, and filters
3772
+ * @returns Promise with paginated result that supports navigation
3773
+ */
3774
+ async getOrders(params) {
3775
+ if (!this.isAuthenticated()) {
3776
+ throw new AuthenticationError('User is not authenticated');
3777
+ }
3778
+ const page = params?.page || 1;
3779
+ const perPage = params?.perPage || 100;
3780
+ const filter = params?.filter;
3781
+ return this.getOrdersPage(page, perPage, filter);
3782
+ }
3783
+ /**
3784
+ * Get user's positions with pagination and optional filtering
3785
+ * @param params - Query parameters including page, perPage, and filters
3786
+ * @returns Promise with paginated result that supports navigation
3787
+ */
3788
+ async getPositions(params) {
3789
+ if (!this.isAuthenticated()) {
3790
+ throw new AuthenticationError('User is not authenticated');
3791
+ }
3792
+ const page = params?.page || 1;
3793
+ const perPage = params?.perPage || 100;
3794
+ const filter = params?.filter;
3795
+ return this.getPositionsPage(page, perPage, filter);
3796
+ }
3797
+ /**
3798
+ * Get user's accounts with pagination and optional filtering
3799
+ * @param params - Query parameters including page, perPage, and filters
3800
+ * @returns Promise with paginated result that supports navigation
3801
+ */
3802
+ async getAccounts(params) {
3803
+ if (!this.isAuthenticated()) {
3804
+ throw new AuthenticationError('User is not authenticated');
3805
+ }
3806
+ const page = params?.page || 1;
3807
+ const perPage = params?.perPage || 100;
3808
+ const filter = params?.filter;
3809
+ return this.getAccountsPage(page, perPage, filter);
3810
+ }
3811
+ /**
3812
+ * Revoke the current user's access
3813
+ */
3814
+ async revokeToken() {
3815
+ if (!this.userToken) {
3816
+ return;
3817
+ }
3818
+ try {
3819
+ await this.apiClient.revokeToken(this.userToken.accessToken);
3820
+ this.userToken = null;
3821
+ }
3822
+ catch (error) {
3823
+ this.emit('error', error);
3824
+ throw error;
3825
+ }
3826
+ }
3827
+ /**
3828
+ * Initialize the Finatic Connect SDK
3829
+ * @param token - The portal token from your backend
3830
+ * @param userId - Optional: The user ID if you have it from a previous session
3831
+ * @param options - Optional configuration including baseUrl
3832
+ * @returns FinaticConnect instance
3833
+ */
3834
+ static async init(token, userId, options) {
3835
+ if (!FinaticConnect.instance) {
3836
+ const connectOptions = {
3837
+ token,
3838
+ baseUrl: options?.baseUrl || 'http://localhost:8000',
3839
+ onSuccess: undefined,
3840
+ onError: undefined,
3841
+ onClose: undefined,
3842
+ };
3843
+ // Generate device info
3844
+ const deviceInfo = {
3845
+ ip_address: '', // Will be set by the server
3846
+ user_agent: navigator.userAgent,
3847
+ fingerprint: btoa([
3848
+ navigator.userAgent,
3849
+ navigator.language,
3850
+ new Date().getTimezoneOffset(),
3851
+ screen.width,
3852
+ screen.height,
3853
+ navigator.hardwareConcurrency,
3854
+ // @ts-expect-error - deviceMemory is not in the Navigator type but exists in modern browsers
3855
+ navigator.deviceMemory || 'unknown',
3856
+ ].join('|')),
3857
+ };
3858
+ FinaticConnect.instance = new FinaticConnect(connectOptions, deviceInfo);
3859
+ // Start session and get session data
3860
+ const normalizedUserId = userId || undefined; // Convert null to undefined
3861
+ const startResponse = await FinaticConnect.instance.apiClient.startSession(token, normalizedUserId);
3862
+ FinaticConnect.instance.sessionId = startResponse.data.session_id;
3863
+ FinaticConnect.instance.companyId = startResponse.data.company_id || '';
3864
+ // Set session context in API client
3865
+ if (FinaticConnect.instance.apiClient &&
3866
+ typeof FinaticConnect.instance.apiClient.setSessionContext === 'function') {
3867
+ FinaticConnect.instance.apiClient.setSessionContext(FinaticConnect.instance.sessionId, FinaticConnect.instance.companyId, startResponse.data.csrf_token // If available in response
3868
+ );
3869
+ }
3870
+ // If userId is provided, authenticate directly
3871
+ if (normalizedUserId) {
3872
+ try {
3873
+ const authResponse = await FinaticConnect.instance.apiClient.authenticateDirectly(startResponse.data.session_id, normalizedUserId);
3874
+ // Convert API response to UserToken format
3875
+ const userToken = {
3876
+ accessToken: authResponse.data.access_token,
3877
+ refreshToken: authResponse.data.refresh_token,
3878
+ expiresIn: 3600, // Default to 1 hour
3879
+ user_id: normalizedUserId,
3880
+ tokenType: 'Bearer',
3881
+ scope: 'api:access',
3882
+ };
3883
+ // Set the tokens in both FinaticConnect and ApiClient
3884
+ FinaticConnect.instance.userToken = userToken;
3885
+ // Set tokens in ApiClient for automatic token management
3886
+ const expiresAt = new Date(Date.now() + 3600 * 1000).toISOString(); // 1 hour from now
3887
+ FinaticConnect.instance.apiClient.setTokens(authResponse.data.access_token, authResponse.data.refresh_token, expiresAt, normalizedUserId);
3888
+ // Emit success event
3889
+ FinaticConnect.instance.emit('success', normalizedUserId);
3890
+ }
3891
+ catch (error) {
3892
+ FinaticConnect.instance.emit('error', error);
3893
+ throw error;
3894
+ }
3895
+ }
3896
+ }
3897
+ return FinaticConnect.instance;
3898
+ }
3899
+ /**
3900
+ * Initialize the SDK with a user ID
3901
+ * @param userId - The user ID from a previous session
3902
+ */
3903
+ async setUserId(userId) {
3904
+ await this.initializeWithUser(userId);
3905
+ }
3906
+ async initializeWithUser(userId) {
3907
+ try {
3908
+ this.userToken = await this.apiClient.getUserToken(userId);
3909
+ // Set tokens in ApiClient for automatic token management
3910
+ if (this.userToken) {
3911
+ const expiresAt = new Date(Date.now() + 3600 * 1000).toISOString(); // 1 hour from now
3912
+ this.apiClient.setTokens(this.userToken.accessToken, this.userToken.refreshToken, expiresAt, userId);
3913
+ }
3914
+ this.emit('success', userId);
3915
+ }
3916
+ catch (error) {
3917
+ this.emit('error', error);
3918
+ }
3919
+ }
3920
+ /**
3921
+ * Handle company access error by opening the portal
3922
+ * @param error The company access error
3923
+ * @param options Optional configuration for the portal
3924
+ */
3925
+ async handleCompanyAccessError(error, options) {
3926
+ // Emit a specific event for company access errors
3927
+ this.emit('companyAccessError', error);
3928
+ // Open the portal to allow the user to connect a broker
3929
+ await this.openPortal(options);
3930
+ }
3931
+ /**
3932
+ * Open the portal for user authentication
3933
+ * @param options Optional configuration for the portal
3934
+ */
3935
+ async openPortal(options) {
3936
+ try {
3937
+ if (!this.sessionId) {
3938
+ throw new SessionError('Session not initialized');
3939
+ }
3940
+ // Ensure session is active
3941
+ const sessionState = this.apiClient.getCurrentSessionState();
3942
+ if (sessionState !== SessionState.ACTIVE) {
3943
+ // If not active, try to start a new session
3944
+ const startResponse = await this.apiClient.startSession(this.options.token);
3945
+ this.sessionId = startResponse.data.session_id;
3946
+ this.companyId = startResponse.data.company_id || '';
3947
+ // Set session context in API client
3948
+ if (this.apiClient && typeof this.apiClient.setSessionContext === 'function') {
3949
+ this.apiClient.setSessionContext(this.sessionId, this.companyId, startResponse.data.csrf_token // If available in response
3950
+ );
3951
+ }
3952
+ // Wait for session to become active
3953
+ const maxAttempts = 5;
3954
+ let attempts = 0;
3955
+ while (attempts < maxAttempts) {
3956
+ const sessionResponse = await this.apiClient.validatePortalSession(this.sessionId, '');
3957
+ if (sessionResponse.status === 'active') {
3958
+ break;
3959
+ }
3960
+ await new Promise(resolve => setTimeout(resolve, 1000)); // Wait 1 second between attempts
3961
+ attempts++;
3962
+ }
3963
+ if (attempts === maxAttempts) {
3964
+ throw new SessionError('Session failed to become active');
3965
+ }
3966
+ }
3967
+ // Get portal URL
3968
+ const portalResponse = await this.apiClient.getPortalUrl(this.sessionId);
3969
+ if (!portalResponse.data.portal_url) {
3970
+ throw new Error('Failed to get portal URL');
3971
+ }
3972
+ // Apply theme to portal URL if provided
3973
+ const themedPortalUrl = appendThemeToURL(portalResponse.data.portal_url, options?.theme);
3974
+ // Create portal UI if not exists
3975
+ if (!this.portalUI) {
3976
+ this.portalUI = new PortalUI(this.baseUrl);
3977
+ }
3978
+ // Show portal
3979
+ this.portalUI.show(themedPortalUrl, this.sessionId || '', {
3980
+ onSuccess: async (userId) => {
3981
+ try {
3982
+ if (!this.sessionId) {
3983
+ throw new SessionError('Session not initialized');
3984
+ }
3985
+ // Get tokens from portal UI
3986
+ const userToken = this.portalUI.getTokens();
3987
+ if (!userToken) {
3988
+ throw new Error('No tokens received from portal');
3989
+ }
3990
+ // Set the tokens internally
3991
+ this.userToken = userToken;
3992
+ // Set tokens in ApiClient for automatic token management
3993
+ const expiresAt = new Date(Date.now() + 3600 * 1000).toISOString(); // 1 hour from now
3994
+ this.apiClient.setTokens(userToken.accessToken, userToken.refreshToken, expiresAt, userId);
3995
+ // Emit portal success event
3996
+ this.emit('portal:success', userId);
3997
+ // Emit legacy success event
3998
+ this.emit('success', userId);
3999
+ options?.onSuccess?.(userId);
4000
+ }
4001
+ catch (error) {
4002
+ if (error instanceof CompanyAccessError) {
4003
+ // Handle company access error by opening the portal
4004
+ await this.handleCompanyAccessError(error, options);
4005
+ }
4006
+ else {
4007
+ this.emit('error', error);
4008
+ options?.onError?.(error);
4009
+ }
4010
+ }
4011
+ },
4012
+ onError: (error) => {
4013
+ // Emit portal error event
4014
+ this.emit('portal:error', error);
4015
+ // Emit legacy error event
4016
+ this.emit('error', error);
4017
+ options?.onError?.(error);
4018
+ },
4019
+ onClose: () => {
4020
+ // Emit portal close event
4021
+ this.emit('portal:close');
4022
+ // Emit legacy close event
4023
+ this.emit('close');
4024
+ options?.onClose?.();
4025
+ },
4026
+ onEvent: (type, data) => {
4027
+ console.log('[FinaticConnect] Portal event received:', type, data);
4028
+ // Emit generic event
4029
+ this.emit('event', type, data);
4030
+ // Call the event callback
4031
+ options?.onEvent?.(type, data);
4032
+ },
4033
+ });
4034
+ }
4035
+ catch (error) {
4036
+ if (error instanceof CompanyAccessError) {
4037
+ // Handle company access error by opening the portal
4038
+ await this.handleCompanyAccessError(error, options);
4039
+ }
4040
+ else {
4041
+ this.emit('error', error);
4042
+ options?.onError?.(error);
4043
+ }
4044
+ }
4045
+ }
4046
+ /**
4047
+ * Close the Finatic Connect Portal
4048
+ */
4049
+ closePortal() {
4050
+ this.portalUI.hide();
4051
+ this.emit('close');
4052
+ }
4053
+ /**
4054
+ * Initialize a new session
4055
+ * @param oneTimeToken - The one-time token from initSession
4056
+ */
4057
+ async startSession(oneTimeToken) {
4058
+ try {
4059
+ const response = await this.apiClient.startSession(oneTimeToken);
4060
+ this.sessionId = response.data.session_id;
4061
+ this.currentSessionState = response.data.state;
4062
+ // Set session context in API client
4063
+ if (this.apiClient && typeof this.apiClient.setSessionContext === 'function') {
4064
+ this.apiClient.setSessionContext(this.sessionId, this.companyId, response.data.csrf_token // If available in response
4065
+ );
4066
+ }
4067
+ // For non-direct auth, we need to wait for the session to be ACTIVE
4068
+ if (response.data.state === SessionState.PENDING) {
4069
+ // Wait for session to become active
4070
+ const maxAttempts = 5;
4071
+ let attempts = 0;
4072
+ while (attempts < maxAttempts) {
4073
+ const sessionResponse = await this.apiClient.validatePortalSession(this.sessionId, '');
4074
+ if (sessionResponse.status === 'active') {
4075
+ this.currentSessionState = SessionState.ACTIVE;
4076
+ break;
4077
+ }
4078
+ await new Promise(resolve => setTimeout(resolve, 1000)); // Wait 1 second between attempts
4079
+ attempts++;
4080
+ }
4081
+ if (attempts === maxAttempts) {
4082
+ throw new SessionError('Session failed to become active');
4083
+ }
4084
+ }
4085
+ }
4086
+ catch (error) {
4087
+ if (error instanceof SessionError) {
4088
+ throw new AuthenticationError('Failed to start session', error.details);
4089
+ }
4090
+ throw error;
4091
+ }
4092
+ }
4093
+ /**
4094
+ * Place a new order using the broker order API
4095
+ * @param order - Order details with broker context
4096
+ */
4097
+ async placeOrder(order) {
4098
+ if (!this.userToken) {
4099
+ throw new Error('Not initialized with user');
4100
+ }
4101
+ try {
4102
+ // Convert order format to match broker API
4103
+ const brokerOrder = {
4104
+ symbol: order.symbol,
4105
+ orderQty: order.quantity,
4106
+ action: order.side === 'buy' ? 'Buy' : 'Sell',
4107
+ orderType: order.orderType === 'market'
4108
+ ? 'Market'
4109
+ : order.orderType === 'limit'
4110
+ ? 'Limit'
4111
+ : order.orderType === 'stop'
4112
+ ? 'Stop'
4113
+ : 'TrailingStop',
4114
+ assetType: order.assetType || 'Stock',
4115
+ timeInForce: order.timeInForce,
4116
+ price: order.price,
4117
+ stopPrice: order.stopPrice,
4118
+ broker: order.broker,
4119
+ accountNumber: order.accountNumber,
4120
+ };
4121
+ return await this.apiClient.placeBrokerOrder(this.userToken.accessToken, brokerOrder);
4122
+ }
4123
+ catch (error) {
4124
+ this.emit('error', error);
4125
+ throw error;
4126
+ }
4127
+ }
4128
+ /**
4129
+ * Cancel a broker order
4130
+ * @param orderId - The order ID to cancel
4131
+ * @param broker - Optional broker override
4132
+ */
4133
+ async cancelOrder(orderId, broker) {
4134
+ if (!this.userToken) {
4135
+ throw new Error('Not initialized with user');
4136
+ }
4137
+ try {
4138
+ return await this.apiClient.cancelBrokerOrder(orderId, broker);
4139
+ }
4140
+ catch (error) {
4141
+ this.emit('error', error);
4142
+ throw error;
4143
+ }
4144
+ }
4145
+ /**
4146
+ * Modify a broker order
4147
+ * @param orderId - The order ID to modify
4148
+ * @param modifications - The modifications to apply
4149
+ * @param broker - Optional broker override
4150
+ */
4151
+ async modifyOrder(orderId, modifications, broker) {
4152
+ if (!this.userToken) {
4153
+ throw new Error('Not initialized with user');
4154
+ }
4155
+ try {
4156
+ // Convert modifications to broker format
4157
+ const brokerModifications = {};
4158
+ if (modifications.symbol)
4159
+ brokerModifications.symbol = modifications.symbol;
4160
+ if (modifications.quantity)
4161
+ brokerModifications.orderQty = modifications.quantity;
4162
+ if (modifications.price)
4163
+ brokerModifications.price = modifications.price;
4164
+ if (modifications.stopPrice)
4165
+ brokerModifications.stopPrice = modifications.stopPrice;
4166
+ if (modifications.timeInForce)
4167
+ brokerModifications.timeInForce = modifications.timeInForce;
4168
+ return await this.apiClient.modifyBrokerOrder(orderId, brokerModifications, broker);
4169
+ }
4170
+ catch (error) {
4171
+ this.emit('error', error);
4172
+ throw error;
4173
+ }
4174
+ }
4175
+ /**
4176
+ * Set the broker context for trading
4177
+ * @param broker - The broker to use for trading
4178
+ */
4179
+ setBroker(broker) {
4180
+ this.apiClient.setBroker(broker);
4181
+ }
4182
+ /**
4183
+ * Set the account context for trading
4184
+ * @param accountNumber - The account number to use for trading
4185
+ * @param accountId - Optional account ID
4186
+ */
4187
+ setAccount(accountNumber, accountId) {
4188
+ this.apiClient.setAccount(accountNumber, accountId);
4189
+ }
4190
+ /**
4191
+ * Get the current trading context
4192
+ */
4193
+ getTradingContext() {
4194
+ return this.apiClient.getTradingContext();
4195
+ }
4196
+ /**
4197
+ * Clear the trading context
4198
+ */
4199
+ clearTradingContext() {
4200
+ this.apiClient.clearTradingContext();
4201
+ }
4202
+ /**
4203
+ * Place a stock market order (convenience method)
4204
+ */
4205
+ async placeStockMarketOrder(symbol, quantity, side, broker, accountNumber) {
4206
+ if (!this.userToken) {
4207
+ throw new Error('Not initialized with user');
4208
+ }
4209
+ try {
4210
+ return await this.apiClient.placeStockMarketOrder(this.userToken.accessToken, symbol, quantity, side === 'buy' ? 'Buy' : 'Sell', broker, accountNumber);
4211
+ }
4212
+ catch (error) {
4213
+ this.emit('error', error);
4214
+ throw error;
4215
+ }
4216
+ }
4217
+ /**
4218
+ * Place a stock limit order (convenience method)
4219
+ */
4220
+ async placeStockLimitOrder(symbol, quantity, side, price, timeInForce = 'gtc', broker, accountNumber) {
4221
+ if (!this.userToken) {
4222
+ throw new Error('Not initialized with user');
4223
+ }
4224
+ try {
4225
+ return await this.apiClient.placeStockLimitOrder(this.userToken.accessToken, symbol, quantity, side === 'buy' ? 'Buy' : 'Sell', price, timeInForce, broker, accountNumber);
4226
+ }
4227
+ catch (error) {
4228
+ this.emit('error', error);
4229
+ throw error;
4230
+ }
4231
+ }
4232
+ /**
4233
+ * Get the current user ID
4234
+ * @returns The current user ID or undefined if not authenticated
4235
+ * @throws AuthenticationError if user is not authenticated
4236
+ */
4237
+ getUserId() {
4238
+ if (!this.isAuthenticated()) {
4239
+ return null;
4240
+ }
4241
+ if (!this.userToken?.user_id) {
4242
+ return null;
4243
+ }
4244
+ return this.userToken.user_id;
4245
+ }
4246
+ /**
4247
+ * Get list of supported brokers
4248
+ * @returns Promise with array of broker information
4249
+ */
4250
+ async getBrokerList() {
4251
+ if (!this.isAuthenticated()) {
4252
+ throw new AuthenticationError('Not authenticated');
4253
+ }
4254
+ const response = await this.apiClient.getBrokerListAuto();
4255
+ const baseUrl = this.baseUrl.replace('/api/v1', ''); // Remove /api/v1 to get the base URL
4256
+ // Transform the broker list to include full logo URLs
4257
+ return response.response_data.map((broker) => ({
4258
+ ...broker,
4259
+ logo_path: broker.logo_path ? `${baseUrl}${broker.logo_path}` : '',
4260
+ }));
4261
+ }
4262
+ /**
4263
+ * Get broker connections
4264
+ * @returns Promise with array of broker connections
4265
+ * @throws AuthenticationError if user is not authenticated
4266
+ */
4267
+ async getBrokerConnections() {
4268
+ if (!this.isAuthenticated()) {
4269
+ throw new AuthenticationError('User is not authenticated. Please connect a broker first.');
4270
+ }
4271
+ if (!this.userToken?.user_id) {
4272
+ throw new AuthenticationError('No user ID available. Please connect a broker first.');
4273
+ }
4274
+ const response = await this.apiClient.getBrokerConnectionsAuto();
4275
+ if (response.status_code !== 200) {
4276
+ throw new Error(response.message || 'Failed to retrieve broker connections');
4277
+ }
4278
+ return response.response_data;
4279
+ }
4280
+ // Abstract convenience methods
4281
+ /**
4282
+ * Get only open positions
4283
+ * @returns Promise with array of open positions
4284
+ */
4285
+ async getOpenPositions() {
4286
+ return this.getAllPositions({ position_status: 'open' });
4287
+ }
4288
+ /**
4289
+ * Get only filled orders
4290
+ * @returns Promise with array of filled orders
4291
+ */
4292
+ async getFilledOrders() {
4293
+ return this.getAllOrders({ status: 'filled' });
4294
+ }
4295
+ /**
4296
+ * Get only pending orders
4297
+ * @returns Promise with array of pending orders
4298
+ */
4299
+ async getPendingOrders() {
4300
+ return this.getAllOrders({ status: 'pending' });
4301
+ }
4302
+ /**
4303
+ * Get only active accounts
4304
+ * @returns Promise with array of active accounts
4305
+ */
4306
+ async getActiveAccounts() {
4307
+ return this.getAllAccounts({ status: 'active' });
4308
+ }
4309
+ /**
4310
+ * Get orders for a specific symbol
4311
+ * @param symbol - The symbol to filter by
4312
+ * @returns Promise with array of orders for the symbol
4313
+ */
4314
+ async getOrdersBySymbol(symbol) {
4315
+ return this.getAllOrders({ symbol });
4316
+ }
4317
+ /**
4318
+ * Get positions for a specific symbol
4319
+ * @param symbol - The symbol to filter by
4320
+ * @returns Promise with array of positions for the symbol
4321
+ */
4322
+ async getPositionsBySymbol(symbol) {
4323
+ return this.getAllPositions({ symbol });
4324
+ }
4325
+ /**
4326
+ * Get orders for a specific broker
4327
+ * @param brokerId - The broker ID to filter by
4328
+ * @returns Promise with array of orders for the broker
4329
+ */
4330
+ async getOrdersByBroker(brokerId) {
4331
+ return this.getAllOrders({ broker_id: brokerId });
4332
+ }
4333
+ /**
4334
+ * Get positions for a specific broker
4335
+ * @param brokerId - The broker ID to filter by
4336
+ * @returns Promise with array of positions for the broker
4337
+ */
4338
+ async getPositionsByBroker(brokerId) {
4339
+ return this.getAllPositions({ broker_id: brokerId });
4340
+ }
4341
+ // Pagination methods
4342
+ /**
4343
+ * Get a specific page of orders with pagination metadata
4344
+ * @param page - Page number (default: 1)
4345
+ * @param perPage - Items per page (default: 100)
4346
+ * @param filter - Optional filter parameters
4347
+ * @returns Promise with paginated orders result
4348
+ */
4349
+ async getOrdersPage(page = 1, perPage = 100, filter) {
4350
+ if (!this.isAuthenticated()) {
4351
+ throw new AuthenticationError('User is not authenticated');
4352
+ }
4353
+ return this.apiClient.getBrokerOrdersPage(page, perPage, filter);
4354
+ }
4355
+ /**
4356
+ * Get a specific page of positions with pagination metadata
4357
+ * @param page - Page number (default: 1)
4358
+ * @param perPage - Items per page (default: 100)
4359
+ * @param filter - Optional filter parameters
4360
+ * @returns Promise with paginated positions result
4361
+ */
4362
+ async getPositionsPage(page = 1, perPage = 100, filter) {
4363
+ if (!this.isAuthenticated()) {
4364
+ throw new AuthenticationError('User is not authenticated');
4365
+ }
4366
+ return this.apiClient.getBrokerPositionsPage(page, perPage, filter);
4367
+ }
4368
+ /**
4369
+ * Get a specific page of accounts with pagination metadata
4370
+ * @param page - Page number (default: 1)
4371
+ * @param perPage - Items per page (default: 100)
4372
+ * @param filter - Optional filter parameters
4373
+ * @returns Promise with paginated accounts result
4374
+ */
4375
+ async getAccountsPage(page = 1, perPage = 100, filter) {
4376
+ if (!this.isAuthenticated()) {
4377
+ throw new AuthenticationError('User is not authenticated');
4378
+ }
4379
+ return this.apiClient.getBrokerAccountsPage(page, perPage, filter);
4380
+ }
4381
+ /**
4382
+ * Get the next page of orders
4383
+ * @param previousResult - The previous paginated result
4384
+ * @returns Promise with next page of orders or null if no more pages
4385
+ */
4386
+ async getNextOrdersPage(previousResult) {
4387
+ if (!this.isAuthenticated()) {
4388
+ throw new AuthenticationError('User is not authenticated');
4389
+ }
4390
+ return this.apiClient.getNextPage(previousResult, (offset, limit) => {
4391
+ const page = Math.floor(offset / limit) + 1;
4392
+ return this.apiClient.getBrokerOrdersPage(page, limit);
4393
+ });
4394
+ }
4395
+ /**
4396
+ * Get the next page of positions
4397
+ * @param previousResult - The previous paginated result
4398
+ * @returns Promise with next page of positions or null if no more pages
4399
+ */
4400
+ async getNextPositionsPage(previousResult) {
4401
+ if (!this.isAuthenticated()) {
4402
+ throw new AuthenticationError('User is not authenticated');
4403
+ }
4404
+ return this.apiClient.getNextPage(previousResult, (offset, limit) => {
4405
+ const page = Math.floor(offset / limit) + 1;
4406
+ return this.apiClient.getBrokerPositionsPage(page, limit);
4407
+ });
4408
+ }
4409
+ /**
4410
+ * Get the next page of accounts
4411
+ * @param previousResult - The previous paginated result
4412
+ * @returns Promise with next page of accounts or null if no more pages
4413
+ */
4414
+ async getNextAccountsPage(previousResult) {
4415
+ if (!this.isAuthenticated()) {
4416
+ throw new AuthenticationError('User is not authenticated');
4417
+ }
4418
+ return this.apiClient.getNextPage(previousResult, (offset, limit) => {
4419
+ const page = Math.floor(offset / limit) + 1;
4420
+ return this.apiClient.getBrokerAccountsPage(page, limit);
4421
+ });
4422
+ }
4423
+ /**
4424
+ * Get all orders across all pages (convenience method)
4425
+ * @param filter - Optional filter parameters
4426
+ * @returns Promise with all orders
4427
+ */
4428
+ async getAllOrders(filter) {
4429
+ if (!this.isAuthenticated()) {
4430
+ throw new AuthenticationError('User is not authenticated');
4431
+ }
4432
+ const allData = [];
4433
+ let currentResult = await this.getOrdersPage(1, 100, filter);
4434
+ while (currentResult) {
4435
+ allData.push(...currentResult.data);
4436
+ if (!currentResult.hasNext)
4437
+ break;
4438
+ const nextResult = await this.getNextOrdersPage(currentResult);
4439
+ if (!nextResult)
4440
+ break;
4441
+ currentResult = nextResult;
4442
+ }
4443
+ return allData;
4444
+ }
4445
+ /**
4446
+ * Get all positions across all pages (convenience method)
4447
+ * @param filter - Optional filter parameters
4448
+ * @returns Promise with all positions
4449
+ */
4450
+ async getAllPositions(filter) {
4451
+ if (!this.isAuthenticated()) {
4452
+ throw new AuthenticationError('User is not authenticated');
4453
+ }
4454
+ const allData = [];
4455
+ let currentResult = await this.getPositionsPage(1, 100, filter);
4456
+ while (currentResult) {
4457
+ allData.push(...currentResult.data);
4458
+ if (!currentResult.hasNext)
4459
+ break;
4460
+ const nextResult = await this.getNextPositionsPage(currentResult);
4461
+ if (!nextResult)
4462
+ break;
4463
+ currentResult = nextResult;
4464
+ }
4465
+ return allData;
4466
+ }
4467
+ /**
4468
+ * Get all accounts across all pages (convenience method)
4469
+ * @param filter - Optional filter parameters
4470
+ * @returns Promise with all accounts
4471
+ */
4472
+ async getAllAccounts(filter) {
4473
+ if (!this.isAuthenticated()) {
4474
+ throw new AuthenticationError('User is not authenticated');
4475
+ }
4476
+ const allData = [];
4477
+ let currentResult = await this.getAccountsPage(1, 100, filter);
4478
+ while (currentResult) {
4479
+ allData.push(...currentResult.data);
4480
+ if (!currentResult.hasNext)
4481
+ break;
4482
+ const nextResult = await this.getNextAccountsPage(currentResult);
4483
+ if (!nextResult)
4484
+ break;
4485
+ currentResult = nextResult;
4486
+ }
4487
+ return allData;
4488
+ }
4489
+ /**
4490
+ * Register automatic session cleanup on page unload/visibility change
4491
+ */
4492
+ registerSessionCleanup() {
4493
+ // Cleanup on page unload (refresh, close tab, navigate away)
4494
+ window.addEventListener('beforeunload', this.handleSessionCleanup.bind(this));
4495
+ // Cleanup on page visibility change (mobile browsers, app switching)
4496
+ document.addEventListener('visibilitychange', this.handleVisibilityChange.bind(this));
4497
+ }
4498
+ /**
4499
+ * Handle session cleanup when page is unloading
4500
+ */
4501
+ async handleSessionCleanup() {
4502
+ if (this.sessionId) {
4503
+ await this.completeSession(this.sessionId);
4504
+ }
4505
+ }
4506
+ /**
4507
+ * Handle visibility change (mobile browsers)
4508
+ */
4509
+ async handleVisibilityChange() {
4510
+ if (document.visibilityState === 'hidden' && this.sessionId) {
4511
+ await this.completeSession(this.sessionId);
4512
+ }
4513
+ }
4514
+ /**
4515
+ * Complete the session by calling the API
4516
+ * @param sessionId - The session ID to complete
4517
+ */
4518
+ async completeSession(sessionId) {
4519
+ try {
4520
+ // Check if we're in mock mode (check if apiClient is a mock client)
4521
+ const isMockMode = this.apiClient &&
4522
+ typeof this.apiClient.isMockClient === 'function' &&
4523
+ this.apiClient.isMockClient();
4524
+ if (isMockMode) {
4525
+ // Mock the completion response
4526
+ console.log('[FinaticConnect] Mock session completion for session:', sessionId);
4527
+ return;
4528
+ }
4529
+ // Real API call
4530
+ const response = await fetch(`${this.baseUrl}/portal/${sessionId}/complete`, {
4531
+ method: 'POST',
4532
+ headers: {
4533
+ Authorization: `Bearer ${this.userToken?.accessToken || ''}`,
4534
+ 'Content-Type': 'application/json',
4535
+ },
4536
+ });
4537
+ if (response.ok) {
4538
+ console.log('[FinaticConnect] Session completed successfully');
4539
+ }
4540
+ else {
4541
+ console.warn('[FinaticConnect] Failed to complete session:', response.status);
4542
+ }
4543
+ }
4544
+ catch (error) {
4545
+ // Silent failure - don't throw errors during cleanup
4546
+ console.warn('[FinaticConnect] Session cleanup failed:', error);
4547
+ }
4548
+ }
4549
+ }
4550
+ FinaticConnect.instance = null;
4551
+
4552
+ class CoreTradingService {
4553
+ constructor(apiClient) {
4554
+ this.apiClient = apiClient;
4555
+ }
4556
+ async getAccounts(filter) {
4557
+ return this.getAllAccounts(filter);
4558
+ }
4559
+ async getOrders(filter) {
4560
+ return this.getAllOrders(filter);
4561
+ }
4562
+ async getPositions(filter) {
4563
+ return this.getAllPositions(filter);
4564
+ }
4565
+ async placeOrder(order) {
4566
+ const response = await this.apiClient.placeOrder(order);
4567
+ return response.data.orderId;
4568
+ }
4569
+ async cancelOrder(orderId) {
4570
+ const response = await this.apiClient.cancelOrder(orderId);
4571
+ return response.data.success;
4572
+ }
4573
+ async placeOptionsOrder(order) {
4574
+ const response = await this.apiClient.placeOptionsOrder(order);
4575
+ return response.data.orderId;
4576
+ }
4577
+ // Pagination methods
4578
+ async getAccountsPage(page = 1, perPage = 100, filter) {
4579
+ return this.apiClient.getBrokerAccountsPage(page, perPage, filter);
4580
+ }
4581
+ async getOrdersPage(page = 1, perPage = 100, filter) {
4582
+ return this.apiClient.getBrokerOrdersPage(page, perPage, filter);
4583
+ }
4584
+ async getPositionsPage(page = 1, perPage = 100, filter) {
4585
+ return this.apiClient.getBrokerPositionsPage(page, perPage, filter);
4586
+ }
4587
+ // Navigation methods
4588
+ async getNextAccountsPage(previousResult) {
4589
+ return this.apiClient.getNextPage(previousResult, (offset, limit) => this.apiClient.getBrokerAccountsPage(1, limit, { offset }));
4590
+ }
4591
+ async getNextOrdersPage(previousResult) {
4592
+ return this.apiClient.getNextPage(previousResult, (offset, limit) => this.apiClient.getBrokerOrdersPage(1, limit, { offset }));
4593
+ }
4594
+ async getNextPositionsPage(previousResult) {
4595
+ return this.apiClient.getNextPage(previousResult, (offset, limit) => this.apiClient.getBrokerPositionsPage(1, limit, { offset }));
4596
+ }
4597
+ // Get all pages convenience methods
4598
+ async getAllAccounts(filter) {
4599
+ const allData = [];
4600
+ let currentResult = await this.getAccountsPage(1, 100, filter);
4601
+ while (currentResult) {
4602
+ allData.push(...currentResult.data);
4603
+ if (!currentResult.hasNext)
4604
+ break;
4605
+ const nextResult = await this.getNextAccountsPage(currentResult);
4606
+ if (!nextResult)
4607
+ break;
4608
+ currentResult = nextResult;
4609
+ }
4610
+ return allData;
4611
+ }
4612
+ async getAllOrders(filter) {
4613
+ const allData = [];
4614
+ let currentResult = await this.getOrdersPage(1, 100, filter);
4615
+ while (currentResult) {
4616
+ allData.push(...currentResult.data);
4617
+ if (!currentResult.hasNext)
4618
+ break;
4619
+ const nextResult = await this.getNextOrdersPage(currentResult);
4620
+ if (!nextResult)
4621
+ break;
4622
+ currentResult = nextResult;
4623
+ }
4624
+ return allData;
4625
+ }
4626
+ async getAllPositions(filter) {
4627
+ const allData = [];
4628
+ let currentResult = await this.getPositionsPage(1, 100, filter);
4629
+ while (currentResult) {
4630
+ allData.push(...currentResult.data);
4631
+ if (!currentResult.hasNext)
4632
+ break;
4633
+ const nextResult = await this.getNextPositionsPage(currentResult);
4634
+ if (!nextResult)
4635
+ break;
4636
+ currentResult = nextResult;
4637
+ }
4638
+ return allData;
4639
+ }
4640
+ // Abstract convenience methods
4641
+ async getOpenPositions() {
4642
+ return this.getPositions({ position_status: 'open' });
4643
+ }
4644
+ async getFilledOrders() {
4645
+ return this.getOrders({ status: 'filled' });
4646
+ }
4647
+ async getPendingOrders() {
4648
+ return this.getOrders({ status: 'pending' });
4649
+ }
4650
+ async getActiveAccounts() {
4651
+ return this.getAccounts({ status: 'active' });
4652
+ }
4653
+ async getOrdersBySymbol(symbol) {
4654
+ return this.getOrders({ symbol });
4655
+ }
4656
+ async getPositionsBySymbol(symbol) {
4657
+ return this.getPositions({ symbol });
4658
+ }
4659
+ async getOrdersByBroker(brokerId) {
4660
+ return this.getOrders({ broker_id: brokerId });
4661
+ }
4662
+ async getPositionsByBroker(brokerId) {
4663
+ return this.getPositions({ broker_id: brokerId });
4664
+ }
4665
+ }
4666
+
4667
+ class CoreAnalyticsService {
4668
+ constructor(apiClient) {
4669
+ this.apiClient = apiClient;
4670
+ }
4671
+ async getPerformance() {
4672
+ const response = await this.apiClient.getPerformance();
4673
+ return response.data.performance;
4674
+ }
4675
+ async getDailyHistory() {
4676
+ const response = await this.apiClient.getDailyHistory();
4677
+ return response.data.history;
4678
+ }
4679
+ async getWeeklySnapshots() {
4680
+ const response = await this.apiClient.getWeeklySnapshots();
4681
+ return response.data.snapshots;
4682
+ }
4683
+ async getPortfolioDeltas() {
4684
+ const response = await this.apiClient.getPortfolioDeltas();
4685
+ return response.data.deltas;
4686
+ }
4687
+ async getUserLogs(userId) {
4688
+ const response = await this.apiClient.getUserLogs(userId);
4689
+ return response.data.logs;
4690
+ }
4691
+ }
4692
+
4693
+ class CorePortalService {
4694
+ constructor(apiClient) {
4695
+ this.apiClient = apiClient;
4696
+ this.iframe = null;
4697
+ }
4698
+ async createPortal(config) {
4699
+ // Create iframe
4700
+ this.iframe = document.createElement('iframe');
4701
+ this.iframe.style.display = 'none';
4702
+ document.body.appendChild(this.iframe);
4703
+ // Get portal content from API
4704
+ const response = await this.apiClient.getPortalContent();
4705
+ const content = response.data.content;
4706
+ // Set iframe content
4707
+ if (this.iframe.contentWindow) {
4708
+ this.iframe.contentWindow.document.open();
4709
+ this.iframe.contentWindow.document.write(content);
4710
+ this.iframe.contentWindow.document.close();
4711
+ }
4712
+ // Position and style iframe
4713
+ this.positionIframe(config);
4714
+ this.styleIframe(config);
4715
+ // Show iframe
4716
+ this.iframe.style.display = 'block';
4717
+ }
4718
+ async closePortal() {
4719
+ if (this.iframe) {
4720
+ this.iframe.remove();
4721
+ this.iframe = null;
4722
+ }
4723
+ }
4724
+ async updateTheme(theme) {
4725
+ if (this.iframe?.contentWindow) {
4726
+ this.iframe.contentWindow.postMessage({ type: 'update_theme', theme }, '*');
4727
+ }
4728
+ }
4729
+ async getBrokerAccounts() {
4730
+ const response = await this.apiClient.getBrokerAccounts();
4731
+ return response.data.accounts;
4732
+ }
4733
+ async linkBrokerAccount(brokerId) {
4734
+ await this.apiClient.connectBroker(brokerId);
4735
+ }
4736
+ async unlinkBrokerAccount(brokerId) {
4737
+ await this.apiClient.disconnectBroker(brokerId);
4738
+ }
4739
+ positionIframe(config) {
4740
+ if (!this.iframe)
4741
+ return;
4742
+ const position = config.position || 'center';
4743
+ const width = config.width || '600px';
4744
+ const height = config.height || '800px';
4745
+ this.iframe.style.width = width;
4746
+ this.iframe.style.height = height;
4747
+ switch (position) {
4748
+ case 'center':
4749
+ this.iframe.style.position = 'fixed';
4750
+ this.iframe.style.top = '50%';
4751
+ this.iframe.style.left = '50%';
4752
+ this.iframe.style.transform = 'translate(-50%, -50%)';
4753
+ break;
4754
+ case 'top':
4755
+ this.iframe.style.position = 'fixed';
4756
+ this.iframe.style.top = '0';
4757
+ this.iframe.style.left = '50%';
4758
+ this.iframe.style.transform = 'translateX(-50%)';
4759
+ break;
4760
+ case 'bottom':
4761
+ this.iframe.style.position = 'fixed';
4762
+ this.iframe.style.bottom = '0';
4763
+ this.iframe.style.left = '50%';
4764
+ this.iframe.style.transform = 'translateX(-50%)';
4765
+ break;
4766
+ // Note: 'left' and 'right' positions removed from PortalConfig interface
4767
+ // Default to center if invalid position is provided
4768
+ default:
4769
+ this.iframe.style.position = 'fixed';
4770
+ this.iframe.style.top = '50%';
4771
+ this.iframe.style.left = '50%';
4772
+ this.iframe.style.transform = 'translate(-50%, -50%)';
4773
+ break;
4774
+ }
4775
+ }
4776
+ styleIframe(config) {
4777
+ if (!this.iframe)
4778
+ return;
4779
+ // Apply z-index if specified
4780
+ if (config.zIndex) {
4781
+ this.iframe.style.zIndex = config.zIndex.toString();
4782
+ }
4783
+ }
4784
+ }
4785
+
4786
+ export { ApiClient, ApiError, AuthenticationError, AuthorizationError, BaseError, CompanyAccessError, CoreAnalyticsService, CorePortalService, CoreTradingService, EventEmitter, FinaticConnect, MockFactory, NetworkError, OrderError, OrderValidationError, PaginatedResult, RateLimitError, SecurityError, SessionError, TokenError, ValidationError, appendThemeToURL, createCustomThemeFromPreset, generatePortalThemeURL, getThemePreset, portalThemePresets, validateCustomTheme };
4787
+ //# sourceMappingURL=index.mjs.map