@@ -3,11 +3,12 @@ import { dashboardClient } from './sql/client.js';
33
44export const ACCOUNTS_TABLE = 'gp_accounts' ;
55export const MEMBERS_TABLE = 'gp_org_members' ;
6+ const ORGS_TABLE = 'gp_orgs' ;
67const ACCOUNT_TTL = 60 * 60 * 1000 ;
78const ROLE_TTL = 60 * 1000 ;
89
910export type AccountRole = 'owner' | 'admin' | 'member' | 'viewer' ;
10- export type ResolvedAccount = { id : string ; role : AccountRole } ;
11+ export type ResolvedAccount = { id : string ; role : AccountRole ; userType : 'member' | 'sponsor' | 'special' } ;
1112
1213const accountIdsByUserId = new TTLCache < string , string > ( { ttl : ACCOUNT_TTL } ) ;
1314const rolesByUserAndAccount = new TTLCache < string , ResolvedAccount | null > ( { ttl : ROLE_TTL } ) ;
@@ -29,7 +30,7 @@ export const getUserAccountId = async (userId: string): Promise<string | null> =
2930} ;
3031
3132// The role of the user in the account: 'owner' for their own, their membership role for an org, null if they are not in it.
32- export const getAccountRole = async ( accountId : string , userId : string ) : Promise < ResolvedAccount | null > => {
33+ export const getOrgRole = async ( accountId : string , userId : string ) : Promise < ResolvedAccount | null > => {
3334 const key = `${ userId } :${ accountId } ` ;
3435 const cached = rolesByUserAndAccount . get ( key ) ;
3536
@@ -38,20 +39,15 @@ export const getAccountRole = async (accountId: string, userId: string): Promise
3839 }
3940
4041 const account = await dashboardClient ( ACCOUNTS_TABLE )
41- . leftJoin ( MEMBERS_TABLE , function ( ) {
42+ . join ( MEMBERS_TABLE , function ( ) {
4243 this . on ( `${ MEMBERS_TABLE } .org` , `${ ACCOUNTS_TABLE } .org` )
4344 . andOnVal ( `${ MEMBERS_TABLE } .user` , '=' , userId ) ;
4445 } )
46+ . join ( ORGS_TABLE , `${ ORGS_TABLE } .id` , `${ ACCOUNTS_TABLE } .org` )
4547 . where ( `${ ACCOUNTS_TABLE } .id` , accountId )
46- . where ( function ( ) {
47- this . where ( `${ ACCOUNTS_TABLE } .user` , userId ) . orWhereNotNull ( `${ MEMBERS_TABLE } .id` ) ;
48- } )
49- . first < { id : string ; role : AccountRole } | undefined > (
50- `${ ACCOUNTS_TABLE } .id` ,
51- dashboardClient . raw ( `IF(?? = ?, 'owner', ??) AS role` , [ `${ ACCOUNTS_TABLE } .user` , userId , `${ MEMBERS_TABLE } .role` ] ) ,
52- ) ;
48+ . first < ResolvedAccount | undefined > ( `${ ACCOUNTS_TABLE } .id` , `${ MEMBERS_TABLE } .role` , `${ ORGS_TABLE } .user_type AS userType` ) ;
5349
54- const role = account ? { id : account . id , role : account . role } : null ;
50+ const role = account ? { id : account . id , role : account . role , userType : account . userType } : null ;
5551 rolesByUserAndAccount . set ( key , role ) ;
5652
5753 return role ;
0 commit comments