|
1 | | -import { handleApiCall } from '../../utils/api.mts' |
2 | 1 | import { isSupportedConfigKey } from '../../utils/config.mts' |
3 | | -import { hasDefaultToken, setupSdk } from '../../utils/sdk.mts' |
| 2 | +import { hasDefaultToken } from '../../utils/sdk.mts' |
| 3 | +import { fetchOrganization } from '../organization/fetch-organization-list.mts' |
4 | 4 |
|
5 | 5 | import type { CResult } from '../../types.mts' |
6 | 6 |
|
@@ -130,54 +130,32 @@ export async function discoverConfigValue( |
130 | 130 | async function getDefaultOrgFromToken(): Promise< |
131 | 131 | string[] | string | undefined |
132 | 132 | > { |
133 | | - const sockSdkCResult = await setupSdk() |
134 | | - if (!sockSdkCResult.ok) { |
| 133 | + const orgsCResult = await fetchOrganization() |
| 134 | + if (!orgsCResult.ok) { |
135 | 135 | return undefined |
136 | 136 | } |
137 | | - const sockSdk = sockSdkCResult.data |
138 | | - |
139 | | - const result = await handleApiCall( |
140 | | - sockSdk.getOrganizations(), |
141 | | - 'list of organizations', |
142 | | - ) |
143 | | - |
144 | | - if (result.ok) { |
145 | | - const arr = Array.from(Object.values(result.data.organizations)).map( |
146 | | - ({ slug }) => slug, |
147 | | - ) |
148 | | - if (arr.length === 0) { |
149 | | - return undefined |
150 | | - } |
151 | | - if (arr.length === 1) { |
152 | | - return arr[0] |
153 | | - } |
154 | | - return arr |
155 | | - } |
156 | 137 |
|
157 | | - return undefined |
| 138 | + const { organizations } = orgsCResult.data |
| 139 | + const slugs = Array.from(Object.values(organizations)).map(o => o.slug) |
| 140 | + if (slugs.length === 0) { |
| 141 | + return undefined |
| 142 | + } |
| 143 | + if (slugs.length === 1) { |
| 144 | + return slugs[0] |
| 145 | + } |
| 146 | + return slugs |
158 | 147 | } |
159 | 148 |
|
160 | 149 | async function getEnforceableOrgsFromToken(): Promise<string[] | undefined> { |
161 | | - const sockSdkCResult = await setupSdk() |
162 | | - if (!sockSdkCResult.ok) { |
| 150 | + const orgsCResult = await fetchOrganization() |
| 151 | + if (!orgsCResult.ok) { |
163 | 152 | return undefined |
164 | 153 | } |
165 | | - const sockSdk = sockSdkCResult.data |
166 | | - |
167 | | - const result = await handleApiCall( |
168 | | - sockSdk.getOrganizations(), |
169 | | - 'list of organizations', |
170 | | - ) |
171 | | - |
172 | | - if (result.ok) { |
173 | | - const arr = Array.from(Object.values(result.data.organizations)).map( |
174 | | - ({ slug }) => slug, |
175 | | - ) |
176 | | - if (arr.length === 0) { |
177 | | - return undefined |
178 | | - } |
179 | | - return arr |
180 | | - } |
181 | 154 |
|
182 | | - return undefined |
| 155 | + const { organizations } = orgsCResult.data |
| 156 | + const slugs = Array.from(Object.values(organizations)).map(o => o.slug) |
| 157 | + if (!slugs.length) { |
| 158 | + return undefined |
| 159 | + } |
| 160 | + return slugs |
183 | 161 | } |
0 commit comments