@ -608,126 +608,54 @@ export const PlatformServiceMixin = {
* /
async $getActiveIdentity ( ) : Promise < { activeDid : string } > {
try {
logger . debug (
"[PlatformServiceMixin] $getActiveIdentity() called - API layer verification" ,
) ;
logger . debug (
"[PlatformServiceMixin] Getting active identity from active_identity table" ,
) ;
const result = await this . $dbQuery (
"SELECT activeDid FROM active_identity WHERE id = 1" ,
) ;
if ( result ? . values ? . length ) {
const activeDid = result . values [ 0 ] [ 0 ] as string ;
logger . debug ( "[PlatformServiceMixin] Active identity found:" , {
activeDid ,
} ) ;
logger . debug (
"[PlatformServiceMixin] $getActiveIdentity(): activeDid resolved" ,
{ activeDid } ,
) ;
if ( ! result ? . values ? . length ) {
logger . warn ( "[PlatformServiceMixin] Active identity table is empty - this may indicate a migration issue" ) ;
return { activeDid : "" } ;
}
// Handle null activeDid (initial state after migration)
if ( activeDid === null ) {
logger . debug (
"[PlatformServiceMixin] Active identity is null (initial state), attempting auto-selection" ,
) ;
const activeDid = result . values [ 0 ] [ 0 ] as string | null ;
// Try to auto-select first available account
const availableAccounts = await this . $dbQuery (
// Handle null activeDid (initial state after migration) - auto-select first account
if ( activeDid === null ) {
const firstAccount = await this . $dbQuery (
"SELECT did FROM accounts ORDER BY dateCreated, did LIMIT 1" ,
) ;
if ( availableAccounts ? . values ? . length ) {
const firstAccountDid = availableAccounts . values [ 0 ] [ 0 ] as string ;
logger . debug (
"[PlatformServiceMixin] Auto-selecting first account as active:" ,
{ firstAccountDid } ,
) ;
// Update active_identity table with the first account
if ( firstAccount ? . values ? . length ) {
const firstAccountDid = firstAccount . values [ 0 ] [ 0 ] as string ;
await this . $dbExec (
"UPDATE active_identity SET activeDid = ?, lastUpdated = datetime('now') WHERE id = 1" ,
[ firstAccountDid ] ,
) ;
logger . debug (
"[PlatformServiceMixin] Active identity auto-selected successfully" ,
) ;
return { activeDid : firstAccountDid } ;
} else {
logger . warn (
"[PlatformServiceMixin] No accounts available for auto-selection" ,
) ;
return { activeDid : "" } ;
}
logger . warn ( "[PlatformServiceMixin] No accounts available for auto-selection" ) ;
return { activeDid : "" } ;
}
// Validate activeDid exists in accounts
if ( activeDid ) {
const accountExists = await this . $dbQuery (
"SELECT did FROM accounts WHERE did = ?" ,
[ activeDid ] ,
) ;
if ( accountExists ? . values ? . length ) {
logger . debug (
"[PlatformServiceMixin] Active identity validated in accounts" ,
) ;
return { activeDid } ;
} else {
// Clear corrupted activeDid
logger . warn (
"[PlatformServiceMixin] Active identity not found in accounts, clearing" ,
) ;
await this . $dbExec (
"UPDATE active_identity SET activeDid = NULL, lastUpdated = datetime('now') WHERE id = 1" ,
) ;
return { activeDid : "" } ;
}
} else {
// activeDid is empty string, return it
logger . debug (
"[PlatformServiceMixin] Active identity is empty string, returning as-is" ,
) ;
return { activeDid : "" } ;
}
}
// Handle empty active_identity table - this indicates a migration issue
// Instead of auto-fixing, we log the issue for user awareness
logger . warn (
"[PlatformServiceMixin] Active identity table is empty - this may indicate a migration issue" ,
) ;
// Check if there are any accounts available for user selection
const availableAccounts = await this . $dbQuery (
"SELECT did FROM accounts ORDER BY did LIMIT 5" ,
) ;
if ( availableAccounts ? . values ? . length ) {
const accountDids = availableAccounts . values . map (
( row : SqlValue [ ] ) = > row [ 0 ] as string ,
) ;
logger . debug (
"[PlatformServiceMixin] Available accounts for user selection:" ,
{ accountDids } ,
) ;
} else {
logger . warn ( "[PlatformServiceMixin] No accounts found in database" ) ;
}
logger . debug (
"[PlatformServiceMixin] No active identity found, returning empty" ,
// Clear corrupted activeDid and return empty
logger . warn ( "[PlatformServiceMixin] Active identity not found in accounts, clearing" ) ;
await this . $dbExec (
"UPDATE active_identity SET activeDid = NULL, lastUpdated = datetime('now') WHERE id = 1" ,
) ;
return { activeDid : "" } ;
} catch ( error ) {
logger . error (
"[PlatformServiceMixin] Error getting active identity:" ,
error ,
) ;
logger . error ( "[PlatformServiceMixin] Error getting active identity:" , error ) ;
return { activeDid : "" } ;
}
} ,