diff --git a/doc/activeDid-migration-plan.md b/doc/activeDid-migration-plan.md index a5fb3e8e..26b68f7f 100644 --- a/doc/activeDid-migration-plan.md +++ b/doc/activeDid-migration-plan.md @@ -118,6 +118,7 @@ onNumNewOffersToUserChange(newValue: number, oldValue: number) { **Status**: 23 components successfully migrated. 11 components remaining. API layer ready for systematic updates. ### Phase 4: Testing 🟡 PARTIALLY STARTED + - [x] Test Web platform (verified working) - [ ] Test Electron platform - [ ] Test iOS platform diff --git a/src/utils/PlatformServiceMixin.ts b/src/utils/PlatformServiceMixin.ts index 3a6ac7f2..c3ae7783 100644 --- a/src/utils/PlatformServiceMixin.ts +++ b/src/utils/PlatformServiceMixin.ts @@ -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 }, - ); - - // Handle null activeDid (initial state after migration) - if (activeDid === null) { - logger.debug( - "[PlatformServiceMixin] Active identity is null (initial state), attempting auto-selection", - ); - - // Try to auto-select first available account - const availableAccounts = 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 - 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: "" }; - } - } + if (!result?.values?.length) { + logger.warn("[PlatformServiceMixin] Active identity table is empty - this may indicate a migration issue"); + return { activeDid: "" }; + } - // Validate activeDid exists in accounts - if (activeDid) { - const accountExists = await this.$dbQuery( - "SELECT did FROM accounts WHERE did = ?", - [activeDid], - ); + const activeDid = result.values[0][0] as string | null; - 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", + // 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 (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], ); - return { activeDid: "" }; + return { activeDid: firstAccountDid }; } + + logger.warn("[PlatformServiceMixin] No accounts available for auto-selection"); + 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", + // Validate activeDid exists in accounts + const accountExists = await this.$dbQuery( + "SELECT did FROM accounts WHERE did = ?", + [activeDid], ); - 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"); + if (accountExists?.values?.length) { + return { activeDid }; } - 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: "" }; } },