@@ -52,6 +52,11 @@ export type AccountOrgs = {
5252 orgs : readonly Org [ ]
5353}
5454
55+ export type ActiveOrg = {
56+ account : Info
57+ org : Org
58+ }
59+
5560class RemoteConfig extends Schema . Class < RemoteConfig > ( "RemoteConfig" ) ( {
5661 config : Schema . Record ( Schema . String , Schema . Json ) ,
5762} ) { }
@@ -137,6 +142,7 @@ const mapAccountServiceError =
137142export namespace Account {
138143 export interface Interface {
139144 readonly active : ( ) => Effect . Effect < Option . Option < Info > , AccountError >
145+ readonly activeOrg : ( ) => Effect . Effect < Option . Option < ActiveOrg > , AccountError >
140146 readonly list : ( ) => Effect . Effect < Info [ ] , AccountError >
141147 readonly orgsByAccount : ( ) => Effect . Effect < readonly AccountOrgs [ ] , AccountError >
142148 readonly remove : ( accountID : AccountID ) => Effect . Effect < void , AccountError >
@@ -279,19 +285,31 @@ export namespace Account {
279285 resolveAccess ( accountID ) . pipe ( Effect . map ( Option . map ( ( r ) => r . accessToken ) ) ) ,
280286 )
281287
288+ const activeOrg = Effect . fn ( "Account.activeOrg" ) ( function * ( ) {
289+ const activeAccount = yield * repo . active ( )
290+ if ( Option . isNone ( activeAccount ) ) return Option . none < ActiveOrg > ( )
291+
292+ const account = activeAccount . value
293+ if ( ! account . active_org_id ) return Option . none < ActiveOrg > ( )
294+
295+ const accountOrgs = yield * orgs ( account . id )
296+ const org = accountOrgs . find ( ( item ) => item . id === account . active_org_id )
297+ if ( ! org ) return Option . none < ActiveOrg > ( )
298+
299+ return Option . some ( { account, org } )
300+ } )
301+
282302 const orgsByAccount = Effect . fn ( "Account.orgsByAccount" ) ( function * ( ) {
283303 const accounts = yield * repo . list ( )
284- const [ errors , results ] = yield * Effect . partition (
304+ return yield * Effect . forEach (
285305 accounts ,
286- ( account ) => orgs ( account . id ) . pipe ( Effect . map ( ( orgs ) => ( { account, orgs } ) ) ) ,
306+ ( account ) =>
307+ orgs ( account . id ) . pipe (
308+ Effect . catch ( ( ) => Effect . succeed ( [ ] as readonly Org [ ] ) ) ,
309+ Effect . map ( ( orgs ) => ( { account, orgs } ) ) ,
310+ ) ,
287311 { concurrency : 3 } ,
288312 )
289- for ( const error of errors ) {
290- yield * Effect . logWarning ( "failed to fetch orgs for account" ) . pipe (
291- Effect . annotateLogs ( { error : String ( error ) } ) ,
292- )
293- }
294- return results
295313 } )
296314
297315 const orgs = Effect . fn ( "Account.orgs" ) ( function * ( accountID : AccountID ) {
@@ -396,6 +414,7 @@ export namespace Account {
396414
397415 return Service . of ( {
398416 active : repo . active ,
417+ activeOrg,
399418 list : repo . list ,
400419 orgsByAccount,
401420 remove : repo . remove ,
@@ -417,6 +436,26 @@ export namespace Account {
417436 return Option . getOrUndefined ( await runPromise ( ( service ) => service . active ( ) ) )
418437 }
419438
439+ export async function list ( ) : Promise < Info [ ] > {
440+ return runPromise ( ( service ) => service . list ( ) )
441+ }
442+
443+ export async function activeOrg ( ) : Promise < ActiveOrg | undefined > {
444+ return Option . getOrUndefined ( await runPromise ( ( service ) => service . activeOrg ( ) ) )
445+ }
446+
447+ export async function orgsByAccount ( ) : Promise < readonly AccountOrgs [ ] > {
448+ return runPromise ( ( service ) => service . orgsByAccount ( ) )
449+ }
450+
451+ export async function orgs ( accountID : AccountID ) : Promise < readonly Org [ ] > {
452+ return runPromise ( ( service ) => service . orgs ( accountID ) )
453+ }
454+
455+ export async function switchOrg ( accountID : AccountID , orgID : OrgID ) {
456+ return runPromise ( ( service ) => service . use ( accountID , Option . some ( orgID ) ) )
457+ }
458+
420459 export async function token ( accountID : AccountID ) : Promise < AccessToken | undefined > {
421460 const t = await runPromise ( ( service ) => service . token ( accountID ) )
422461 return Option . getOrUndefined ( t )
0 commit comments