11import terminalLink from 'terminal-link'
22
3+ import { joinAnd } from '@socketsecurity/registry/lib/arrays'
34import { logger } from '@socketsecurity/registry/lib/logger'
45import { confirm , password , select } from '@socketsecurity/registry/lib/prompts'
56
67import { applyLogin } from './apply-login.mts'
78import constants from '../../constants.mts'
8- import { handleApiCall } from '../../utils/api.mts'
99import {
1010 getConfigValueOrUndef ,
1111 isReadOnlyConfig ,
1212 updateConfigValue ,
1313} from '../../utils/config.mts'
1414import { failMsgWithBadge } from '../../utils/fail-msg-with-badge.mts'
15+ import { getEnterpriseOrgs , getOrgSlugs } from '../../utils/organization.mts'
1516import { setupSdk } from '../../utils/sdk.mts'
1617import { setupTabCompletion } from '../install/setup-tab-completion.mts'
18+ import { fetchOrganization } from '../organization/fetch-organization-list.mts'
1719
1820import type { Choice , Separator } from '@socketsecurity/registry/lib/prompts'
1921
@@ -50,8 +52,9 @@ export async function attemptLogin(
5052
5153 const sockSdk = sockSdkCResult . data
5254
53- const orgsCResult = await handleApiCall ( sockSdk . getOrganizations ( ) , {
55+ const orgsCResult = await fetchOrganization ( {
5456 desc : 'token verification' ,
57+ sdk : sockSdk ,
5558 } )
5659 if ( ! orgsCResult . ok ) {
5760 process . exitCode = 1
@@ -60,27 +63,31 @@ export async function attemptLogin(
6063 }
6164
6265 const { organizations } = orgsCResult . data
63- const orgSlugs = Object . values ( organizations ) . map ( obj => obj . slug )
6466
65- logger . success ( `API token verified: ${ orgSlugs } ` )
67+ const orgSlugs = getOrgSlugs ( organizations )
6668
67- const enforcedChoices : OrgChoices = Object . values ( organizations )
68- . filter ( org => org ?. plan === 'enterprise' )
69- . map ( org => ( {
70- name : org . name ?? 'undefined' ,
71- value : org . id ,
72- } ) )
69+ logger . success ( `API token verified: ${ joinAnd ( orgSlugs ) } ` )
70+
71+ const enterpriseOrgs = getEnterpriseOrgs ( organizations )
72+
73+ const enforcedChoices : OrgChoices = enterpriseOrgs . map ( org => ( {
74+ name : org . name ?? 'undefined' ,
75+ value : org . id ,
76+ } ) )
7377
7478 let enforcedOrgs : string [ ] = [ ]
7579 if ( enforcedChoices . length > 1 ) {
7680 const id = await select ( {
7781 message :
7882 "Which organization's policies should Socket enforce system-wide?" ,
79- choices : enforcedChoices . concat ( {
80- name : 'None' ,
81- value : '' ,
82- description : 'Pick "None" if this is a personal device' ,
83- } ) ,
83+ choices : [
84+ ...enforcedChoices ,
85+ {
86+ name : 'None' ,
87+ value : '' ,
88+ description : 'Pick "None" if this is a personal device' ,
89+ } ,
90+ ] ,
8491 } )
8592 if ( id === undefined ) {
8693 logger . fail ( 'Canceled by user' )
0 commit comments