|
1 | 1 | import { ApiRequestError } from '@jetstream/salesforce-api'; |
2 | | -import { ERROR_MESSAGES } from '@jetstream/shared/constants'; |
3 | | -import { SObjectOrganization } from '@jetstream/types'; |
| 2 | +import { ERROR_MESSAGES, HTTP } from '@jetstream/shared/constants'; |
| 3 | +import { OrgsWithGroupResponse, SObjectOrganization } from '@jetstream/types'; |
| 4 | +import { app } from 'electron'; |
| 5 | +import logger from 'electron-log'; |
4 | 6 | import { z } from 'zod'; |
| 7 | +import { ENV } from '../config/environment'; |
5 | 8 | import * as dataService from '../services/persistence.service'; |
6 | | -import { createRoute, handleErrorResponse, handleJsonResponse, RouteValidator } from '../utils/route.utils'; |
| 9 | +import { createRoute, getTokens, handleErrorResponse, handleJsonResponse, RouteValidator } from '../utils/route.utils'; |
7 | 10 |
|
8 | 11 | export const routeDefinition = { |
9 | 12 | getOrgs: { |
@@ -52,7 +55,30 @@ export const routeDefinition = { |
52 | 55 |
|
53 | 56 | const getOrgs = createRoute(routeDefinition.getOrgs.validators, async ({}) => { |
54 | 57 | try { |
55 | | - const orgs = dataService.getSalesforceOrgs().map((org) => ({ ...org, accessToken: undefined })); |
| 58 | + const { authTokens, extIdentifier } = getTokens(); |
| 59 | + |
| 60 | + const webAppOrgs = await fetch(`${ENV.SERVER_URL}/desktop-app/orgs`, { |
| 61 | + method: 'GET', |
| 62 | + headers: { |
| 63 | + Accept: 'application/json', |
| 64 | + Authorization: `Bearer ${authTokens?.accessToken}`, |
| 65 | + [HTTP.HEADERS.X_EXT_DEVICE_ID]: extIdentifier.id, |
| 66 | + [HTTP.HEADERS.X_APP_VERSION]: app.getVersion(), |
| 67 | + }, |
| 68 | + }) |
| 69 | + .then(async (res) => { |
| 70 | + if (!res.ok) { |
| 71 | + logger.warn('Failed to fetch orgs from web app', { error: await res.text().catch(() => 'Unable to read response body') }); |
| 72 | + return null; |
| 73 | + } |
| 74 | + return (res.json() as Promise<{ data: OrgsWithGroupResponse }>).then(({ data }) => data); |
| 75 | + }) |
| 76 | + .catch((ex) => { |
| 77 | + logger.warn('Failed to fetch orgs from web app', { error: ex }); |
| 78 | + return null; |
| 79 | + }); |
| 80 | + |
| 81 | + const orgs = dataService.mergeWebAppOrgsWithDesktopOrgs(webAppOrgs).map((org) => ({ ...org, accessToken: undefined })); |
56 | 82 |
|
57 | 83 | return handleJsonResponse(orgs); |
58 | 84 | } catch (ex) { |
|
0 commit comments